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" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 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" |
| 26 #include "extensions/common/constants.h" | 26 #include "extensions/common/constants.h" |
| 27 #include "extensions/common/extension.h" | 27 #include "extensions/common/extension.h" |
| 28 #include "extensions/common/manifest_constants.h" | 28 #include "extensions/common/manifest_constants.h" |
| 29 #include "grit/generated_resources.h" | 29 #include "grit/generated_resources.h" |
| 30 #include "third_party/re2/re2/re2.h" | 30 #include "third_party/re2/re2/re2.h" |
| 31 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
| 32 #include "ui/shell_dialogs/select_file_dialog.h" | 32 #include "ui/shell_dialogs/select_file_dialog.h" |
| 33 | 33 |
| 34 #include "base/debug/stack_trace.h" | |
| 35 | |
| 36 class ExtensionErrorReporter; | |
| 37 class ExtensionErrorReporterObserver; | |
| 38 | |
| 34 namespace extensions { | 39 namespace extensions { |
| 35 | 40 |
| 36 namespace { | 41 namespace { |
| 37 | 42 |
| 38 // Read a file to a string and return. | 43 // Read a file to a string and return. |
| 39 std::string ReadFileToString(const base::FilePath& path) { | 44 std::string ReadFileToString(const base::FilePath& path) { |
| 40 std::string data; | 45 std::string data; |
| 41 // This call can fail, but it doesn't matter for our purposes. If it fails, | 46 // This call can fail, but it doesn't matter for our purposes. If it fails, |
| 42 // we simply return an empty string for the manifest, and ignore it. | 47 // we simply return an empty string for the manifest, and ignore it. |
| 43 base::ReadFileToString(path, &data); | 48 base::ReadFileToString(path, &data); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 } | 129 } |
| 125 | 130 |
| 126 void ExtensionLoaderHandler::FileHelper::MultiFilesSelected( | 131 void ExtensionLoaderHandler::FileHelper::MultiFilesSelected( |
| 127 const std::vector<base::FilePath>& files, void* params) { | 132 const std::vector<base::FilePath>& files, void* params) { |
| 128 NOTREACHED(); | 133 NOTREACHED(); |
| 129 } | 134 } |
| 130 | 135 |
| 131 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile) | 136 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile) |
| 132 : profile_(profile), | 137 : profile_(profile), |
| 133 file_helper_(new FileHelper(this)), | 138 file_helper_(new FileHelper(this)), |
| 134 weak_ptr_factory_(this) { | 139 weak_ptr_factory_(this), |
| 140 extension_error_reporter_observer_(this) { | |
| 135 DCHECK(profile_); | 141 DCHECK(profile_); |
| 142 DLOG(ERROR) << "--------------Constructor"; | |
| 143 extension_error_reporter_observer_.Add(ExtensionErrorReporter::GetInstance()); | |
| 136 } | 144 } |
| 137 | 145 |
| 138 ExtensionLoaderHandler::~ExtensionLoaderHandler() { | 146 ExtensionLoaderHandler::~ExtensionLoaderHandler() { |
| 139 } | 147 } |
| 140 | 148 |
| 141 void ExtensionLoaderHandler::GetLocalizedValues( | 149 void ExtensionLoaderHandler::GetLocalizedValues( |
| 142 content::WebUIDataSource* source) { | 150 content::WebUIDataSource* source) { |
| 143 source->AddString( | 151 source->AddString( |
| 144 "extensionLoadErrorHeading", | 152 "extensionLoadErrorHeading", |
| 145 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING)); | 153 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING)); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 175 | 183 |
| 176 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) { | 184 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) { |
| 177 DCHECK(args->empty()); | 185 DCHECK(args->empty()); |
| 178 LoadUnpackedExtensionImpl(failed_path_); | 186 LoadUnpackedExtensionImpl(failed_path_); |
| 179 } | 187 } |
| 180 | 188 |
| 181 void ExtensionLoaderHandler::LoadUnpackedExtensionImpl( | 189 void ExtensionLoaderHandler::LoadUnpackedExtensionImpl( |
| 182 const base::FilePath& file_path) { | 190 const base::FilePath& file_path) { |
| 183 scoped_refptr<UnpackedInstaller> installer = UnpackedInstaller::Create( | 191 scoped_refptr<UnpackedInstaller> installer = UnpackedInstaller::Create( |
| 184 ExtensionSystem::Get(profile_)->extension_service()); | 192 ExtensionSystem::Get(profile_)->extension_service()); |
| 185 installer->set_on_failure_callback( | 193 // installer->set_on_failure_callback( |
| 186 base::Bind(&ExtensionLoaderHandler::OnLoadFailure, | 194 // base::Bind(&ExtensionLoaderHandler::OnLoadFailure, |
| 187 weak_ptr_factory_.GetWeakPtr())); | 195 // weak_ptr_factory_.GetWeakPtr())); |
| 188 | 196 |
| 189 // We do our own error handling, so we don't want a load failure to trigger | 197 // We do our own error handling, so we don't want a load failure to trigger |
| 190 // a dialog. | 198 // a dialog. |
| 191 installer->set_be_noisy_on_failure(false); | 199 installer->set_be_noisy_on_failure(false); |
| 192 | 200 |
| 201 DLOG(ERROR) << "----------LoadUnpackedExtensionImpl"; | |
| 193 installer->Load(file_path); | 202 installer->Load(file_path); |
| 194 } | 203 } |
| 195 | 204 |
| 196 void ExtensionLoaderHandler::OnLoadFailure(const base::FilePath& file_path, | 205 void ExtensionLoaderHandler::OnLoadFailure(const base::FilePath& file_path, |
| 197 const std::string& error) { | 206 const std::string& error) { |
| 207 DLOG(ERROR) << "-------OnLoadFailure"; | |
| 198 failed_path_ = file_path; | 208 failed_path_ = file_path; |
| 199 size_t line = 0u; | 209 size_t line = 0u; |
| 200 size_t column = 0u; | 210 size_t column = 0u; |
| 201 std::string regex = | 211 std::string regex = |
| 202 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.", | 212 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.", |
| 203 manifest_errors::kManifestParseError); | 213 manifest_errors::kManifestParseError); |
| 204 // If this was a JSON parse error, we can highlight the exact line with the | 214 // 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, | 215 // 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, | 216 // reference, and so that if we ever make this really fancy and add an editor, |
| 207 // it's ready). | 217 // it's ready). |
| 208 // | 218 // |
| 209 // This regex call can fail, but if it does, we just don't highlight anything. | 219 // This regex call can fail, but if it does, we just don't highlight anything. |
| 210 re2::RE2::FullMatch(error, regex, &line, &column); | 220 re2::RE2::FullMatch(error, regex, &line, &column); |
| 211 | 221 |
| 212 // This will read the manifest and call NotifyFrontendOfFailure with the read | 222 // This will read the manifest and call NotifyFrontendOfFailure with the read |
| 213 // manifest contents. | 223 // manifest contents. |
| 214 base::PostTaskAndReplyWithResult( | 224 base::PostTaskAndReplyWithResult( |
| 215 content::BrowserThread::GetBlockingPool(), | 225 content::BrowserThread::GetBlockingPool(), |
| 216 FROM_HERE, | 226 FROM_HERE, |
| 217 base::Bind(&ReadFileToString, file_path.Append(kManifestFilename)), | 227 base::Bind(&ReadFileToString, file_path.Append(kManifestFilename)), |
| 218 base::Bind(&ExtensionLoaderHandler::NotifyFrontendOfFailure, | 228 base::Bind(&ExtensionLoaderHandler::NotifyFrontendOfFailure, |
| 219 weak_ptr_factory_.GetWeakPtr(), | 229 weak_ptr_factory_.GetWeakPtr(), |
| 220 file_path, | 230 file_path, |
| 221 error, | 231 error, |
| 222 line)); | 232 line)); |
| 233 /* | |
|
gpdavis
2014/06/18 23:53:16
Replacing the PostTaskAndReplyWithResult with this
| |
| 234 const std::string& manifest = | |
| 235 ReadFileToString(file_path.Append(kManifestFilename)); | |
| 236 ExtensionLoaderHandler::NotifyFrontendOfFailure(file_path, error, line, | |
| 237 manifest); | |
| 238 */ | |
| 223 } | 239 } |
| 224 | 240 |
| 225 void ExtensionLoaderHandler::NotifyFrontendOfFailure( | 241 void ExtensionLoaderHandler::NotifyFrontendOfFailure( |
| 226 const base::FilePath& file_path, | 242 const base::FilePath& file_path, |
| 227 const std::string& error, | 243 const std::string& error, |
| 228 size_t line_number, | 244 size_t line_number, |
| 229 const std::string& manifest) { | 245 const std::string& manifest) { |
| 230 base::StringValue file_value(file_path.LossyDisplayName()); | 246 base::StringValue file_value(file_path.LossyDisplayName()); |
| 231 base::StringValue error_value(base::UTF8ToUTF16(error)); | 247 base::StringValue error_value(base::UTF8ToUTF16(error)); |
| 232 | 248 |
| 233 base::DictionaryValue manifest_value; | 249 base::DictionaryValue manifest_value; |
| 234 SourceHighlighter highlighter(manifest, line_number); | 250 SourceHighlighter highlighter(manifest, line_number); |
| 235 // If the line number is 0, this highlights no regions, but still adds the | 251 // If the line number is 0, this highlights no regions, but still adds the |
| 236 // full manifest. | 252 // full manifest. |
| 237 highlighter.SetHighlightedRegions(&manifest_value); | 253 highlighter.SetHighlightedRegions(&manifest_value); |
| 238 | 254 |
| 255 DLOG(ERROR) << "-NotifyFrontendOfFailure"; | |
| 256 base::debug::StackTrace trace; | |
| 257 LOG(WARNING) << "My trace is: " << trace.ToString(); | |
| 258 | |
| 239 web_ui()->CallJavascriptFunction( | 259 web_ui()->CallJavascriptFunction( |
| 240 "extensions.ExtensionLoader.notifyLoadFailed", | 260 "extensions.ExtensionLoader.notifyLoadFailed", |
| 241 file_value, | 261 file_value, |
| 242 error_value, | 262 error_value, |
| 243 manifest_value); | 263 manifest_value); |
| 244 } | 264 } |
| 245 | 265 |
| 246 } // namespace extensions | 266 } // namespace extensions |
| OLD | NEW |