| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h" | 5 #include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/feature_list.h" | 9 #include "base/feature_list.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "chrome/browser/engagement/site_engagement_service.h" | 11 #include "chrome/browser/engagement/site_engagement_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_list.h" | 14 #include "chrome/browser/ui/browser_list.h" |
| 15 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" | 15 #include "chrome/browser/ui/tab_modal_confirm_dialog.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/common/chrome_features.h" | 17 #include "chrome/common/chrome_features.h" |
| 18 #include "components/app_modal/javascript_dialog_manager.h" | 18 #include "components/app_modal/javascript_dialog_manager.h" |
| 19 #include "content/public/browser/navigation_handle.h" | 19 #include "content/public/browser/navigation_handle.h" |
| 20 #include "content/public/browser/render_frame_host.h" | 20 #include "content/public/browser/render_frame_host.h" |
| 21 #include "ui/gfx/text_elider.h" |
| 21 | 22 |
| 22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(JavaScriptDialogTabHelper); | 23 DEFINE_WEB_CONTENTS_USER_DATA_KEY(JavaScriptDialogTabHelper); |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 bool IsEnabled() { | 27 bool IsEnabled() { |
| 27 return base::FeatureList::IsEnabled(features::kAutoDismissingDialogs); | 28 return base::FeatureList::IsEnabled(features::kAutoDismissingDialogs); |
| 28 } | 29 } |
| 29 | 30 |
| 30 app_modal::JavaScriptDialogManager* AppModalDialogManager() { | 31 app_modal::JavaScriptDialogManager* AppModalDialogManager() { |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 "https://www.chromestatus.com/feature/5637107137642496"); | 145 "https://www.chromestatus.com/feature/5637107137642496"); |
| 145 return; | 146 return; |
| 146 } | 147 } |
| 147 | 148 |
| 148 if (dialog_) { | 149 if (dialog_) { |
| 149 // There's already a dialog up; clear it out. | 150 // There's already a dialog up; clear it out. |
| 150 CloseDialog(false, base::string16(), | 151 CloseDialog(false, base::string16(), |
| 151 DismissalCause::SUBSEQUENT_DIALOG_SHOWN); | 152 DismissalCause::SUBSEQUENT_DIALOG_SHOWN); |
| 152 } | 153 } |
| 153 | 154 |
| 155 // Enforce sane sizes. ElideRectangleString breaks horizontally, which isn't |
| 156 // strictly needed, but it restricts the vertical size, which is crucial. |
| 157 // This gives about 2000 characters, which is about the same as the |
| 158 // AppModalDialogManager provides, but allows no more than 24 lines. |
| 159 const int kMessageTextMaxRows = 24; |
| 160 const int kMessageTextMaxCols = 80; |
| 161 const size_t kDefaultPromptMaxSize = 2000; |
| 162 base::string16 truncated_message_text; |
| 163 gfx::ElideRectangleString(message_text, kMessageTextMaxRows, |
| 164 kMessageTextMaxCols, false, |
| 165 &truncated_message_text); |
| 166 base::string16 truncated_default_prompt_text; |
| 167 gfx::ElideString(default_prompt_text, kDefaultPromptMaxSize, |
| 168 &truncated_default_prompt_text); |
| 169 |
| 154 base::string16 title = | 170 base::string16 title = |
| 155 AppModalDialogManager()->GetTitle(alerting_web_contents, origin_url); | 171 AppModalDialogManager()->GetTitle(alerting_web_contents, origin_url); |
| 156 dialog_callback_ = callback; | 172 dialog_callback_ = callback; |
| 157 dialog_type_ = dialog_type; | 173 dialog_type_ = dialog_type; |
| 158 dialog_ = JavaScriptDialog::Create( | 174 dialog_ = JavaScriptDialog::Create( |
| 159 parent_web_contents, alerting_web_contents, title, dialog_type, | 175 parent_web_contents, alerting_web_contents, title, dialog_type, |
| 160 message_text, default_prompt_text, | 176 truncated_message_text, truncated_default_prompt_text, |
| 161 base::Bind(&JavaScriptDialogTabHelper::OnDialogClosed, | 177 base::Bind(&JavaScriptDialogTabHelper::OnDialogClosed, |
| 162 base::Unretained(this), callback)); | 178 base::Unretained(this), callback)); |
| 163 | 179 |
| 164 BrowserList::AddObserver(this); | 180 BrowserList::AddObserver(this); |
| 165 | 181 |
| 166 // Message suppression is something that we don't give the user a checkbox | 182 // Message suppression is something that we don't give the user a checkbox |
| 167 // for any more. It was useful back in the day when dialogs were app-modal | 183 // for any more. It was useful back in the day when dialogs were app-modal |
| 168 // and clicking the checkbox was the only way to escape a loop that the page | 184 // and clicking the checkbox was the only way to escape a loop that the page |
| 169 // was doing, but now the user can just close the page. | 185 // was doing, but now the user can just close the page. |
| 170 *did_suppress_message = false; | 186 *did_suppress_message = false; |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 dialog_callback_.Run(success, user_input); | 344 dialog_callback_.Run(success, user_input); |
| 329 | 345 |
| 330 ClearDialogInfo(); | 346 ClearDialogInfo(); |
| 331 } | 347 } |
| 332 | 348 |
| 333 void JavaScriptDialogTabHelper::ClearDialogInfo() { | 349 void JavaScriptDialogTabHelper::ClearDialogInfo() { |
| 334 dialog_.reset(); | 350 dialog_.reset(); |
| 335 dialog_callback_.Reset(); | 351 dialog_callback_.Reset(); |
| 336 BrowserList::RemoveObserver(this); | 352 BrowserList::RemoveObserver(this); |
| 337 } | 353 } |
| OLD | NEW |