| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/extensions/extensions_service.h" | 5 #include "chrome/browser/extensions/extensions_service.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/scoped_handle.h" | 8 #include "base/scoped_handle.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 // Explicit UI loads are always noisy. | 251 // Explicit UI loads are always noisy. |
| 252 alert_on_error_ = true; | 252 alert_on_error_ = true; |
| 253 | 253 |
| 254 FilePath extension_path = path_in; | 254 FilePath extension_path = path_in; |
| 255 if (!file_util::AbsolutePath(&extension_path)) | 255 if (!file_util::AbsolutePath(&extension_path)) |
| 256 NOTREACHED(); | 256 NOTREACHED(); |
| 257 | 257 |
| 258 LOG(INFO) << "Loading single extension from " << | 258 LOG(INFO) << "Loading single extension from " << |
| 259 WideToASCII(extension_path.BaseName().ToWStringHack()); | 259 WideToASCII(extension_path.BaseName().ToWStringHack()); |
| 260 | 260 |
| 261 Extension* extension = LoadExtension(extension_path); | 261 Extension* extension = LoadExtension(extension_path, |
| 262 false); // don't require ID |
| 262 if (extension) { | 263 if (extension) { |
| 263 if (extension->id().empty()) { | |
| 264 // Generate an ID | |
| 265 static int counter = 0; | |
| 266 std::string id = StringPrintf("%x", counter); | |
| 267 ++counter; | |
| 268 | |
| 269 // pad the string out to 40 chars with zeroes. | |
| 270 id.insert(0, 40 - id.length(), '0'); | |
| 271 extension->set_id(id); | |
| 272 } | |
| 273 ExtensionList* extensions = new ExtensionList; | 264 ExtensionList* extensions = new ExtensionList; |
| 274 extensions->push_back(extension); | 265 extensions->push_back(extension); |
| 275 ReportExtensionsLoaded(extensions); | 266 ReportExtensionsLoaded(extensions); |
| 276 } | 267 } |
| 277 } | 268 } |
| 278 | 269 |
| 279 Extension* ExtensionsServiceBackend::LoadExtensionCurrentVersion( | 270 Extension* ExtensionsServiceBackend::LoadExtensionCurrentVersion( |
| 280 const FilePath& extension_path) { | 271 const FilePath& extension_path) { |
| 281 std::string version_str; | 272 std::string version_str; |
| 282 if (!ReadCurrentVersion(extension_path, &version_str)) { | 273 if (!ReadCurrentVersion(extension_path, &version_str)) { |
| 283 ReportExtensionLoadError(extension_path, | 274 ReportExtensionLoadError(extension_path, |
| 284 StringPrintf("Could not read '%s' file.", | 275 StringPrintf("Could not read '%s' file.", |
| 285 ExtensionsService::kCurrentVersionFileName)); | 276 ExtensionsService::kCurrentVersionFileName)); |
| 286 return NULL; | 277 return NULL; |
| 287 } | 278 } |
| 288 | 279 |
| 289 LOG(INFO) << " " << | 280 LOG(INFO) << " " << |
| 290 WideToASCII(extension_path.BaseName().ToWStringHack()) << | 281 WideToASCII(extension_path.BaseName().ToWStringHack()) << |
| 291 " version: " << version_str; | 282 " version: " << version_str; |
| 292 | 283 |
| 293 return LoadExtension(extension_path.AppendASCII(version_str)); | 284 return LoadExtension(extension_path.AppendASCII(version_str), |
| 285 true); // require id |
| 294 } | 286 } |
| 295 | 287 |
| 296 Extension* ExtensionsServiceBackend::LoadExtension( | 288 Extension* ExtensionsServiceBackend::LoadExtension( |
| 297 const FilePath& extension_path) { | 289 const FilePath& extension_path, bool require_id) { |
| 298 FilePath manifest_path = | 290 FilePath manifest_path = |
| 299 extension_path.AppendASCII(Extension::kManifestFilename); | 291 extension_path.AppendASCII(Extension::kManifestFilename); |
| 300 if (!file_util::PathExists(manifest_path)) { | 292 if (!file_util::PathExists(manifest_path)) { |
| 301 ReportExtensionLoadError(extension_path, Extension::kInvalidManifestError); | 293 ReportExtensionLoadError(extension_path, Extension::kInvalidManifestError); |
| 302 return NULL; | 294 return NULL; |
| 303 } | 295 } |
| 304 | 296 |
| 305 JSONFileValueSerializer serializer(manifest_path); | 297 JSONFileValueSerializer serializer(manifest_path); |
| 306 std::string error; | 298 std::string error; |
| 307 scoped_ptr<Value> root(serializer.Deserialize(&error)); | 299 scoped_ptr<Value> root(serializer.Deserialize(&error)); |
| 308 if (!root.get()) { | 300 if (!root.get()) { |
| 309 ReportExtensionLoadError(extension_path, error); | 301 ReportExtensionLoadError(extension_path, error); |
| 310 return NULL; | 302 return NULL; |
| 311 } | 303 } |
| 312 | 304 |
| 313 if (!root->IsType(Value::TYPE_DICTIONARY)) { | 305 if (!root->IsType(Value::TYPE_DICTIONARY)) { |
| 314 ReportExtensionLoadError(extension_path, Extension::kInvalidManifestError); | 306 ReportExtensionLoadError(extension_path, Extension::kInvalidManifestError); |
| 315 return NULL; | 307 return NULL; |
| 316 } | 308 } |
| 317 | 309 |
| 318 scoped_ptr<Extension> extension(new Extension(extension_path)); | 310 scoped_ptr<Extension> extension(new Extension(extension_path)); |
| 319 if (!extension->InitFromValue(*static_cast<DictionaryValue*>(root.get()), | 311 if (!extension->InitFromValue(*static_cast<DictionaryValue*>(root.get()), |
| 320 &error)) { | 312 require_id, &error)) { |
| 321 ReportExtensionLoadError(extension_path, error); | 313 ReportExtensionLoadError(extension_path, error); |
| 322 return NULL; | 314 return NULL; |
| 323 } | 315 } |
| 324 | 316 |
| 325 // Validate that claimed resources actually exist. | 317 // Validate that claimed resources actually exist. |
| 326 for (size_t i = 0; i < extension->content_scripts().size(); ++i) { | 318 for (size_t i = 0; i < extension->content_scripts().size(); ++i) { |
| 327 const UserScript& script = extension->content_scripts()[i]; | 319 const UserScript& script = extension->content_scripts()[i]; |
| 328 | 320 |
| 329 for (size_t j = 0; j < script.js_scripts().size(); j++) { | 321 for (size_t j = 0; j < script.js_scripts().size(); j++) { |
| 330 const FilePath& path = script.js_scripts()[j].path(); | 322 const FilePath& path = script.js_scripts()[j].path(); |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 | 623 |
| 632 // Read and verify the extension. | 624 // Read and verify the extension. |
| 633 scoped_ptr<DictionaryValue> manifest(ReadManifest(source_file)); | 625 scoped_ptr<DictionaryValue> manifest(ReadManifest(source_file)); |
| 634 if (!manifest.get()) { | 626 if (!manifest.get()) { |
| 635 // ReadManifest has already reported the extension error. | 627 // ReadManifest has already reported the extension error. |
| 636 return false; | 628 return false; |
| 637 } | 629 } |
| 638 DictionaryValue* dict = manifest.get(); | 630 DictionaryValue* dict = manifest.get(); |
| 639 Extension extension; | 631 Extension extension; |
| 640 std::string error; | 632 std::string error; |
| 641 if (!extension.InitFromValue(*dict, &error)) { | 633 if (!extension.InitFromValue(*dict, |
| 634 true, // require ID |
| 635 &error)) { |
| 642 ReportExtensionInstallError(source_file, | 636 ReportExtensionInstallError(source_file, |
| 643 "Invalid extension manifest."); | 637 "Invalid extension manifest."); |
| 644 return false; | 638 return false; |
| 645 } | 639 } |
| 646 | 640 |
| 647 // ID is required for installed extensions. | 641 // ID is required for installed extensions. |
| 648 if (extension.id().empty()) { | 642 if (extension.id().empty()) { |
| 649 ReportExtensionInstallError(source_file, "Required value 'id' is missing."); | 643 ReportExtensionInstallError(source_file, "Required value 'id' is missing."); |
| 650 return false; | 644 return false; |
| 651 } | 645 } |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 843 | 837 |
| 844 bool ExtensionsServiceBackend::ShouldInstall(const std::string& id, | 838 bool ExtensionsServiceBackend::ShouldInstall(const std::string& id, |
| 845 const std::string& version) { | 839 const std::string& version) { |
| 846 FilePath dir(install_directory_.AppendASCII(id.c_str())); | 840 FilePath dir(install_directory_.AppendASCII(id.c_str())); |
| 847 std::string current_version; | 841 std::string current_version; |
| 848 if (ReadCurrentVersion(dir, ¤t_version)) { | 842 if (ReadCurrentVersion(dir, ¤t_version)) { |
| 849 return !CheckCurrentVersion(version, current_version, dir); | 843 return !CheckCurrentVersion(version, current_version, dir); |
| 850 } | 844 } |
| 851 return true; | 845 return true; |
| 852 } | 846 } |
| OLD | NEW |