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 | |
oshima | f6539842 | 2014-11-18 23:30:42 | [diff] [blame] | 5 | #ifndef COMPONENTS_APP_MODAL_JAVASCRIPT_DIALOG_MANAGER_H_ |
| 6 | #define COMPONENTS_APP_MODAL_JAVASCRIPT_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 | 82c4500 | 2017-11-29 22:32:29 | [diff] [blame] | 10 | #include "base/gtest_prod_util.h" |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 11 | #include "base/macros.h" |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 12 | #include "base/memory/singleton.h" |
joenotcharles | 850904a | 2016-02-09 01:50:44 | [diff] [blame] | 13 | #include "base/time/time.h" |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 14 | #include "components/app_modal/javascript_app_modal_dialog.h" |
| 15 | #include "content/public/browser/javascript_dialog_manager.h" |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 16 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 17 | namespace app_modal { |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 18 | |
| 19 | class JavaScriptDialogExtensionsClient; |
| 20 | class JavaScriptNativeDialogFactory; |
| 21 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 22 | class JavaScriptDialogManager : public content::JavaScriptDialogManager { |
| 23 | public: |
| 24 | static JavaScriptDialogManager* GetInstance(); |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 25 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 26 | JavaScriptNativeDialogFactory* native_dialog_factory() { |
| 27 | return native_dialog_factory_.get(); |
| 28 | } |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 29 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 30 | // Sets the JavaScriptNativeDialogFactory used to create platform specific |
| 31 | // dialog window instances. |
| 32 | void SetNativeDialogFactory( |
dcheng | a0ee5fb | 2016-04-26 02:46:55 | [diff] [blame] | 33 | std::unique_ptr<JavaScriptNativeDialogFactory> factory); |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 34 | |
| 35 | // JavaScript dialogs may be opened by an extensions/app, thus they need |
| 36 | // access to extensions functionality. This sets a client interface to |
| 37 | // access //extensions. |
| 38 | void SetExtensionsClient( |
dcheng | a0ee5fb | 2016-04-26 02:46:55 | [diff] [blame] | 39 | std::unique_ptr<JavaScriptDialogExtensionsClient> extensions_client); |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 40 | |
avi | 7a1b55b | 2016-10-19 04:18:38 | [diff] [blame] | 41 | // Gets the title for a dialog. |
| 42 | base::string16 GetTitle(content::WebContents* web_contents, |
Devlin Cronin | 347452f3 | 2017-08-28 18:22:31 | [diff] [blame] | 43 | const GURL& alerting_frame_url); |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 44 | |
Renee Wright | 5ab5786 | 2018-05-30 00:09:15 | [diff] [blame^] | 45 | // Displays a dialog asking the user if they want to leave a page. Displays |
| 46 | // a different message if the site is in an app window. |
| 47 | void RunBeforeUnloadDialogWithOptions( |
| 48 | content::WebContents* web_contents, |
| 49 | content::RenderFrameHost* render_frame_host, |
| 50 | bool is_reload, |
| 51 | bool is_app, |
| 52 | DialogClosedCallback callback); |
| 53 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 54 | // JavaScriptDialogManager: |
| 55 | void RunJavaScriptDialog(content::WebContents* web_contents, |
Avi Drissman | 2fefc4b | 2018-02-22 20:41:01 | [diff] [blame] | 56 | content::RenderFrameHost* render_frame_host, |
avi | 777ff45 | 2017-02-09 19:04:48 | [diff] [blame] | 57 | content::JavaScriptDialogType dialog_type, |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 58 | const base::string16& message_text, |
| 59 | const base::string16& default_prompt_text, |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 60 | DialogClosedCallback callback, |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 61 | bool* did_suppress_message) override; |
| 62 | void RunBeforeUnloadDialog(content::WebContents* web_contents, |
Avi Drissman | ff9ed75 | 2017-11-28 21:18:12 | [diff] [blame] | 63 | content::RenderFrameHost* render_frame_host, |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 64 | bool is_reload, |
Avi Drissman | e04d399 | 2017-10-05 15:11:36 | [diff] [blame] | 65 | DialogClosedCallback callback) override; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 66 | bool HandleJavaScriptDialog(content::WebContents* web_contents, |
| 67 | bool accept, |
| 68 | const base::string16* prompt_override) override; |
avi | 5d3b869 | 2016-10-12 22:00:46 | [diff] [blame] | 69 | void CancelDialogs(content::WebContents* web_contents, |
avi | 5d3b869 | 2016-10-12 22:00:46 | [diff] [blame] | 70 | bool reset_state) override; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 71 | |
avi | 7a1b55b | 2016-10-19 04:18:38 | [diff] [blame] | 72 | private: |
Avi Drissman | 82c4500 | 2017-11-29 22:32:29 | [diff] [blame] | 73 | FRIEND_TEST_ALL_PREFIXES(JavaScriptDialogManagerTest, GetTitle); |
avi | 7a1b55b | 2016-10-19 04:18:38 | [diff] [blame] | 74 | friend struct base::DefaultSingletonTraits<JavaScriptDialogManager>; |
| 75 | |
| 76 | JavaScriptDialogManager(); |
| 77 | ~JavaScriptDialogManager() override; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 78 | |
avi | e163d284 | 2016-02-22 22:39:21 | [diff] [blame] | 79 | // Wrapper around OnDialogClosed; logs UMA stats before continuing on. |
| 80 | void OnBeforeUnloadDialogClosed(content::WebContents* web_contents, |
| 81 | DialogClosedCallback callback, |
| 82 | bool success, |
| 83 | const base::string16& user_input); |
| 84 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 85 | // Wrapper around a DialogClosedCallback so that we can intercept it before |
| 86 | // passing it onto the original callback. |
| 87 | void OnDialogClosed(content::WebContents* web_contents, |
| 88 | DialogClosedCallback callback, |
| 89 | bool success, |
| 90 | const base::string16& user_input); |
| 91 | |
Avi Drissman | 82c4500 | 2017-11-29 22:32:29 | [diff] [blame] | 92 | static base::string16 GetTitleImpl(const GURL& parent_frame_url, |
| 93 | const GURL& alerting_frame_url); |
| 94 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 95 | // Mapping between the WebContents and their extra data. The key |
| 96 | // is a void* because the pointer is just a cookie and is never dereferenced. |
| 97 | JavaScriptAppModalDialog::ExtraDataMap javascript_dialog_extra_data_; |
| 98 | |
dcheng | a0ee5fb | 2016-04-26 02:46:55 | [diff] [blame] | 99 | std::unique_ptr<JavaScriptNativeDialogFactory> native_dialog_factory_; |
| 100 | std::unique_ptr<JavaScriptDialogExtensionsClient> extensions_client_; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 101 | |
joenotcharles | 850904a | 2016-02-09 01:50:44 | [diff] [blame] | 102 | // Record a single create and close timestamp to track the time between |
| 103 | // dialogs. (Since Javascript dialogs are modal, this is even accurate!) |
| 104 | base::TimeTicks last_close_time_; |
| 105 | base::TimeTicks last_creation_time_; |
| 106 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 107 | DISALLOW_COPY_AND_ASSIGN(JavaScriptDialogManager); |
| 108 | }; |
| 109 | |
| 110 | } // namespace app_modal |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 111 | |
oshima | f6539842 | 2014-11-18 23:30:42 | [diff] [blame] | 112 | #endif // COMPONENTS_APP_MODAL_JAVASCRIPT_DIALOG_MANAGER_H_ |