blob: 9d70bd329d701bd5eb0a410bb359108b43b8f0ec [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
avibc5337b2015-12-25 23:16:338#include "base/macros.h"
oshima758abebc2014-11-06 10:55:509#include "base/memory/scoped_ptr.h"
oshima0929be2a2014-11-19 22:21:0310#include "base/memory/singleton.h"
joenotcharles850904a2016-02-09 01:50:4411#include "base/time/time.h"
oshima0929be2a2014-11-19 22:21:0312#include "components/app_modal/javascript_app_modal_dialog.h"
13#include "content/public/browser/javascript_dialog_manager.h"
oshima758abebc2014-11-06 10:55:5014
oshima0929be2a2014-11-19 22:21:0315namespace app_modal {
oshima758abebc2014-11-06 10:55:5016
17class JavaScriptDialogExtensionsClient;
18class JavaScriptNativeDialogFactory;
19
oshima0929be2a2014-11-19 22:21:0320class JavaScriptDialogManager : public content::JavaScriptDialogManager {
21 public:
22 static JavaScriptDialogManager* GetInstance();
oshima758abebc2014-11-06 10:55:5023
oshima0929be2a2014-11-19 22:21:0324 JavaScriptNativeDialogFactory* native_dialog_factory() {
25 return native_dialog_factory_.get();
26 }
oshima758abebc2014-11-06 10:55:5027
oshima0929be2a2014-11-19 22:21:0328 // Sets the JavaScriptNativeDialogFactory used to create platform specific
29 // dialog window instances.
30 void SetNativeDialogFactory(
31 scoped_ptr<JavaScriptNativeDialogFactory> factory);
32
33 // JavaScript dialogs may be opened by an extensions/app, thus they need
34 // access to extensions functionality. This sets a client interface to
35 // access //extensions.
36 void SetExtensionsClient(
37 scoped_ptr<JavaScriptDialogExtensionsClient> extensions_client);
38
39 private:
olli.raula36aa8be2015-09-10 11:14:2240 friend struct base::DefaultSingletonTraits<JavaScriptDialogManager>;
oshima0929be2a2014-11-19 22:21:0341
42 JavaScriptDialogManager();
43 ~JavaScriptDialogManager() override;
44
45 // JavaScriptDialogManager:
46 void RunJavaScriptDialog(content::WebContents* web_contents,
47 const GURL& origin_url,
48 const std::string& accept_lang,
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,
55 const base::string16& message_text,
56 bool is_reload,
57 const DialogClosedCallback& callback) override;
58 bool HandleJavaScriptDialog(content::WebContents* web_contents,
59 bool accept,
60 const base::string16* prompt_override) override;
61 void CancelActiveAndPendingDialogs(
62 content::WebContents* web_contents) override;
avi2460c762015-04-17 15:21:5463 void ResetDialogState(content::WebContents* web_contents) override;
oshima0929be2a2014-11-19 22:21:0364
65 base::string16 GetTitle(content::WebContents* web_contents,
66 const GURL& origin_url,
67 const std::string& accept_lang,
68 bool is_alert);
69
70 // Wrapper around a DialogClosedCallback so that we can intercept it before
71 // passing it onto the original callback.
72 void OnDialogClosed(content::WebContents* web_contents,
73 DialogClosedCallback callback,
74 bool success,
75 const base::string16& user_input);
76
77 // Mapping between the WebContents and their extra data. The key
78 // is a void* because the pointer is just a cookie and is never dereferenced.
79 JavaScriptAppModalDialog::ExtraDataMap javascript_dialog_extra_data_;
80
81 scoped_ptr<JavaScriptNativeDialogFactory> native_dialog_factory_;
82 scoped_ptr<JavaScriptDialogExtensionsClient> extensions_client_;
83
joenotcharles850904a2016-02-09 01:50:4484 // Record a single create and close timestamp to track the time between
85 // dialogs. (Since Javascript dialogs are modal, this is even accurate!)
86 base::TimeTicks last_close_time_;
87 base::TimeTicks last_creation_time_;
88
oshima0929be2a2014-11-19 22:21:0389 DISALLOW_COPY_AND_ASSIGN(JavaScriptDialogManager);
90};
91
92} // namespace app_modal
oshima758abebc2014-11-06 10:55:5093
oshimaf65398422014-11-18 23:30:4294#endif // COMPONENTS_APP_MODAL_JAVASCRIPT_DIALOG_MANAGER_H_