blob: 490881465eefc9dfc9489cd0a5c68e29eacb53e7 [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
oshimaf65398422014-11-18 23:30:425#ifndef COMPONENTS_APP_MODAL_JAVASCRIPT_DIALOG_MANAGER_H_
6#define COMPONENTS_APP_MODAL_JAVASCRIPT_DIALOG_MANAGER_H_
oshima758abebc2014-11-06 10:55:507
dchenga0ee5fb2016-04-26 02:46:558#include <memory>
9
Avi Drissman82c45002017-11-29 22:32:2910#include "base/gtest_prod_util.h"
avibc5337b2015-12-25 23:16:3311#include "base/macros.h"
oshima0929be2a2014-11-19 22:21:0312#include "base/memory/singleton.h"
joenotcharles850904a2016-02-09 01:50:4413#include "base/time/time.h"
oshima0929be2a2014-11-19 22:21:0314#include "components/app_modal/javascript_app_modal_dialog.h"
15#include "content/public/browser/javascript_dialog_manager.h"
oshima758abebc2014-11-06 10:55:5016
oshima0929be2a2014-11-19 22:21:0317namespace app_modal {
oshima758abebc2014-11-06 10:55:5018
19class JavaScriptDialogExtensionsClient;
20class JavaScriptNativeDialogFactory;
21
oshima0929be2a2014-11-19 22:21:0322class JavaScriptDialogManager : public content::JavaScriptDialogManager {
23 public:
24 static JavaScriptDialogManager* GetInstance();
oshima758abebc2014-11-06 10:55:5025
oshima0929be2a2014-11-19 22:21:0326 JavaScriptNativeDialogFactory* native_dialog_factory() {
27 return native_dialog_factory_.get();
28 }
oshima758abebc2014-11-06 10:55:5029
oshima0929be2a2014-11-19 22:21:0330 // Sets the JavaScriptNativeDialogFactory used to create platform specific
31 // dialog window instances.
32 void SetNativeDialogFactory(
dchenga0ee5fb2016-04-26 02:46:5533 std::unique_ptr<JavaScriptNativeDialogFactory> factory);
oshima0929be2a2014-11-19 22:21:0334
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(
dchenga0ee5fb2016-04-26 02:46:5539 std::unique_ptr<JavaScriptDialogExtensionsClient> extensions_client);
oshima0929be2a2014-11-19 22:21:0340
avi7a1b55b2016-10-19 04:18:3841 // Gets the title for a dialog.
42 base::string16 GetTitle(content::WebContents* web_contents,
Devlin Cronin347452f32017-08-28 18:22:3143 const GURL& alerting_frame_url);
oshima0929be2a2014-11-19 22:21:0344
Renee Wright5ab57862018-05-30 00:09:1545 // 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
oshima0929be2a2014-11-19 22:21:0354 // JavaScriptDialogManager:
55 void RunJavaScriptDialog(content::WebContents* web_contents,
Avi Drissman2fefc4b2018-02-22 20:41:0156 content::RenderFrameHost* render_frame_host,
avi777ff452017-02-09 19:04:4857 content::JavaScriptDialogType dialog_type,
oshima0929be2a2014-11-19 22:21:0358 const base::string16& message_text,
59 const base::string16& default_prompt_text,
Avi Drissmane04d3992017-10-05 15:11:3660 DialogClosedCallback callback,
oshima0929be2a2014-11-19 22:21:0361 bool* did_suppress_message) override;
62 void RunBeforeUnloadDialog(content::WebContents* web_contents,
Avi Drissmanff9ed752017-11-28 21:18:1263 content::RenderFrameHost* render_frame_host,
oshima0929be2a2014-11-19 22:21:0364 bool is_reload,
Avi Drissmane04d3992017-10-05 15:11:3665 DialogClosedCallback callback) override;
oshima0929be2a2014-11-19 22:21:0366 bool HandleJavaScriptDialog(content::WebContents* web_contents,
67 bool accept,
68 const base::string16* prompt_override) override;
avi5d3b8692016-10-12 22:00:4669 void CancelDialogs(content::WebContents* web_contents,
avi5d3b8692016-10-12 22:00:4670 bool reset_state) override;
oshima0929be2a2014-11-19 22:21:0371
avi7a1b55b2016-10-19 04:18:3872 private:
Avi Drissman82c45002017-11-29 22:32:2973 FRIEND_TEST_ALL_PREFIXES(JavaScriptDialogManagerTest, GetTitle);
avi7a1b55b2016-10-19 04:18:3874 friend struct base::DefaultSingletonTraits<JavaScriptDialogManager>;
75
76 JavaScriptDialogManager();
77 ~JavaScriptDialogManager() override;
oshima0929be2a2014-11-19 22:21:0378
avie163d2842016-02-22 22:39:2179 // 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
oshima0929be2a2014-11-19 22:21:0385 // 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 Drissman82c45002017-11-29 22:32:2992 static base::string16 GetTitleImpl(const GURL& parent_frame_url,
93 const GURL& alerting_frame_url);
94
oshima0929be2a2014-11-19 22:21:0395 // 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
dchenga0ee5fb2016-04-26 02:46:5599 std::unique_ptr<JavaScriptNativeDialogFactory> native_dialog_factory_;
100 std::unique_ptr<JavaScriptDialogExtensionsClient> extensions_client_;
oshima0929be2a2014-11-19 22:21:03101
joenotcharles850904a2016-02-09 01:50:44102 // 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
oshima0929be2a2014-11-19 22:21:03107 DISALLOW_COPY_AND_ASSIGN(JavaScriptDialogManager);
108};
109
110} // namespace app_modal
oshima758abebc2014-11-06 10:55:50111
oshimaf65398422014-11-18 23:30:42112#endif // COMPONENTS_APP_MODAL_JAVASCRIPT_DIALOG_MANAGER_H_