blob: 97bb6b523605668a1d0368818f8e2f15d0e80aa1 [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
Evan Stade7220e472020-01-31 17:06:575#ifndef COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_MANAGER_H_
6#define COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_MANAGER_H_
oshima758abebc2014-11-06 10:55:507
dchenga0ee5fb2016-04-26 02:46:558#include <memory>
9
Evan Stade0fa711a2020-01-13 21:03:0310#include "base/callback.h"
Avi Drissman82c45002017-11-29 22:32:2911#include "base/gtest_prod_util.h"
avibc5337b2015-12-25 23:16:3312#include "base/macros.h"
oshima0929be2a2014-11-19 22:21:0313#include "base/memory/singleton.h"
Evan Stade7220e472020-01-31 17:06:5714#include "components/javascript_dialogs/app_modal_dialog_controller.h"
oshima0929be2a2014-11-19 22:21:0315#include "content/public/browser/javascript_dialog_manager.h"
oshima758abebc2014-11-06 10:55:5016
Evan Stade7220e472020-01-31 17:06:5717namespace javascript_dialogs {
oshima758abebc2014-11-06 10:55:5018
Evan Stade7220e472020-01-31 17:06:5719class ExtensionsClient;
20class AppModalViewFactory;
oshima758abebc2014-11-06 10:55:5021
Evan Stade7220e472020-01-31 17:06:5722class AppModalDialogManager : public content::JavaScriptDialogManager {
oshima0929be2a2014-11-19 22:21:0323 public:
Evan Stade0fa711a2020-01-13 21:03:0324 // A factory method to create and returns a platform-specific dialog class.
25 // The returned object should own itself.
Evan Stade7220e472020-01-31 17:06:5726 using AppModalViewFactory =
27 base::RepeatingCallback<AppModalDialogView*(AppModalDialogController*)>;
oshima758abebc2014-11-06 10:55:5028
Evan Stade7220e472020-01-31 17:06:5729 static AppModalDialogManager* GetInstance();
oshima758abebc2014-11-06 10:55:5030
Evan Stade7220e472020-01-31 17:06:5731 // Sets the AppModalViewFactory used to create platform specific
oshima0929be2a2014-11-19 22:21:0332 // dialog window instances.
Evan Stade7220e472020-01-31 17:06:5733 void SetNativeDialogFactory(AppModalViewFactory factory);
Evan Stade0fa711a2020-01-13 21:03:0334
Evan Stade7220e472020-01-31 17:06:5735 AppModalViewFactory* view_factory() { return &view_factory_; }
oshima0929be2a2014-11-19 22:21:0336
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 Stade7220e472020-01-31 17:06:5740 void SetExtensionsClient(std::unique_ptr<ExtensionsClient> extensions_client);
oshima0929be2a2014-11-19 22:21:0341
avi7a1b55b2016-10-19 04:18:3842 // Gets the title for a dialog.
43 base::string16 GetTitle(content::WebContents* web_contents,
Devlin Cronin347452f32017-08-28 18:22:3144 const GURL& alerting_frame_url);
oshima0929be2a2014-11-19 22:21:0345
Renee Wright5ab57862018-05-30 00:09:1546 // 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 Stade7220e472020-01-31 17:06:5755 // content::JavaScriptDialogManager:
oshima0929be2a2014-11-19 22:21:0356 void RunJavaScriptDialog(content::WebContents* web_contents,
Avi Drissman2fefc4b2018-02-22 20:41:0157 content::RenderFrameHost* render_frame_host,
avi777ff452017-02-09 19:04:4858 content::JavaScriptDialogType dialog_type,
oshima0929be2a2014-11-19 22:21:0359 const base::string16& message_text,
60 const base::string16& default_prompt_text,
Avi Drissmane04d3992017-10-05 15:11:3661 DialogClosedCallback callback,
oshima0929be2a2014-11-19 22:21:0362 bool* did_suppress_message) override;
63 void RunBeforeUnloadDialog(content::WebContents* web_contents,
Avi Drissmanff9ed752017-11-28 21:18:1264 content::RenderFrameHost* render_frame_host,
oshima0929be2a2014-11-19 22:21:0365 bool is_reload,
Avi Drissmane04d3992017-10-05 15:11:3666 DialogClosedCallback callback) override;
oshima0929be2a2014-11-19 22:21:0367 bool HandleJavaScriptDialog(content::WebContents* web_contents,
68 bool accept,
69 const base::string16* prompt_override) override;
avi5d3b8692016-10-12 22:00:4670 void CancelDialogs(content::WebContents* web_contents,
avi5d3b8692016-10-12 22:00:4671 bool reset_state) override;
oshima0929be2a2014-11-19 22:21:0372
avi7a1b55b2016-10-19 04:18:3873 private:
Evan Stade7220e472020-01-31 17:06:5774 FRIEND_TEST_ALL_PREFIXES(AppModalDialogManagerTest, GetTitle);
75 friend struct base::DefaultSingletonTraits<AppModalDialogManager>;
avi7a1b55b2016-10-19 04:18:3876
Evan Stade7220e472020-01-31 17:06:5777 AppModalDialogManager();
78 ~AppModalDialogManager() override;
oshima0929be2a2014-11-19 22:21:0379
avie163d2842016-02-22 22:39:2180 // 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
oshima0929be2a2014-11-19 22:21:0386 // 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 Drissman82c45002017-11-29 22:32:2993 static base::string16 GetTitleImpl(const GURL& parent_frame_url,
94 const GURL& alerting_frame_url);
95
oshima0929be2a2014-11-19 22:21:0396 // 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 Stade7220e472020-01-31 17:06:5798 AppModalDialogController::ExtraDataMap javascript_dialog_extra_data_;
oshima0929be2a2014-11-19 22:21:0399
Evan Stade7220e472020-01-31 17:06:57100 AppModalViewFactory view_factory_;
101 std::unique_ptr<ExtensionsClient> extensions_client_;
oshima0929be2a2014-11-19 22:21:03102
Evan Stade7220e472020-01-31 17:06:57103 DISALLOW_COPY_AND_ASSIGN(AppModalDialogManager);
oshima0929be2a2014-11-19 22:21:03104};
105
Evan Stade7220e472020-01-31 17:06:57106} // namespace javascript_dialogs
oshima758abebc2014-11-06 10:55:50107
Evan Stade7220e472020-01-31 17:06:57108#endif // COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_MANAGER_H_