1 // Copyright (c) 2012 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.
5 #include "chrome/browser/extensions/extension_warning_set.h"
7 #include "base/files/file_path.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/extension_set.h"
13 #include "grit/chromium_strings.h"
14 #include "grit/generated_resources.h"
15 #include "net/base/escape.h"
16 #include "ui/base/l10n/l10n_util.h"
18 using content::BrowserThread
;
21 // Prefix for message parameters indicating that the parameter needs to
22 // be translated from an extension id to the extension name.
23 const char kTranslate
[] = "TO_TRANSLATE:";
24 const size_t kMaxNumberOfParameters
= 4;
27 namespace extensions
{
33 ExtensionWarning::ExtensionWarning(
35 const std::string
& extension_id
,
37 const std::vector
<std::string
>& message_parameters
)
39 extension_id_(extension_id
),
40 message_id_(message_id
),
41 message_parameters_(message_parameters
) {
42 // These are invalid here because they do not have corresponding warning
43 // messages in the UI.
44 CHECK_NE(type
, kInvalid
);
45 CHECK_NE(type
, kMaxWarningType
);
46 CHECK_LE(message_parameters
.size(), kMaxNumberOfParameters
);
49 ExtensionWarning::ExtensionWarning(const ExtensionWarning
& other
)
51 extension_id_(other
.extension_id_
),
52 message_id_(other
.message_id_
),
53 message_parameters_(other
.message_parameters_
) {}
55 ExtensionWarning::~ExtensionWarning() {
58 ExtensionWarning
& ExtensionWarning::operator=(const ExtensionWarning
& other
) {
60 extension_id_
= other
.extension_id_
;
61 message_id_
= other
.message_id_
;
62 message_parameters_
= other
.message_parameters_
;
67 ExtensionWarning
ExtensionWarning::CreateNetworkDelayWarning(
68 const std::string
& extension_id
) {
69 std::vector
<std::string
> message_parameters
;
70 message_parameters
.push_back(l10n_util::GetStringUTF8(IDS_PRODUCT_NAME
));
71 return ExtensionWarning(
74 IDS_EXTENSION_WARNINGS_NETWORK_DELAY
,
79 ExtensionWarning
ExtensionWarning::CreateNetworkConflictWarning(
80 const std::string
& extension_id
) {
81 std::vector
<std::string
> message_parameters
;
82 return ExtensionWarning(
85 IDS_EXTENSION_WARNINGS_NETWORK_CONFLICT
,
90 ExtensionWarning
ExtensionWarning::CreateRedirectConflictWarning(
91 const std::string
& extension_id
,
92 const std::string
& winning_extension_id
,
93 const GURL
& attempted_redirect_url
,
94 const GURL
& winning_redirect_url
) {
95 std::vector
<std::string
> message_parameters
;
96 message_parameters
.push_back(attempted_redirect_url
.spec());
97 message_parameters
.push_back(kTranslate
+ winning_extension_id
);
98 message_parameters
.push_back(winning_redirect_url
.spec());
99 return ExtensionWarning(
102 IDS_EXTENSION_WARNINGS_REDIRECT_CONFLICT
,
107 ExtensionWarning
ExtensionWarning::CreateRequestHeaderConflictWarning(
108 const std::string
& extension_id
,
109 const std::string
& winning_extension_id
,
110 const std::string
& conflicting_header
) {
111 std::vector
<std::string
> message_parameters
;
112 message_parameters
.push_back(conflicting_header
);
113 message_parameters
.push_back(kTranslate
+ winning_extension_id
);
114 return ExtensionWarning(
117 IDS_EXTENSION_WARNINGS_REQUEST_HEADER_CONFLICT
,
122 ExtensionWarning
ExtensionWarning::CreateResponseHeaderConflictWarning(
123 const std::string
& extension_id
,
124 const std::string
& winning_extension_id
,
125 const std::string
& conflicting_header
) {
126 std::vector
<std::string
> message_parameters
;
127 message_parameters
.push_back(conflicting_header
);
128 message_parameters
.push_back(kTranslate
+ winning_extension_id
);
129 return ExtensionWarning(
132 IDS_EXTENSION_WARNINGS_RESPONSE_HEADER_CONFLICT
,
137 ExtensionWarning
ExtensionWarning::CreateCredentialsConflictWarning(
138 const std::string
& extension_id
,
139 const std::string
& winning_extension_id
) {
140 std::vector
<std::string
> message_parameters
;
141 message_parameters
.push_back(kTranslate
+ winning_extension_id
);
142 return ExtensionWarning(
145 IDS_EXTENSION_WARNINGS_CREDENTIALS_CONFLICT
,
150 ExtensionWarning
ExtensionWarning::CreateRepeatedCacheFlushesWarning(
151 const std::string
& extension_id
) {
152 std::vector
<std::string
> message_parameters
;
153 message_parameters
.push_back(l10n_util::GetStringUTF8(IDS_PRODUCT_NAME
));
154 return ExtensionWarning(
155 kRepeatedCacheFlushes
,
157 IDS_EXTENSION_WARNINGS_NETWORK_DELAY
,
162 ExtensionWarning
ExtensionWarning::CreateDownloadFilenameConflictWarning(
163 const std::string
& losing_extension_id
,
164 const std::string
& winning_extension_id
,
165 const base::FilePath
& losing_filename
,
166 const base::FilePath
& winning_filename
) {
167 std::vector
<std::string
> message_parameters
;
168 message_parameters
.push_back(base::UTF16ToUTF8(
169 losing_filename
.LossyDisplayName()));
170 message_parameters
.push_back(kTranslate
+ winning_extension_id
);
171 message_parameters
.push_back(base::UTF16ToUTF8(
172 winning_filename
.LossyDisplayName()));
173 return ExtensionWarning(
174 kDownloadFilenameConflict
,
176 IDS_EXTENSION_WARNINGS_DOWNLOAD_FILENAME_CONFLICT
,
181 ExtensionWarning
ExtensionWarning::CreateReloadTooFrequentWarning(
182 const std::string
& extension_id
) {
183 std::vector
<std::string
> message_parameters
;
184 return ExtensionWarning(kReloadTooFrequent
,
186 IDS_EXTENSION_WARNING_RELOAD_TOO_FREQUENT
,
190 std::string
ExtensionWarning::GetLocalizedMessage(
191 const ExtensionSet
* extensions
) const {
192 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
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.
197 std::vector
<base::string16
> final_parameters
;
198 for (size_t i
= 0; i
< message_parameters_
.size(); ++i
) {
199 std::string message
= message_parameters_
[i
];
200 if (StartsWithASCII(message
, kTranslate
, true)) {
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
;
206 final_parameters
.push_back(base::UTF8ToUTF16(net::EscapeForHTML(message
)));
209 COMPILE_ASSERT(kMaxNumberOfParameters
== 4u, YouNeedToAddMoreCaseStatements
);
210 switch (final_parameters
.size()) {
212 return l10n_util::GetStringUTF8(message_id_
);
214 return l10n_util::GetStringFUTF8(message_id_
, final_parameters
[0]);
216 return l10n_util::GetStringFUTF8(message_id_
, final_parameters
[0],
217 final_parameters
[1]);
219 return l10n_util::GetStringFUTF8(message_id_
, final_parameters
[0],
220 final_parameters
[1], final_parameters
[2]);
222 return l10n_util::GetStringFUTF8(message_id_
, final_parameters
[0],
223 final_parameters
[1], final_parameters
[2], final_parameters
[3]);
226 return std::string();
230 bool operator<(const ExtensionWarning
& a
, const ExtensionWarning
& b
) {
231 if (a
.extension_id() != b
.extension_id())
232 return a
.extension_id() < b
.extension_id();
233 return a
.warning_type() < b
.warning_type();
236 } // namespace extensions