| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/common/extensions/extension_file_util.h" | 5 #include "chrome/common/extensions/extension_file_util.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 // We don't care about the return value. If this fails (and it can, due to | 86 // We don't care about the return value. If this fails (and it can, due to |
| 87 // plugins that aren't unloaded yet, it will get cleaned up by | 87 // plugins that aren't unloaded yet, it will get cleaned up by |
| 88 // ExtensionService::GarbageCollectExtensions). | 88 // ExtensionService::GarbageCollectExtensions). |
| 89 file_util::Delete(extensions_dir.AppendASCII(id), true); // recursive. | 89 file_util::Delete(extensions_dir.AppendASCII(id), true); // recursive. |
| 90 } | 90 } |
| 91 | 91 |
| 92 scoped_refptr<Extension> LoadExtension(const FilePath& extension_path, | 92 scoped_refptr<Extension> LoadExtension(const FilePath& extension_path, |
| 93 Extension::Location location, | 93 Extension::Location location, |
| 94 int flags, | 94 int flags, |
| 95 std::string* error) { | 95 std::string* error) { |
| 96 scoped_ptr<DictionaryValue> manifest(LoadManifest(extension_path, error)); |
| 97 if (!manifest.get()) |
| 98 return NULL; |
| 99 if (!extension_l10n_util::LocalizeExtension(extension_path, manifest.get(), |
| 100 error)) |
| 101 return NULL; |
| 102 |
| 103 scoped_refptr<Extension> extension(Extension::Create( |
| 104 extension_path, |
| 105 location, |
| 106 *manifest, |
| 107 flags, |
| 108 error)); |
| 109 if (!extension.get()) |
| 110 return NULL; |
| 111 |
| 112 if (!ValidateExtension(extension.get(), error)) |
| 113 return NULL; |
| 114 |
| 115 return extension; |
| 116 } |
| 117 |
| 118 DictionaryValue* LoadManifest(const FilePath& extension_path, |
| 119 std::string* error) { |
| 96 FilePath manifest_path = | 120 FilePath manifest_path = |
| 97 extension_path.Append(Extension::kManifestFilename); | 121 extension_path.Append(Extension::kManifestFilename); |
| 98 if (!file_util::PathExists(manifest_path)) { | 122 if (!file_util::PathExists(manifest_path)) { |
| 99 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); | 123 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); |
| 100 return NULL; | 124 return NULL; |
| 101 } | 125 } |
| 102 | 126 |
| 103 JSONFileValueSerializer serializer(manifest_path); | 127 JSONFileValueSerializer serializer(manifest_path); |
| 104 scoped_ptr<Value> root(serializer.Deserialize(NULL, error)); | 128 scoped_ptr<Value> root(serializer.Deserialize(NULL, error)); |
| 105 if (!root.get()) { | 129 if (!root.get()) { |
| 106 if (error->empty()) { | 130 if (error->empty()) { |
| 107 // If |error| is empty, than the file could not be read. | 131 // If |error| is empty, than the file could not be read. |
| 108 // It would be cleaner to have the JSON reader give a specific error | 132 // It would be cleaner to have the JSON reader give a specific error |
| 109 // in this case, but other code tests for a file error with | 133 // in this case, but other code tests for a file error with |
| 110 // error->empty(). For now, be consistent. | 134 // error->empty(). For now, be consistent. |
| 111 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); | 135 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); |
| 112 } else { | 136 } else { |
| 113 *error = base::StringPrintf("%s %s", | 137 *error = base::StringPrintf("%s %s", |
| 114 errors::kManifestParseError, | 138 errors::kManifestParseError, |
| 115 error->c_str()); | 139 error->c_str()); |
| 116 } | 140 } |
| 117 return NULL; | 141 return NULL; |
| 118 } | 142 } |
| 119 | 143 |
| 120 if (!root->IsType(Value::TYPE_DICTIONARY)) { | 144 if (!root->IsType(Value::TYPE_DICTIONARY)) { |
| 121 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_INVALID); | 145 *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_INVALID); |
| 122 return NULL; | 146 return NULL; |
| 123 } | 147 } |
| 124 | 148 |
| 125 DictionaryValue* manifest = static_cast<DictionaryValue*>(root.get()); | 149 return static_cast<DictionaryValue*>(root.release()); |
| 126 if (!extension_l10n_util::LocalizeExtension(extension_path, manifest, error)) | |
| 127 return NULL; | |
| 128 | |
| 129 scoped_refptr<Extension> extension(Extension::Create( | |
| 130 extension_path, | |
| 131 location, | |
| 132 *manifest, | |
| 133 flags, | |
| 134 error)); | |
| 135 if (!extension.get()) | |
| 136 return NULL; | |
| 137 | |
| 138 if (!ValidateExtension(extension.get(), error)) | |
| 139 return NULL; | |
| 140 | |
| 141 return extension; | |
| 142 } | 150 } |
| 143 | 151 |
| 144 bool ValidateExtension(const Extension* extension, std::string* error) { | 152 bool ValidateExtension(const Extension* extension, std::string* error) { |
| 145 // Validate icons exist. | 153 // Validate icons exist. |
| 146 for (ExtensionIconSet::IconMap::const_iterator iter = | 154 for (ExtensionIconSet::IconMap::const_iterator iter = |
| 147 extension->icons().map().begin(); | 155 extension->icons().map().begin(); |
| 148 iter != extension->icons().map().end(); | 156 iter != extension->icons().map().end(); |
| 149 ++iter) { | 157 ++iter) { |
| 150 const FilePath path = extension->GetResource(iter->second).GetFilePath(); | 158 const FilePath path = extension->GetResource(iter->second).GetFilePath(); |
| 151 if (!file_util::PathExists(path)) { | 159 if (!file_util::PathExists(path)) { |
| (...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 return temp_path; | 649 return temp_path; |
| 642 | 650 |
| 643 return FilePath(); | 651 return FilePath(); |
| 644 } | 652 } |
| 645 | 653 |
| 646 void DeleteFile(const FilePath& path, bool recursive) { | 654 void DeleteFile(const FilePath& path, bool recursive) { |
| 647 file_util::Delete(path, recursive); | 655 file_util::Delete(path, recursive); |
| 648 } | 656 } |
| 649 | 657 |
| 650 } // namespace extension_file_util | 658 } // namespace extension_file_util |
| OLD | NEW |