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

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: Prettified failure path 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"
11 #include "base/strings/string16.h" 11 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h" 15 #include "chrome/browser/extensions/path_util.h"
16 #include "chrome/browser/extensions/unpacked_installer.h" 16 #include "chrome/browser/extensions/unpacked_installer.h"
17 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/chrome_select_file_policy.h" 18 #include "chrome/browser/ui/chrome_select_file_policy.h"
19 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/user_metrics.h" 20 #include "content/public/browser/user_metrics.h"
21 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_ui.h" 22 #include "content/public/browser/web_ui.h"
23 #include "content/public/browser/web_ui_data_source.h" 23 #include "content/public/browser/web_ui_data_source.h"
24 #include "extensions/browser/extension_system.h" 24 #include "extensions/browser/extension_system.h"
25 #include "extensions/browser/file_highlighter.h" 25 #include "extensions/browser/file_highlighter.h"
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile) 131 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile)
132 : profile_(profile), 132 : profile_(profile),
133 file_helper_(new FileHelper(this)), 133 file_helper_(new FileHelper(this)),
134 extension_error_reporter_observer_(this),
135 display_ready_(false),
134 weak_ptr_factory_(this) { 136 weak_ptr_factory_(this) {
135 DCHECK(profile_); 137 DCHECK(profile_);
138 extension_error_reporter_observer_.Add(ExtensionErrorReporter::GetInstance());
136 } 139 }
137 140
138 ExtensionLoaderHandler::~ExtensionLoaderHandler() { 141 ExtensionLoaderHandler::~ExtensionLoaderHandler() {
139 } 142 }
140 143
141 void ExtensionLoaderHandler::GetLocalizedValues( 144 void ExtensionLoaderHandler::GetLocalizedValues(
142 content::WebUIDataSource* source) { 145 content::WebUIDataSource* source) {
143 source->AddString( 146 source->AddString(
144 "extensionLoadErrorHeading", 147 "extensionLoadErrorHeading",
145 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING)); 148 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING));
146 source->AddString( 149 source->AddString(
147 "extensionLoadErrorMessage", 150 "extensionLoadErrorMessage",
148 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE)); 151 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE));
149 source->AddString( 152 source->AddString(
150 "extensionLoadErrorRetry", 153 "extensionLoadErrorRetry",
151 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_RETRY)); 154 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_RETRY));
152 source->AddString( 155 source->AddString(
153 "extensionLoadErrorGiveUp", 156 "extensionLoadErrorGiveUp",
154 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_GIVE_UP)); 157 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_GIVE_UP));
155 source->AddString( 158 source->AddString(
156 "extensionLoadCouldNotLoadManifest", 159 "extensionLoadCouldNotLoadManifest",
157 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_COULD_NOT_LOAD_MANIFEST)); 160 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_COULD_NOT_LOAD_MANIFEST));
161 source->AddString(
162 "extensionLoadAdditionalFailures",
163 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ADDITIONAL_FAILURES));
158 } 164 }
159 165
160 void ExtensionLoaderHandler::RegisterMessages() { 166 void ExtensionLoaderHandler::RegisterMessages() {
167 content::WebContentsObserver::Observe(web_ui()->GetWebContents());
Finnur 2014/07/14 10:25:58 Do we need to call Observe(NULL) when we are done?
gpdavis 2014/07/14 18:58:19 Done with what, exactly? Done with observing for
Finnur 2014/07/15 10:38:45 Sounds good. Maybe that warrants a comment in the
gpdavis 2014/07/15 18:33:14 Will do!
161 web_ui()->RegisterMessageCallback( 168 web_ui()->RegisterMessageCallback(
162 "extensionLoaderLoadUnpacked", 169 "extensionLoaderLoadUnpacked",
163 base::Bind(&ExtensionLoaderHandler::HandleLoadUnpacked, 170 base::Bind(&ExtensionLoaderHandler::HandleLoadUnpacked,
164 weak_ptr_factory_.GetWeakPtr())); 171 weak_ptr_factory_.GetWeakPtr()));
165 web_ui()->RegisterMessageCallback( 172 web_ui()->RegisterMessageCallback(
166 "extensionLoaderRetry", 173 "extensionLoaderRetry",
167 base::Bind(&ExtensionLoaderHandler::HandleRetry, 174 base::Bind(&ExtensionLoaderHandler::HandleRetry,
168 weak_ptr_factory_.GetWeakPtr())); 175 weak_ptr_factory_.GetWeakPtr()));
176 web_ui()->RegisterMessageCallback(
177 "extensionLoaderIgnoreFailure",
178 base::Bind(&ExtensionLoaderHandler::HandleIgnoreFailure,
179 weak_ptr_factory_.GetWeakPtr()));
180 web_ui()->RegisterMessageCallback(
181 "extensionLoaderDisplayFailures",
182 base::Bind(&ExtensionLoaderHandler::HandleDisplayFailures,
183 weak_ptr_factory_.GetWeakPtr()));
169 } 184 }
170 185
171 void ExtensionLoaderHandler::HandleLoadUnpacked(const base::ListValue* args) { 186 void ExtensionLoaderHandler::HandleLoadUnpacked(const base::ListValue* args) {
172 DCHECK(args->empty()); 187 DCHECK(args->empty());
173 file_helper_->ChooseFile(); 188 file_helper_->ChooseFile();
174 } 189 }
175 190
176 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) { 191 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) {
177 DCHECK(args->empty()); 192 DCHECK(args->empty());
178 LoadUnpackedExtensionImpl(failed_path_); 193 const base::FilePath file_path = failed_paths_.back();
194 failed_paths_.pop_back();
195 LoadUnpackedExtensionImpl(file_path);
Finnur 2014/07/14 10:25:58 This got me thinking... If I have two failures (ex
gpdavis 2014/07/14 18:58:19 Yes, that is the current functionality. The "Retr
Finnur 2014/07/15 10:38:45 I see. That's good.
196 }
197
198 void ExtensionLoaderHandler::HandleIgnoreFailure(const base::ListValue* args) {
199 DCHECK(args->empty());
200 failed_paths_.pop_back();
Finnur 2014/07/14 10:25:58 Similar argument here. Two failures: Should Ignore
gpdavis 2014/07/14 18:58:19 I guess that's more of a design question for you g
Finnur 2014/07/15 10:38:45 Yeah, that's fine.
Devlin 2014/07/15 17:51:44 Just a reminder: There are only multiple failures
201 }
202
203 void ExtensionLoaderHandler::HandleDisplayFailures(
204 const base::ListValue* args) {
205 DCHECK(args->empty());
206 display_ready_ = true;
207
208 if (!failures_.empty())
209 NotifyFrontendOfFailure();
179 } 210 }
180 211
181 void ExtensionLoaderHandler::LoadUnpackedExtensionImpl( 212 void ExtensionLoaderHandler::LoadUnpackedExtensionImpl(
182 const base::FilePath& file_path) { 213 const base::FilePath& file_path) {
183 scoped_refptr<UnpackedInstaller> installer = UnpackedInstaller::Create( 214 scoped_refptr<UnpackedInstaller> installer = UnpackedInstaller::Create(
184 ExtensionSystem::Get(profile_)->extension_service()); 215 ExtensionSystem::Get(profile_)->extension_service());
185 installer->set_on_failure_callback(
186 base::Bind(&ExtensionLoaderHandler::OnLoadFailure,
187 weak_ptr_factory_.GetWeakPtr()));
188 216
189 // We do our own error handling, so we don't want a load failure to trigger 217 // We do our own error handling, so we don't want a load failure to trigger
190 // a dialog. 218 // a dialog.
191 installer->set_be_noisy_on_failure(false); 219 installer->set_be_noisy_on_failure(false);
192 220
193 installer->Load(file_path); 221 installer->Load(file_path);
194 } 222 }
195 223
196 void ExtensionLoaderHandler::OnLoadFailure(const base::FilePath& file_path, 224 void ExtensionLoaderHandler::OnLoadFailure(const base::FilePath& file_path,
197 const std::string& error) { 225 const std::string& error) {
198 failed_path_ = file_path;
199 size_t line = 0u; 226 size_t line = 0u;
200 size_t column = 0u; 227 size_t column = 0u;
201 std::string regex = 228 std::string regex =
202 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.", 229 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.",
203 manifest_errors::kManifestParseError); 230 manifest_errors::kManifestParseError);
204 // If this was a JSON parse error, we can highlight the exact line with the 231 // 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, 232 // 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, 233 // reference, and so that if we ever make this really fancy and add an editor,
207 // it's ready). 234 // it's ready).
208 // 235 //
209 // This regex call can fail, but if it does, we just don't highlight anything. 236 // This regex call can fail, but if it does, we just don't highlight anything.
210 re2::RE2::FullMatch(error, regex, &line, &column); 237 re2::RE2::FullMatch(error, regex, &line, &column);
211 238
212 // This will read the manifest and call NotifyFrontendOfFailure with the read 239 // This will read the manifest and call NotifyFrontendOfFailure with the read
213 // manifest contents. 240 // manifest contents.
214 base::PostTaskAndReplyWithResult( 241 base::PostTaskAndReplyWithResult(
215 content::BrowserThread::GetBlockingPool(), 242 content::BrowserThread::GetBlockingPool(),
216 FROM_HERE, 243 FROM_HERE,
217 base::Bind(&ReadFileToString, file_path.Append(kManifestFilename)), 244 base::Bind(&ReadFileToString, file_path.Append(kManifestFilename)),
218 base::Bind(&ExtensionLoaderHandler::NotifyFrontendOfFailure, 245 base::Bind(&ExtensionLoaderHandler::AddFailure,
219 weak_ptr_factory_.GetWeakPtr(), 246 weak_ptr_factory_.GetWeakPtr(),
220 file_path, 247 file_path,
221 error, 248 error,
222 line)); 249 line));
223 } 250 }
224 251
225 void ExtensionLoaderHandler::NotifyFrontendOfFailure( 252 void ExtensionLoaderHandler::DidStartNavigationToPendingEntry(
253 const GURL& url,
254 content::NavigationController::ReloadType reload_type) {
255 if (reload_type != content::NavigationController::NO_RELOAD)
256 display_ready_ = false;
257 }
258
259 void ExtensionLoaderHandler::AddFailure(
226 const base::FilePath& file_path, 260 const base::FilePath& file_path,
227 const std::string& error, 261 const std::string& error,
228 size_t line_number, 262 size_t line_number,
229 const std::string& manifest) { 263 const std::string& manifest) {
230 base::StringValue file_value(file_path.LossyDisplayName()); 264 failed_paths_.push_back(file_path);
231 base::StringValue error_value(base::UTF8ToUTF16(error)); 265 base::FilePath prettified_path = path_util::PrettifyPath(file_path);
232 266
233 base::DictionaryValue manifest_value; 267 scoped_ptr<base::DictionaryValue> manifest_value(new base::DictionaryValue());
234 SourceHighlighter highlighter(manifest, line_number); 268 SourceHighlighter highlighter(manifest, line_number);
235 // If the line number is 0, this highlights no regions, but still adds the 269 // If the line number is 0, this highlights no regions, but still adds the
236 // full manifest. 270 // full manifest.
237 highlighter.SetHighlightedRegions(&manifest_value); 271 highlighter.SetHighlightedRegions(manifest_value.get());
238 272
273 scoped_ptr<base::DictionaryValue> failure(new base::DictionaryValue());
274 failure->Set("path",
275 new base::StringValue(prettified_path.LossyDisplayName()));
276 failure->Set("error", new base::StringValue(base::UTF8ToUTF16(error)));
277 failure->Set("manifest", manifest_value.release());
278 failures_.Append(failure.release());
279
280 // Only notify the frontend if the display is ready.
281 if (display_ready_)
282 NotifyFrontendOfFailure();
283 }
284
285 void ExtensionLoaderHandler::NotifyFrontendOfFailure() {
239 web_ui()->CallJavascriptFunction( 286 web_ui()->CallJavascriptFunction(
240 "extensions.ExtensionLoader.notifyLoadFailed", 287 "extensions.ExtensionLoader.notifyLoadFailed",
241 file_value, 288 failures_);
242 error_value, 289 failures_.Clear();
243 manifest_value);
244 } 290 }
245 291
246 } // namespace extensions 292 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698