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
|
// 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/external_pref_loader.h"
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/json/json_file_value_serializer.h"
#include "base/json/json_string_value_serializer.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace {
base::FilePath::CharType kExternalExtensionJson[] =
FILE_PATH_LITERAL("external_extensions.json");
std::set<base::FilePath> GetPrefsCandidateFilesFromFolder(
const base::FilePath& external_extension_search_path) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
std::set<base::FilePath> external_extension_paths;
if (!file_util::PathExists(external_extension_search_path)) {
// Does not have to exist.
return external_extension_paths;
}
file_util::FileEnumerator json_files(
external_extension_search_path,
false, // Recursive.
file_util::FileEnumerator::FILES);
#if defined(OS_WIN)
base::FilePath::StringType extension = UTF8ToWide(std::string(".json"));
#elif defined(OS_POSIX)
base::FilePath::StringType extension(".json");
#endif
do {
base::FilePath file = json_files.Next();
if (file.BaseName().value() == kExternalExtensionJson)
continue; // Already taken care of elsewhere.
if (file.empty())
break;
if (file.MatchesExtension(extension)) {
external_extension_paths.insert(file.BaseName());
} else {
DVLOG(1) << "Not considering: " << file.LossyDisplayName()
<< " (does not have a .json extension)";
}
} while (true);
return external_extension_paths;
}
// Extracts extension information from a json file serialized by |serializer|.
// |path| is only used for informational purposes (outputted when an error
// occurs). An empty dictionary is returned in case of failure (e.g. invalid
// path or json content).
// Caller takes ownership of the returned dictionary.
DictionaryValue* ExtractExtensionPrefs(base::ValueSerializer* serializer,
const base::FilePath& path) {
std::string error_msg;
Value* extensions = serializer->Deserialize(NULL, &error_msg);
if (!extensions) {
LOG(WARNING) << "Unable to deserialize json data: " << error_msg
<< " in file " << path.value() << ".";
return new DictionaryValue;
}
DictionaryValue* ext_dictionary = NULL;
if (extensions->GetAsDictionary(&ext_dictionary))
return ext_dictionary;
LOG(WARNING) << "Expected a JSON dictionary in file "
<< path.value() << ".";
return new DictionaryValue;
}
} // namespace
namespace extensions {
ExternalPrefLoader::ExternalPrefLoader(int base_path_id, Options options)
: base_path_id_(base_path_id), options_(options) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
const base::FilePath ExternalPrefLoader::GetBaseCrxFilePath() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// |base_path_| was set in LoadOnFileThread().
return base_path_;
}
void ExternalPrefLoader::StartLoading() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(&ExternalPrefLoader::LoadOnFileThread, this));
}
void ExternalPrefLoader::LoadOnFileThread() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
// TODO(skerner): Some values of base_path_id_ will cause
// PathService::Get() to return false, because the path does
// not exist. Find and fix the build/install scripts so that
// this can become a CHECK(). Known examples include chrome
// OS developer builds and linux install packages.
// Tracked as crbug.com/70402 .
if (!PathService::Get(base_path_id_, &base_path_))
return;
scoped_ptr<DictionaryValue> prefs(new DictionaryValue);
ReadExternalExtensionPrefFile(prefs.get());
if (!prefs->empty())
LOG(WARNING) << "You are using an old-style extension deployment method "
"(external_extensions.json), which will soon be "
"deprecated. (see http://code.google.com/chrome/"
"extensions/external_extensions.html )";
ReadStandaloneExtensionPrefFiles(prefs.get());
prefs_.swap(prefs);
if (!prefs_.get())
prefs_.reset(new DictionaryValue());
if (base_path_id_ == chrome::DIR_EXTERNAL_EXTENSIONS) {
UMA_HISTOGRAM_COUNTS_100("Extensions.ExternalJsonCount",
prefs_->size());
}
// If we have any records to process, then we must have
// read at least one .json file. If so, then we should have
// set |base_path_|.
if (!prefs_->empty())
CHECK(!base_path_.empty());
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&ExternalPrefLoader::LoadFinished, this));
}
void ExternalPrefLoader::ReadExternalExtensionPrefFile(DictionaryValue* prefs) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
CHECK(NULL != prefs);
base::FilePath json_file = base_path_.Append(kExternalExtensionJson);
if (!file_util::PathExists(json_file)) {
// This is not an error. The file does not exist by default.
return;
}
if (IsOptionSet(ENSURE_PATH_CONTROLLED_BY_ADMIN)) {
#if defined(OS_MACOSX)
if (!file_util::VerifyPathControlledByAdmin(json_file)) {
LOG(ERROR) << "Can not read external extensions source. The file "
<< json_file.value() << " and every directory in its path, "
<< "must be owned by root, have group \"admin\", and not be "
<< "writable by all users. These restrictions prevent "
<< "unprivleged users from making chrome install extensions "
<< "on other users' accounts.";
return;
}
#else
// The only platform that uses this check is Mac OS. If you add one,
// you need to implement file_util::VerifyPathControlledByAdmin() for
// that platform.
NOTREACHED();
#endif // defined(OS_MACOSX)
}
JSONFileValueSerializer serializer(json_file);
scoped_ptr<DictionaryValue> ext_prefs(
ExtractExtensionPrefs(&serializer, json_file));
if (ext_prefs.get())
prefs->MergeDictionary(ext_prefs.get());
}
void ExternalPrefLoader::ReadStandaloneExtensionPrefFiles(
DictionaryValue* prefs) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
CHECK(NULL != prefs);
// First list the potential .json candidates.
std::set<base::FilePath>
candidates = GetPrefsCandidateFilesFromFolder(base_path_);
if (candidates.empty()) {
DVLOG(1) << "Extension candidates list empty";
return;
}
// For each file read the json description & build the proper
// associated prefs.
for (std::set<base::FilePath>::const_iterator it = candidates.begin();
it != candidates.end();
++it) {
base::FilePath extension_candidate_path = base_path_.Append(*it);
std::string id =
#if defined(OS_WIN)
WideToASCII(
extension_candidate_path.RemoveExtension().BaseName().value());
#elif defined(OS_POSIX)
extension_candidate_path.RemoveExtension().BaseName().value().c_str();
#endif
DVLOG(1) << "Reading json file: "
<< extension_candidate_path.LossyDisplayName().c_str();
JSONFileValueSerializer serializer(extension_candidate_path);
scoped_ptr<DictionaryValue> ext_prefs(
ExtractExtensionPrefs(&serializer, extension_candidate_path));
if (ext_prefs.get()) {
DVLOG(1) << "Adding extension with id: " << id;
prefs->Set(id, ext_prefs.release());
}
}
}
ExternalTestingLoader::ExternalTestingLoader(
const std::string& json_data,
const base::FilePath& fake_base_path)
: fake_base_path_(fake_base_path) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
JSONStringValueSerializer serializer(json_data);
base::FilePath fake_json_path = fake_base_path.AppendASCII("fake.json");
testing_prefs_.reset(ExtractExtensionPrefs(&serializer, fake_json_path));
}
void ExternalTestingLoader::StartLoading() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
prefs_.reset(testing_prefs_->DeepCopy());
LoadFinished();
}
ExternalTestingLoader::~ExternalTestingLoader() {}
const base::FilePath ExternalTestingLoader::GetBaseCrxFilePath() {
return fake_base_path_;
}
} // extensions
|