Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" | |
| 16 #include "chrome/browser/extensions/unpacked_installer.h" | 15 #include "chrome/browser/extensions/unpacked_installer.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/chrome_select_file_policy.h" | 17 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 19 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/user_metrics.h" | 19 #include "content/public/browser/user_metrics.h" |
| 21 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/browser/web_ui.h" | 21 #include "content/public/browser/web_ui.h" |
| 23 #include "content/public/browser/web_ui_data_source.h" | 22 #include "content/public/browser/web_ui_data_source.h" |
| 24 #include "extensions/browser/extension_system.h" | 23 #include "extensions/browser/extension_system.h" |
| 25 #include "extensions/browser/file_highlighter.h" | 24 #include "extensions/browser/file_highlighter.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 } | 123 } |
| 125 | 124 |
| 126 void ExtensionLoaderHandler::FileHelper::MultiFilesSelected( | 125 void ExtensionLoaderHandler::FileHelper::MultiFilesSelected( |
| 127 const std::vector<base::FilePath>& files, void* params) { | 126 const std::vector<base::FilePath>& files, void* params) { |
| 128 NOTREACHED(); | 127 NOTREACHED(); |
| 129 } | 128 } |
| 130 | 129 |
| 131 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile) | 130 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile) |
| 132 : profile_(profile), | 131 : profile_(profile), |
| 133 file_helper_(new FileHelper(this)), | 132 file_helper_(new FileHelper(this)), |
| 133 extension_error_reporter_observer_(this), | |
| 134 display_ready_(false), | |
| 134 weak_ptr_factory_(this) { | 135 weak_ptr_factory_(this) { |
| 135 DCHECK(profile_); | 136 DCHECK(profile_); |
| 137 extension_error_reporter_observer_.Add(ExtensionErrorReporter::GetInstance()); | |
| 136 } | 138 } |
| 137 | 139 |
| 138 ExtensionLoaderHandler::~ExtensionLoaderHandler() { | 140 ExtensionLoaderHandler::~ExtensionLoaderHandler() { |
| 139 } | 141 } |
| 140 | 142 |
| 141 void ExtensionLoaderHandler::GetLocalizedValues( | 143 void ExtensionLoaderHandler::GetLocalizedValues( |
| 142 content::WebUIDataSource* source) { | 144 content::WebUIDataSource* source) { |
| 143 source->AddString( | 145 source->AddString( |
| 144 "extensionLoadErrorHeading", | 146 "extensionLoadErrorHeading", |
| 145 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING)); | 147 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING)); |
| 146 source->AddString( | 148 source->AddString( |
| 147 "extensionLoadErrorMessage", | 149 "extensionLoadErrorMessage", |
| 148 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE)); | 150 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE)); |
| 149 source->AddString( | 151 source->AddString( |
| 150 "extensionLoadErrorRetry", | 152 "extensionLoadErrorRetry", |
| 151 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_RETRY)); | 153 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_RETRY)); |
| 152 source->AddString( | 154 source->AddString( |
| 153 "extensionLoadErrorGiveUp", | 155 "extensionLoadErrorGiveUp", |
| 154 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_GIVE_UP)); | 156 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_GIVE_UP)); |
| 155 source->AddString( | 157 source->AddString( |
| 156 "extensionLoadCouldNotLoadManifest", | 158 "extensionLoadCouldNotLoadManifest", |
| 157 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_COULD_NOT_LOAD_MANIFEST)); | 159 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_COULD_NOT_LOAD_MANIFEST)); |
| 160 source->AddString( | |
| 161 "extensionLoadAdditionalFailures", | |
| 162 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ADDITIONAL_FAILURES)); | |
| 158 } | 163 } |
| 159 | 164 |
| 160 void ExtensionLoaderHandler::RegisterMessages() { | 165 void ExtensionLoaderHandler::RegisterMessages() { |
| 166 content::WebContentsObserver::Observe(web_ui()->GetWebContents()); | |
| 161 web_ui()->RegisterMessageCallback( | 167 web_ui()->RegisterMessageCallback( |
| 162 "extensionLoaderLoadUnpacked", | 168 "extensionLoaderLoadUnpacked", |
| 163 base::Bind(&ExtensionLoaderHandler::HandleLoadUnpacked, | 169 base::Bind(&ExtensionLoaderHandler::HandleLoadUnpacked, |
| 164 weak_ptr_factory_.GetWeakPtr())); | 170 weak_ptr_factory_.GetWeakPtr())); |
| 165 web_ui()->RegisterMessageCallback( | 171 web_ui()->RegisterMessageCallback( |
| 166 "extensionLoaderRetry", | 172 "extensionLoaderRetry", |
| 167 base::Bind(&ExtensionLoaderHandler::HandleRetry, | 173 base::Bind(&ExtensionLoaderHandler::HandleRetry, |
| 168 weak_ptr_factory_.GetWeakPtr())); | 174 weak_ptr_factory_.GetWeakPtr())); |
| 175 web_ui()->RegisterMessageCallback( | |
| 176 "extensionLoaderIgnoreFailure", | |
| 177 base::Bind(&ExtensionLoaderHandler::HandleIgnoreFailure, | |
| 178 weak_ptr_factory_.GetWeakPtr())); | |
| 179 web_ui()->RegisterMessageCallback( | |
| 180 "extensionLoaderDisplayFailures", | |
| 181 base::Bind(&ExtensionLoaderHandler::HandleDisplayFailures, | |
| 182 weak_ptr_factory_.GetWeakPtr())); | |
| 183 } | |
| 184 | |
| 185 void ExtensionLoaderHandler::DidStartNavigationToPendingEntry( | |
| 186 const GURL& url, | |
| 187 content::NavigationController::ReloadType reload_type) { | |
| 188 if (reload_type != content::NavigationController::NO_RELOAD) { | |
|
Devlin
2014/07/09 23:03:32
no brackets around single-line ifs.
gpdavis
2014/07/10 00:59:26
Dang, I keep forgetting to remove those after I ta
| |
| 189 display_ready_ = false; | |
| 190 } | |
| 169 } | 191 } |
| 170 | 192 |
| 171 void ExtensionLoaderHandler::HandleLoadUnpacked(const base::ListValue* args) { | 193 void ExtensionLoaderHandler::HandleLoadUnpacked(const base::ListValue* args) { |
| 172 DCHECK(args->empty()); | 194 DCHECK(args->empty()); |
| 173 file_helper_->ChooseFile(); | 195 file_helper_->ChooseFile(); |
| 174 } | 196 } |
| 175 | 197 |
| 176 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) { | 198 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) { |
| 177 DCHECK(args->empty()); | 199 DCHECK(args->empty()); |
| 178 LoadUnpackedExtensionImpl(failed_path_); | 200 const base::FilePath file_path(failed_paths_.back()); |
|
Devlin
2014/07/09 23:03:32
nit: I find this cleaner as file_path = failed_pat
gpdavis
2014/07/10 00:59:26
Done.
| |
| 201 failed_paths_.pop_back(); | |
| 202 LoadUnpackedExtensionImpl(file_path); | |
| 203 } | |
| 204 | |
| 205 void ExtensionLoaderHandler::HandleIgnoreFailure(const base::ListValue* args) { | |
| 206 DCHECK(args->empty()); | |
| 207 failed_paths_.pop_back(); | |
| 208 } | |
| 209 | |
| 210 void ExtensionLoaderHandler::HandleDisplayFailures( | |
| 211 const base::ListValue* args) { | |
| 212 DCHECK(args->empty()); | |
| 213 display_ready_ = true; | |
| 214 | |
| 215 if (!failures_.empty()) | |
| 216 NotifyFrontendOfFailure(); | |
| 179 } | 217 } |
| 180 | 218 |
| 181 void ExtensionLoaderHandler::LoadUnpackedExtensionImpl( | 219 void ExtensionLoaderHandler::LoadUnpackedExtensionImpl( |
| 182 const base::FilePath& file_path) { | 220 const base::FilePath& file_path) { |
| 183 scoped_refptr<UnpackedInstaller> installer = UnpackedInstaller::Create( | 221 scoped_refptr<UnpackedInstaller> installer = UnpackedInstaller::Create( |
| 184 ExtensionSystem::Get(profile_)->extension_service()); | 222 ExtensionSystem::Get(profile_)->extension_service()); |
| 185 installer->set_on_failure_callback( | |
| 186 base::Bind(&ExtensionLoaderHandler::OnLoadFailure, | |
| 187 weak_ptr_factory_.GetWeakPtr())); | |
| 188 | 223 |
| 189 // We do our own error handling, so we don't want a load failure to trigger | 224 // We do our own error handling, so we don't want a load failure to trigger |
| 190 // a dialog. | 225 // a dialog. |
| 191 installer->set_be_noisy_on_failure(false); | 226 installer->set_be_noisy_on_failure(false); |
| 192 | 227 |
| 193 installer->Load(file_path); | 228 installer->Load(file_path); |
| 194 } | 229 } |
| 195 | 230 |
| 196 void ExtensionLoaderHandler::OnLoadFailure(const base::FilePath& file_path, | 231 void ExtensionLoaderHandler::OnLoadFailure(const base::FilePath& file_path, |
| 197 const std::string& error) { | 232 const std::string& error) { |
| 198 failed_path_ = file_path; | 233 failed_paths_.push_back(file_path); |
|
Devlin
2014/07/09 23:03:32
Let's reduce the risk of a race a little bit by mo
gpdavis
2014/07/10 00:59:26
Done.
| |
| 199 size_t line = 0u; | 234 size_t line = 0u; |
| 200 size_t column = 0u; | 235 size_t column = 0u; |
| 201 std::string regex = | 236 std::string regex = |
| 202 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.", | 237 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.", |
| 203 manifest_errors::kManifestParseError); | 238 manifest_errors::kManifestParseError); |
| 204 // If this was a JSON parse error, we can highlight the exact line with the | 239 // 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, | 240 // 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, | 241 // reference, and so that if we ever make this really fancy and add an editor, |
| 207 // it's ready). | 242 // it's ready). |
| 208 // | 243 // |
| 209 // This regex call can fail, but if it does, we just don't highlight anything. | 244 // This regex call can fail, but if it does, we just don't highlight anything. |
| 210 re2::RE2::FullMatch(error, regex, &line, &column); | 245 re2::RE2::FullMatch(error, regex, &line, &column); |
| 211 | 246 |
| 212 // This will read the manifest and call NotifyFrontendOfFailure with the read | 247 // This will read the manifest and call NotifyFrontendOfFailure with the read |
| 213 // manifest contents. | 248 // manifest contents. |
| 214 base::PostTaskAndReplyWithResult( | 249 base::PostTaskAndReplyWithResult( |
| 215 content::BrowserThread::GetBlockingPool(), | 250 content::BrowserThread::GetBlockingPool(), |
| 216 FROM_HERE, | 251 FROM_HERE, |
| 217 base::Bind(&ReadFileToString, file_path.Append(kManifestFilename)), | 252 base::Bind(&ReadFileToString, file_path.Append(kManifestFilename)), |
| 218 base::Bind(&ExtensionLoaderHandler::NotifyFrontendOfFailure, | 253 base::Bind(&ExtensionLoaderHandler::AddFailure, |
| 219 weak_ptr_factory_.GetWeakPtr(), | 254 weak_ptr_factory_.GetWeakPtr(), |
| 220 file_path, | 255 file_path, |
| 221 error, | 256 error, |
| 222 line)); | 257 line)); |
| 223 } | 258 } |
| 224 | 259 |
| 225 void ExtensionLoaderHandler::NotifyFrontendOfFailure( | 260 void ExtensionLoaderHandler::AddFailure( |
| 226 const base::FilePath& file_path, | 261 const base::FilePath& file_path, |
| 227 const std::string& error, | 262 const std::string& error, |
| 228 size_t line_number, | 263 size_t line_number, |
| 229 const std::string& manifest) { | 264 const std::string& manifest) { |
| 230 base::StringValue file_value(file_path.LossyDisplayName()); | 265 scoped_ptr<base::DictionaryValue> manifest_value(new base::DictionaryValue()); |
| 231 base::StringValue error_value(base::UTF8ToUTF16(error)); | |
| 232 | |
| 233 base::DictionaryValue manifest_value; | |
| 234 SourceHighlighter highlighter(manifest, line_number); | 266 SourceHighlighter highlighter(manifest, line_number); |
| 235 // If the line number is 0, this highlights no regions, but still adds the | 267 // If the line number is 0, this highlights no regions, but still adds the |
| 236 // full manifest. | 268 // full manifest. |
| 237 highlighter.SetHighlightedRegions(&manifest_value); | 269 highlighter.SetHighlightedRegions(manifest_value.get()); |
| 238 | 270 |
| 271 scoped_ptr<base::DictionaryValue> failure(new base::DictionaryValue()); | |
| 272 failure->Set("path", new base::StringValue(file_path.LossyDisplayName())); | |
| 273 failure->Set("error", new base::StringValue(base::UTF8ToUTF16(error))); | |
| 274 failure->Set("manifest", manifest_value.release()); | |
| 275 failures_.Append(failure.release()); | |
| 276 | |
| 277 // Only notify the frontend if the display is ready. | |
| 278 if (display_ready_) | |
| 279 NotifyFrontendOfFailure(); | |
| 280 } | |
| 281 | |
| 282 void ExtensionLoaderHandler::NotifyFrontendOfFailure() { | |
| 239 web_ui()->CallJavascriptFunction( | 283 web_ui()->CallJavascriptFunction( |
| 240 "extensions.ExtensionLoader.notifyLoadFailed", | 284 "extensions.ExtensionLoader.notifyLoadFailed", |
| 241 file_value, | 285 failures_); |
| 242 error_value, | 286 failures_.Clear(); |
| 243 manifest_value); | |
| 244 } | 287 } |
| 245 | 288 |
| 246 } // namespace extensions | 289 } // namespace extensions |
| OLD | NEW |