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

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

Issue 8417012: Refactor loading out of ExtensionService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: aa's Created 9 years, 1 month 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/component_loader.h"
6
7 #include "base/command_line.h"
8 #include "base/path_service.h"
9 #include "base/json/json_value_serializer.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/common/chrome_paths.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/extensions/extension.h"
14 #include "grit/browser_resources.h"
15 #include "ui/base/resource/resource_bundle.h"
16
17 namespace {
18
19 typedef std::list<std::pair<FilePath::StringType, int> >
20 ComponentExtensionList;
21
22 #if defined(FILE_MANAGER_EXTENSION)
23 void AddFileManagerExtension(ComponentExtensionList* component_extensions) {
24 #ifndef NDEBUG
25 const CommandLine* command_line = CommandLine::ForCurrentProcess();
26 if (command_line->HasSwitch(switches::kFileManagerExtensionPath)) {
27 FilePath filemgr_extension_path =
28 command_line->GetSwitchValuePath(switches::kFileManagerExtensionPath);
29 component_extensions->push_back(std::make_pair(
30 filemgr_extension_path.value(),
31 IDR_FILEMANAGER_MANIFEST));
32 return;
33 }
34 #endif // NDEBUG
35 component_extensions->push_back(std::make_pair(
36 FILE_PATH_LITERAL("file_manager"),
37 IDR_FILEMANAGER_MANIFEST));
38 }
39 #endif // defined(FILE_MANAGER_EXTENSION)
40
41 } // namespace
42
43 namespace extensions {
44
45 bool ComponentLoader::ComponentExtensionInfo::Equals(
46 const ComponentExtensionInfo& other) const {
47 return other.manifest == manifest && other.root_directory == root_directory;
48 }
49
50 ComponentLoader::ComponentLoader(ExtensionService* extension_service)
51 : extension_service_(extension_service) {
52 }
53
54 ComponentLoader::~ComponentLoader() {
55 }
56
57 void ComponentLoader::LoadComponentExtensions() {
58 for (RegisteredComponentExtensions::iterator it =
59 component_extension_manifests_.begin();
60 it != component_extension_manifests_.end(); ++it) {
61 LoadComponentExtension(it->manifest, it->root_directory);
62 }
63 }
64
65 const Extension* ComponentLoader::LoadComponentExtension(
66 const std::string& manifest, const FilePath& root_directory) {
67 JSONStringValueSerializer serializer(manifest);
68 scoped_ptr<Value> manifest_parsed(serializer.Deserialize(NULL, NULL));
69 if (!manifest_parsed.get()) {
70 LOG(ERROR) << "Failed to parse manifest for extension";
71 return NULL;
72 }
73
74 int flags = Extension::REQUIRE_KEY;
75 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT))
76 flags |= Extension::STRICT_ERROR_CHECKS;
77 std::string error;
78 scoped_refptr<const Extension> extension(Extension::Create(
79 root_directory,
80 Extension::COMPONENT,
81 *static_cast<DictionaryValue*>(manifest_parsed.get()),
82 flags,
83 &error));
84 if (!extension.get()) {
85 LOG(ERROR) << error;
86 return NULL;
87 }
88 extension_service_->AddExtension(extension);
89 return extension;
90 }
91
92 void ComponentLoader::UnloadComponentExtension(const std::string& manifest) {
93 JSONStringValueSerializer serializer(manifest);
94 scoped_ptr<Value> manifest_parsed(serializer.Deserialize(NULL, NULL));
95 if (!manifest_parsed.get()) {
96 LOG(ERROR) << "Failed to parse manifest for extension";
97 return;
98 }
99 std::string public_key;
100 std::string public_key_bytes;
101 std::string id;
102 if (!static_cast<DictionaryValue*>(manifest_parsed.get())->
103 GetString(extension_manifest_keys::kPublicKey, &public_key) ||
104 !Extension::ParsePEMKeyBytes(public_key, &public_key_bytes) ||
105 !Extension::GenerateId(public_key_bytes, &id)) {
106 LOG(ERROR) << "Failed to get extension id";
107 return;
108 }
109 extension_service_->
110 UnloadExtension(id, extension_misc::UNLOAD_REASON_DISABLE);
111 }
112
113 // We take ComponentExtensionList:
114 // path, manifest ID => full manifest, absolute path
115 void ComponentLoader::RegisterDefaultComponentExtensions() {
116 ComponentExtensionList component_extensions;
117
118 // Bookmark manager.
119 component_extensions.push_back(std::make_pair(
120 FILE_PATH_LITERAL("bookmark_manager"),
121 IDR_BOOKMARKS_MANIFEST));
122
123 #if defined(FILE_MANAGER_EXTENSION)
124 AddFileManagerExtension(&component_extensions);
125 #endif
126
127 #if defined(TOUCH_UI)
128 component_extensions.push_back(std::make_pair(
129 FILE_PATH_LITERAL("keyboard"),
130 IDR_KEYBOARD_MANIFEST));
131 #endif
132
133 #if defined(OS_CHROMEOS)
134 component_extensions.push_back(std::make_pair(
135 FILE_PATH_LITERAL("/usr/share/chromeos-assets/mobile"),
136 IDR_MOBILE_MANIFEST));
137
138 const CommandLine* command_line = CommandLine::ForCurrentProcess();
139 if (command_line->HasSwitch(switches::kAuthExtensionPath)) {
140 FilePath auth_extension_path =
141 command_line->GetSwitchValuePath(switches::kAuthExtensionPath);
142 component_extensions.push_back(std::make_pair(
143 auth_extension_path.value(),
144 IDR_GAIA_TEST_AUTH_MANIFEST));
145 } else {
146 component_extensions.push_back(std::make_pair(
147 FILE_PATH_LITERAL("/usr/share/chromeos-assets/gaia_auth"),
148 IDR_GAIA_AUTH_MANIFEST));
149 }
150
151 #if defined(OFFICIAL_BUILD)
152 if (browser_defaults::enable_help_app) {
153 component_extensions.push_back(std::make_pair(
154 FILE_PATH_LITERAL("/usr/share/chromeos-assets/helpapp"),
155 IDR_HELP_MANIFEST));
156 }
157 #endif
158 #endif
159
160 // Web Store.
161 component_extensions.push_back(std::make_pair(
162 FILE_PATH_LITERAL("web_store"),
163 IDR_WEBSTORE_MANIFEST));
164
165 #if !defined(OS_CHROMEOS)
166 // Cloud Print component app. Not required on Chrome OS.
167 component_extensions.push_back(std::make_pair(
168 FILE_PATH_LITERAL("cloud_print"),
169 IDR_CLOUDPRINT_MANIFEST));
170 #endif // !defined(OS_CHROMEOS)
171
172 for (ComponentExtensionList::iterator iter = component_extensions.begin();
173 iter != component_extensions.end(); ++iter) {
174 FilePath path(iter->first);
175 if (!path.IsAbsolute()) {
176 if (PathService::Get(chrome::DIR_RESOURCES, &path)) {
177 path = path.Append(iter->first);
178 } else {
179 NOTREACHED();
180 }
181 }
182
183 std::string manifest =
184 ResourceBundle::GetSharedInstance().GetRawDataResource(
185 iter->second).as_string();
186 RegisterComponentExtension(manifest, path);
187 }
188
189 #if defined(OS_CHROMEOS)
190 // Register access extensions only if accessibility is enabled.
191 if (g_browser_process->local_state()->
192 GetBoolean(prefs::kAccessibilityEnabled)) {
193 FilePath path = FilePath(extension_misc::kAccessExtensionPath)
194 .AppendASCII(extension_misc::kChromeVoxDirectoryName);
195 std::string manifest =
196 ResourceBundle::GetSharedInstance().GetRawDataResource(
197 IDR_CHROMEVOX_MANIFEST).as_string();
198 RegisterComponentExtension(manifest, path);
199 }
200 #endif
201 }
202
203 void ComponentLoader::UnregisterComponentExtension(
204 const std::string& manifest) {
205 RegisteredComponentExtensions new_component_extension_manifests;
206 for (RegisteredComponentExtensions::iterator it =
207 component_extension_manifests_.begin();
208 it != component_extension_manifests_.end(); ++it) {
209 if (it->manifest != manifest)
210 new_component_extension_manifests.push_back(*it);
211 }
212 component_extension_manifests_.swap(new_component_extension_manifests);
213 }
214
215 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698