Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 | |
| 5 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_controller.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/i18n/rtl.h" | |
| 12 #include "base/location.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | |
| 18 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_config.h" | |
| 19 #include "chrome/browser/safe_browsing/settings_reset_prompt/settings_reset_prom pt_model.h" | |
| 20 #include "chrome/browser/ui/browser.h" | |
| 21 #include "chrome/browser/ui/browser_finder.h" | |
| 22 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | |
| 23 #include "chrome/grit/chromium_strings.h" | |
| 24 #include "chrome/grit/generated_resources.h" | |
| 25 #include "components/url_formatter/elide_url.h" | |
| 26 #include "components/url_formatter/url_formatter.h" | |
| 27 #include "content/public/browser/browser_thread.h" | |
| 28 #include "ui/base/l10n/l10n_util.h" | |
| 29 #include "url/gurl.h" | |
| 30 | |
| 31 namespace safe_browsing { | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 base::string16 FormatUrlForDisplay(const GURL& url) { | |
| 36 return base::i18n::GetDisplayStringInLTRDirectionality( | |
| 37 url_formatter::FormatUrl(url)); | |
| 38 } | |
| 39 | |
| 40 base::string16 FormatSearchEngineForDisplay(const GURL& search_url) { | |
| 41 return url_formatter::FormatUrlForSecurityDisplay(search_url); | |
| 42 } | |
| 43 | |
| 44 } // namespace. | |
| 45 | |
| 46 SettingsResetPromptController::LabelInfo::LabelInfo(LabelType type, | |
| 47 const base::string16& text) | |
| 48 : type(type), text(text) {} | |
| 49 | |
| 50 SettingsResetPromptController::LabelInfo::LabelInfo(LabelType type, | |
| 51 int message_id) | |
| 52 : type(type), text(l10n_util::GetStringUTF16(message_id)) {} | |
| 53 | |
| 54 SettingsResetPromptController::LabelInfo::~LabelInfo() {} | |
| 55 | |
| 56 SettingsResetPromptController::SettingsResetPromptController( | |
| 57 std::unique_ptr<SettingsResetPromptModel> model) | |
| 58 : model_(std::move(model)) { | |
| 59 DCHECK(model_); | |
| 60 DCHECK(model_->ShouldPromptForReset()); | |
| 61 | |
| 62 InitMainText(); | |
| 63 InitDetailsText(); | |
| 64 } | |
| 65 | |
| 66 SettingsResetPromptController::~SettingsResetPromptController() {} | |
| 67 | |
| 68 base::string16 SettingsResetPromptController::GetWindowTitle() { | |
| 69 return l10n_util::GetStringUTF16(IDS_SETTINGS_RESET_PROMPT_TITLE); | |
| 70 } | |
| 71 | |
| 72 base::string16 SettingsResetPromptController::GetButtonLabel() { | |
| 73 return l10n_util::GetStringUTF16( | |
| 74 IDS_SETTINGS_RESET_PROMPT_ACCEPT_BUTTON_LABEL); | |
| 75 } | |
| 76 | |
| 77 std::vector<SettingsResetPromptController::LabelInfo> | |
| 78 SettingsResetPromptController::GetMainText() { | |
| 79 DCHECK(!main_text_.empty()); | |
| 80 return main_text_; | |
| 81 } | |
| 82 | |
| 83 std::vector<SettingsResetPromptController::LabelInfo> | |
| 84 SettingsResetPromptController::GetDetailsText() { | |
| 85 DCHECK(!details_text_.empty()); | |
| 86 return details_text_; | |
| 87 } | |
| 88 | |
| 89 base::string16 SettingsResetPromptController::GetShowDetailsLabel() { | |
| 90 return l10n_util::GetStringUTF16( | |
| 91 IDS_SETTINGS_RESET_PROMPT_SHOW_DETAILS_BUTTON_LABEL); | |
| 92 } | |
| 93 | |
| 94 base::string16 SettingsResetPromptController::GetHideDetailsLabel() { | |
| 95 return l10n_util::GetStringUTF16( | |
| 96 IDS_SETTINGS_RESET_PROMPT_HIDE_DETAILS_BUTTON_LABEL); | |
| 97 } | |
| 98 | |
| 99 void SettingsResetPromptController::Accept() { | |
| 100 model_->PerformReset( | |
| 101 base::Bind(&SettingsResetPromptController::OnInteractionDone, | |
| 102 base::Unretained(this))); | |
| 103 } | |
| 104 | |
| 105 void SettingsResetPromptController::Cancel() { | |
| 106 OnInteractionDone(); | |
| 107 } | |
| 108 | |
| 109 void SettingsResetPromptController::InitMainText() { | |
| 110 DCHECK(main_text_.empty()); | |
| 111 | |
| 112 using LabelInfo = SettingsResetPromptController::LabelInfo; | |
|
sky
2017/02/21 17:37:27
I don't think you need the using here. LabelInfo s
alito
2017/02/23 02:31:59
Done.
| |
| 113 | |
| 114 const bool reset_search = model_->default_search_reset_state() == | |
| 115 SettingsResetPromptModel::RESET_REQUIRED; | |
| 116 const bool reset_startup_urls = model_->startup_urls_reset_state() == | |
| 117 SettingsResetPromptModel::RESET_REQUIRED; | |
| 118 const bool reset_homepage = model_->homepage_reset_state() == | |
| 119 SettingsResetPromptModel::RESET_REQUIRED; | |
| 120 DCHECK(reset_search || reset_startup_urls || reset_homepage); | |
| 121 | |
| 122 base::string16 action_message; | |
| 123 if (reset_search + reset_startup_urls + reset_homepage > 1) { | |
|
sky
2017/02/21 17:37:28
Uses casts to make this more obvious.
alito
2017/02/23 02:31:59
Now using int{} to cast the bools to int. Is this
| |
| 124 // When multiple settings need to be reset, display a bulleted list of | |
| 125 // settings with a common explanation text. | |
| 126 main_text_.push_back( | |
| 127 LabelInfo(LabelInfo::PARAGRAPH, | |
| 128 IDS_SETTINGS_RESET_PROMPT_EXPLANATION_FOR_MULTIPLE_SETTINGS)); | |
| 129 if (reset_search) { | |
| 130 main_text_.push_back( | |
| 131 LabelInfo(LabelInfo::BULLET_ITEM, | |
| 132 IDS_SETTINGS_RESET_PROMPT_SEARCH_ENGINE_SETTING_NAME)); | |
| 133 } | |
| 134 if (reset_startup_urls) { | |
| 135 main_text_.push_back( | |
| 136 LabelInfo(LabelInfo::BULLET_ITEM, | |
| 137 IDS_SETTINGS_RESET_PROMPT_STARTUP_PAGE_SETTING_NAME)); | |
| 138 } | |
| 139 if (reset_homepage) { | |
| 140 main_text_.push_back( | |
| 141 LabelInfo(LabelInfo::BULLET_ITEM, | |
| 142 IDS_SETTINGS_RESET_PROMPT_HOMEPAGE_SETTING_NAME)); | |
| 143 } | |
| 144 | |
| 145 // Use a common action message asking if user wants to reset the settings | |
| 146 // that were displayed in the list above. | |
| 147 action_message = l10n_util::GetStringUTF16( | |
| 148 IDS_SETTINGS_RESET_PROMPT_ACTION_EXPLANATION_FOR_MULTIPLE_SETTINGS); | |
| 149 } else { | |
| 150 // When only one setting needs to be reset, display tailored explanation and | |
| 151 // action message for each setting. | |
| 152 if (reset_search) { | |
| 153 main_text_.push_back( | |
| 154 LabelInfo(LabelInfo::PARAGRAPH, | |
| 155 IDS_SETTINGS_RESET_PROMPT_EXPLANATION_FOR_SEARCH_ENGINE)); | |
| 156 action_message = l10n_util::GetStringUTF16( | |
| 157 IDS_SETTINGS_RESET_PROMPT_ACTION_EXPLANATION_FOR_SEARCH_ENGINE); | |
| 158 } | |
| 159 if (reset_startup_urls) { | |
| 160 main_text_.push_back( | |
| 161 LabelInfo(LabelInfo::PARAGRAPH, | |
| 162 IDS_SETTINGS_RESET_PROMPT_EXPLANATION_FOR_STARTUP_PAGE)); | |
| 163 action_message = l10n_util::GetStringUTF16( | |
| 164 IDS_SETTINGS_RESET_PROMPT_ACTION_EXPLANATION_FOR_STARTUP_PAGE); | |
| 165 } | |
| 166 if (reset_homepage) { | |
| 167 main_text_.push_back( | |
| 168 LabelInfo(LabelInfo::PARAGRAPH, | |
| 169 IDS_SETTINGS_RESET_PROMPT_EXPLANATION_FOR_HOMEPAGE)); | |
| 170 action_message = l10n_util::GetStringUTF16( | |
| 171 IDS_SETTINGS_RESET_PROMPT_ACTION_EXPLANATION_FOR_HOMEPAGE); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 // Append the explanation about the number of extensions that will be disabled | |
|
sky
2017/02/21 17:37:27
Define a format for this as appending isn't always
alito
2017/02/23 02:31:59
Done.
| |
| 176 // to the action message. | |
| 177 if (!model_->extensions_to_disable().empty()) { | |
| 178 action_message.push_back(L' '); | |
| 179 if (model_->extensions_to_disable().size() == 1) { | |
| 180 action_message.append(l10n_util::GetStringUTF16( | |
| 181 IDS_SETTINGS_RESET_PROMPT_ACTION_EXPLANATION_FOR_SINGLE_EXTENSION)); | |
| 182 } else { | |
| 183 action_message.append(l10n_util::GetStringFUTF16( | |
| 184 IDS_SETTINGS_RESET_PROMPT_ACTION_EXPLANATION_FOR_MULTIPLE_EXTENSIONS, | |
| 185 base::SizeTToString16(model_->extensions_to_disable().size()))); | |
| 186 } | |
| 187 } | |
| 188 main_text_.push_back(LabelInfo(LabelInfo::PARAGRAPH, action_message)); | |
| 189 } | |
| 190 | |
| 191 void SettingsResetPromptController::InitDetailsText() { | |
| 192 using LabelInfo = SettingsResetPromptController::LabelInfo; | |
|
sky
2017/02/21 17:37:28
Same comment not needing using.
alito
2017/02/23 02:31:59
Done.
| |
| 193 details_text_.clear(); | |
|
sky
2017/02/21 17:37:28
Why don't you DCHECK on empty like you do for main
alito
2017/02/23 02:31:59
Just forgot to change it. Now using DCHECK just li
| |
| 194 | |
| 195 // Add the text explaining which settings are going to be reset. | |
| 196 details_text_.push_back(LabelInfo( | |
| 197 LabelInfo::PARAGRAPH, | |
| 198 l10n_util::GetStringUTF16( | |
| 199 IDS_SETTINGS_RESET_PROMPT_DETAILS_SECTION_SETTINGS_EXPLANATION))); | |
| 200 | |
| 201 // Enumerate the settings that are going to be reset. | |
| 202 if (model_->default_search_reset_state() == | |
| 203 SettingsResetPromptModel::RESET_REQUIRED) { | |
| 204 details_text_.push_back(LabelInfo( | |
| 205 LabelInfo::PARAGRAPH, | |
| 206 l10n_util::GetStringUTF16( | |
| 207 IDS_SETTINGS_RESET_PROMPT_DETAILED_SEARCH_ENGINE_SETTING))); | |
| 208 details_text_.push_back( | |
| 209 LabelInfo(LabelInfo::BULLET_ITEM, | |
| 210 FormatSearchEngineForDisplay(model_->default_search()))); | |
| 211 } | |
| 212 if (model_->homepage_reset_state() == | |
| 213 SettingsResetPromptModel::RESET_REQUIRED) { | |
| 214 details_text_.push_back( | |
| 215 LabelInfo(LabelInfo::PARAGRAPH, | |
| 216 l10n_util::GetStringUTF16( | |
| 217 IDS_SETTINGS_RESET_PROMPT_DETAILED_HOMEPAGE_SETTING))); | |
| 218 details_text_.push_back(LabelInfo(LabelInfo::BULLET_ITEM, | |
| 219 FormatUrlForDisplay(model_->homepage()))); | |
| 220 } | |
| 221 if (model_->startup_urls_reset_state() == | |
| 222 SettingsResetPromptModel::RESET_REQUIRED) { | |
| 223 details_text_.push_back( | |
| 224 LabelInfo(LabelInfo::PARAGRAPH, | |
| 225 l10n_util::GetStringUTF16( | |
| 226 IDS_SETTINGS_RESET_PROMPT_DETAILED_STARTUP_SETTINGS))); | |
| 227 for (const GURL& url : model_->startup_urls()) | |
| 228 details_text_.push_back( | |
| 229 LabelInfo(LabelInfo::BULLET_ITEM, FormatUrlForDisplay(url))); | |
| 230 } | |
| 231 | |
| 232 if (!model_->extensions_to_disable().empty()) { | |
| 233 // Add the text explaining which extensions will be disabled. | |
| 234 details_text_.push_back(LabelInfo( | |
| 235 LabelInfo::PARAGRAPH, | |
| 236 l10n_util::GetStringUTF16( | |
| 237 IDS_SETTINGS_RESET_PROMPT_DETAILS_SECTION_EXTENSION_EXPLANATION))); | |
| 238 | |
| 239 for (const auto& item : model_->extensions_to_disable()) { | |
| 240 const ExtensionInfo& extension_info = item.second; | |
| 241 details_text_.push_back(LabelInfo( | |
| 242 LabelInfo::BULLET_ITEM, base::UTF8ToUTF16(extension_info.name))); | |
| 243 } | |
| 244 } | |
| 245 } | |
| 246 | |
| 247 void SettingsResetPromptController::OnInteractionDone() { | |
| 248 // TODO(alito): Add metrics reporting here. | |
| 249 delete this; | |
|
sky
2017/02/21 17:37:27
If this object is going to control it's lifetime,
alito
2017/02/23 02:31:59
Done.
| |
| 250 } | |
| 251 | |
| 252 } // namespace safe_browsing | |
| OLD | NEW |