Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: chrome/browser/extensions/component_loader.cc

Issue 8659002: Adding the --load-component-extension flag. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Code review fix. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/browser/extensions/component_loader.h" 5 #include "chrome/browser/extensions/component_loader.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/file_util.h"
8 #include "base/path_service.h" 9 #include "base/path_service.h"
9 #include "base/json/json_value_serializer.h" 10 #include "base/json/json_value_serializer.h"
10 #include "chrome/browser/browser_process.h" 11 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/extensions/extension_service.h" 12 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/prefs/pref_change_registrar.h" 13 #include "chrome/browser/prefs/pref_change_registrar.h"
13 #include "chrome/browser/prefs/pref_notifier.h" 14 #include "chrome/browser/prefs/pref_notifier.h"
14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/chrome_notification_types.h" 16 #include "chrome/common/chrome_notification_types.h"
16 #include "chrome/common/chrome_paths.h" 17 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/chrome_switches.h" 18 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/extensions/extension.h" 19 #include "chrome/common/extensions/extension.h"
20 #include "chrome/common/extensions/extension_file_util.h"
19 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
20 #include "content/public/browser/notification_details.h" 22 #include "content/public/browser/notification_details.h"
21 #include "content/public/browser/notification_source.h" 23 #include "content/public/browser/notification_source.h"
22 #include "grit/browser_resources.h" 24 #include "grit/browser_resources.h"
23 #include "ui/base/resource/resource_bundle.h" 25 #include "ui/base/resource/resource_bundle.h"
24 26
25 #if defined(OFFICIAL_BUILD) 27 #if defined(OFFICIAL_BUILD)
26 #include "chrome/browser/defaults.h" 28 #include "chrome/browser/defaults.h"
27 #endif 29 #endif
28 30
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 // required in case LoadAll() is called again. 94 // required in case LoadAll() is called again.
93 DictionaryValue* manifest = ParseManifest(manifest_contents); 95 DictionaryValue* manifest = ParseManifest(manifest_contents);
94 if (manifest) 96 if (manifest)
95 return Add(manifest, root_directory); 97 return Add(manifest, root_directory);
96 return NULL; 98 return NULL;
97 } 99 }
98 100
99 const Extension* ComponentLoader::Add( 101 const Extension* ComponentLoader::Add(
100 const DictionaryValue* parsed_manifest, 102 const DictionaryValue* parsed_manifest,
101 const FilePath& root_directory) { 103 const FilePath& root_directory) {
102 // Get the absolute path to the extension. 104 CHECK(!Exists(GenerateId(parsed_manifest)));
103 FilePath absolute_path(root_directory); 105 ComponentExtensionInfo info(parsed_manifest, root_directory);
104 if (!absolute_path.IsAbsolute()) {
105 if (PathService::Get(chrome::DIR_RESOURCES, &absolute_path)) {
106 absolute_path = absolute_path.Append(root_directory);
107 } else {
108 NOTREACHED();
109 }
110 }
111
112 ComponentExtensionInfo info(parsed_manifest, absolute_path);
113 component_extensions_.push_back(info); 106 component_extensions_.push_back(info);
114 if (extension_service_->is_ready()) 107 if (extension_service_->is_ready())
115 return Load(info); 108 return Load(info);
116 return NULL; 109 return NULL;
117 } 110 }
118 111
112 const Extension* ComponentLoader::AddOrReplace(const FilePath& path) {
113 FilePath absolute_path = path;
114 file_util::AbsolutePath(&absolute_path);
115 std::string error;
116 scoped_ptr<DictionaryValue> manifest(
117 extension_file_util::LoadManifest(absolute_path, &error));
118 if (!manifest.get()) {
119 LOG(ERROR) << "Could not load extension from '" <<
120 absolute_path.value() << "'. " << error;
121 return NULL;
122 }
123 Remove(GenerateId(manifest.get()));
124
125 return Add(manifest.release(), absolute_path);
126 }
127
119 const Extension* ComponentLoader::Load(const ComponentExtensionInfo& info) { 128 const Extension* ComponentLoader::Load(const ComponentExtensionInfo& info) {
120 int flags = Extension::REQUIRE_KEY; 129 int flags = Extension::REQUIRE_KEY;
121 // TODO(abarth): We should REQUIRE_MODERN_MANIFEST_VERSION once we've updated 130 // TODO(abarth): We should REQUIRE_MODERN_MANIFEST_VERSION once we've updated
122 // our component extensions to the new manifest version. 131 // our component extensions to the new manifest version.
123 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT)) 132 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT))
124 flags |= Extension::STRICT_ERROR_CHECKS; 133 flags |= Extension::STRICT_ERROR_CHECKS;
125 std::string error; 134 std::string error;
135
136 // Get the absolute path to the extension.
137 FilePath absolute_path(info.root_directory);
138 if (!absolute_path.IsAbsolute()) {
139 if (PathService::Get(chrome::DIR_RESOURCES, &absolute_path)) {
140 absolute_path = absolute_path.Append(info.root_directory);
141 } else {
142 NOTREACHED();
143 }
144 }
145
126 scoped_refptr<const Extension> extension(Extension::Create( 146 scoped_refptr<const Extension> extension(Extension::Create(
127 info.root_directory, 147 absolute_path,
128 Extension::COMPONENT, 148 Extension::COMPONENT,
129 *info.manifest, 149 *info.manifest,
130 flags, 150 flags,
131 &error)); 151 &error));
132 if (!extension.get()) { 152 if (!extension.get()) {
133 LOG(ERROR) << error; 153 LOG(ERROR) << error;
134 return NULL; 154 return NULL;
135 } 155 }
136 extension_service_->AddExtension(extension); 156 extension_service_->AddExtension(extension);
137 return extension; 157 return extension;
138 } 158 }
139 159
140 void ComponentLoader::Remove(const FilePath& root_directory) { 160 void ComponentLoader::Remove(const FilePath& root_directory) {
141 // Find the ComponentExtensionInfo for the extension. 161 // Find the ComponentExtensionInfo for the extension.
142 RegisteredComponentExtensions::iterator it = component_extensions_.begin(); 162 RegisteredComponentExtensions::iterator it = component_extensions_.begin();
143 for (; it != component_extensions_.end(); ++it) { 163 for (; it != component_extensions_.end(); ++it) {
144 if (it->root_directory == root_directory) 164 if (it->root_directory == root_directory) {
165 Remove(GenerateId(it->manifest));
145 break; 166 break;
167 }
146 } 168 }
147 // If the extension is not in the list, there's nothing to do. 169 }
148 if (it == component_extensions_.end())
149 return;
150 170
151 // The list owns the dictionary, so it must be deleted after removal. 171 void ComponentLoader::Remove(const std::string& id) {
152 scoped_ptr<const DictionaryValue> manifest(it->manifest); 172 RegisteredComponentExtensions::iterator it = component_extensions_.begin();
173 for (; it != component_extensions_.end(); ++it) {
Aaron Boodman 2011/12/06 19:43:35 It seems like this class spends a lot of time find
SeRya 2011/12/06 22:19:37 It only does it when reloads Enterprise Web Store
Aaron Boodman 2011/12/07 00:09:02 I didn't mean compute time, I meant lines of code.
174 if (GenerateId(it->manifest) == id) {
175 delete it->manifest;
176 it = component_extensions_.erase(it);
177 if (extension_service_->is_ready())
178 extension_service_->
179 UnloadExtension(id, extension_misc::UNLOAD_REASON_DISABLE);
180 break;
181 }
182 }
183 }
153 184
154 // Remove the extension from the list of registered extensions. 185 bool ComponentLoader::Exists(const std::string& id) const {
155 *it = component_extensions_.back(); 186 RegisteredComponentExtensions::const_iterator it =
156 component_extensions_.pop_back(); 187 component_extensions_.begin();
188 for (; it != component_extensions_.end(); ++it)
189 if (GenerateId(it->manifest) == id)
190 return true;
191 return false;
192 }
157 193
158 // Determine the extension id and unload the extension. 194 std::string ComponentLoader::GenerateId(const DictionaryValue* manifest) {
159 std::string public_key; 195 std::string public_key;
160 std::string public_key_bytes; 196 std::string public_key_bytes;
161 std::string id; 197 std::string id;
162 if (!manifest->GetString( 198 if (!manifest->GetString(
163 extension_manifest_keys::kPublicKey, &public_key) || 199 extension_manifest_keys::kPublicKey, &public_key) ||
164 !Extension::ParsePEMKeyBytes(public_key, &public_key_bytes) || 200 !Extension::ParsePEMKeyBytes(public_key, &public_key_bytes) ||
165 !Extension::GenerateId(public_key_bytes, &id)) { 201 !Extension::GenerateId(public_key_bytes, &id)) {
166 LOG(ERROR) << "Failed to get extension id"; 202 return std::string();
167 return;
168 } 203 }
169 extension_service_-> 204 return id;
170 UnloadExtension(id, extension_misc::UNLOAD_REASON_DISABLE);
171 } 205 }
172 206
173 void ComponentLoader::AddFileManagerExtension() { 207 void ComponentLoader::AddFileManagerExtension() {
174 #if defined(FILE_MANAGER_EXTENSION) 208 #if defined(FILE_MANAGER_EXTENSION)
175 #ifndef NDEBUG 209 #ifndef NDEBUG
176 const CommandLine* command_line = CommandLine::ForCurrentProcess(); 210 const CommandLine* command_line = CommandLine::ForCurrentProcess();
177 if (command_line->HasSwitch(switches::kFileManagerExtensionPath)) { 211 if (command_line->HasSwitch(switches::kFileManagerExtensionPath)) {
178 FilePath filemgr_extension_path( 212 FilePath filemgr_extension_path(
179 command_line->GetSwitchValuePath(switches::kFileManagerExtensionPath)); 213 command_line->GetSwitchValuePath(switches::kFileManagerExtensionPath));
180 Add(IDR_FILEMANAGER_MANIFEST, filemgr_extension_path); 214 Add(IDR_FILEMANAGER_MANIFEST, filemgr_extension_path);
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 void ComponentLoader::RegisterUserPrefs(PrefService* prefs) { 321 void ComponentLoader::RegisterUserPrefs(PrefService* prefs) {
288 prefs->RegisterStringPref(prefs::kEnterpriseWebStoreURL, 322 prefs->RegisterStringPref(prefs::kEnterpriseWebStoreURL,
289 std::string() /* default_value */, 323 std::string() /* default_value */,
290 PrefService::UNSYNCABLE_PREF); 324 PrefService::UNSYNCABLE_PREF);
291 prefs->RegisterStringPref(prefs::kEnterpriseWebStoreName, 325 prefs->RegisterStringPref(prefs::kEnterpriseWebStoreName,
292 std::string() /* default_value */, 326 std::string() /* default_value */,
293 PrefService::UNSYNCABLE_PREF); 327 PrefService::UNSYNCABLE_PREF);
294 } 328 }
295 329
296 } // namespace extensions 330 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698