Mac: Fix performance issues with remote CoreAnimation
[chromium-blink-merge.git] / components / app_modal / javascript_dialog_manager.cc
blob5bf42fd4bfcdb8c9b3884cc47c272ffc6024a529
1 // Copyright 2014 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.
5 #include "components/app_modal/javascript_dialog_manager.h"
7 #include "base/bind.h"
8 #include "base/i18n/rtl.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/app_modal/app_modal_dialog.h"
11 #include "components/app_modal/app_modal_dialog_queue.h"
12 #include "components/app_modal/javascript_dialog_extensions_client.h"
13 #include "components/app_modal/javascript_native_dialog_factory.h"
14 #include "components/app_modal/native_app_modal_dialog.h"
15 #include "content/public/common/javascript_message_type.h"
16 #include "grit/components_strings.h"
17 #include "net/base/net_util.h"
18 #include "ui/base/l10n/l10n_util.h"
20 namespace app_modal {
21 namespace {
23 class DefaultExtensionsClient : public JavaScriptDialogExtensionsClient {
24 public:
25 DefaultExtensionsClient() {}
26 ~DefaultExtensionsClient() override {}
28 private:
29 // JavaScriptDialogExtensionsClient:
30 void OnDialogOpened(content::WebContents* web_contents) override {}
31 void OnDialogClosed(content::WebContents* web_contents) override {}
32 bool GetExtensionName(content::WebContents* web_contents,
33 const GURL& origin_url,
34 std::string* name_out) override {
35 return false;
38 DISALLOW_COPY_AND_ASSIGN(DefaultExtensionsClient);
41 } // namespace
43 ////////////////////////////////////////////////////////////////////////////////
44 // JavaScriptDialogManager, public:
46 // static
47 JavaScriptDialogManager* JavaScriptDialogManager::GetInstance() {
48 return Singleton<JavaScriptDialogManager>::get();
51 void JavaScriptDialogManager::SetNativeDialogFactory(
52 scoped_ptr<JavaScriptNativeDialogFactory> factory) {
53 native_dialog_factory_ = factory.Pass();
56 void JavaScriptDialogManager::SetExtensionsClient(
57 scoped_ptr<JavaScriptDialogExtensionsClient> extensions_client) {
58 extensions_client_ = extensions_client.Pass();
61 ////////////////////////////////////////////////////////////////////////////////
62 // JavaScriptDialogManager, private:
64 JavaScriptDialogManager::JavaScriptDialogManager()
65 : extensions_client_(new DefaultExtensionsClient) {
68 JavaScriptDialogManager::~JavaScriptDialogManager() {
71 void JavaScriptDialogManager::RunJavaScriptDialog(
72 content::WebContents* web_contents,
73 const GURL& origin_url,
74 const std::string& accept_lang,
75 content::JavaScriptMessageType message_type,
76 const base::string16& message_text,
77 const base::string16& default_prompt_text,
78 const DialogClosedCallback& callback,
79 bool* did_suppress_message) {
80 *did_suppress_message = false;
82 ChromeJavaScriptDialogExtraData* extra_data =
83 &javascript_dialog_extra_data_[web_contents];
85 if (extra_data->suppress_javascript_messages_) {
86 *did_suppress_message = true;
87 return;
90 base::TimeDelta time_since_last_message = base::TimeTicks::Now() -
91 extra_data->last_javascript_message_dismissal_;
92 bool display_suppress_checkbox = false;
93 // If a WebContents is impolite and displays a second JavaScript
94 // alert within kJavaScriptMessageExpectedDelay of a previous
95 // JavaScript alert being dismissed, show a checkbox offering to
96 // suppress future alerts from this WebContents.
97 const int kJavaScriptMessageExpectedDelay = 1000;
99 if (time_since_last_message <
100 base::TimeDelta::FromMilliseconds(kJavaScriptMessageExpectedDelay)) {
101 display_suppress_checkbox = true;
102 } else {
103 display_suppress_checkbox = false;
106 bool is_alert = message_type == content::JAVASCRIPT_MESSAGE_TYPE_ALERT;
107 base::string16 dialog_title =
108 GetTitle(web_contents, origin_url, accept_lang, is_alert);
110 extensions_client_->OnDialogOpened(web_contents);
112 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog(
113 web_contents,
114 &javascript_dialog_extra_data_,
115 dialog_title,
116 message_type,
117 message_text,
118 default_prompt_text,
119 display_suppress_checkbox,
120 false, // is_before_unload_dialog
121 false, // is_reload
122 base::Bind(&JavaScriptDialogManager::OnDialogClosed,
123 base::Unretained(this), web_contents, callback)));
126 void JavaScriptDialogManager::RunBeforeUnloadDialog(
127 content::WebContents* web_contents,
128 const base::string16& message_text,
129 bool is_reload,
130 const DialogClosedCallback& callback) {
131 const base::string16 title = l10n_util::GetStringUTF16(is_reload ?
132 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE);
133 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ?
134 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER);
136 base::string16 full_message =
137 message_text + base::ASCIIToUTF16("\n\n") + footer;
139 extensions_client_->OnDialogOpened(web_contents);
141 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog(
142 web_contents,
143 &javascript_dialog_extra_data_,
144 title,
145 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM,
146 full_message,
147 base::string16(), // default_prompt_text
148 false, // display_suppress_checkbox
149 true, // is_before_unload_dialog
150 is_reload,
151 base::Bind(&JavaScriptDialogManager::OnDialogClosed,
152 base::Unretained(this), web_contents, callback)));
155 bool JavaScriptDialogManager::HandleJavaScriptDialog(
156 content::WebContents* web_contents,
157 bool accept,
158 const base::string16* prompt_override) {
159 AppModalDialogQueue* dialog_queue = AppModalDialogQueue::GetInstance();
160 if (!dialog_queue->HasActiveDialog() ||
161 !dialog_queue->active_dialog()->IsJavaScriptModalDialog() ||
162 dialog_queue->active_dialog()->web_contents() != web_contents) {
163 return false;
165 JavaScriptAppModalDialog* dialog = static_cast<JavaScriptAppModalDialog*>(
166 dialog_queue->active_dialog());
167 if (accept) {
168 if (prompt_override)
169 dialog->SetOverridePromptText(*prompt_override);
170 dialog->native_dialog()->AcceptAppModalDialog();
171 } else {
172 dialog->native_dialog()->CancelAppModalDialog();
174 return true;
177 void JavaScriptDialogManager::WebContentsDestroyed(
178 content::WebContents* web_contents) {
179 CancelActiveAndPendingDialogs(web_contents);
180 javascript_dialog_extra_data_.erase(web_contents);
183 base::string16 JavaScriptDialogManager::GetTitle(
184 content::WebContents* web_contents,
185 const GURL& origin_url,
186 const std::string& accept_lang,
187 bool is_alert) {
188 // If the URL hasn't any host, return the default string.
189 if (!origin_url.has_host()) {
190 return l10n_util::GetStringUTF16(
191 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
192 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
195 // For extensions, show the extension name, but only if the origin of
196 // the alert matches the top-level WebContents.
197 std::string name;
198 if (extensions_client_->GetExtensionName(web_contents, origin_url, &name))
199 return base::UTF8ToUTF16(name);
201 // Otherwise, return the formatted URL.
202 // In this case, force URL to have LTR directionality.
203 base::string16 url_string = net::FormatUrl(origin_url, accept_lang);
204 return l10n_util::GetStringFUTF16(
205 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE
206 : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
207 base::i18n::GetDisplayStringInLTRDirectionality(url_string));
210 void JavaScriptDialogManager::CancelActiveAndPendingDialogs(
211 content::WebContents* web_contents) {
212 AppModalDialogQueue* queue = AppModalDialogQueue::GetInstance();
213 AppModalDialog* active_dialog = queue->active_dialog();
214 if (active_dialog && active_dialog->web_contents() == web_contents)
215 active_dialog->Invalidate();
216 for (AppModalDialogQueue::iterator i = queue->begin();
217 i != queue->end(); ++i) {
218 if ((*i)->web_contents() == web_contents)
219 (*i)->Invalidate();
223 void JavaScriptDialogManager::OnDialogClosed(
224 content::WebContents* web_contents,
225 DialogClosedCallback callback,
226 bool success,
227 const base::string16& user_input) {
228 // If an extension opened this dialog then the extension may shut down its
229 // lazy background page after the dialog closes. (Dialogs are closed before
230 // their WebContents is destroyed so |web_contents| is still valid here.)
231 extensions_client_->OnDialogClosed(web_contents);
233 callback.Run(success, user_input);
236 } // namespace app_modal