[email protected] | b20729fe | 2012-01-25 21:42:52 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 7c1490da | 2011-10-11 18:53:25 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 5 | #include "extensions/browser/warning_set.h" |
[email protected] | 7c1490da | 2011-10-11 18:53:25 | [diff] [blame] | 6 | |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | dc6cb14 | 2013-08-10 18:14:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | 539f6b3 | 2014-08-12 02:50:00 | [diff] [blame] | 10 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 12 | #include "content/public/browser/browser_thread.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 13 | #include "extensions/common/extension.h" |
[email protected] | 289c44b | 2013-12-17 03:26:57 | [diff] [blame] | 14 | #include "extensions/common/extension_set.h" |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 15 | #include "extensions/common/extensions_client.h" |
| 16 | #include "extensions/strings/grit/extensions_strings.h" |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 17 | #include "net/base/escape.h" |
[email protected] | 7c1490da | 2011-10-11 18:53:25 | [diff] [blame] | 18 | #include "ui/base/l10n/l10n_util.h" |
| 19 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 20 | using content::BrowserThread; |
| 21 | |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 22 | namespace { |
| 23 | // Prefix for message parameters indicating that the parameter needs to |
| 24 | // be translated from an extension id to the extension name. |
| 25 | const char kTranslate[] = "TO_TRANSLATE:"; |
| 26 | const size_t kMaxNumberOfParameters = 4; |
[email protected] | 7c1490da | 2011-10-11 18:53:25 | [diff] [blame] | 27 | } |
| 28 | |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 29 | namespace extensions { |
| 30 | |
| 31 | // |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 32 | // Warning |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 33 | // |
| 34 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 35 | Warning::Warning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 36 | WarningType type, |
| 37 | const std::string& extension_id, |
| 38 | int message_id, |
| 39 | const std::vector<std::string>& message_parameters) |
| 40 | : type_(type), |
| 41 | extension_id_(extension_id), |
| 42 | message_id_(message_id), |
| 43 | message_parameters_(message_parameters) { |
[email protected] | a9632c9f | 2011-10-26 16:04:16 | [diff] [blame] | 44 | // These are invalid here because they do not have corresponding warning |
| 45 | // messages in the UI. |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 46 | CHECK_NE(type, kInvalid); |
| 47 | CHECK_NE(type, kMaxWarningType); |
| 48 | CHECK_LE(message_parameters.size(), kMaxNumberOfParameters); |
[email protected] | 7c1490da | 2011-10-11 18:53:25 | [diff] [blame] | 49 | } |
| 50 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 51 | Warning::Warning(const Warning& other) |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 52 | : type_(other.type_), |
| 53 | extension_id_(other.extension_id_), |
| 54 | message_id_(other.message_id_), |
| 55 | message_parameters_(other.message_parameters_) {} |
| 56 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 57 | Warning::~Warning() { |
[email protected] | 7c1490da | 2011-10-11 18:53:25 | [diff] [blame] | 58 | } |
| 59 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 60 | Warning& Warning::operator=(const Warning& other) { |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 61 | type_ = other.type_; |
| 62 | extension_id_ = other.extension_id_; |
| 63 | message_id_ = other.message_id_; |
| 64 | message_parameters_ = other.message_parameters_; |
| 65 | return *this; |
| 66 | } |
| 67 | |
| 68 | // static |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 69 | Warning Warning::CreateNetworkDelayWarning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 70 | const std::string& extension_id) { |
| 71 | std::vector<std::string> message_parameters; |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 72 | message_parameters.push_back(ExtensionsClient::Get()->GetProductName()); |
| 73 | return Warning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 74 | kNetworkDelay, |
| 75 | extension_id, |
| 76 | IDS_EXTENSION_WARNINGS_NETWORK_DELAY, |
| 77 | message_parameters); |
| 78 | } |
| 79 | |
| 80 | // static |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 81 | Warning Warning::CreateNetworkConflictWarning(const std::string& extension_id) { |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 82 | std::vector<std::string> message_parameters; |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 83 | return Warning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 84 | kNetworkConflict, |
| 85 | extension_id, |
| 86 | IDS_EXTENSION_WARNINGS_NETWORK_CONFLICT, |
| 87 | message_parameters); |
| 88 | } |
| 89 | |
| 90 | // static |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 91 | Warning Warning::CreateRedirectConflictWarning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 92 | const std::string& extension_id, |
| 93 | const std::string& winning_extension_id, |
| 94 | const GURL& attempted_redirect_url, |
| 95 | const GURL& winning_redirect_url) { |
| 96 | std::vector<std::string> message_parameters; |
| 97 | message_parameters.push_back(attempted_redirect_url.spec()); |
| 98 | message_parameters.push_back(kTranslate + winning_extension_id); |
| 99 | message_parameters.push_back(winning_redirect_url.spec()); |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 100 | return Warning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 101 | kRedirectConflict, |
| 102 | extension_id, |
| 103 | IDS_EXTENSION_WARNINGS_REDIRECT_CONFLICT, |
| 104 | message_parameters); |
| 105 | } |
| 106 | |
| 107 | // static |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 108 | Warning Warning::CreateRequestHeaderConflictWarning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 109 | const std::string& extension_id, |
| 110 | const std::string& winning_extension_id, |
| 111 | const std::string& conflicting_header) { |
| 112 | std::vector<std::string> message_parameters; |
| 113 | message_parameters.push_back(conflicting_header); |
| 114 | message_parameters.push_back(kTranslate + winning_extension_id); |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 115 | return Warning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 116 | kNetworkConflict, |
| 117 | extension_id, |
| 118 | IDS_EXTENSION_WARNINGS_REQUEST_HEADER_CONFLICT, |
| 119 | message_parameters); |
| 120 | } |
| 121 | |
| 122 | // static |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 123 | Warning Warning::CreateResponseHeaderConflictWarning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 124 | const std::string& extension_id, |
| 125 | const std::string& winning_extension_id, |
| 126 | const std::string& conflicting_header) { |
| 127 | std::vector<std::string> message_parameters; |
| 128 | message_parameters.push_back(conflicting_header); |
| 129 | message_parameters.push_back(kTranslate + winning_extension_id); |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 130 | return Warning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 131 | kNetworkConflict, |
| 132 | extension_id, |
| 133 | IDS_EXTENSION_WARNINGS_RESPONSE_HEADER_CONFLICT, |
| 134 | message_parameters); |
| 135 | } |
| 136 | |
| 137 | // static |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 138 | Warning Warning::CreateCredentialsConflictWarning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 139 | const std::string& extension_id, |
| 140 | const std::string& winning_extension_id) { |
| 141 | std::vector<std::string> message_parameters; |
| 142 | message_parameters.push_back(kTranslate + winning_extension_id); |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 143 | return Warning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 144 | kNetworkConflict, |
| 145 | extension_id, |
| 146 | IDS_EXTENSION_WARNINGS_CREDENTIALS_CONFLICT, |
| 147 | message_parameters); |
| 148 | } |
| 149 | |
| 150 | // static |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 151 | Warning Warning::CreateRepeatedCacheFlushesWarning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 152 | const std::string& extension_id) { |
| 153 | std::vector<std::string> message_parameters; |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 154 | message_parameters.push_back(ExtensionsClient::Get()->GetProductName()); |
| 155 | return Warning( |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 156 | kRepeatedCacheFlushes, |
| 157 | extension_id, |
| 158 | IDS_EXTENSION_WARNINGS_NETWORK_DELAY, |
| 159 | message_parameters); |
| 160 | } |
| 161 | |
[email protected] | dc6cb14 | 2013-08-10 18:14:52 | [diff] [blame] | 162 | // static |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 163 | Warning Warning::CreateDownloadFilenameConflictWarning( |
[email protected] | dc6cb14 | 2013-08-10 18:14:52 | [diff] [blame] | 164 | const std::string& losing_extension_id, |
| 165 | const std::string& winning_extension_id, |
| 166 | const base::FilePath& losing_filename, |
| 167 | const base::FilePath& winning_filename) { |
| 168 | std::vector<std::string> message_parameters; |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 169 | message_parameters.push_back(base::UTF16ToUTF8( |
| 170 | losing_filename.LossyDisplayName())); |
[email protected] | dc6cb14 | 2013-08-10 18:14:52 | [diff] [blame] | 171 | message_parameters.push_back(kTranslate + winning_extension_id); |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 172 | message_parameters.push_back(base::UTF16ToUTF8( |
[email protected] | dc6cb14 | 2013-08-10 18:14:52 | [diff] [blame] | 173 | winning_filename.LossyDisplayName())); |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 174 | return Warning( |
[email protected] | dc6cb14 | 2013-08-10 18:14:52 | [diff] [blame] | 175 | kDownloadFilenameConflict, |
| 176 | losing_extension_id, |
| 177 | IDS_EXTENSION_WARNINGS_DOWNLOAD_FILENAME_CONFLICT, |
| 178 | message_parameters); |
| 179 | } |
| 180 | |
[email protected] | e9d7496e | 2014-04-18 01:25:46 | [diff] [blame] | 181 | // static |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 182 | Warning Warning::CreateReloadTooFrequentWarning( |
[email protected] | e9d7496e | 2014-04-18 01:25:46 | [diff] [blame] | 183 | const std::string& extension_id) { |
| 184 | std::vector<std::string> message_parameters; |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 185 | return Warning(kReloadTooFrequent, |
[email protected] | e9d7496e | 2014-04-18 01:25:46 | [diff] [blame] | 186 | extension_id, |
| 187 | IDS_EXTENSION_WARNING_RELOAD_TOO_FREQUENT, |
| 188 | message_parameters); |
| 189 | } |
| 190 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 191 | std::string Warning::GetLocalizedMessage(const ExtensionSet* extensions) const { |
[email protected] | 54ee819 | 2014-03-29 17:37:24 | [diff] [blame] | 192 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 193 | |
| 194 | // These parameters may be unsafe (URLs and Extension names) and need |
| 195 | // to be HTML-escaped before being embedded in the UI. Also extension IDs |
| 196 | // are translated to full extension names. |
[email protected] | d2065e06 | 2013-12-12 23:49:52 | [diff] [blame] | 197 | std::vector<base::string16> final_parameters; |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 198 | for (size_t i = 0; i < message_parameters_.size(); ++i) { |
| 199 | std::string message = message_parameters_[i]; |
brettw | 9550931 | 2015-07-16 23:57:33 | [diff] [blame] | 200 | if (base::StartsWith(message, kTranslate, base::CompareCase::SENSITIVE)) { |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 201 | std::string extension_id = message.substr(sizeof(kTranslate) - 1); |
| 202 | const extensions::Extension* extension = |
| 203 | extensions->GetByID(extension_id); |
| 204 | message = extension ? extension->name() : extension_id; |
| 205 | } |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 206 | final_parameters.push_back(base::UTF8ToUTF16(net::EscapeForHTML(message))); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 207 | } |
| 208 | |
anujk.sharma | 200c9546 | 2015-01-27 04:56:20 | [diff] [blame] | 209 | static_assert(kMaxNumberOfParameters == 4u, |
| 210 | "You Need To Add More Case Statements"); |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 211 | switch (final_parameters.size()) { |
| 212 | case 0: |
| 213 | return l10n_util::GetStringUTF8(message_id_); |
| 214 | case 1: |
| 215 | return l10n_util::GetStringFUTF8(message_id_, final_parameters[0]); |
| 216 | case 2: |
| 217 | return l10n_util::GetStringFUTF8(message_id_, final_parameters[0], |
| 218 | final_parameters[1]); |
| 219 | case 3: |
| 220 | return l10n_util::GetStringFUTF8(message_id_, final_parameters[0], |
| 221 | final_parameters[1], final_parameters[2]); |
| 222 | case 4: |
| 223 | return l10n_util::GetStringFUTF8(message_id_, final_parameters[0], |
| 224 | final_parameters[1], final_parameters[2], final_parameters[3]); |
| 225 | default: |
| 226 | NOTREACHED(); |
| 227 | return std::string(); |
| 228 | } |
| 229 | } |
| 230 | |
hanxi | c7e5520 | 2014-08-28 14:13:21 | [diff] [blame] | 231 | bool operator<(const Warning& a, const Warning& b) { |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 232 | if (a.extension_id() != b.extension_id()) |
[email protected] | 7c1490da | 2011-10-11 18:53:25 | [diff] [blame] | 233 | return a.extension_id() < b.extension_id(); |
| 234 | return a.warning_type() < b.warning_type(); |
| 235 | } |
| 236 | |
[email protected] | b4d3771d | 2012-11-14 14:44:10 | [diff] [blame] | 237 | } // namespace extensions |