| 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 | |
| 263 if (extension) { | 262 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 } |
| 264 ExtensionList* extensions = new ExtensionList; | 273 ExtensionList* extensions = new ExtensionList; |
| 265 extensions->push_back(extension); | 274 extensions->push_back(extension); |
| 266 ReportExtensionsLoaded(extensions); | 275 ReportExtensionsLoaded(extensions); |
| 267 } | 276 } |
| 268 } | 277 } |
| 269 | 278 |
| 270 Extension* ExtensionsServiceBackend::LoadExtensionCurrentVersion( | 279 Extension* ExtensionsServiceBackend::LoadExtensionCurrentVersion( |
| 271 const FilePath& extension_path) { | 280 const FilePath& extension_path) { |
| 272 std::string version_str; | 281 std::string version_str; |
| 273 if (!ReadCurrentVersion(extension_path, &version_str)) { | 282 if (!ReadCurrentVersion(extension_path, &version_str)) { |
| 274 ReportExtensionLoadError(extension_path, | 283 ReportExtensionLoadError(extension_path, |
| 275 StringPrintf("Could not read '%s' file.", | 284 StringPrintf("Could not read '%s' file.", |
| 276 ExtensionsService::kCurrentVersionFileName)); | 285 ExtensionsService::kCurrentVersionFileName)); |
| 277 return NULL; | 286 return NULL; |
| 278 } | 287 } |
| 279 | 288 |
| 280 LOG(INFO) << " " << | 289 LOG(INFO) << " " << |
| 281 WideToASCII(extension_path.BaseName().ToWStringHack()) << | 290 WideToASCII(extension_path.BaseName().ToWStringHack()) << |
| 282 " version: " << version_str; | 291 " version: " << version_str; |
| 283 | 292 |
| 284 return LoadExtension(extension_path.AppendASCII(version_str), | 293 return LoadExtension(extension_path.AppendASCII(version_str)); |
| 285 true); // require id | |
| 286 } | 294 } |
| 287 | 295 |
| 288 Extension* ExtensionsServiceBackend::LoadExtension( | 296 Extension* ExtensionsServiceBackend::LoadExtension( |
| 289 const FilePath& extension_path, bool require_id) { | 297 const FilePath& extension_path) { |
| 290 FilePath manifest_path = | 298 FilePath manifest_path = |
| 291 extension_path.AppendASCII(Extension::kManifestFilename); | 299 extension_path.AppendASCII(Extension::kManifestFilename); |
| 292 if (!file_util::PathExists(manifest_path)) { | 300 if (!file_util::PathExists(manifest_path)) { |
| 293 ReportExtensionLoadError(extension_path, Extension::kInvalidManifestError); | 301 ReportExtensionLoadError(extension_path, Extension::kInvalidManifestError); |
| 294 return NULL; | 302 return NULL; |
| 295 } | 303 } |
| 296 | 304 |
| 297 JSONFileValueSerializer serializer(manifest_path); | 305 JSONFileValueSerializer serializer(manifest_path); |
| 298 std::string error; | 306 std::string error; |
| 299 scoped_ptr<Value> root(serializer.Deserialize(&error)); | 307 scoped_ptr<Value> root(serializer.Deserialize(&error)); |
| 300 if (!root.get()) { | 308 if (!root.get()) { |
| 301 ReportExtensionLoadError(extension_path, error); | 309 ReportExtensionLoadError(extension_path, error); |
| 302 return NULL; | 310 return NULL; |
| 303 } | 311 } |
| 304 | 312 |
| 305 if (!root->IsType(Value::TYPE_DICTIONARY)) { | 313 if (!root->IsType(Value::TYPE_DICTIONARY)) { |
| 306 ReportExtensionLoadError(extension_path, Extension::kInvalidManifestError); | 314 ReportExtensionLoadError(extension_path, Extension::kInvalidManifestError); |
| 307 return NULL; | 315 return NULL; |
| 308 } | 316 } |
| 309 | 317 |
| 310 scoped_ptr<Extension> extension(new Extension(extension_path)); | 318 scoped_ptr<Extension> extension(new Extension(extension_path)); |
| 311 if (!extension->InitFromValue(*static_cast<DictionaryValue*>(root.get()), | 319 if (!extension->InitFromValue(*static_cast<DictionaryValue*>(root.get()), |
| 312 require_id, &error)) { | 320 &error)) { |
| 313 ReportExtensionLoadError(extension_path, error); | 321 ReportExtensionLoadError(extension_path, error); |
| 314 return NULL; | 322 return NULL; |
| 315 } | 323 } |
| 316 | 324 |
| 317 // Validate that claimed resources actually exist. | 325 // Validate that claimed resources actually exist. |
| 318 for (size_t i = 0; i < extension->content_scripts().size(); ++i) { | 326 for (size_t i = 0; i < extension->content_scripts().size(); ++i) { |
| 319 const UserScript& script = extension->content_scripts()[i]; | 327 const UserScript& script = extension->content_scripts()[i]; |
| 320 | 328 |
| 321 for (size_t j = 0; j < script.js_scripts().size(); j++) { | 329 for (size_t j = 0; j < script.js_scripts().size(); j++) { |
| 322 const FilePath& path = script.js_scripts()[j].path(); | 330 const FilePath& path = script.js_scripts()[j].path(); |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 623 | 631 |
| 624 // Read and verify the extension. | 632 // Read and verify the extension. |
| 625 scoped_ptr<DictionaryValue> manifest(ReadManifest(source_file)); | 633 scoped_ptr<DictionaryValue> manifest(ReadManifest(source_file)); |
| 626 if (!manifest.get()) { | 634 if (!manifest.get()) { |
| 627 // ReadManifest has already reported the extension error. | 635 // ReadManifest has already reported the extension error. |
| 628 return false; | 636 return false; |
| 629 } | 637 } |
| 630 DictionaryValue* dict = manifest.get(); | 638 DictionaryValue* dict = manifest.get(); |
| 631 Extension extension; | 639 Extension extension; |
| 632 std::string error; | 640 std::string error; |
| 633 if (!extension.InitFromValue(*dict, | 641 if (!extension.InitFromValue(*dict, &error)) { |
| 634 true, // require ID | |
| 635 &error)) { | |
| 636 ReportExtensionInstallError(source_file, | 642 ReportExtensionInstallError(source_file, |
| 637 "Invalid extension manifest."); | 643 "Invalid extension manifest."); |
| 638 return false; | 644 return false; |
| 639 } | 645 } |
| 640 | 646 |
| 641 // ID is required for installed extensions. | 647 // ID is required for installed extensions. |
| 642 if (extension.id().empty()) { | 648 if (extension.id().empty()) { |
| 643 ReportExtensionInstallError(source_file, "Required value 'id' is missing."); | 649 ReportExtensionInstallError(source_file, "Required value 'id' is missing."); |
| 644 return false; | 650 return false; |
| 645 } | 651 } |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 837 | 843 |
| 838 bool ExtensionsServiceBackend::ShouldInstall(const std::string& id, | 844 bool ExtensionsServiceBackend::ShouldInstall(const std::string& id, |
| 839 const std::string& version) { | 845 const std::string& version) { |
| 840 FilePath dir(install_directory_.AppendASCII(id.c_str())); | 846 FilePath dir(install_directory_.AppendASCII(id.c_str())); |
| 841 std::string current_version; | 847 std::string current_version; |
| 842 if (ReadCurrentVersion(dir, ¤t_version)) { | 848 if (ReadCurrentVersion(dir, ¤t_version)) { |
| 843 return !CheckCurrentVersion(version, current_version, dir); | 849 return !CheckCurrentVersion(version, current_version, dir); |
| 844 } | 850 } |
| 845 return true; | 851 return true; |
| 846 } | 852 } |
| OLD | NEW |