Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: chrome/browser/ui/webui/extensions/extension_loader_handler.cc

Issue 342003005: Show alert failure for reloading unpacked extensions with bad manifest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved path handling back to C++ Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/extensions/extension_loader_handler.h" 5 #include "chrome/browser/ui/webui/extensions/extension_loader_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 void ExtensionLoaderHandler::FileHelper::FileSelected( 121 void ExtensionLoaderHandler::FileHelper::FileSelected(
122 const base::FilePath& path, int index, void* params) { 122 const base::FilePath& path, int index, void* params) {
123 loader_handler_->LoadUnpackedExtensionImpl(path); 123 loader_handler_->LoadUnpackedExtensionImpl(path);
124 } 124 }
125 125
126 void ExtensionLoaderHandler::FileHelper::MultiFilesSelected( 126 void ExtensionLoaderHandler::FileHelper::MultiFilesSelected(
127 const std::vector<base::FilePath>& files, void* params) { 127 const std::vector<base::FilePath>& files, void* params) {
128 NOTREACHED(); 128 NOTREACHED();
129 } 129 }
130 130
131 // Struct to hold failure data when NotifyFrontendOfFailure is called before
132 // the extensions page is fully loaded.
133 struct ExtensionLoaderHandler::FailureData {
134 FailureData(base::FilePath file_path,
Devlin 2014/07/02 17:46:50 this should take const file path and string.
gpdavis 2014/07/02 19:13:55 Done.
135 std::string error,
136 size_t line_number,
137 std::string manifest)
138 : file_path(file_path),
139 error(error),
140 line_number(line_number),
141 manifest(manifest) {}
142
143 ~FailureData() {}
144
145 base::FilePath file_path;
146 std::string error;
147 size_t line_number;
148 std::string manifest;
149 };
150
151 /*
152 ExtensionLoaderHandler::FailureData::FailureData(base::FilePath file_path,
Devlin 2014/07/02 17:46:50 prefer this one :)
gpdavis 2014/07/02 19:13:55 Done.
153 std::string error,
154 size_t line_number,
155 std::string manifest)
156 : file_path(file_path),
157 error(error),
158 line_number(line_number),
159 manifest(manifest) {}
160 */
161
131 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile) 162 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile)
132 : profile_(profile), 163 : profile_(profile),
133 file_helper_(new FileHelper(this)), 164 file_helper_(new FileHelper(this)),
165 extension_error_reporter_observer_(this),
166 display_ready_(false),
134 weak_ptr_factory_(this) { 167 weak_ptr_factory_(this) {
135 DCHECK(profile_); 168 DCHECK(profile_);
169 extension_error_reporter_observer_.Add(ExtensionErrorReporter::GetInstance());
136 } 170 }
137 171
138 ExtensionLoaderHandler::~ExtensionLoaderHandler() { 172 ExtensionLoaderHandler::~ExtensionLoaderHandler() {
139 } 173 }
140 174
141 void ExtensionLoaderHandler::GetLocalizedValues( 175 void ExtensionLoaderHandler::GetLocalizedValues(
142 content::WebUIDataSource* source) { 176 content::WebUIDataSource* source) {
143 source->AddString( 177 source->AddString(
144 "extensionLoadErrorHeading", 178 "extensionLoadErrorHeading",
145 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING)); 179 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING));
146 source->AddString( 180 source->AddString(
147 "extensionLoadErrorMessage", 181 "extensionLoadErrorMessage",
148 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE)); 182 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE));
149 source->AddString( 183 source->AddString(
150 "extensionLoadErrorRetry", 184 "extensionLoadErrorRetry",
151 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_RETRY)); 185 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_RETRY));
152 source->AddString( 186 source->AddString(
153 "extensionLoadErrorGiveUp", 187 "extensionLoadErrorGiveUp",
154 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_GIVE_UP)); 188 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_GIVE_UP));
155 source->AddString( 189 source->AddString(
156 "extensionLoadCouldNotLoadManifest", 190 "extensionLoadCouldNotLoadManifest",
157 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_COULD_NOT_LOAD_MANIFEST)); 191 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_COULD_NOT_LOAD_MANIFEST));
192 source->AddString(
193 "extensionLoadAdditionalFailures",
194 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ADDITIONAL_FAILURES));
158 } 195 }
159 196
160 void ExtensionLoaderHandler::RegisterMessages() { 197 void ExtensionLoaderHandler::RegisterMessages() {
161 web_ui()->RegisterMessageCallback( 198 web_ui()->RegisterMessageCallback(
162 "extensionLoaderLoadUnpacked", 199 "extensionLoaderLoadUnpacked",
163 base::Bind(&ExtensionLoaderHandler::HandleLoadUnpacked, 200 base::Bind(&ExtensionLoaderHandler::HandleLoadUnpacked,
164 weak_ptr_factory_.GetWeakPtr())); 201 weak_ptr_factory_.GetWeakPtr()));
165 web_ui()->RegisterMessageCallback( 202 web_ui()->RegisterMessageCallback(
166 "extensionLoaderRetry", 203 "extensionLoaderRetry",
167 base::Bind(&ExtensionLoaderHandler::HandleRetry, 204 base::Bind(&ExtensionLoaderHandler::HandleRetry,
168 weak_ptr_factory_.GetWeakPtr())); 205 weak_ptr_factory_.GetWeakPtr()));
206 web_ui()->RegisterMessageCallback(
207 "extensionLoaderGiveUp",
208 base::Bind(&ExtensionLoaderHandler::HandleGiveUp,
Devlin 2014/07/02 17:46:50 Even though we call it give up in the text, let's
gpdavis 2014/07/02 19:13:55 Done.
209 weak_ptr_factory_.GetWeakPtr()));
210 web_ui()->RegisterMessageCallback(
211 "extensionLoaderSetDisplayLoading",
212 base::Bind(&ExtensionLoaderHandler::HandleSetDisplayLoading,
213 weak_ptr_factory_.GetWeakPtr()));
214 web_ui()->RegisterMessageCallback(
215 "extensionLoaderDisplayFailures",
216 base::Bind(&ExtensionLoaderHandler::HandleDisplayFailures,
217 weak_ptr_factory_.GetWeakPtr()));
169 } 218 }
170 219
171 void ExtensionLoaderHandler::HandleLoadUnpacked(const base::ListValue* args) { 220 void ExtensionLoaderHandler::HandleLoadUnpacked(const base::ListValue* args) {
172 DCHECK(args->empty()); 221 DCHECK(args->empty());
173 file_helper_->ChooseFile(); 222 file_helper_->ChooseFile();
174 } 223 }
175 224
176 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) { 225 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) {
177 DCHECK(args->empty()); 226 DCHECK(args->empty());
178 LoadUnpackedExtensionImpl(failed_path_); 227 LoadUnpackedExtensionImpl(failed_paths_.back());
228 failed_paths_.pop_back();
229 }
230
231 void ExtensionLoaderHandler::HandleGiveUp(const base::ListValue* args) {
232 DCHECK(args->empty());
233 failed_paths_.pop_back();
234 }
235
236 void ExtensionLoaderHandler::HandleSetDisplayLoading(
237 const base::ListValue* args) {
238 DCHECK(args->empty());
239 display_ready_ = false;
240 }
241
242 void ExtensionLoaderHandler::HandleDisplayFailures(
243 const base::ListValue* args) {
244 DCHECK(args->empty());
245 display_ready_ = true;
246
247 if (failures_.empty())
248 return;
249
250 for (size_t i = 0; i < failures_.size(); ++i) {
251 FailureData* failure = failures_[i];
252 NotifyFrontendOfFailure(failure->file_path,
Devlin 2014/07/02 17:46:50 I'd still like to be able to notify the frontend i
253 failure->error,
254 failure->line_number,
255 failure->manifest);
256 }
257 failures_.clear();
179 } 258 }
180 259
181 void ExtensionLoaderHandler::LoadUnpackedExtensionImpl( 260 void ExtensionLoaderHandler::LoadUnpackedExtensionImpl(
182 const base::FilePath& file_path) { 261 const base::FilePath& file_path) {
183 scoped_refptr<UnpackedInstaller> installer = UnpackedInstaller::Create( 262 scoped_refptr<UnpackedInstaller> installer = UnpackedInstaller::Create(
184 ExtensionSystem::Get(profile_)->extension_service()); 263 ExtensionSystem::Get(profile_)->extension_service());
185 installer->set_on_failure_callback(
186 base::Bind(&ExtensionLoaderHandler::OnLoadFailure,
187 weak_ptr_factory_.GetWeakPtr()));
188 264
189 // We do our own error handling, so we don't want a load failure to trigger 265 // We do our own error handling, so we don't want a load failure to trigger
190 // a dialog. 266 // a dialog.
191 installer->set_be_noisy_on_failure(false); 267 installer->set_be_noisy_on_failure(false);
192 268
193 installer->Load(file_path); 269 installer->Load(file_path);
194 } 270 }
195 271
196 void ExtensionLoaderHandler::OnLoadFailure(const base::FilePath& file_path, 272 void ExtensionLoaderHandler::OnLoadFailure(const base::FilePath& file_path,
197 const std::string& error) { 273 const std::string& error) {
198 failed_path_ = file_path; 274 failed_paths_.push_back(file_path);
199 size_t line = 0u; 275 size_t line = 0u;
200 size_t column = 0u; 276 size_t column = 0u;
201 std::string regex = 277 std::string regex =
202 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.", 278 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.",
203 manifest_errors::kManifestParseError); 279 manifest_errors::kManifestParseError);
204 // If this was a JSON parse error, we can highlight the exact line with the 280 // If this was a JSON parse error, we can highlight the exact line with the
205 // error. Otherwise, we should still display the manifest (for consistency, 281 // error. Otherwise, we should still display the manifest (for consistency,
206 // reference, and so that if we ever make this really fancy and add an editor, 282 // reference, and so that if we ever make this really fancy and add an editor,
207 // it's ready). 283 // it's ready).
208 // 284 //
(...skipping 11 matching lines...) Expand all
220 file_path, 296 file_path,
221 error, 297 error,
222 line)); 298 line));
223 } 299 }
224 300
225 void ExtensionLoaderHandler::NotifyFrontendOfFailure( 301 void ExtensionLoaderHandler::NotifyFrontendOfFailure(
226 const base::FilePath& file_path, 302 const base::FilePath& file_path,
227 const std::string& error, 303 const std::string& error,
228 size_t line_number, 304 size_t line_number,
229 const std::string& manifest) { 305 const std::string& manifest) {
306 // If the extensions page is not loaded, delay frontend notification.
307 if (!display_ready_) {
308 failures_.push_back(new FailureData(file_path,
309 error,
310 line_number,
311 manifest));
312 return;
313 }
314
230 base::StringValue file_value(file_path.LossyDisplayName()); 315 base::StringValue file_value(file_path.LossyDisplayName());
231 base::StringValue error_value(base::UTF8ToUTF16(error)); 316 base::StringValue error_value(base::UTF8ToUTF16(error));
232 317
233 base::DictionaryValue manifest_value; 318 base::DictionaryValue manifest_value;
234 SourceHighlighter highlighter(manifest, line_number); 319 SourceHighlighter highlighter(manifest, line_number);
235 // If the line number is 0, this highlights no regions, but still adds the 320 // If the line number is 0, this highlights no regions, but still adds the
236 // full manifest. 321 // full manifest.
237 highlighter.SetHighlightedRegions(&manifest_value); 322 highlighter.SetHighlightedRegions(&manifest_value);
238 323
239 web_ui()->CallJavascriptFunction( 324 web_ui()->CallJavascriptFunction(
240 "extensions.ExtensionLoader.notifyLoadFailed", 325 "extensions.ExtensionLoader.notifyLoadFailed",
241 file_value, 326 file_value,
242 error_value, 327 error_value,
243 manifest_value); 328 manifest_value);
244 } 329 }
245 330
246 } // namespace extensions 331 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698