blob: d9e3e97bb567b43ef6e3e3f09b2da5f24fabb421 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2012 The Chromium Authors
oshima758abebc2014-11-06 10:55:502// 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
Avi Drissman12be0312023-01-11 09:16:0910#include "base/functional/callback.h"
Avi Drissman82c45002017-11-29 22:32:2911#include "base/gtest_prod_util.h"
oshima0929be2a2014-11-19 22:21:0312#include "base/memory/singleton.h"
Evan Stade7220e472020-01-31 17:06:5713#include "components/javascript_dialogs/app_modal_dialog_controller.h"
Zelin Liue93cb9b2023-04-10 22:13:1614#include "components/javascript_dialogs/app_modal_dialog_manager_delegate.h"
oshima0929be2a2014-11-19 22:21:0315#include "content/public/browser/javascript_dialog_manager.h"
oshima758abebc2014-11-06 10:55:5016
Avi Drissman649f10a2021-09-07 19:23:0317namespace url {
18class Origin;
19}
20
Evan Stade7220e472020-01-31 17:06:5721namespace javascript_dialogs {
oshima758abebc2014-11-06 10:55:5022
Evan Stade7220e472020-01-31 17:06:5723class ExtensionsClient;
24class AppModalViewFactory;
Zelin Liue93cb9b2023-04-10 22:13:1625class AppModalDialogManagerDelegate;
oshima758abebc2014-11-06 10:55:5026
Evan Stade7220e472020-01-31 17:06:5727class AppModalDialogManager : public content::JavaScriptDialogManager {
oshima0929be2a2014-11-19 22:21:0328 public:
Evan Stade0fa711a2020-01-13 21:03:0329 // A factory method to create and returns a platform-specific dialog class.
30 // The returned object should own itself.
Evan Stade7220e472020-01-31 17:06:5731 using AppModalViewFactory =
32 base::RepeatingCallback<AppModalDialogView*(AppModalDialogController*)>;
oshima758abebc2014-11-06 10:55:5033
Evan Stade7220e472020-01-31 17:06:5734 static AppModalDialogManager* GetInstance();
oshima758abebc2014-11-06 10:55:5035
Peter Boström9f667c382021-10-01 20:09:3136 AppModalDialogManager(const AppModalDialogManager&) = delete;
37 AppModalDialogManager& operator=(const AppModalDialogManager&) = delete;
38
Evan Stade7220e472020-01-31 17:06:5739 // Sets the AppModalViewFactory used to create platform specific
oshima0929be2a2014-11-19 22:21:0340 // dialog window instances.
Evan Stade7220e472020-01-31 17:06:5741 void SetNativeDialogFactory(AppModalViewFactory factory);
Evan Stade0fa711a2020-01-13 21:03:0342
Evan Stade7220e472020-01-31 17:06:5743 AppModalViewFactory* view_factory() { return &view_factory_; }
oshima0929be2a2014-11-19 22:21:0344
45 // JavaScript dialogs may be opened by an extensions/app, thus they need
46 // access to extensions functionality. This sets a client interface to
47 // access //extensions.
Evan Stade7220e472020-01-31 17:06:5748 void SetExtensionsClient(std::unique_ptr<ExtensionsClient> extensions_client);
oshima0929be2a2014-11-19 22:21:0349
Zelin Liue93cb9b2023-04-10 22:13:1650 void SetDelegate(std::unique_ptr<AppModalDialogManagerDelegate> delegate);
51
avi7a1b55b2016-10-19 04:18:3852 // Gets the title for a dialog.
Jan Wilken Dörriefa241ba2021-03-11 17:57:0153 std::u16string GetTitle(content::WebContents* web_contents,
Avi Drissman649f10a2021-09-07 19:23:0354 const url::Origin& alerting_frame_origin);
oshima0929be2a2014-11-19 22:21:0355
Renee Wright5ab57862018-05-30 00:09:1556 // Displays a dialog asking the user if they want to leave a page. Displays
57 // a different message if the site is in an app window.
58 void RunBeforeUnloadDialogWithOptions(
59 content::WebContents* web_contents,
60 content::RenderFrameHost* render_frame_host,
61 bool is_reload,
62 bool is_app,
63 DialogClosedCallback callback);
64
Evan Stade7220e472020-01-31 17:06:5765 // content::JavaScriptDialogManager:
oshima0929be2a2014-11-19 22:21:0366 void RunJavaScriptDialog(content::WebContents* web_contents,
Avi Drissman2fefc4b2018-02-22 20:41:0167 content::RenderFrameHost* render_frame_host,
avi777ff452017-02-09 19:04:4868 content::JavaScriptDialogType dialog_type,
Jan Wilken Dörriefa241ba2021-03-11 17:57:0169 const std::u16string& message_text,
70 const std::u16string& default_prompt_text,
Avi Drissmane04d3992017-10-05 15:11:3671 DialogClosedCallback callback,
oshima0929be2a2014-11-19 22:21:0372 bool* did_suppress_message) override;
73 void RunBeforeUnloadDialog(content::WebContents* web_contents,
Avi Drissmanff9ed752017-11-28 21:18:1274 content::RenderFrameHost* render_frame_host,
oshima0929be2a2014-11-19 22:21:0375 bool is_reload,
Avi Drissmane04d3992017-10-05 15:11:3676 DialogClosedCallback callback) override;
oshima0929be2a2014-11-19 22:21:0377 bool HandleJavaScriptDialog(content::WebContents* web_contents,
78 bool accept,
Jan Wilken Dörriefa241ba2021-03-11 17:57:0179 const std::u16string* prompt_override) override;
avi5d3b8692016-10-12 22:00:4680 void CancelDialogs(content::WebContents* web_contents,
avi5d3b8692016-10-12 22:00:4681 bool reset_state) override;
oshima0929be2a2014-11-19 22:21:0382
Zelin Liue93cb9b2023-04-10 22:13:1683 static std::u16string GetSiteFrameTitle(
84 const url::Origin& main_frame_origin,
85 const url::Origin& alerting_frame_origin);
86
avi7a1b55b2016-10-19 04:18:3887 private:
Evan Stade7220e472020-01-31 17:06:5788 FRIEND_TEST_ALL_PREFIXES(AppModalDialogManagerTest, GetTitle);
89 friend struct base::DefaultSingletonTraits<AppModalDialogManager>;
avi7a1b55b2016-10-19 04:18:3890
Evan Stade7220e472020-01-31 17:06:5791 AppModalDialogManager();
92 ~AppModalDialogManager() override;
oshima0929be2a2014-11-19 22:21:0393
94 // Wrapper around a DialogClosedCallback so that we can intercept it before
95 // passing it onto the original callback.
96 void OnDialogClosed(content::WebContents* web_contents,
97 DialogClosedCallback callback,
98 bool success,
Jan Wilken Dörriefa241ba2021-03-11 17:57:0199 const std::u16string& user_input);
oshima0929be2a2014-11-19 22:21:03100
101 // Mapping between the WebContents and their extra data. The key
102 // is a void* because the pointer is just a cookie and is never dereferenced.
Evan Stade7220e472020-01-31 17:06:57103 AppModalDialogController::ExtraDataMap javascript_dialog_extra_data_;
oshima0929be2a2014-11-19 22:21:03104
Evan Stade7220e472020-01-31 17:06:57105 AppModalViewFactory view_factory_;
Zelin Liue93cb9b2023-04-10 22:13:16106
Evan Stade7220e472020-01-31 17:06:57107 std::unique_ptr<ExtensionsClient> extensions_client_;
Zelin Liue93cb9b2023-04-10 22:13:16108
109 std::unique_ptr<AppModalDialogManagerDelegate> delegate_;
oshima0929be2a2014-11-19 22:21:03110};
111
Evan Stade7220e472020-01-31 17:06:57112} // namespace javascript_dialogs
oshima758abebc2014-11-06 10:55:50113
Evan Stade7220e472020-01-31 17:06:57114#endif // COMPONENTS_JAVASCRIPT_DIALOGS_APP_MODAL_DIALOG_MANAGER_H_