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 "extensions/browser/warning_set.h"
7 #include "base/files/file_path.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/extension_set.h"
13 #include "extensions/common/extensions_client.h"
14 #include "extensions/strings/grit/extensions_strings.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
{
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 Warning::Warning(const Warning
& other
)
51 extension_id_(other
.extension_id_
),
52 message_id_(other
.message_id_
),
53 message_parameters_(other
.message_parameters_
) {}
58 Warning
& Warning::operator=(const Warning
& other
) {
60 extension_id_
= other
.extension_id_
;
61 message_id_
= other
.message_id_
;
62 message_parameters_
= other
.message_parameters_
;
67 Warning
Warning::CreateNetworkDelayWarning(
68 const std::string
& extension_id
) {
69 std::vector
<std::string
> message_parameters
;
70 message_parameters
.push_back(ExtensionsClient::Get()->GetProductName());
74 IDS_EXTENSION_WARNINGS_NETWORK_DELAY
,
79 Warning
Warning::CreateNetworkConflictWarning(const std::string
& extension_id
) {
80 std::vector
<std::string
> message_parameters
;
84 IDS_EXTENSION_WARNINGS_NETWORK_CONFLICT
,
89 Warning
Warning::CreateRedirectConflictWarning(
90 const std::string
& extension_id
,
91 const std::string
& winning_extension_id
,
92 const GURL
& attempted_redirect_url
,
93 const GURL
& winning_redirect_url
) {
94 std::vector
<std::string
> message_parameters
;
95 message_parameters
.push_back(attempted_redirect_url
.spec());
96 message_parameters
.push_back(kTranslate
+ winning_extension_id
);
97 message_parameters
.push_back(winning_redirect_url
.spec());
101 IDS_EXTENSION_WARNINGS_REDIRECT_CONFLICT
,
106 Warning
Warning::CreateRequestHeaderConflictWarning(
107 const std::string
& extension_id
,
108 const std::string
& winning_extension_id
,
109 const std::string
& conflicting_header
) {
110 std::vector
<std::string
> message_parameters
;
111 message_parameters
.push_back(conflicting_header
);
112 message_parameters
.push_back(kTranslate
+ winning_extension_id
);
116 IDS_EXTENSION_WARNINGS_REQUEST_HEADER_CONFLICT
,
121 Warning
Warning::CreateResponseHeaderConflictWarning(
122 const std::string
& extension_id
,
123 const std::string
& winning_extension_id
,
124 const std::string
& conflicting_header
) {
125 std::vector
<std::string
> message_parameters
;
126 message_parameters
.push_back(conflicting_header
);
127 message_parameters
.push_back(kTranslate
+ winning_extension_id
);
131 IDS_EXTENSION_WARNINGS_RESPONSE_HEADER_CONFLICT
,
136 Warning
Warning::CreateCredentialsConflictWarning(
137 const std::string
& extension_id
,
138 const std::string
& winning_extension_id
) {
139 std::vector
<std::string
> message_parameters
;
140 message_parameters
.push_back(kTranslate
+ winning_extension_id
);
144 IDS_EXTENSION_WARNINGS_CREDENTIALS_CONFLICT
,
149 Warning
Warning::CreateRepeatedCacheFlushesWarning(
150 const std::string
& extension_id
) {
151 std::vector
<std::string
> message_parameters
;
152 message_parameters
.push_back(ExtensionsClient::Get()->GetProductName());
154 kRepeatedCacheFlushes
,
156 IDS_EXTENSION_WARNINGS_NETWORK_DELAY
,
161 Warning
Warning::CreateDownloadFilenameConflictWarning(
162 const std::string
& losing_extension_id
,
163 const std::string
& winning_extension_id
,
164 const base::FilePath
& losing_filename
,
165 const base::FilePath
& winning_filename
) {
166 std::vector
<std::string
> message_parameters
;
167 message_parameters
.push_back(base::UTF16ToUTF8(
168 losing_filename
.LossyDisplayName()));
169 message_parameters
.push_back(kTranslate
+ winning_extension_id
);
170 message_parameters
.push_back(base::UTF16ToUTF8(
171 winning_filename
.LossyDisplayName()));
173 kDownloadFilenameConflict
,
175 IDS_EXTENSION_WARNINGS_DOWNLOAD_FILENAME_CONFLICT
,
180 Warning
Warning::CreateReloadTooFrequentWarning(
181 const std::string
& extension_id
) {
182 std::vector
<std::string
> message_parameters
;
183 return Warning(kReloadTooFrequent
,
185 IDS_EXTENSION_WARNING_RELOAD_TOO_FREQUENT
,
189 std::string
Warning::GetLocalizedMessage(const ExtensionSet
* extensions
) const {
190 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
192 // These parameters may be unsafe (URLs and Extension names) and need
193 // to be HTML-escaped before being embedded in the UI. Also extension IDs
194 // are translated to full extension names.
195 std::vector
<base::string16
> final_parameters
;
196 for (size_t i
= 0; i
< message_parameters_
.size(); ++i
) {
197 std::string message
= message_parameters_
[i
];
198 if (base::StartsWith(message
, kTranslate
, base::CompareCase::SENSITIVE
)) {
199 std::string extension_id
= message
.substr(sizeof(kTranslate
) - 1);
200 const extensions::Extension
* extension
=
201 extensions
->GetByID(extension_id
);
202 message
= extension
? extension
->name() : extension_id
;
204 final_parameters
.push_back(base::UTF8ToUTF16(net::EscapeForHTML(message
)));
207 static_assert(kMaxNumberOfParameters
== 4u,
208 "You Need To Add More Case Statements");
209 switch (final_parameters
.size()) {
211 return l10n_util::GetStringUTF8(message_id_
);
213 return l10n_util::GetStringFUTF8(message_id_
, final_parameters
[0]);
215 return l10n_util::GetStringFUTF8(message_id_
, final_parameters
[0],
216 final_parameters
[1]);
218 return l10n_util::GetStringFUTF8(message_id_
, final_parameters
[0],
219 final_parameters
[1], final_parameters
[2]);
221 return l10n_util::GetStringFUTF8(message_id_
, final_parameters
[0],
222 final_parameters
[1], final_parameters
[2], final_parameters
[3]);
225 return std::string();
229 bool operator<(const Warning
& a
, const Warning
& b
) {
230 if (a
.extension_id() != b
.extension_id())
231 return a
.extension_id() < b
.extension_id();
232 return a
.warning_type() < b
.warning_type();
235 } // namespace extensions