blob: 8f750d182b6473d69c0b175b7f591a690fb1d523 [file] [log] [blame]
[email protected]b20729fe2012-01-25 21:42:521// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]7c1490da2011-10-11 18:53:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
hanxic7e55202014-08-28 14:13:215#include "extensions/browser/warning_set.h"
[email protected]7c1490da2011-10-11 18:53:256
avic9cec102015-12-23 00:39:267#include <stddef.h>
8
[email protected]dc6cb142013-08-10 18:14:529#include "base/files/file_path.h"
[email protected]539f6b32014-08-12 02:50:0010#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1811#include "base/strings/utf_string_conversions.h"
[email protected]c38831a12011-10-28 12:44:4912#include "content/public/browser/browser_thread.h"
[email protected]e4452d32013-11-15 23:07:4113#include "extensions/common/extension.h"
[email protected]289c44b2013-12-17 03:26:5714#include "extensions/common/extension_set.h"
hanxic7e55202014-08-28 14:13:2115#include "extensions/common/extensions_client.h"
16#include "extensions/strings/grit/extensions_strings.h"
[email protected]b4d3771d2012-11-14 14:44:1017#include "net/base/escape.h"
[email protected]7c1490da2011-10-11 18:53:2518#include "ui/base/l10n/l10n_util.h"
19
[email protected]631bb742011-11-02 11:29:3920using content::BrowserThread;
21
[email protected]b4d3771d2012-11-14 14:44:1022namespace {
23// Prefix for message parameters indicating that the parameter needs to
24// be translated from an extension id to the extension name.
25const char kTranslate[] = "TO_TRANSLATE:";
26const size_t kMaxNumberOfParameters = 4;
[email protected]7c1490da2011-10-11 18:53:2527}
28
[email protected]b4d3771d2012-11-14 14:44:1029namespace extensions {
30
31//
hanxic7e55202014-08-28 14:13:2132// Warning
[email protected]b4d3771d2012-11-14 14:44:1033//
34
hanxic7e55202014-08-28 14:13:2135Warning::Warning(
[email protected]b4d3771d2012-11-14 14:44:1036 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]a9632c9f2011-10-26 16:04:1644 // These are invalid here because they do not have corresponding warning
45 // messages in the UI.
[email protected]b4d3771d2012-11-14 14:44:1046 CHECK_NE(type, kInvalid);
47 CHECK_NE(type, kMaxWarningType);
48 CHECK_LE(message_parameters.size(), kMaxNumberOfParameters);
[email protected]7c1490da2011-10-11 18:53:2549}
50
hanxic7e55202014-08-28 14:13:2151Warning::Warning(const Warning& other)
[email protected]b4d3771d2012-11-14 14:44:1052 : type_(other.type_),
53 extension_id_(other.extension_id_),
54 message_id_(other.message_id_),
55 message_parameters_(other.message_parameters_) {}
56
hanxic7e55202014-08-28 14:13:2157Warning::~Warning() {
[email protected]7c1490da2011-10-11 18:53:2558}
59
hanxic7e55202014-08-28 14:13:2160Warning& Warning::operator=(const Warning& other) {
[email protected]b4d3771d2012-11-14 14:44:1061 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
hanxic7e55202014-08-28 14:13:2169Warning Warning::CreateNetworkDelayWarning(
[email protected]b4d3771d2012-11-14 14:44:1070 const std::string& extension_id) {
71 std::vector<std::string> message_parameters;
hanxic7e55202014-08-28 14:13:2172 message_parameters.push_back(ExtensionsClient::Get()->GetProductName());
73 return Warning(
[email protected]b4d3771d2012-11-14 14:44:1074 kNetworkDelay,
75 extension_id,
76 IDS_EXTENSION_WARNINGS_NETWORK_DELAY,
77 message_parameters);
78}
79
80// static
hanxic7e55202014-08-28 14:13:2181Warning Warning::CreateNetworkConflictWarning(const std::string& extension_id) {
[email protected]b4d3771d2012-11-14 14:44:1082 std::vector<std::string> message_parameters;
hanxic7e55202014-08-28 14:13:2183 return Warning(
[email protected]b4d3771d2012-11-14 14:44:1084 kNetworkConflict,
85 extension_id,
86 IDS_EXTENSION_WARNINGS_NETWORK_CONFLICT,
87 message_parameters);
88}
89
90// static
hanxic7e55202014-08-28 14:13:2191Warning Warning::CreateRedirectConflictWarning(
[email protected]b4d3771d2012-11-14 14:44:1092 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());
hanxic7e55202014-08-28 14:13:21100 return Warning(
[email protected]b4d3771d2012-11-14 14:44:10101 kRedirectConflict,
102 extension_id,
103 IDS_EXTENSION_WARNINGS_REDIRECT_CONFLICT,
104 message_parameters);
105}
106
107// static
hanxic7e55202014-08-28 14:13:21108Warning Warning::CreateRequestHeaderConflictWarning(
[email protected]b4d3771d2012-11-14 14:44:10109 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);
hanxic7e55202014-08-28 14:13:21115 return Warning(
[email protected]b4d3771d2012-11-14 14:44:10116 kNetworkConflict,
117 extension_id,
118 IDS_EXTENSION_WARNINGS_REQUEST_HEADER_CONFLICT,
119 message_parameters);
120}
121
122// static
hanxic7e55202014-08-28 14:13:21123Warning Warning::CreateResponseHeaderConflictWarning(
[email protected]b4d3771d2012-11-14 14:44:10124 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);
hanxic7e55202014-08-28 14:13:21130 return Warning(
[email protected]b4d3771d2012-11-14 14:44:10131 kNetworkConflict,
132 extension_id,
133 IDS_EXTENSION_WARNINGS_RESPONSE_HEADER_CONFLICT,
134 message_parameters);
135}
136
137// static
hanxic7e55202014-08-28 14:13:21138Warning Warning::CreateCredentialsConflictWarning(
[email protected]b4d3771d2012-11-14 14:44:10139 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);
hanxic7e55202014-08-28 14:13:21143 return Warning(
[email protected]b4d3771d2012-11-14 14:44:10144 kNetworkConflict,
145 extension_id,
146 IDS_EXTENSION_WARNINGS_CREDENTIALS_CONFLICT,
147 message_parameters);
148}
149
150// static
hanxic7e55202014-08-28 14:13:21151Warning Warning::CreateRepeatedCacheFlushesWarning(
[email protected]b4d3771d2012-11-14 14:44:10152 const std::string& extension_id) {
153 std::vector<std::string> message_parameters;
hanxic7e55202014-08-28 14:13:21154 message_parameters.push_back(ExtensionsClient::Get()->GetProductName());
155 return Warning(
[email protected]b4d3771d2012-11-14 14:44:10156 kRepeatedCacheFlushes,
157 extension_id,
158 IDS_EXTENSION_WARNINGS_NETWORK_DELAY,
159 message_parameters);
160}
161
[email protected]dc6cb142013-08-10 18:14:52162// static
hanxic7e55202014-08-28 14:13:21163Warning Warning::CreateDownloadFilenameConflictWarning(
[email protected]dc6cb142013-08-10 18:14:52164 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]04338722013-12-24 23:18:05169 message_parameters.push_back(base::UTF16ToUTF8(
170 losing_filename.LossyDisplayName()));
[email protected]dc6cb142013-08-10 18:14:52171 message_parameters.push_back(kTranslate + winning_extension_id);
[email protected]04338722013-12-24 23:18:05172 message_parameters.push_back(base::UTF16ToUTF8(
[email protected]dc6cb142013-08-10 18:14:52173 winning_filename.LossyDisplayName()));
hanxic7e55202014-08-28 14:13:21174 return Warning(
[email protected]dc6cb142013-08-10 18:14:52175 kDownloadFilenameConflict,
176 losing_extension_id,
177 IDS_EXTENSION_WARNINGS_DOWNLOAD_FILENAME_CONFLICT,
178 message_parameters);
179}
180
[email protected]e9d7496e2014-04-18 01:25:46181// static
hanxic7e55202014-08-28 14:13:21182Warning Warning::CreateReloadTooFrequentWarning(
[email protected]e9d7496e2014-04-18 01:25:46183 const std::string& extension_id) {
184 std::vector<std::string> message_parameters;
hanxic7e55202014-08-28 14:13:21185 return Warning(kReloadTooFrequent,
[email protected]e9d7496e2014-04-18 01:25:46186 extension_id,
187 IDS_EXTENSION_WARNING_RELOAD_TOO_FREQUENT,
188 message_parameters);
189}
190
hanxic7e55202014-08-28 14:13:21191std::string Warning::GetLocalizedMessage(const ExtensionSet* extensions) const {
[email protected]54ee8192014-03-29 17:37:24192 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b4d3771d2012-11-14 14:44:10193
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]d2065e062013-12-12 23:49:52197 std::vector<base::string16> final_parameters;
[email protected]b4d3771d2012-11-14 14:44:10198 for (size_t i = 0; i < message_parameters_.size(); ++i) {
199 std::string message = message_parameters_[i];
brettw95509312015-07-16 23:57:33200 if (base::StartsWith(message, kTranslate, base::CompareCase::SENSITIVE)) {
[email protected]b4d3771d2012-11-14 14:44:10201 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]04338722013-12-24 23:18:05206 final_parameters.push_back(base::UTF8ToUTF16(net::EscapeForHTML(message)));
[email protected]b4d3771d2012-11-14 14:44:10207 }
208
anujk.sharma200c95462015-01-27 04:56:20209 static_assert(kMaxNumberOfParameters == 4u,
210 "You Need To Add More Case Statements");
[email protected]b4d3771d2012-11-14 14:44:10211 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
hanxic7e55202014-08-28 14:13:21231bool operator<(const Warning& a, const Warning& b) {
[email protected]b4d3771d2012-11-14 14:44:10232 if (a.extension_id() != b.extension_id())
[email protected]7c1490da2011-10-11 18:53:25233 return a.extension_id() < b.extension_id();
234 return a.warning_type() < b.warning_type();
235}
236
[email protected]b4d3771d2012-11-14 14:44:10237} // namespace extensions