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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 // Class to hold failure data when NotifyFrontendOfFailure is called before | |
| 132 // the extensions page is fully loaded. | |
| 133 class ExtensionLoaderHandler::FailureData { | |
| 134 public: | |
|
Devlin
2014/06/27 22:31:10
fix indentation.
gpdavis
2014/06/28 02:31:18
Done.
| |
| 135 explicit FailureData(base::FilePath path, std::string error, | |
|
Devlin
2014/06/27 22:31:09
nit: one param per line.
Devlin
2014/06/27 22:31:10
const & on non-simple arguments.
gpdavis
2014/06/28 02:31:18
Done.
gpdavis
2014/06/28 02:31:18
Done.
| |
| 136 size_t line, std::string manifest) | |
| 137 : file_path(path), | |
| 138 error(error), | |
| 139 line_number(line), | |
| 140 manifest(manifest) {} | |
| 141 | |
| 142 base::FilePath file_path; | |
|
Devlin
2014/06/27 22:31:09
entirely public classes are what structs are for ;
gpdavis
2014/06/27 23:42:54
That was what I started with, but I wasn't sure if
Devlin
2014/06/28 08:40:02
A couple of C++ finer points:
- A vector of struct
| |
| 143 std::string error; | |
| 144 size_t line_number; | |
| 145 std::string manifest; | |
| 146 }; | |
| 147 | |
| 131 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile) | 148 ExtensionLoaderHandler::ExtensionLoaderHandler(Profile* profile) |
| 132 : profile_(profile), | 149 : profile_(profile), |
| 133 file_helper_(new FileHelper(this)), | 150 file_helper_(new FileHelper(this)), |
| 134 weak_ptr_factory_(this) { | 151 weak_ptr_factory_(this), |
|
Devlin
2014/06/27 22:31:09
weak_ptr_factory_ should be the final data member.
gpdavis
2014/06/28 02:31:18
Done.
| |
| 152 extension_error_reporter_observer_(this), | |
| 153 display_ready_(false) { | |
| 135 DCHECK(profile_); | 154 DCHECK(profile_); |
| 155 extension_error_reporter_observer_.Add(ExtensionErrorReporter::GetInstance()); | |
| 136 } | 156 } |
| 137 | 157 |
| 138 ExtensionLoaderHandler::~ExtensionLoaderHandler() { | 158 ExtensionLoaderHandler::~ExtensionLoaderHandler() { |
| 139 } | 159 } |
| 140 | 160 |
| 141 void ExtensionLoaderHandler::GetLocalizedValues( | 161 void ExtensionLoaderHandler::GetLocalizedValues( |
| 142 content::WebUIDataSource* source) { | 162 content::WebUIDataSource* source) { |
| 143 source->AddString( | 163 source->AddString( |
| 144 "extensionLoadErrorHeading", | 164 "extensionLoadErrorHeading", |
| 145 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING)); | 165 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING)); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 159 | 179 |
| 160 void ExtensionLoaderHandler::RegisterMessages() { | 180 void ExtensionLoaderHandler::RegisterMessages() { |
| 161 web_ui()->RegisterMessageCallback( | 181 web_ui()->RegisterMessageCallback( |
| 162 "extensionLoaderLoadUnpacked", | 182 "extensionLoaderLoadUnpacked", |
| 163 base::Bind(&ExtensionLoaderHandler::HandleLoadUnpacked, | 183 base::Bind(&ExtensionLoaderHandler::HandleLoadUnpacked, |
| 164 weak_ptr_factory_.GetWeakPtr())); | 184 weak_ptr_factory_.GetWeakPtr())); |
| 165 web_ui()->RegisterMessageCallback( | 185 web_ui()->RegisterMessageCallback( |
| 166 "extensionLoaderRetry", | 186 "extensionLoaderRetry", |
| 167 base::Bind(&ExtensionLoaderHandler::HandleRetry, | 187 base::Bind(&ExtensionLoaderHandler::HandleRetry, |
| 168 weak_ptr_factory_.GetWeakPtr())); | 188 weak_ptr_factory_.GetWeakPtr())); |
| 189 web_ui()->RegisterMessageCallback( | |
| 190 "extensionLoaderSetDisplayLoading", | |
| 191 base::Bind(&ExtensionLoaderHandler::HandleSetDisplayLoading, | |
| 192 weak_ptr_factory_.GetWeakPtr())); | |
| 193 web_ui()->RegisterMessageCallback( | |
| 194 "extensionLoaderDisplayFailures", | |
| 195 base::Bind(&ExtensionLoaderHandler::HandleDisplayFailures, | |
| 196 weak_ptr_factory_.GetWeakPtr())); | |
| 169 } | 197 } |
| 170 | 198 |
| 171 void ExtensionLoaderHandler::HandleLoadUnpacked(const base::ListValue* args) { | 199 void ExtensionLoaderHandler::HandleLoadUnpacked(const base::ListValue* args) { |
| 172 DCHECK(args->empty()); | 200 DCHECK(args->empty()); |
| 173 file_helper_->ChooseFile(); | 201 file_helper_->ChooseFile(); |
| 174 } | 202 } |
| 175 | 203 |
| 176 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) { | 204 void ExtensionLoaderHandler::HandleRetry(const base::ListValue* args) { |
| 177 DCHECK(args->empty()); | 205 DCHECK(args->empty()); |
| 178 LoadUnpackedExtensionImpl(failed_path_); | 206 LoadUnpackedExtensionImpl(failed_path_); |
| 179 } | 207 } |
| 180 | 208 |
| 209 void ExtensionLoaderHandler::HandleSetDisplayLoading( | |
| 210 const base::ListValue* args) { | |
| 211 DCHECK(args->empty()); | |
| 212 display_ready_ = false; | |
| 213 } | |
| 214 | |
| 215 void ExtensionLoaderHandler::HandleDisplayFailures( | |
| 216 const base::ListValue* args) { | |
| 217 DCHECK(args->empty()); | |
| 218 display_ready_ = true; | |
| 219 if (!failures_.empty()) { | |
|
Devlin
2014/06/27 22:31:09
instead of:
function {
if (condition) {
<lot
gpdavis
2014/06/27 23:42:54
That is indeed much better. Will fix (y)
gpdavis
2014/06/28 02:31:18
Done.
| |
| 220 FailureData *failure = failures_[0]; | |
|
Devlin
2014/06/27 22:31:09
nit: FailureData* failure
gpdavis
2014/06/28 02:31:18
Done.
| |
| 221 NotifyFrontendOfFailure(failure->file_path, failure->error, | |
|
Devlin
2014/06/27 22:31:09
one arg per line.
gpdavis
2014/06/28 02:31:18
Done.
| |
| 222 failure->line_number, failure->manifest); | |
| 223 | |
| 224 std::string list("Additional failures:"); | |
|
Devlin
2014/06/27 22:31:09
This needs to be i18n.
gpdavis
2014/06/28 02:31:18
Could you elaborate a little on this? I looked up
Devlin
2014/06/28 08:40:02
Ah, sorry :) i18n = internationalization. (See w
| |
| 225 for (size_t i = 1; i < failures_.size(); i++) { | |
|
Devlin
2014/06/27 22:31:09
++i.
gpdavis
2014/06/28 02:31:18
Done.
| |
| 226 FailureData *failure = failures_[i]; | |
| 227 list.append("\n"); | |
| 228 list.append(failure->file_path.value()); | |
|
Devlin
2014/06/27 22:31:10
Use one of the display functions in FilePath.
gpdavis
2014/06/27 23:42:54
Sounds good. LossyDisplayName seem reasonable?
Devlin
2014/06/28 08:40:02
Yep!
| |
| 229 } | |
| 230 failures_.clear(); | |
|
Devlin
2014/06/27 22:31:09
Gaaah! Leaaaaaaak!
;)
But really. Leaks are bad
gpdavis
2014/06/27 23:42:54
I had a feeling there was going to be a memory lea
Devlin
2014/06/28 08:40:02
For vectors, take a look at scoped_vector.h. Hand
| |
| 231 base::StringValue list_value(list); | |
| 232 web_ui()->CallJavascriptFunction( | |
| 233 "extensions.ExtensionLoader.notifyAdditional", | |
| 234 list_value); | |
|
Devlin
2014/06/27 22:31:09
If we go with this approach for multi errors, I th
gpdavis
2014/06/27 23:42:54
This is certainly going to require heavier UI adju
Devlin
2014/06/28 08:40:02
Sure, I'm not too worried about it. But the only
| |
| 235 } | |
| 236 } | |
| 237 | |
| 181 void ExtensionLoaderHandler::LoadUnpackedExtensionImpl( | 238 void ExtensionLoaderHandler::LoadUnpackedExtensionImpl( |
| 182 const base::FilePath& file_path) { | 239 const base::FilePath& file_path) { |
| 183 scoped_refptr<UnpackedInstaller> installer = UnpackedInstaller::Create( | 240 scoped_refptr<UnpackedInstaller> installer = UnpackedInstaller::Create( |
| 184 ExtensionSystem::Get(profile_)->extension_service()); | 241 ExtensionSystem::Get(profile_)->extension_service()); |
| 185 installer->set_on_failure_callback( | |
| 186 base::Bind(&ExtensionLoaderHandler::OnLoadFailure, | |
| 187 weak_ptr_factory_.GetWeakPtr())); | |
| 188 | 242 |
| 189 // We do our own error handling, so we don't want a load failure to trigger | 243 // We do our own error handling, so we don't want a load failure to trigger |
| 190 // a dialog. | 244 // a dialog. |
| 191 installer->set_be_noisy_on_failure(false); | 245 installer->set_be_noisy_on_failure(false); |
| 192 | 246 |
| 247 display_ready_ = true; | |
|
Devlin
2014/06/27 22:31:10
This seems like an odd time to set |display_ready_
gpdavis
2014/06/28 02:31:18
Done.
| |
| 193 installer->Load(file_path); | 248 installer->Load(file_path); |
| 194 } | 249 } |
| 195 | 250 |
| 196 void ExtensionLoaderHandler::OnLoadFailure(const base::FilePath& file_path, | 251 void ExtensionLoaderHandler::OnLoadFailure(const base::FilePath& file_path, |
| 197 const std::string& error) { | 252 const std::string& error) { |
| 198 failed_path_ = file_path; | 253 failed_path_ = file_path; |
| 199 size_t line = 0u; | 254 size_t line = 0u; |
| 200 size_t column = 0u; | 255 size_t column = 0u; |
| 201 std::string regex = | 256 std::string regex = |
| 202 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.", | 257 base::StringPrintf("%s Line: (\\d+), column: (\\d+), Syntax error.", |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 220 file_path, | 275 file_path, |
| 221 error, | 276 error, |
| 222 line)); | 277 line)); |
| 223 } | 278 } |
| 224 | 279 |
| 225 void ExtensionLoaderHandler::NotifyFrontendOfFailure( | 280 void ExtensionLoaderHandler::NotifyFrontendOfFailure( |
| 226 const base::FilePath& file_path, | 281 const base::FilePath& file_path, |
| 227 const std::string& error, | 282 const std::string& error, |
| 228 size_t line_number, | 283 size_t line_number, |
| 229 const std::string& manifest) { | 284 const std::string& manifest) { |
| 285 // If the extensions page is not loaded, delay frontend notification. | |
| 286 if (!display_ready_) { | |
| 287 failures_.push_back(new FailureData(file_path, | |
| 288 error, | |
| 289 line_number, | |
| 290 manifest)); | |
| 291 return; | |
| 292 } | |
| 293 | |
| 230 base::StringValue file_value(file_path.LossyDisplayName()); | 294 base::StringValue file_value(file_path.LossyDisplayName()); |
| 231 base::StringValue error_value(base::UTF8ToUTF16(error)); | 295 base::StringValue error_value(base::UTF8ToUTF16(error)); |
| 232 | 296 |
| 233 base::DictionaryValue manifest_value; | 297 base::DictionaryValue manifest_value; |
| 234 SourceHighlighter highlighter(manifest, line_number); | 298 SourceHighlighter highlighter(manifest, line_number); |
| 235 // If the line number is 0, this highlights no regions, but still adds the | 299 // If the line number is 0, this highlights no regions, but still adds the |
| 236 // full manifest. | 300 // full manifest. |
| 237 highlighter.SetHighlightedRegions(&manifest_value); | 301 highlighter.SetHighlightedRegions(&manifest_value); |
| 238 | 302 |
| 239 web_ui()->CallJavascriptFunction( | 303 web_ui()->CallJavascriptFunction( |
| 240 "extensions.ExtensionLoader.notifyLoadFailed", | 304 "extensions.ExtensionLoader.notifyLoadFailed", |
| 241 file_value, | 305 file_value, |
| 242 error_value, | 306 error_value, |
| 243 manifest_value); | 307 manifest_value); |
| 308 display_ready_ = false; | |
|
Devlin
2014/06/27 22:31:09
This too seems like an odd time to set display rea
gpdavis
2014/06/27 23:42:54
This was initially done because I started off impl
gpdavis
2014/06/28 02:31:18
Done.
| |
| 244 } | 309 } |
| 245 | 310 |
| 246 } // namespace extensions | 311 } // namespace extensions |
| OLD | NEW |