Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 5 | #ifndef COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_MANAGER_H_ |
| 6 | #define COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_MANAGER_H_ |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 7 | |
dcheng | a0ee5fb | 2016-04-26 02:46:55 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Avi Drissman | 12be031 | 2023-01-11 09:16:09 | [diff] [blame] | 10 | #include "base/functional/callback.h" |
Avi Drissman | 82c4500 | 2017-11-29 22:32:29 | [diff] [blame] | 11 | #include "base/gtest_prod_util.h" |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 12 | #include "base/memory/singleton.h" |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 13 | #include "components/javascript_dialogs/app_modal_dialog_controller.h" |
Zelin Liu | e93cb9b | 2023-04-10 22:13:16 | [diff] [blame] | 14 | #include "components/javascript_dialogs/app_modal_dialog_manager_delegate.h" |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 15 | #include "content/public/browser/javascript_dialog_manager.h" |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 16 | |
Avi Drissman | 649f10a | 2021-09-07 19:23:03 | [diff] [blame] | 17 | namespace url { |
| 18 | class Origin; |
| 19 | } |
| 20 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 21 | namespace javascript_dialogs { |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 22 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 23 | class ExtensionsClient; |
| 24 | class AppModalViewFactory; |
Zelin Liu | e93cb9b | 2023-04-10 22:13:16 | [diff] [blame] | 25 | class AppModalDialogManagerDelegate; |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 26 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 27 | class AppModalDialogManager : public content::JavaScriptDialogManager { |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 28 | public: |
Evan Stade | 0fa711a | 2020-01-13 21:03:03 | [diff] [blame] | 29 | // A factory method to create and returns a platform-specific dialog class. |
| 30 | // The returned object should own itself. |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 31 | using AppModalViewFactory = |
| 32 | base::RepeatingCallback<AppModalDialogView*(AppModalDialogController*)>; |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 33 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 34 | static AppModalDialogManager* GetInstance(); |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 35 | |
Peter Boström | 9f667c38 | 2021-10-01 20:09:31 | [diff] [blame] | 36 | AppModalDialogManager(const AppModalDialogManager&) = delete; |
| 37 | AppModalDialogManager& operator=(const AppModalDialogManager&) = delete; |
| 38 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 39 | // Sets the AppModalViewFactory used to create platform specific |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 40 | // dialog window instances. |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 41 | void SetNativeDialogFactory(AppModalViewFactory factory); |
Evan Stade | 0fa711a | 2020-01-13 21:03:03 | [diff] [blame] | 42 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 43 | AppModalViewFactory* view_factory() { return &view_factory_; } |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 44 | |
| 45 | // JavaScript dialogs may be opened by an extensions/app, thus they need |
| 46 | // access to extensions functionality. This sets a client interface to |
| 47 | // access //extensions. |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 48 | void SetExtensionsClient(std::unique_ptr<ExtensionsClient> extensions_client); |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 49 | |
Zelin Liu | e93cb9b | 2023-04-10 22:13:16 | [diff] [blame] | 50 | void SetDelegate(std::unique_ptr<AppModalDialogManagerDelegate> delegate); |
| 51 | |
avi | 7a1b55b | 2016-10-19 04:18:38 | [diff] [blame] | 52 | // Gets the title for a dialog. |
Jan Wilken Dörrie | fa241ba | 2021-03-11 17:57:01 | [diff] [blame] | 53 | std::u16string GetTitle(content::WebContents* web_contents, |
Avi Drissman | 649f10a | 2021-09-07 19:23:03 | [diff] [blame] | 54 | const url::Origin& alerting_frame_origin); |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 55 | |
Renee Wright | 5ab5786 | 2018-05-30 00:09:15 | [diff] [blame] | 56 | // Displays a dialog asking the user if they want to leave a page. Displays |
| 57 | // a different message if the site is in an app window. |
| 58 | void RunBeforeUnloadDialogWithOptions( |
| 59 | content::WebContents* web_contents, |
| 60 | content::RenderFrameHost* render_frame_host, |
| 61 | bool is_reload, |
| 62 | bool is_app, |
| 63 | DialogClosedCallback callback); |
| 64 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 65 | // content::JavaScriptDialogManager: |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 66 | void RunJavaScriptDialog(content::WebContents* web_contents, |
Avi Drissman | 2fefc4b | 2018-02-22 20:41:01 | [diff] [blame] | 67 | content::RenderFrameHost* render_frame_host, |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 68 | content::JavaScriptDialogType dialog_type, |
Jan Wilken Dörrie | fa241ba | 2021-03-11 17:57:01 | [diff] [blame] | 69 | const std::u16string& message_text, |
| 70 | const std::u16string& default_prompt_text, |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 71 | DialogClosedCallback callback, |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 72 | bool* did_suppress_message) override; |
| 73 | void RunBeforeUnloadDialog(content::WebContents* web_contents, |
Avi Drissman | ff9ed75 | 2017-11-28 21:18:12 | [diff] [blame] | 74 | content::RenderFrameHost* render_frame_host, |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 75 | bool is_reload, |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 76 | DialogClosedCallback callback) override; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 77 | bool HandleJavaScriptDialog(content::WebContents* web_contents, |
| 78 | bool accept, |
Jan Wilken Dörrie | fa241ba | 2021-03-11 17:57:01 | [diff] [blame] | 79 | const std::u16string* prompt_override) override; |
avi | 5d3b869 | 2016-10-12 22:00:46 | [diff] [blame] | 80 | void CancelDialogs(content::WebContents* web_contents, |
avi | 5d3b869 | 2016-10-12 22:00:46 | [diff] [blame] | 81 | bool reset_state) override; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 82 | |
Zelin Liu | e93cb9b | 2023-04-10 22:13:16 | [diff] [blame] | 83 | static std::u16string GetSiteFrameTitle( |
| 84 | const url::Origin& main_frame_origin, |
| 85 | const url::Origin& alerting_frame_origin); |
| 86 | |
avi | 7a1b55b | 2016-10-19 04:18:38 | [diff] [blame] | 87 | private: |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 88 | FRIEND_TEST_ALL_PREFIXES(AppModalDialogManagerTest, GetTitle); |
| 89 | friend struct base::DefaultSingletonTraits<AppModalDialogManager>; |
avi | 7a1b55b | 2016-10-19 04:18:38 | [diff] [blame] | 90 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 91 | AppModalDialogManager(); |
| 92 | ~AppModalDialogManager() override; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 93 | |
| 94 | // Wrapper around a DialogClosedCallback so that we can intercept it before |
| 95 | // passing it onto the original callback. |
| 96 | void OnDialogClosed(content::WebContents* web_contents, |
| 97 | DialogClosedCallback callback, |
| 98 | bool success, |
Jan Wilken Dörrie | fa241ba | 2021-03-11 17:57:01 | [diff] [blame] | 99 | const std::u16string& user_input); |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 100 | |
| 101 | // Mapping between the WebContents and their extra data. The key |
| 102 | // is a void* because the pointer is just a cookie and is never dereferenced. |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 103 | AppModalDialogController::ExtraDataMap javascript_dialog_extra_data_; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 104 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 105 | AppModalViewFactory view_factory_; |
Zelin Liu | e93cb9b | 2023-04-10 22:13:16 | [diff] [blame] | 106 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 107 | std::unique_ptr<ExtensionsClient> extensions_client_; |
Zelin Liu | e93cb9b | 2023-04-10 22:13:16 | [diff] [blame] | 108 | |
| 109 | std::unique_ptr<AppModalDialogManagerDelegate> delegate_; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 110 | }; |
| 111 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 112 | } // namespace javascript_dialogs |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 113 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame] | 114 | #endif // COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_MANAGER_H_ |