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

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: 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 bool ComponentLoader::ComponentExtensionInfo::Equals(
44 const ComponentExtensionInfo& other) const {
45 return other.manifest == manifest && other.root_directory == root_directory;
46 }
47
48 ComponentLoader::ComponentLoader(ExtensionService* extension_service)
49 : extension_service_(extension_service) {
50 }
51
52 ComponentLoader::~ComponentLoader() {
53 }
54
55 void ComponentLoader::LoadComponentExtensions() {
56 for (RegisteredComponentExtensions::iterator it =
57 component_extension_manifests_.begin();
58 it != component_extension_manifests_.end(); ++it) {
59 LoadComponentExtension(*it);
60 }
61 }
62
63 const Extension* ComponentLoader::LoadComponentExtension(
64 const ComponentExtensionInfo &info) {
65 JSONStringValueSerializer serializer(info.manifest);
66 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
67 if (!manifest.get()) {
68 LOG(ERROR) << "Failed to parse manifest for extension";
69 return NULL;
70 }
71
72 int flags = Extension::REQUIRE_KEY;
73 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT))
74 flags |= Extension::STRICT_ERROR_CHECKS;
75 std::string error;
76 scoped_refptr<const Extension> extension(Extension::Create(
77 info.root_directory,
78 Extension::COMPONENT,
79 *static_cast<DictionaryValue*>(manifest.get()),
80 flags,
81 &error));
82 if (!extension.get()) {
83 LOG(ERROR) << error;
84 return NULL;
85 }
86 extension_service_->AddExtension(extension);
87 return extension;
88 }
89
90 void ComponentLoader::UnloadComponentExtension(
91 const ComponentExtensionInfo& info) {
92 JSONStringValueSerializer serializer(info.manifest);
93 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
94 if (!manifest.get()) {
95 LOG(ERROR) << "Failed to parse manifest for extension";
96 return;
97 }
98 std::string public_key;
99 std::string public_key_bytes;
100 std::string id;
101 if (!static_cast<DictionaryValue*>(manifest.get())->
102 GetString(extension_manifest_keys::kPublicKey, &public_key) ||
103 !Extension::ParsePEMKeyBytes(public_key, &public_key_bytes) ||
104 !Extension::GenerateId(public_key_bytes, &id)) {
105 LOG(ERROR) << "Failed to get extension id";
106 return;
107 }
108 extension_service_->
109 UnloadExtension(id, extension_misc::UNLOAD_REASON_DISABLE);
110 }
111
112 // We take ComponentExtensionList:
113 // path, manifest ID => full manifest, absolute path
114 void ComponentLoader::RegisterDefaultComponentExtensions() {
115 ComponentExtensionList component_extensions;
116
117 // Bookmark manager.
118 component_extensions.push_back(std::make_pair(
119 FILE_PATH_LITERAL("bookmark_manager"),
120 IDR_BOOKMARKS_MANIFEST));
121
122 #if defined(FILE_MANAGER_EXTENSION)
123 AddFileManagerExtension(&component_extensions);
124 #endif
125
126 #if defined(TOUCH_UI)
127 component_extensions.push_back(std::make_pair(
128 FILE_PATH_LITERAL("keyboard"),
129 IDR_KEYBOARD_MANIFEST));
130 #endif
131
132 #if defined(OS_CHROMEOS)
133 component_extensions.push_back(std::make_pair(
134 FILE_PATH_LITERAL("/usr/share/chromeos-assets/mobile"),
135 IDR_MOBILE_MANIFEST));
136
137 const CommandLine* command_line = CommandLine::ForCurrentProcess();
138 if (command_line->HasSwitch(switches::kAuthExtensionPath)) {
139 FilePath auth_extension_path =
140 command_line->GetSwitchValuePath(switches::kAuthExtensionPath);
141 component_extensions.push_back(std::make_pair(
142 auth_extension_path.value(),
143 IDR_GAIA_TEST_AUTH_MANIFEST));
144 } else {
145 component_extensions.push_back(std::make_pair(
146 FILE_PATH_LITERAL("/usr/share/chromeos-assets/gaia_auth"),
147 IDR_GAIA_AUTH_MANIFEST));
148 }
149
150 #if defined(OFFICIAL_BUILD)
151 if (browser_defaults::enable_help_app) {
152 component_extensions.push_back(std::make_pair(
153 FILE_PATH_LITERAL("/usr/share/chromeos-assets/helpapp"),
154 IDR_HELP_MANIFEST));
155 }
156 #endif
157 #endif
158
159 // Web Store.
160 component_extensions.push_back(std::make_pair(
161 FILE_PATH_LITERAL("web_store"),
162 IDR_WEBSTORE_MANIFEST));
163
164 #if !defined(OS_CHROMEOS)
165 // Cloud Print component app. Not required on Chrome OS.
166 component_extensions.push_back(std::make_pair(
167 FILE_PATH_LITERAL("cloud_print"),
168 IDR_CLOUDPRINT_MANIFEST));
169 #endif // !defined(OS_CHROMEOS)
170
171 for (ComponentExtensionList::iterator iter = component_extensions.begin();
172 iter != component_extensions.end(); ++iter) {
173 FilePath path(iter->first);
174 if (!path.IsAbsolute()) {
175 if (PathService::Get(chrome::DIR_RESOURCES, &path)) {
176 path = path.Append(iter->first);
177 } else {
178 NOTREACHED();
179 }
180 }
181
182 std::string manifest =
183 ResourceBundle::GetSharedInstance().GetRawDataResource(
184 iter->second).as_string();
185 RegisterComponentExtension(ComponentExtensionInfo(manifest, path));
186 }
187
188 #if defined(OS_CHROMEOS)
189 // Register access extensions only if accessibility is enabled.
190 if (g_browser_process->local_state()->
191 GetBoolean(prefs::kAccessibilityEnabled)) {
192 FilePath path = FilePath(extension_misc::kAccessExtensionPath)
193 .AppendASCII(extension_misc::kChromeVoxDirectoryName);
194 std::string manifest =
195 ResourceBundle::GetSharedInstance().GetRawDataResource(
196 IDR_CHROMEVOX_MANIFEST).as_string();
197 RegisterComponentExtension(ComponentExtensionInfo(manifest, path));
198 }
199 #endif
200 }
201
202 void ComponentLoader::UnregisterComponentExtension(
203 const ComponentExtensionInfo& info) {
204 RegisteredComponentExtensions new_component_extension_manifests;
205 for (RegisteredComponentExtensions::iterator it =
206 component_extension_manifests_.begin();
207 it != component_extension_manifests_.end(); ++it) {
208 if (!it->Equals(info))
209 new_component_extension_manifests.push_back(*it);
210 }
211 component_extension_manifests_.swap(new_component_extension_manifests);
212 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698