blob: 1fcff0a07efcd5986402cde22c192bd0462ba019 [file] [log] [blame]
oshima758abebc2014-11-06 10:55:501// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Evan Stade7220e472020-01-31 17:06:575#ifndef COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_MANAGER_H_
6#define COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_MANAGER_H_
oshima758abebc2014-11-06 10:55:507
dchenga0ee5fb2016-04-26 02:46:558#include <memory>
9
Evan Stade0fa711a2020-01-13 21:03:0310#include "base/callback.h"
Avi Drissman82c45002017-11-29 22:32:2911#include "base/gtest_prod_util.h"
avibc5337b2015-12-25 23:16:3312#include "base/macros.h"
oshima0929be2a2014-11-19 22:21:0313#include "base/memory/singleton.h"
Evan Stade7220e472020-01-31 17:06:5714#include "components/javascript_dialogs/app_modal_dialog_controller.h"
oshima0929be2a2014-11-19 22:21:0315#include "content/public/browser/javascript_dialog_manager.h"
oshima758abebc2014-11-06 10:55:5016
Evan Stade7220e472020-01-31 17:06:5717namespace javascript_dialogs {
oshima758abebc2014-11-06 10:55:5018
Evan Stade7220e472020-01-31 17:06:5719class ExtensionsClient;
20class AppModalViewFactory;
oshima758abebc2014-11-06 10:55:5021
Evan Stade7220e472020-01-31 17:06:5722class AppModalDialogManager : public content::JavaScriptDialogManager {
oshima0929be2a2014-11-19 22:21:0323 public:
Evan Stade0fa711a2020-01-13 21:03:0324 // A factory method to create and returns a platform-specific dialog class.
25 // The returned object should own itself.
Evan Stade7220e472020-01-31 17:06:5726 using AppModalViewFactory =
27 base::RepeatingCallback<AppModalDialogView*(AppModalDialogController*)>;
oshima758abebc2014-11-06 10:55:5028
Evan Stade7220e472020-01-31 17:06:5729 static AppModalDialogManager* GetInstance();
oshima758abebc2014-11-06 10:55:5030
Evan Stade7220e472020-01-31 17:06:5731 // Sets the AppModalViewFactory used to create platform specific
oshima0929be2a2014-11-19 22:21:0332 // dialog window instances.
Evan Stade7220e472020-01-31 17:06:5733 void SetNativeDialogFactory(AppModalViewFactory factory);
Evan Stade0fa711a2020-01-13 21:03:0334
Evan Stade7220e472020-01-31 17:06:5735 AppModalViewFactory* view_factory() { return &view_factory_; }
oshima0929be2a2014-11-19 22:21:0336
37 // JavaScript dialogs may be opened by an extensions/app, thus they need
38 // access to extensions functionality. This sets a client interface to
39 // access //extensions.
Evan Stade7220e472020-01-31 17:06:5740 void SetExtensionsClient(std::unique_ptr<ExtensionsClient> extensions_client);
oshima0929be2a2014-11-19 22:21:0341
avi7a1b55b2016-10-19 04:18:3842 // Gets the title for a dialog.
Jan Wilken Dörriefa241ba2021-03-11 17:57:0143 std::u16string GetTitle(content::WebContents* web_contents,
Devlin Cronin347452f32017-08-28 18:22:3144 const GURL& alerting_frame_url);
oshima0929be2a2014-11-19 22:21:0345
Renee Wright5ab57862018-05-30 00:09:1546 // Displays a dialog asking the user if they want to leave a page. Displays
47 // a different message if the site is in an app window.
48 void RunBeforeUnloadDialogWithOptions(
49 content::WebContents* web_contents,
50 content::RenderFrameHost* render_frame_host,
51 bool is_reload,
52 bool is_app,
53 DialogClosedCallback callback);
54
Evan Stade7220e472020-01-31 17:06:5755 // content::JavaScriptDialogManager:
oshima0929be2a2014-11-19 22:21:0356 void RunJavaScriptDialog(content::WebContents* web_contents,
Avi Drissman2fefc4b2018-02-22 20:41:0157 content::RenderFrameHost* render_frame_host,
avi777ff452017-02-09 19:04:4858 content::JavaScriptDialogType dialog_type,
Jan Wilken Dörriefa241ba2021-03-11 17:57:0159 const std::u16string& message_text,
60 const std::u16string& default_prompt_text,
Avi Drissmane04d3992017-10-05 15:11:3661 DialogClosedCallback callback,
oshima0929be2a2014-11-19 22:21:0362 bool* did_suppress_message) override;
63 void RunBeforeUnloadDialog(content::WebContents* web_contents,
Avi Drissmanff9ed752017-11-28 21:18:1264 content::RenderFrameHost* render_frame_host,
oshima0929be2a2014-11-19 22:21:0365 bool is_reload,
Avi Drissmane04d3992017-10-05 15:11:3666 DialogClosedCallback callback) override;
oshima0929be2a2014-11-19 22:21:0367 bool HandleJavaScriptDialog(content::WebContents* web_contents,
68 bool accept,
Jan Wilken Dörriefa241ba2021-03-11 17:57:0169 const std::u16string* prompt_override) override;
avi5d3b8692016-10-12 22:00:4670 void CancelDialogs(content::WebContents* web_contents,
avi5d3b8692016-10-12 22:00:4671 bool reset_state) override;
oshima0929be2a2014-11-19 22:21:0372
avi7a1b55b2016-10-19 04:18:3873 private:
Evan Stade7220e472020-01-31 17:06:5774 FRIEND_TEST_ALL_PREFIXES(AppModalDialogManagerTest, GetTitle);
75 friend struct base::DefaultSingletonTraits<AppModalDialogManager>;
avi7a1b55b2016-10-19 04:18:3876
Evan Stade7220e472020-01-31 17:06:5777 AppModalDialogManager();
78 ~AppModalDialogManager() override;
oshima0929be2a2014-11-19 22:21:0379
oshima0929be2a2014-11-19 22:21:0380 // Wrapper around a DialogClosedCallback so that we can intercept it before
81 // passing it onto the original callback.
82 void OnDialogClosed(content::WebContents* web_contents,
83 DialogClosedCallback callback,
84 bool success,
Jan Wilken Dörriefa241ba2021-03-11 17:57:0185 const std::u16string& user_input);
oshima0929be2a2014-11-19 22:21:0386
Jan Wilken Dörriefa241ba2021-03-11 17:57:0187 static std::u16string GetTitleImpl(const GURL& parent_frame_url,
Avi Drissman82c45002017-11-29 22:32:2988 const GURL& alerting_frame_url);
89
oshima0929be2a2014-11-19 22:21:0390 // Mapping between the WebContents and their extra data. The key
91 // is a void* because the pointer is just a cookie and is never dereferenced.
Evan Stade7220e472020-01-31 17:06:5792 AppModalDialogController::ExtraDataMap javascript_dialog_extra_data_;
oshima0929be2a2014-11-19 22:21:0393
Evan Stade7220e472020-01-31 17:06:5794 AppModalViewFactory view_factory_;
95 std::unique_ptr<ExtensionsClient> extensions_client_;
oshima0929be2a2014-11-19 22:21:0396
Evan Stade7220e472020-01-31 17:06:5797 DISALLOW_COPY_AND_ASSIGN(AppModalDialogManager);
oshima0929be2a2014-11-19 22:21:0398};
99
Evan Stade7220e472020-01-31 17:06:57100} // namespace javascript_dialogs
oshima758abebc2014-11-06 10:55:50101
Evan Stade7220e472020-01-31 17:06:57102#endif // COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_MANAGER_H_