Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "components/app_modal/app_modal_dialog.h" | 14 #include "components/app_modal/app_modal_dialog.h" |
| 15 #include "components/app_modal/app_modal_dialog_queue.h" | 15 #include "components/app_modal/app_modal_dialog_queue.h" |
| 16 #include "components/app_modal/javascript_app_modal_dialog.h" | 16 #include "components/app_modal/javascript_app_modal_dialog.h" |
| 17 #include "components/app_modal/javascript_dialog_extensions_client.h" | 17 #include "components/app_modal/javascript_dialog_extensions_client.h" |
| 18 #include "components/app_modal/javascript_native_dialog_factory.h" | 18 #include "components/app_modal/javascript_native_dialog_factory.h" |
| 19 #include "components/app_modal/native_app_modal_dialog.h" | 19 #include "components/app_modal/native_app_modal_dialog.h" |
| 20 #include "components/url_formatter/elide_url.h" | 20 #include "components/url_formatter/elide_url.h" |
| 21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/common/javascript_message_type.h" | 22 #include "content/public/common/javascript_message_type.h" |
| 23 #include "grit/components_strings.h" | 23 #include "grit/components_strings.h" |
| 24 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
| 25 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 26 #include "ui/gfx/font_list.h" | 26 #include "ui/gfx/font_list.h" |
| 27 #include "ui/gfx/text_elider.h" | |
| 27 | 28 |
| 28 namespace app_modal { | 29 namespace app_modal { |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 #if !defined(OS_ANDROID) | 32 #if !defined(OS_ANDROID) |
| 32 // Keep in sync with kDefaultMessageWidth, but allow some space for the rest of | 33 // Keep in sync with kDefaultMessageWidth, but allow some space for the rest of |
| 33 // the text. | 34 // the text. |
| 34 const int kUrlElideWidth = 350; | 35 const int kUrlElideWidth = 350; |
| 35 #endif | 36 #endif |
| 36 | 37 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 50 } | 51 } |
| 51 | 52 |
| 52 DISALLOW_COPY_AND_ASSIGN(DefaultExtensionsClient); | 53 DISALLOW_COPY_AND_ASSIGN(DefaultExtensionsClient); |
| 53 }; | 54 }; |
| 54 | 55 |
| 55 bool ShouldDisplaySuppressCheckbox( | 56 bool ShouldDisplaySuppressCheckbox( |
| 56 ChromeJavaScriptDialogExtraData* extra_data) { | 57 ChromeJavaScriptDialogExtraData* extra_data) { |
| 57 return extra_data->has_already_shown_a_dialog_; | 58 return extra_data->has_already_shown_a_dialog_; |
| 58 } | 59 } |
| 59 | 60 |
| 61 // JavaScript dialogs are heavily used for scams; reduce their power by removing | |
| 62 // their ability to show long messages. | |
| 63 base::string16 TruncateMessage(const base::string16& message) { | |
| 64 return gfx::TruncateString(message, 100, gfx::CHARACTER_BREAK); | |
|
Peter Kasting
2016/02/19 02:18:34
Please use an eliding method rather than a truncat
Avi (use Gerrit)
2016/02/19 02:24:42
This was a bad implementation; doing it in a new w
| |
| 65 } | |
| 66 | |
| 60 } // namespace | 67 } // namespace |
| 61 | 68 |
| 62 //////////////////////////////////////////////////////////////////////////////// | 69 //////////////////////////////////////////////////////////////////////////////// |
| 63 // JavaScriptDialogManager, public: | 70 // JavaScriptDialogManager, public: |
| 64 | 71 |
| 65 // static | 72 // static |
| 66 JavaScriptDialogManager* JavaScriptDialogManager::GetInstance() { | 73 JavaScriptDialogManager* JavaScriptDialogManager::GetInstance() { |
| 67 return base::Singleton<JavaScriptDialogManager>::get(); | 74 return base::Singleton<JavaScriptDialogManager>::get(); |
| 68 } | 75 } |
| 69 | 76 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 146 base::string16 dialog_title = | 153 base::string16 dialog_title = |
| 147 GetTitle(web_contents, origin_url, accept_lang, is_alert); | 154 GetTitle(web_contents, origin_url, accept_lang, is_alert); |
| 148 | 155 |
| 149 extensions_client_->OnDialogOpened(web_contents); | 156 extensions_client_->OnDialogOpened(web_contents); |
| 150 | 157 |
| 151 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | 158 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( |
| 152 web_contents, | 159 web_contents, |
| 153 &javascript_dialog_extra_data_, | 160 &javascript_dialog_extra_data_, |
| 154 dialog_title, | 161 dialog_title, |
| 155 message_type, | 162 message_type, |
| 156 message_text, | 163 TruncateMessage(message_text), |
| 157 default_prompt_text, | 164 default_prompt_text, |
| 158 ShouldDisplaySuppressCheckbox(extra_data), | 165 ShouldDisplaySuppressCheckbox(extra_data), |
| 159 false, // is_before_unload_dialog | 166 false, // is_before_unload_dialog |
| 160 false, // is_reload | 167 false, // is_reload |
| 161 base::Bind(&JavaScriptDialogManager::OnDialogClosed, | 168 base::Bind(&JavaScriptDialogManager::OnDialogClosed, |
| 162 base::Unretained(this), web_contents, callback))); | 169 base::Unretained(this), web_contents, callback))); |
| 163 } | 170 } |
| 164 | 171 |
| 165 void JavaScriptDialogManager::RunBeforeUnloadDialog( | 172 void JavaScriptDialogManager::RunBeforeUnloadDialog( |
| 166 content::WebContents* web_contents, | 173 content::WebContents* web_contents, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 177 // lost its privilege to deny unloading. | 184 // lost its privilege to deny unloading. |
| 178 callback.Run(true, base::string16()); | 185 callback.Run(true, base::string16()); |
| 179 return; | 186 return; |
| 180 } | 187 } |
| 181 | 188 |
| 182 const base::string16 title = l10n_util::GetStringUTF16(is_reload ? | 189 const base::string16 title = l10n_util::GetStringUTF16(is_reload ? |
| 183 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); | 190 IDS_BEFORERELOAD_MESSAGEBOX_TITLE : IDS_BEFOREUNLOAD_MESSAGEBOX_TITLE); |
| 184 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ? | 191 const base::string16 footer = l10n_util::GetStringUTF16(is_reload ? |
| 185 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); | 192 IDS_BEFORERELOAD_MESSAGEBOX_FOOTER : IDS_BEFOREUNLOAD_MESSAGEBOX_FOOTER); |
| 186 | 193 |
| 187 base::string16 full_message = | 194 base::string16 message = |
| 188 message_text + base::ASCIIToUTF16("\n\n") + footer; | 195 TruncateMessage(message_text) + base::ASCIIToUTF16("\n\n") + footer; |
| 189 | 196 |
| 190 extensions_client_->OnDialogOpened(web_contents); | 197 extensions_client_->OnDialogOpened(web_contents); |
| 191 | 198 |
| 192 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( | 199 AppModalDialogQueue::GetInstance()->AddDialog(new JavaScriptAppModalDialog( |
| 193 web_contents, | 200 web_contents, |
| 194 &javascript_dialog_extra_data_, | 201 &javascript_dialog_extra_data_, |
| 195 title, | 202 title, |
| 196 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, | 203 content::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, |
| 197 full_message, | 204 message, |
| 198 base::string16(), // default_prompt_text | 205 base::string16(), // default_prompt_text |
| 199 ShouldDisplaySuppressCheckbox(extra_data), | 206 ShouldDisplaySuppressCheckbox(extra_data), |
| 200 true, // is_before_unload_dialog | 207 true, // is_before_unload_dialog |
| 201 is_reload, | 208 is_reload, |
| 202 base::Bind(&JavaScriptDialogManager::OnDialogClosed, | 209 base::Bind(&JavaScriptDialogManager::OnDialogClosed, |
| 203 base::Unretained(this), web_contents, callback))); | 210 base::Unretained(this), web_contents, callback))); |
| 204 } | 211 } |
| 205 | 212 |
| 206 bool JavaScriptDialogManager::HandleJavaScriptDialog( | 213 bool JavaScriptDialogManager::HandleJavaScriptDialog( |
| 207 content::WebContents* web_contents, | 214 content::WebContents* web_contents, |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 295 // lazy background page after the dialog closes. (Dialogs are closed before | 302 // lazy background page after the dialog closes. (Dialogs are closed before |
| 296 // their WebContents is destroyed so |web_contents| is still valid here.) | 303 // their WebContents is destroyed so |web_contents| is still valid here.) |
| 297 extensions_client_->OnDialogClosed(web_contents); | 304 extensions_client_->OnDialogClosed(web_contents); |
| 298 | 305 |
| 299 last_close_time_ = base::TimeTicks::Now(); | 306 last_close_time_ = base::TimeTicks::Now(); |
| 300 | 307 |
| 301 callback.Run(success, user_input); | 308 callback.Run(success, user_input); |
| 302 } | 309 } |
| 303 | 310 |
| 304 } // namespace app_modal | 311 } // namespace app_modal |
| OLD | NEW |