1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/component_loader.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/json/json_string_value_serializer.h"
#include "base/path_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/prefs/pref_change_registrar.h"
#include "chrome/browser/prefs/pref_notifier.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_file_util.h"
#include "chrome/common/extensions/extension_manifest_constants.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "grit/browser_resources.h"
#include "ui/base/layout.h"
#include "ui/base/resource/resource_bundle.h"
#if defined(OFFICIAL_BUILD)
#include "chrome/browser/defaults.h"
#endif
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/login/user_manager.h"
#endif
namespace extensions {
ComponentLoader::ComponentLoader(ExtensionServiceInterface* extension_service,
PrefService* prefs,
PrefService* local_state)
: prefs_(prefs),
local_state_(local_state),
extension_service_(extension_service) {
pref_change_registrar_.Init(prefs);
// This pref is set by policy. We have to watch it for change because on
// ChromeOS, policy isn't loaded until after the browser process is started.
pref_change_registrar_.Add(prefs::kEnterpriseWebStoreURL, this);
}
ComponentLoader::~ComponentLoader() {
ClearAllRegistered();
}
void ComponentLoader::LoadAll() {
for (RegisteredComponentExtensions::iterator it =
component_extensions_.begin();
it != component_extensions_.end(); ++it) {
Load(*it);
}
}
DictionaryValue* ComponentLoader::ParseManifest(
const std::string& manifest_contents) const {
JSONStringValueSerializer serializer(manifest_contents);
scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
if (!manifest.get() || !manifest->IsType(Value::TYPE_DICTIONARY)) {
LOG(ERROR) << "Failed to parse extension manifest.";
return NULL;
}
// Transfer ownership to the caller.
return static_cast<DictionaryValue*>(manifest.release());
}
void ComponentLoader::ClearAllRegistered() {
for (RegisteredComponentExtensions::iterator it =
component_extensions_.begin();
it != component_extensions_.end(); ++it) {
delete it->manifest;
}
component_extensions_.clear();
}
const Extension* ComponentLoader::Add(
int manifest_resource_id,
const FilePath& root_directory) {
std::string manifest_contents =
ResourceBundle::GetSharedInstance().GetRawDataResource(
manifest_resource_id,
ui::SCALE_FACTOR_NONE).as_string();
return Add(manifest_contents, root_directory);
}
const Extension* ComponentLoader::Add(
std::string& manifest_contents,
const FilePath& root_directory) {
// The Value is kept for the lifetime of the ComponentLoader. This is
// required in case LoadAll() is called again.
DictionaryValue* manifest = ParseManifest(manifest_contents);
if (manifest)
return Add(manifest, root_directory);
return NULL;
}
const Extension* ComponentLoader::Add(
const DictionaryValue* parsed_manifest,
const FilePath& root_directory) {
CHECK(!Exists(GenerateId(parsed_manifest)));
ComponentExtensionInfo info(parsed_manifest, root_directory);
component_extensions_.push_back(info);
if (extension_service_->is_ready())
return Load(info);
return NULL;
}
const Extension* ComponentLoader::AddOrReplace(const FilePath& path) {
FilePath absolute_path = path;
file_util::AbsolutePath(&absolute_path);
std::string error;
scoped_ptr<DictionaryValue> manifest(
extension_file_util::LoadManifest(absolute_path, &error));
if (!manifest.get()) {
LOG(ERROR) << "Could not load extension from '" <<
absolute_path.value() << "'. " << error;
return NULL;
}
Remove(GenerateId(manifest.get()));
return Add(manifest.release(), absolute_path);
}
void ComponentLoader::Reload(const std::string& extension_id) {
for (RegisteredComponentExtensions::iterator it =
component_extensions_.begin(); it != component_extensions_.end();
++it) {
if (GenerateId(it->manifest) == extension_id) {
Load(*it);
break;
}
}
}
const Extension* ComponentLoader::Load(const ComponentExtensionInfo& info) {
int flags = Extension::REQUIRE_KEY;
// TODO(abarth): We should REQUIRE_MODERN_MANIFEST_VERSION once we've updated
// our component extensions to the new manifest version.
if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT))
flags |= Extension::STRICT_ERROR_CHECKS;
std::string error;
// Get the absolute path to the extension.
FilePath absolute_path(info.root_directory);
if (!absolute_path.IsAbsolute()) {
if (PathService::Get(chrome::DIR_RESOURCES, &absolute_path)) {
absolute_path = absolute_path.Append(info.root_directory);
} else {
NOTREACHED();
}
}
scoped_refptr<const Extension> extension(Extension::Create(
absolute_path,
Extension::COMPONENT,
*info.manifest,
flags,
&error));
if (!extension.get()) {
LOG(ERROR) << error;
return NULL;
}
extension_service_->AddExtension(extension);
return extension;
}
void ComponentLoader::Remove(const FilePath& root_directory) {
// Find the ComponentExtensionInfo for the extension.
RegisteredComponentExtensions::iterator it = component_extensions_.begin();
for (; it != component_extensions_.end(); ++it) {
if (it->root_directory == root_directory) {
Remove(GenerateId(it->manifest));
break;
}
}
}
void ComponentLoader::Remove(const std::string& id) {
RegisteredComponentExtensions::iterator it = component_extensions_.begin();
for (; it != component_extensions_.end(); ++it) {
if (GenerateId(it->manifest) == id) {
delete it->manifest;
it = component_extensions_.erase(it);
if (extension_service_->is_ready())
extension_service_->
UnloadExtension(id, extension_misc::UNLOAD_REASON_DISABLE);
break;
}
}
}
bool ComponentLoader::Exists(const std::string& id) const {
RegisteredComponentExtensions::const_iterator it =
component_extensions_.begin();
for (; it != component_extensions_.end(); ++it)
if (GenerateId(it->manifest) == id)
return true;
return false;
}
std::string ComponentLoader::GenerateId(const DictionaryValue* manifest) {
std::string public_key;
std::string public_key_bytes;
std::string id;
if (!manifest->GetString(
extension_manifest_keys::kPublicKey, &public_key) ||
!Extension::ParsePEMKeyBytes(public_key, &public_key_bytes) ||
!Extension::GenerateId(public_key_bytes, &id)) {
return std::string();
}
return id;
}
void ComponentLoader::AddFileManagerExtension() {
#if defined(FILE_MANAGER_EXTENSION)
#ifndef NDEBUG
const CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kFileManagerExtensionPath)) {
FilePath filemgr_extension_path(
command_line->GetSwitchValuePath(switches::kFileManagerExtensionPath));
Add(IDR_FILEMANAGER_MANIFEST, filemgr_extension_path);
return;
}
#endif // NDEBUG
Add(IDR_FILEMANAGER_MANIFEST, FilePath(FILE_PATH_LITERAL("file_manager")));
#endif // defined(FILE_MANAGER_EXTENSION)
}
void ComponentLoader::AddOrReloadEnterpriseWebStore() {
FilePath path(FILE_PATH_LITERAL("enterprise_web_store"));
// Remove the extension if it was already loaded.
Remove(path);
std::string enterprise_webstore_url =
prefs_->GetString(prefs::kEnterpriseWebStoreURL);
// Load the extension only if the URL preference is set.
if (!enterprise_webstore_url.empty()) {
std::string manifest_contents =
ResourceBundle::GetSharedInstance().GetRawDataResource(
IDR_ENTERPRISE_WEBSTORE_MANIFEST,
ui::SCALE_FACTOR_NONE).as_string();
// The manifest is missing some values that are provided by policy.
DictionaryValue* manifest = ParseManifest(manifest_contents);
if (manifest) {
std::string name = prefs_->GetString(prefs::kEnterpriseWebStoreName);
manifest->SetString("app.launch.web_url", enterprise_webstore_url);
manifest->SetString("name", name);
Add(manifest, path);
}
}
}
void ComponentLoader::AddDefaultComponentExtensions() {
#if defined(OS_CHROMEOS)
if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kGuestSession))
Add(IDR_BOOKMARKS_MANIFEST,
FilePath(FILE_PATH_LITERAL("bookmark_manager")));
#else
Add(IDR_BOOKMARKS_MANIFEST, FilePath(FILE_PATH_LITERAL("bookmark_manager")));
#endif
#if defined(FILE_MANAGER_EXTENSION)
AddFileManagerExtension();
#endif
#if defined(USE_VIRTUAL_KEYBOARD)
Add(IDR_KEYBOARD_MANIFEST, FilePath(FILE_PATH_LITERAL("keyboard")));
#endif
#if defined(OS_CHROMEOS)
Add(IDR_MOBILE_MANIFEST,
FilePath(FILE_PATH_LITERAL("/usr/share/chromeos-assets/mobile")));
Add(IDR_CROSH_BUILTIN_MANIFEST, FilePath(FILE_PATH_LITERAL(
"/usr/share/chromeos-assets/crosh_builtin")));
const CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kAuthExtensionPath)) {
FilePath auth_extension_path =
command_line->GetSwitchValuePath(switches::kAuthExtensionPath);
Add(IDR_GAIA_TEST_AUTH_MANIFEST, auth_extension_path);
} else {
Add(IDR_GAIA_AUTH_MANIFEST,
FilePath(FILE_PATH_LITERAL("/usr/share/chromeos-assets/gaia_auth")));
}
// TODO(gauravsh): Only include echo extension on official builds.
FilePath echo_extension_path(FILE_PATH_LITERAL(
"/usr/share/chromeos-assets/echo"));
if (command_line->HasSwitch(switches::kEchoExtensionPath)) {
echo_extension_path =
command_line->GetSwitchValuePath(switches::kEchoExtensionPath);
}
Add(IDR_ECHO_MANIFEST, echo_extension_path);
#if defined(OFFICIAL_BUILD)
if (browser_defaults::enable_help_app) {
Add(IDR_HELP_MANIFEST,
FilePath(FILE_PATH_LITERAL("/usr/share/chromeos-assets/helpapp")));
}
#endif
#endif // !defined(OS_CHROMEOS)
Add(IDR_WEBSTORE_MANIFEST, FilePath(FILE_PATH_LITERAL("web_store")));
#if !defined(OS_CHROMEOS)
// Cloud Print component app. Not required on Chrome OS.
Add(IDR_CLOUDPRINT_MANIFEST, FilePath(FILE_PATH_LITERAL("cloud_print")));
#endif
#if defined(OS_CHROMEOS)
// Register access extensions only if accessibility is enabled.
if (local_state_->GetBoolean(prefs::kSpokenFeedbackEnabled)) {
FilePath path = FilePath(extension_misc::kAccessExtensionPath)
.AppendASCII(extension_misc::kChromeVoxDirectoryName);
Add(IDR_CHROMEVOX_MANIFEST, path);
}
#endif
// If a URL for the enterprise webstore has been specified, load the
// component extension. This extension might also be loaded later, because
// it is specified by policy, and on ChromeOS policies are loaded after
// the browser process has started.
AddOrReloadEnterpriseWebStore();
}
void ComponentLoader::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (type == chrome::NOTIFICATION_PREF_CHANGED) {
const std::string* name =
content::Details<const std::string>(details).ptr();
if (*name == prefs::kEnterpriseWebStoreURL)
AddOrReloadEnterpriseWebStore();
else
NOTREACHED();
} else {
NOTREACHED();
}
}
// static
void ComponentLoader::RegisterUserPrefs(PrefService* prefs) {
prefs->RegisterStringPref(prefs::kEnterpriseWebStoreURL,
std::string() /* default_value */,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterStringPref(prefs::kEnterpriseWebStoreName,
std::string() /* default_value */,
PrefService::UNSYNCABLE_PREF);
}
} // namespace extensions
|