blob: 626057da6e96f3b8a7f5423e7870fa0ab4e7be99 [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
avibc5337b2015-12-25 23:16:3310#include "base/macros.h"
oshima0929be2a2014-11-19 22:21:0311#include "base/memory/singleton.h"
joenotcharles850904a2016-02-09 01:50:4412#include "base/time/time.h"
oshima0929be2a2014-11-19 22:21:0313#include "components/app_modal/javascript_app_modal_dialog.h"
14#include "content/public/browser/javascript_dialog_manager.h"
oshima758abebc2014-11-06 10:55:5015
oshima0929be2a2014-11-19 22:21:0316namespace app_modal {
oshima758abebc2014-11-06 10:55:5017
18class JavaScriptDialogExtensionsClient;
19class JavaScriptNativeDialogFactory;
20
oshima0929be2a2014-11-19 22:21:0321class JavaScriptDialogManager : public content::JavaScriptDialogManager {
22 public:
23 static JavaScriptDialogManager* GetInstance();
oshima758abebc2014-11-06 10:55:5024
oshima0929be2a2014-11-19 22:21:0325 JavaScriptNativeDialogFactory* native_dialog_factory() {
26 return native_dialog_factory_.get();
27 }
oshima758abebc2014-11-06 10:55:5028
oshima0929be2a2014-11-19 22:21:0329 // Sets the JavaScriptNativeDialogFactory used to create platform specific
30 // dialog window instances.
31 void SetNativeDialogFactory(
dchenga0ee5fb2016-04-26 02:46:5532 std::unique_ptr<JavaScriptNativeDialogFactory> factory);
oshima0929be2a2014-11-19 22:21:0333
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(
dchenga0ee5fb2016-04-26 02:46:5538 std::unique_ptr<JavaScriptDialogExtensionsClient> extensions_client);
oshima0929be2a2014-11-19 22:21:0339
40 private:
olli.raula36aa8be2015-09-10 11:14:2241 friend struct base::DefaultSingletonTraits<JavaScriptDialogManager>;
oshima0929be2a2014-11-19 22:21:0342
43 JavaScriptDialogManager();
44 ~JavaScriptDialogManager() override;
45
46 // JavaScriptDialogManager:
47 void RunJavaScriptDialog(content::WebContents* web_contents,
48 const GURL& origin_url,
oshima0929be2a2014-11-19 22:21:0349 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,
oshima0929be2a2014-11-19 22:21:0355 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;
avi2460c762015-04-17 15:21:5462 void ResetDialogState(content::WebContents* web_contents) override;
oshima0929be2a2014-11-19 22:21:0363
64 base::string16 GetTitle(content::WebContents* web_contents,
65 const GURL& origin_url,
oshima0929be2a2014-11-19 22:21:0366 bool is_alert);
67
avie163d2842016-02-22 22:39:2168 // 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
oshima0929be2a2014-11-19 22:21:0374 // 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
dchenga0ee5fb2016-04-26 02:46:5585 std::unique_ptr<JavaScriptNativeDialogFactory> native_dialog_factory_;
86 std::unique_ptr<JavaScriptDialogExtensionsClient> extensions_client_;
oshima0929be2a2014-11-19 22:21:0387
joenotcharles850904a2016-02-09 01:50:4488 // 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
oshima0929be2a2014-11-19 22:21:0393 DISALLOW_COPY_AND_ASSIGN(JavaScriptDialogManager);
94};
95
96} // namespace app_modal
oshima758abebc2014-11-06 10:55:5097
oshimaf65398422014-11-18 23:30:4298#endif // COMPONENTS_APP_MODAL_JAVASCRIPT_DIALOG_MANAGER_H_