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/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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 } | 111 } |
110 } | 112 } |
111 | 113 |
112 ComponentExtensionInfo info(parsed_manifest, absolute_path); | 114 ComponentExtensionInfo info(parsed_manifest, absolute_path); |
113 component_extensions_.push_back(info); | 115 component_extensions_.push_back(info); |
114 if (extension_service_->is_ready()) | 116 if (extension_service_->is_ready()) |
115 return Load(info); | 117 return Load(info); |
116 return NULL; | 118 return NULL; |
117 } | 119 } |
118 | 120 |
121 const Extension* ComponentLoader::AddFromCommandLine(const FilePath& path) { | |
122 // Load extensions from the command line synchronously to avoid a race | |
123 // between extension loading and loading an URL from the command line. | |
124 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
Aaron Boodman
2011/11/29 05:32:25
Move this up to the call site in profile_impl.cc.
SeRya
2011/11/29 21:31:00
Removed completely. It's not necessary for the pla
| |
125 | |
126 FilePath absolute_path = path; | |
127 file_util::AbsolutePath(&absolute_path); | |
128 std::string error; | |
129 scoped_ptr<DictionaryValue> manifest( | |
130 extension_file_util::LoadManifest(absolute_path, &error)); | |
131 if (!manifest.get()) { | |
132 LOG(ERROR) << "Could not load extension from '" << | |
133 absolute_path.value() << "'. " << error; | |
134 return NULL; | |
135 } | |
136 Remove(GenerateId(manifest.get())); | |
Aaron Boodman
2011/11/29 05:32:25
What happens if the extension is already loaded? Y
SeRya
2011/11/29 21:31:00
Previous will be removed and unloaded.
| |
137 | |
138 ComponentExtensionInfo info(manifest.release(), absolute_path); | |
Aaron Boodman
2011/11/29 05:32:25
Can you delegate to the Add() method that takes a
SeRya
2011/11/29 21:31:00
Done.
| |
139 component_extensions_.push_back(info); | |
140 if (extension_service_->is_ready()) | |
141 return Load(info); | |
142 return NULL; | |
143 } | |
144 | |
119 const Extension* ComponentLoader::Load(const ComponentExtensionInfo& info) { | 145 const Extension* ComponentLoader::Load(const ComponentExtensionInfo& info) { |
120 int flags = Extension::REQUIRE_KEY; | 146 int flags = Extension::REQUIRE_KEY; |
121 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT)) | 147 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT)) |
122 flags |= Extension::STRICT_ERROR_CHECKS; | 148 flags |= Extension::STRICT_ERROR_CHECKS; |
123 std::string error; | 149 std::string error; |
124 scoped_refptr<const Extension> extension(Extension::Create( | 150 scoped_refptr<const Extension> extension(Extension::Create( |
125 info.root_directory, | 151 info.root_directory, |
126 Extension::COMPONENT, | 152 Extension::COMPONENT, |
127 *info.manifest, | 153 *info.manifest, |
128 flags, | 154 flags, |
(...skipping 17 matching lines...) Expand all Loading... | |
146 if (it == component_extensions_.end()) | 172 if (it == component_extensions_.end()) |
147 return; | 173 return; |
148 | 174 |
149 // The list owns the dictionary, so it must be deleted after removal. | 175 // The list owns the dictionary, so it must be deleted after removal. |
150 scoped_ptr<const DictionaryValue> manifest(it->manifest); | 176 scoped_ptr<const DictionaryValue> manifest(it->manifest); |
151 | 177 |
152 // Remove the extension from the list of registered extensions. | 178 // Remove the extension from the list of registered extensions. |
153 *it = component_extensions_.back(); | 179 *it = component_extensions_.back(); |
154 component_extensions_.pop_back(); | 180 component_extensions_.pop_back(); |
155 | 181 |
156 // Determine the extension id and unload the extension. | 182 std::string id = GenerateId(manifest.get()); |
183 if (id.empty()) { | |
184 LOG(ERROR) << "Failed to get extension id"; | |
185 return; | |
186 } | |
187 extension_service_-> | |
188 UnloadExtension(id, extension_misc::UNLOAD_REASON_DISABLE); | |
189 } | |
190 | |
191 void ComponentLoader::Remove(const std::string& id) { | |
Aaron Boodman
2011/11/29 05:32:25
Don't duplicate code. One of these methods should
SeRya
2011/11/29 21:31:00
Done.
| |
192 RegisteredComponentExtensions::iterator it = component_extensions_.begin(); | |
193 for (; it != component_extensions_.end(); ++it) { | |
194 if (GenerateId(it->manifest) == id) { | |
195 delete it->manifest; | |
196 it = component_extensions_.erase(it); | |
197 if (extension_service_->is_ready()) | |
198 extension_service_-> | |
199 UnloadExtension(id, extension_misc::UNLOAD_REASON_DISABLE); | |
200 break; | |
201 } | |
202 } | |
203 } | |
204 | |
205 std::string ComponentLoader::GenerateId(const DictionaryValue* manifest) { | |
157 std::string public_key; | 206 std::string public_key; |
158 std::string public_key_bytes; | 207 std::string public_key_bytes; |
159 std::string id; | 208 std::string id; |
160 if (!manifest->GetString( | 209 if (!manifest->GetString( |
161 extension_manifest_keys::kPublicKey, &public_key) || | 210 extension_manifest_keys::kPublicKey, &public_key) || |
162 !Extension::ParsePEMKeyBytes(public_key, &public_key_bytes) || | 211 !Extension::ParsePEMKeyBytes(public_key, &public_key_bytes) || |
163 !Extension::GenerateId(public_key_bytes, &id)) { | 212 !Extension::GenerateId(public_key_bytes, &id)) { |
164 LOG(ERROR) << "Failed to get extension id"; | 213 return std::string(); |
165 return; | |
166 } | 214 } |
167 extension_service_-> | 215 return id; |
168 UnloadExtension(id, extension_misc::UNLOAD_REASON_DISABLE); | |
169 } | 216 } |
170 | 217 |
171 void ComponentLoader::AddFileManagerExtension() { | 218 void ComponentLoader::AddFileManagerExtension() { |
172 #if defined(FILE_MANAGER_EXTENSION) | 219 #if defined(FILE_MANAGER_EXTENSION) |
173 #ifndef NDEBUG | 220 #ifndef NDEBUG |
174 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 221 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
175 if (command_line->HasSwitch(switches::kFileManagerExtensionPath)) { | 222 if (command_line->HasSwitch(switches::kFileManagerExtensionPath)) { |
176 FilePath filemgr_extension_path( | 223 FilePath filemgr_extension_path( |
177 command_line->GetSwitchValuePath(switches::kFileManagerExtensionPath)); | 224 command_line->GetSwitchValuePath(switches::kFileManagerExtensionPath)); |
178 Add(IDR_FILEMANAGER_MANIFEST, filemgr_extension_path); | 225 Add(IDR_FILEMANAGER_MANIFEST, filemgr_extension_path); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
285 void ComponentLoader::RegisterUserPrefs(PrefService* prefs) { | 332 void ComponentLoader::RegisterUserPrefs(PrefService* prefs) { |
286 prefs->RegisterStringPref(prefs::kEnterpriseWebStoreURL, | 333 prefs->RegisterStringPref(prefs::kEnterpriseWebStoreURL, |
287 std::string() /* default_value */, | 334 std::string() /* default_value */, |
288 PrefService::UNSYNCABLE_PREF); | 335 PrefService::UNSYNCABLE_PREF); |
289 prefs->RegisterStringPref(prefs::kEnterpriseWebStoreName, | 336 prefs->RegisterStringPref(prefs::kEnterpriseWebStoreName, |
290 std::string() /* default_value */, | 337 std::string() /* default_value */, |
291 PrefService::UNSYNCABLE_PREF); | 338 PrefService::UNSYNCABLE_PREF); |
292 } | 339 } |
293 | 340 |
294 } // namespace extensions | 341 } // namespace extensions |
OLD | NEW |