Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(632)

Side by Side Diff: components/app_modal/javascript_dialog_manager.cc

Issue 688853005: Consolidate javascript_dialog_manager.h and javascript_dialog_manager_impl.h (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/app_modal/javascript_dialog_manager.h" 5 #include "components/app_modal/javascript_dialog_manager.h"
6 6
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"
7 #include "components/app_modal/javascript_dialog_extensions_client.h" 12 #include "components/app_modal/javascript_dialog_extensions_client.h"
8 #include "components/app_modal/javascript_dialog_manager_impl.h"
9 #include "components/app_modal/javascript_native_dialog_factory.h" 13 #include "components/app_modal/javascript_native_dialog_factory.h"
10 14 #include "components/app_modal/native_app_modal_dialog.h"
11 content::JavaScriptDialogManager* GetJavaScriptDialogManagerInstance() { 15 #include "content/public/common/javascript_message_type.h"
12 return JavaScriptDialogManagerImpl::GetInstance(); 16 #include "grit/components_strings.h"
13 } 17 #include "net/base/net_util.h"
14 18 #include "ui/base/l10n/l10n_util.h"
15 void SetJavaScriptNativeDialogFactory( 19
16 scoped_ptr<JavaScriptNativeDialogFactory> new_factory) { 20 namespace app_modal {
17 JavaScriptDialogManagerImpl::GetInstance()->SetNativeDialogFactory( 21 namespace {
18 new_factory.Pass()); 22
19 } 23 class DefaultExtensionsClient : public JavaScriptDialogExtensionsClient {
20 24 public:
21 void SetJavaScriptDialogExtensionsClient( 25 DefaultExtensionsClient() {}
22 scoped_ptr<JavaScriptDialogExtensionsClient> new_client) { 26 ~DefaultExtensionsClient() override {}
23 JavaScriptDialogManagerImpl::GetInstance()->SetExtensionsClient( 27
24 new_client.Pass()); 28 private:
25 } 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;
36 }
37
38 DISALLOW_COPY_AND_ASSIGN(DefaultExtensionsClient);
39 };
40
41 } // namespace
42
43 ////////////////////////////////////////////////////////////////////////////////
44 // JavaScriptDialogManager, public:
45
46 // static
47 JavaScriptDialogManager* JavaScriptDialogManager::GetInstance() {
48 return Singleton<JavaScriptDialogManager>::get();
49 }
50
51 void JavaScriptDialogManager::SetNativeDialogFactory(
52 scoped_ptr<JavaScriptNativeDialogFactory> factory) {
53 native_dialog_factory_ = factory.Pass();
54 }
55
56 void JavaScriptDialogManager::SetExtensionsClient(
57 scoped_ptr<JavaScriptDialogExtensionsClient> extensions_client) {
58 extensions_client_ = extensions_client.Pass();
59 }
60
61 ////////////////////////////////////////////////////////////////////////////////
62 // JavaScriptDialogManager, private:
63
64 JavaScriptDialogManager::JavaScriptDialogManager()
65 : extensions_client_(new DefaultExtensionsClient) {
66 }
67
68 JavaScriptDialogManager::~JavaScriptDialogManager() {
69 }
70
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;
81
82 ChromeJavaScriptDialogExtraData* extra_data =
83 &javascript_dialog_extra_data_[web_contents];
84
85 if (extra_data->suppress_javascript_messages_) {
86 *did_suppress_message = true;
87 return;
88 }
89
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;
98
99 if (time_since_last_message <
100 base::TimeDelta::FromMilliseconds(kJavaScriptMessageExpectedDelay)) {
101 display_suppress_checkbox = true;
102 } else {
103 display_suppress_checkbox = false;
104 }
105
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);
109
110 extensions_client_->OnDialogOpened(web_contents);
111
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)));
124 }
125
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);
135
136 base::string16 full_message =
137 message_text + base::ASCIIToUTF16("\n\n") + footer;
138
139 extensions_client_->OnDialogOpened(web_contents);
140
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)));
153 }
154
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;
164 }
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();
173 }
174 return true;
175 }
176
177 void JavaScriptDialogManager::WebContentsDestroyed(
178 content::WebContents* web_contents) {
179 CancelActiveAndPendingDialogs(web_contents);
180 javascript_dialog_extra_data_.erase(web_contents);
181 }
182
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);
193 }
194
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);
200
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));
208 }
209
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();
220 }
221 }
222
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);
232
233 callback.Run(success, user_input);
234 }
235
236 } // namespace app_modal
OLDNEW
« no previous file with comments | « components/app_modal/javascript_dialog_manager.h ('k') | components/app_modal/javascript_dialog_manager_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698