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
|
// 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/chromeos/customization_document.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/string_tokenizer.h"
#include "base/string_util.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/browser/chromeos/cros/network_library.h"
#include "chrome/browser/chromeos/login/wizard_controller.h"
#include "chrome/browser/chromeos/system/statistics_provider.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/public/browser/browser_thread.h"
#include "net/url_request/url_fetcher.h"
using content::BrowserThread;
// Manifest attributes names.
namespace {
const char kVersionAttr[] = "version";
const char kDefaultAttr[] = "default";
const char kInitialLocaleAttr[] = "initial_locale";
const char kInitialTimezoneAttr[] = "initial_timezone";
const char kKeyboardLayoutAttr[] = "keyboard_layout";
const char kRegistrationUrlAttr[] = "registration_url";
const char kHwidMapAttr[] = "hwid_map";
const char kHwidMaskAttr[] = "hwid_mask";
const char kSetupContentAttr[] = "setup_content";
const char kHelpPageAttr[] = "help_page";
const char kEulaPageAttr[] = "eula_page";
const char kAppContentAttr[] = "app_content";
const char kInitialStartPageAttr[] = "initial_start_page";
const char kSupportPageAttr[] = "support_page";
const char kAcceptedManifestVersion[] = "1.0";
const char kHardwareClass[] = "hardware_class";
// Path to OEM partner startup customization manifest.
const char kStartupCustomizationManifestPath[] =
"/opt/oem/etc/startup_manifest.json";
// URL where to fetch OEM services customization manifest from.
const char kServicesCustomizationManifestUrl[] =
"file:///opt/oem/etc/services_manifest.json";
// Name of local state option that tracks if services customization has been
// applied.
const char kServicesCustomizationAppliedPref[] = "ServicesCustomizationApplied";
// Maximum number of retries to fetch file if network is not available.
const int kMaxFetchRetries = 3;
// Delay between file fetch retries if network is not available.
const int kRetriesDelayInSec = 2;
} // anonymous namespace
namespace chromeos {
// CustomizationDocument implementation. ---------------------------------------
CustomizationDocument::CustomizationDocument(
const std::string& accepted_version)
: accepted_version_(accepted_version) {}
CustomizationDocument::~CustomizationDocument() {}
bool CustomizationDocument::LoadManifestFromFile(
const FilePath& manifest_path) {
std::string manifest;
if (!file_util::ReadFileToString(manifest_path, &manifest))
return false;
return LoadManifestFromString(manifest);
}
bool CustomizationDocument::LoadManifestFromString(
const std::string& manifest) {
int error_code = 0;
std::string error;
scoped_ptr<Value> root(base::JSONReader::ReadAndReturnError(manifest,
base::JSON_ALLOW_TRAILING_COMMAS, &error_code, &error));
if (error_code != base::JSONReader::JSON_NO_ERROR)
LOG(ERROR) << error;
DCHECK(root.get() != NULL);
if (root.get() == NULL)
return false;
DCHECK(root->GetType() == Value::TYPE_DICTIONARY);
if (root->GetType() == Value::TYPE_DICTIONARY) {
root_.reset(static_cast<DictionaryValue*>(root.release()));
std::string result;
if (root_->GetString(kVersionAttr, &result) &&
result == accepted_version_)
return true;
LOG(ERROR) << "Wrong customization manifest version";
root_.reset(NULL);
}
return false;
}
std::string CustomizationDocument::GetLocaleSpecificString(
const std::string& locale,
const std::string& dictionary_name,
const std::string& entry_name) const {
DictionaryValue* dictionary_content = NULL;
if (!root_.get() ||
!root_->GetDictionary(dictionary_name, &dictionary_content))
return std::string();
DictionaryValue* locale_dictionary = NULL;
if (dictionary_content->GetDictionary(locale, &locale_dictionary)) {
std::string result;
if (locale_dictionary->GetString(entry_name, &result))
return result;
}
DictionaryValue* default_dictionary = NULL;
if (dictionary_content->GetDictionary(kDefaultAttr, &default_dictionary)) {
std::string result;
if (default_dictionary->GetString(entry_name, &result))
return result;
}
return std::string();
}
// StartupCustomizationDocument implementation. --------------------------------
StartupCustomizationDocument::StartupCustomizationDocument()
: CustomizationDocument(kAcceptedManifestVersion) {
{
// Loading manifest causes us to do blocking IO on UI thread.
// Temporarily allow it until we fix http://crosbug.com/11103
base::ThreadRestrictions::ScopedAllowIO allow_io;
LoadManifestFromFile(FilePath(kStartupCustomizationManifestPath));
}
Init(chromeos::system::StatisticsProvider::GetInstance());
}
StartupCustomizationDocument::StartupCustomizationDocument(
chromeos::system::StatisticsProvider* statistics_provider,
const std::string& manifest)
: CustomizationDocument(kAcceptedManifestVersion) {
LoadManifestFromString(manifest);
Init(statistics_provider);
}
StartupCustomizationDocument::~StartupCustomizationDocument() {}
StartupCustomizationDocument* StartupCustomizationDocument::GetInstance() {
return Singleton<StartupCustomizationDocument,
DefaultSingletonTraits<StartupCustomizationDocument> >::get();
}
void StartupCustomizationDocument::Init(
chromeos::system::StatisticsProvider* statistics_provider) {
if (IsReady()) {
root_->GetString(kInitialLocaleAttr, &initial_locale_);
root_->GetString(kInitialTimezoneAttr, &initial_timezone_);
root_->GetString(kKeyboardLayoutAttr, &keyboard_layout_);
root_->GetString(kRegistrationUrlAttr, ®istration_url_);
std::string hwid;
if (statistics_provider->GetMachineStatistic(kHardwareClass, &hwid)) {
ListValue* hwid_list = NULL;
if (root_->GetList(kHwidMapAttr, &hwid_list)) {
for (size_t i = 0; i < hwid_list->GetSize(); ++i) {
DictionaryValue* hwid_dictionary = NULL;
std::string hwid_mask;
if (hwid_list->GetDictionary(i, &hwid_dictionary) &&
hwid_dictionary->GetString(kHwidMaskAttr, &hwid_mask)) {
if (MatchPattern(hwid, hwid_mask)) {
// If HWID for this machine matches some mask, use HWID specific
// settings.
std::string result;
if (hwid_dictionary->GetString(kInitialLocaleAttr, &result))
initial_locale_ = result;
if (hwid_dictionary->GetString(kInitialTimezoneAttr, &result))
initial_timezone_ = result;
if (hwid_dictionary->GetString(kKeyboardLayoutAttr, &result))
keyboard_layout_ = result;
}
// Don't break here to allow other entires to be applied if match.
} else {
LOG(ERROR) << "Syntax error in customization manifest";
}
}
}
} else {
LOG(ERROR) << "HWID is missing in machine statistics";
}
}
// If manifest doesn't exist still apply values from VPD.
statistics_provider->GetMachineStatistic(kInitialLocaleAttr,
&initial_locale_);
statistics_provider->GetMachineStatistic(kInitialTimezoneAttr,
&initial_timezone_);
statistics_provider->GetMachineStatistic(kKeyboardLayoutAttr,
&keyboard_layout_);
}
std::string StartupCustomizationDocument::GetHelpPage(
const std::string& locale) const {
return GetLocaleSpecificString(locale, kSetupContentAttr, kHelpPageAttr);
}
std::string StartupCustomizationDocument::GetEULAPage(
const std::string& locale) const {
return GetLocaleSpecificString(locale, kSetupContentAttr, kEulaPageAttr);
}
// ServicesCustomizationDocument implementation. -------------------------------
ServicesCustomizationDocument::ServicesCustomizationDocument()
: CustomizationDocument(kAcceptedManifestVersion),
url_(kServicesCustomizationManifestUrl) {
}
ServicesCustomizationDocument::ServicesCustomizationDocument(
const std::string& manifest)
: CustomizationDocument(kAcceptedManifestVersion) {
LoadManifestFromString(manifest);
}
ServicesCustomizationDocument::~ServicesCustomizationDocument() {}
// static
ServicesCustomizationDocument* ServicesCustomizationDocument::GetInstance() {
return Singleton<ServicesCustomizationDocument,
DefaultSingletonTraits<ServicesCustomizationDocument> >::get();
}
// static
void ServicesCustomizationDocument::RegisterPrefs(
PrefServiceSimple* local_state) {
local_state->RegisterBooleanPref(kServicesCustomizationAppliedPref, false);
}
// static
bool ServicesCustomizationDocument::WasApplied() {
PrefService* prefs = g_browser_process->local_state();
return prefs->GetBoolean(kServicesCustomizationAppliedPref);
}
// static
void ServicesCustomizationDocument::SetApplied(bool val) {
PrefService* prefs = g_browser_process->local_state();
prefs->SetBoolean(kServicesCustomizationAppliedPref, val);
}
void ServicesCustomizationDocument::StartFetching() {
if (url_.SchemeIsFile()) {
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&ServicesCustomizationDocument::ReadFileInBackground,
base::Unretained(this), // this class is a singleton.
FilePath(url_.path())));
} else {
StartFileFetch();
}
}
void ServicesCustomizationDocument::ReadFileInBackground(const FilePath& file) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
std::string manifest;
if (file_util::ReadFileToString(file, &manifest)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(
base::IgnoreResult(
&ServicesCustomizationDocument::LoadManifestFromString),
base::Unretained(this), // this class is a singleton.
manifest));
} else {
VLOG(1) << "Failed to load services customization manifest from: "
<< file.value();
}
}
void ServicesCustomizationDocument::StartFileFetch() {
DCHECK(url_.is_valid());
url_fetcher_.reset(net::URLFetcher::Create(
url_, net::URLFetcher::GET, this));
url_fetcher_->SetRequestContext(
ProfileManager::GetDefaultProfile()->GetRequestContext());
url_fetcher_->Start();
}
void ServicesCustomizationDocument::OnURLFetchComplete(
const net::URLFetcher* source) {
if (source->GetResponseCode() == 200) {
std::string data;
source->GetResponseAsString(&data);
LoadManifestFromString(data);
} else {
NetworkLibrary* network = CrosLibrary::Get()->GetNetworkLibrary();
if (!network->Connected() && num_retries_ < kMaxFetchRetries) {
num_retries_++;
retry_timer_.Start(FROM_HERE,
base::TimeDelta::FromSeconds(kRetriesDelayInSec),
this, &ServicesCustomizationDocument::StartFileFetch);
return;
}
LOG(ERROR) << "URL fetch for services customization failed:"
<< " response code = " << source->GetResponseCode()
<< " URL = " << source->GetURL().spec();
}
}
bool ServicesCustomizationDocument::ApplyCustomization() {
// TODO(dpolukhin): apply customized apps, exts and support page.
SetApplied(true);
return true;
}
std::string ServicesCustomizationDocument::GetInitialStartPage(
const std::string& locale) const {
return GetLocaleSpecificString(
locale, kAppContentAttr, kInitialStartPageAttr);
}
std::string ServicesCustomizationDocument::GetSupportPage(
const std::string& locale) const {
return GetLocaleSpecificString(
locale, kAppContentAttr, kSupportPageAttr);
}
} // namespace chromeos
|