oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 1 | // 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 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 | |
Evan Stade | 0fa711a | 2020-01-13 21:03:03 | [diff] [blame] | 10 | #include "base/callback.h" |
Avi Drissman | 82c4500 | 2017-11-29 22:32:29 | [diff] [blame] | 11 | #include "base/gtest_prod_util.h" |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 12 | #include "base/macros.h" |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 13 | #include "base/memory/singleton.h" |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 14 | #include "components/javascript_dialogs/app_modal_dialog_controller.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 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 17 | namespace javascript_dialogs { |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 18 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 19 | class ExtensionsClient; |
| 20 | class AppModalViewFactory; |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 21 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 22 | class AppModalDialogManager : public content::JavaScriptDialogManager { |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 23 | public: |
Evan Stade | 0fa711a | 2020-01-13 21:03:03 | [diff] [blame] | 24 | // A factory method to create and returns a platform-specific dialog class. |
| 25 | // The returned object should own itself. |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 26 | using AppModalViewFactory = |
| 27 | base::RepeatingCallback<AppModalDialogView*(AppModalDialogController*)>; |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 28 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 29 | static AppModalDialogManager* GetInstance(); |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 30 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 31 | // Sets the AppModalViewFactory used to create platform specific |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 32 | // dialog window instances. |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 33 | void SetNativeDialogFactory(AppModalViewFactory factory); |
Evan Stade | 0fa711a | 2020-01-13 21:03:03 | [diff] [blame] | 34 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 35 | AppModalViewFactory* view_factory() { return &view_factory_; } |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 36 | |
| 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 Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 40 | void SetExtensionsClient(std::unique_ptr<ExtensionsClient> extensions_client); |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 41 | |
avi | 7a1b55b | 2016-10-19 04:18:38 | [diff] [blame] | 42 | // Gets the title for a dialog. |
| 43 | base::string16 GetTitle(content::WebContents* web_contents, |
Devlin Cronin | 347452f3 | 2017-08-28 18:22:31 | [diff] [blame] | 44 | const GURL& alerting_frame_url); |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 45 | |
Renee Wright | 5ab5786 | 2018-05-30 00:09:15 | [diff] [blame] | 46 | // 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 Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 55 | // content::JavaScriptDialogManager: |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 56 | void RunJavaScriptDialog(content::WebContents* web_contents, |
Avi Drissman | 2fefc4b | 2018-02-22 20:41:01 | [diff] [blame] | 57 | content::RenderFrameHost* render_frame_host, |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 58 | content::JavaScriptDialogType dialog_type, |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 59 | const base::string16& message_text, |
| 60 | const base::string16& default_prompt_text, |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 61 | DialogClosedCallback callback, |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 62 | bool* did_suppress_message) override; |
| 63 | void RunBeforeUnloadDialog(content::WebContents* web_contents, |
Avi Drissman | ff9ed75 | 2017-11-28 21:18:12 | [diff] [blame] | 64 | content::RenderFrameHost* render_frame_host, |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 65 | bool is_reload, |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 66 | DialogClosedCallback callback) override; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 67 | bool HandleJavaScriptDialog(content::WebContents* web_contents, |
| 68 | bool accept, |
| 69 | const base::string16* prompt_override) override; |
avi | 5d3b869 | 2016-10-12 22:00:46 | [diff] [blame] | 70 | void CancelDialogs(content::WebContents* web_contents, |
avi | 5d3b869 | 2016-10-12 22:00:46 | [diff] [blame] | 71 | bool reset_state) override; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 72 | |
avi | 7a1b55b | 2016-10-19 04:18:38 | [diff] [blame] | 73 | private: |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 74 | FRIEND_TEST_ALL_PREFIXES(AppModalDialogManagerTest, GetTitle); |
| 75 | friend struct base::DefaultSingletonTraits<AppModalDialogManager>; |
avi | 7a1b55b | 2016-10-19 04:18:38 | [diff] [blame] | 76 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 77 | AppModalDialogManager(); |
| 78 | ~AppModalDialogManager() override; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 79 | |
avi | e163d284 | 2016-02-22 22:39:21 | [diff] [blame] | 80 | // Wrapper around OnDialogClosed; logs UMA stats before continuing on. |
| 81 | void OnBeforeUnloadDialogClosed(content::WebContents* web_contents, |
| 82 | DialogClosedCallback callback, |
| 83 | bool success, |
| 84 | const base::string16& user_input); |
| 85 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 86 | // Wrapper around a DialogClosedCallback so that we can intercept it before |
| 87 | // passing it onto the original callback. |
| 88 | void OnDialogClosed(content::WebContents* web_contents, |
| 89 | DialogClosedCallback callback, |
| 90 | bool success, |
| 91 | const base::string16& user_input); |
| 92 | |
Avi Drissman | 82c4500 | 2017-11-29 22:32:29 | [diff] [blame] | 93 | static base::string16 GetTitleImpl(const GURL& parent_frame_url, |
| 94 | const GURL& alerting_frame_url); |
| 95 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 96 | // Mapping between the WebContents and their extra data. The key |
| 97 | // 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^] | 98 | AppModalDialogController::ExtraDataMap javascript_dialog_extra_data_; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 99 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 100 | AppModalViewFactory view_factory_; |
| 101 | std::unique_ptr<ExtensionsClient> extensions_client_; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 102 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 103 | DISALLOW_COPY_AND_ASSIGN(AppModalDialogManager); |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 104 | }; |
| 105 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 106 | } // namespace javascript_dialogs |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 107 | |
Evan Stade | 7220e47 | 2020-01-31 17:06:57 | [diff] [blame^] | 108 | #endif // COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_MANAGER_H_ |