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 | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 10 | #include "base/macros.h" |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 11 | #include "base/memory/singleton.h" |
joenotcharles | 850904a | 2016-02-09 01:50:44 | [diff] [blame] | 12 | #include "base/time/time.h" |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 13 | #include "components/app_modal/javascript_app_modal_dialog.h" |
| 14 | #include "content/public/browser/javascript_dialog_manager.h" |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 15 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 16 | namespace app_modal { |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 17 | |
| 18 | class JavaScriptDialogExtensionsClient; |
| 19 | class JavaScriptNativeDialogFactory; |
| 20 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 21 | class JavaScriptDialogManager : public content::JavaScriptDialogManager { |
| 22 | public: |
| 23 | static JavaScriptDialogManager* GetInstance(); |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 24 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 25 | JavaScriptNativeDialogFactory* native_dialog_factory() { |
| 26 | return native_dialog_factory_.get(); |
| 27 | } |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 28 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 29 | // Sets the JavaScriptNativeDialogFactory used to create platform specific |
| 30 | // dialog window instances. |
| 31 | void SetNativeDialogFactory( |
dcheng | a0ee5fb | 2016-04-26 02:46:55 | [diff] [blame^] | 32 | std::unique_ptr<JavaScriptNativeDialogFactory> factory); |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 33 | |
| 34 | // JavaScript dialogs may be opened by an extensions/app, thus they need |
| 35 | // access to extensions functionality. This sets a client interface to |
| 36 | // access //extensions. |
| 37 | void SetExtensionsClient( |
dcheng | a0ee5fb | 2016-04-26 02:46:55 | [diff] [blame^] | 38 | std::unique_ptr<JavaScriptDialogExtensionsClient> extensions_client); |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 39 | |
| 40 | private: |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 41 | friend struct base::DefaultSingletonTraits<JavaScriptDialogManager>; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 42 | |
| 43 | JavaScriptDialogManager(); |
| 44 | ~JavaScriptDialogManager() override; |
| 45 | |
| 46 | // JavaScriptDialogManager: |
| 47 | void RunJavaScriptDialog(content::WebContents* web_contents, |
| 48 | const GURL& origin_url, |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 49 | content::JavaScriptMessageType message_type, |
| 50 | const base::string16& message_text, |
| 51 | const base::string16& default_prompt_text, |
| 52 | const DialogClosedCallback& callback, |
| 53 | bool* did_suppress_message) override; |
| 54 | void RunBeforeUnloadDialog(content::WebContents* web_contents, |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 55 | bool is_reload, |
| 56 | const DialogClosedCallback& callback) override; |
| 57 | bool HandleJavaScriptDialog(content::WebContents* web_contents, |
| 58 | bool accept, |
| 59 | const base::string16* prompt_override) override; |
| 60 | void CancelActiveAndPendingDialogs( |
| 61 | content::WebContents* web_contents) override; |
avi | 2460c76 | 2015-04-17 15:21:54 | [diff] [blame] | 62 | void ResetDialogState(content::WebContents* web_contents) override; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 63 | |
| 64 | base::string16 GetTitle(content::WebContents* web_contents, |
| 65 | const GURL& origin_url, |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 66 | bool is_alert); |
| 67 | |
avi | e163d284 | 2016-02-22 22:39:21 | [diff] [blame] | 68 | // Wrapper around OnDialogClosed; logs UMA stats before continuing on. |
| 69 | void OnBeforeUnloadDialogClosed(content::WebContents* web_contents, |
| 70 | DialogClosedCallback callback, |
| 71 | bool success, |
| 72 | const base::string16& user_input); |
| 73 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 74 | // Wrapper around a DialogClosedCallback so that we can intercept it before |
| 75 | // passing it onto the original callback. |
| 76 | void OnDialogClosed(content::WebContents* web_contents, |
| 77 | DialogClosedCallback callback, |
| 78 | bool success, |
| 79 | const base::string16& user_input); |
| 80 | |
| 81 | // Mapping between the WebContents and their extra data. The key |
| 82 | // is a void* because the pointer is just a cookie and is never dereferenced. |
| 83 | JavaScriptAppModalDialog::ExtraDataMap javascript_dialog_extra_data_; |
| 84 | |
dcheng | a0ee5fb | 2016-04-26 02:46:55 | [diff] [blame^] | 85 | std::unique_ptr<JavaScriptNativeDialogFactory> native_dialog_factory_; |
| 86 | std::unique_ptr<JavaScriptDialogExtensionsClient> extensions_client_; |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 87 | |
joenotcharles | 850904a | 2016-02-09 01:50:44 | [diff] [blame] | 88 | // Record a single create and close timestamp to track the time between |
| 89 | // dialogs. (Since Javascript dialogs are modal, this is even accurate!) |
| 90 | base::TimeTicks last_close_time_; |
| 91 | base::TimeTicks last_creation_time_; |
| 92 | |
oshima | 0929be2a | 2014-11-19 22:21:03 | [diff] [blame] | 93 | DISALLOW_COPY_AND_ASSIGN(JavaScriptDialogManager); |
| 94 | }; |
| 95 | |
| 96 | } // namespace app_modal |
oshima | 758abebc | 2014-11-06 10:55:50 | [diff] [blame] | 97 | |
oshima | f6539842 | 2014-11-18 23:30:42 | [diff] [blame] | 98 | #endif // COMPONENTS_APP_MODAL_JAVASCRIPT_DIALOG_MANAGER_H_ |