OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/customization_document.h" | 5 #include "chrome/browser/chromeos/customization_document.h" |
6 | 6 |
7 #include "base/file_path.h" | |
7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
8 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/string_tokenizer.h" | 11 #include "base/string_tokenizer.h" |
11 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "chrome/browser/browser_process.h" | |
14 #include "chrome/browser/chromeos/cros/cros_library.h" | |
15 #include "chrome/browser/chromeos/cros/network_library.h" | |
12 #include "chrome/browser/chromeos/system_access.h" | 16 #include "chrome/browser/chromeos/system_access.h" |
17 #include "chrome/browser/prefs/pref_service.h" | |
18 #include "chrome/browser/profiles/profile_manager.h" | |
19 #include "content/browser/browser_thread.h" | |
13 | 20 |
14 // Manifest attributes names. | 21 // Manifest attributes names. |
15 | 22 |
16 namespace { | 23 namespace { |
17 | 24 |
18 const char kVersionAttr[] = "version"; | 25 const char kVersionAttr[] = "version"; |
19 const char kDefaultAttr[] = "default"; | 26 const char kDefaultAttr[] = "default"; |
20 const char kInitialLocaleAttr[] = "initial_locale"; | 27 const char kInitialLocaleAttr[] = "initial_locale"; |
21 const char kInitialTimezoneAttr[] = "initial_timezone"; | 28 const char kInitialTimezoneAttr[] = "initial_timezone"; |
22 const char kKeyboardLayoutAttr[] = "keyboard_layout"; | 29 const char kKeyboardLayoutAttr[] = "keyboard_layout"; |
23 const char kRegistrationUrlAttr[] = "registration_url"; | 30 const char kRegistrationUrlAttr[] = "registration_url"; |
24 const char kHwidMapAttr[] = "hwid_map"; | 31 const char kHwidMapAttr[] = "hwid_map"; |
25 const char kHwidMaskAttr[] = "hwid_mask"; | 32 const char kHwidMaskAttr[] = "hwid_mask"; |
26 const char kSetupContentAttr[] = "setup_content"; | 33 const char kSetupContentAttr[] = "setup_content"; |
27 const char kHelpPageAttr[] = "help_page"; | 34 const char kHelpPageAttr[] = "help_page"; |
28 const char kEulaPageAttr[] = "eula_page"; | 35 const char kEulaPageAttr[] = "eula_page"; |
29 const char kAppContentAttr[] = "app_content"; | 36 const char kAppContentAttr[] = "app_content"; |
30 const char kInitialStartPageAttr[] = "initial_start_page"; | 37 const char kInitialStartPageAttr[] = "initial_start_page"; |
31 const char kSupportPageAttr[] = "support_page"; | 38 const char kSupportPageAttr[] = "support_page"; |
32 | 39 |
33 const char kAcceptedManifestVersion[] = "1.0"; | 40 const char kAcceptedManifestVersion[] = "1.0"; |
34 | 41 |
35 const char kHwid[] = "hwid"; | 42 const char kHwid[] = "hwid"; |
36 | 43 |
44 // Path to OEM partner startup customization manifest. | |
45 const char kStartupCustomizationManifestPath[] = | |
46 "/opt/oem/etc/startup_manifest.json"; | |
47 | |
48 // URL where to fetch OEM services customization manifest from. | |
49 const char kServicesCustomizationManifestUrl[] = | |
50 "file:///opt/oem/etc/services_manifest.json"; | |
51 | |
52 // Name of local state option that tracks if services customization has been | |
53 // applied. | |
54 const char kServicesCustomizationAppliedPref[] = "ServicesCustomizationApplied"; | |
55 | |
56 // Maximum number of retries to fetch file if network is not available. | |
57 const int kMaxFetchRetries = 3; | |
58 | |
59 // Delay between file fetch retries if network is not available. | |
60 const int kRetriesDelayInSec = 2; | |
61 | |
37 } // anonymous namespace | 62 } // anonymous namespace |
38 | 63 |
64 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::ServicesCustomizationDocument); | |
65 | |
39 namespace chromeos { | 66 namespace chromeos { |
40 | 67 |
41 // CustomizationDocument implementation. | 68 // CustomizationDocument implementation. |
42 bool CustomizationDocument::LoadManifestFromFile( | 69 bool CustomizationDocument::LoadManifestFromFile( |
43 const FilePath& manifest_path) { | 70 const FilePath& manifest_path) { |
44 std::string manifest; | 71 std::string manifest; |
45 if (!file_util::ReadFileToString(manifest_path, &manifest)) | 72 if (!file_util::ReadFileToString(manifest_path, &manifest)) |
46 return false; | 73 return false; |
47 return LoadManifestFromString(manifest); | 74 return LoadManifestFromString(manifest); |
48 } | 75 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 DictionaryValue* default_dictionary = NULL; | 112 DictionaryValue* default_dictionary = NULL; |
86 if (dictionary_content->GetDictionary(kDefaultAttr, &default_dictionary)) { | 113 if (dictionary_content->GetDictionary(kDefaultAttr, &default_dictionary)) { |
87 std::string result; | 114 std::string result; |
88 if (default_dictionary->GetString(entry_name, &result)) | 115 if (default_dictionary->GetString(entry_name, &result)) |
89 return result; | 116 return result; |
90 } | 117 } |
91 | 118 |
92 return std::string(); | 119 return std::string(); |
93 } | 120 } |
94 | 121 |
95 // StartupCustomizationDocument implementation. | 122 // StartupCustomizationDocument implementation. |
Nikita (slow)
2011/04/27 13:57:07
Please use this style here and trough the file:
/
Dmitry Polukhin
2011/04/27 14:56:15
Done.
| |
96 StartupCustomizationDocument::StartupCustomizationDocument( | 123 StartupCustomizationDocument::StartupCustomizationDocument() { |
97 SystemAccess* system_access) | 124 { |
98 : system_access_(system_access) { | 125 // Loading manifest causes us to do blocking IO on UI thread. |
126 // Temporarily allow it until we fix http://crosbug.com/11103 | |
127 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
128 LoadManifestFromFile(FilePath(kStartupCustomizationManifestPath)); | |
129 } | |
130 Init(SystemAccess::GetInstance()); | |
99 } | 131 } |
100 | 132 |
101 bool StartupCustomizationDocument::LoadManifestFromString( | 133 StartupCustomizationDocument::StartupCustomizationDocument( |
102 const std::string& manifest) { | 134 SystemAccess* system_access, const std::string& manifest) { |
103 DCHECK(system_access_); | 135 LoadManifestFromString(manifest); |
Nikita (slow)
2011/04/27 13:57:07
Are thread restrictions applied during testing?
Dmitry Polukhin
2011/04/27 14:56:15
I think so. But in test we don't ready manifest fr
| |
136 Init(system_access); | |
137 } | |
104 | 138 |
105 if (!CustomizationDocument::LoadManifestFromString(manifest)) { | 139 StartupCustomizationDocument* StartupCustomizationDocument::GetInstance() { |
106 return false; | 140 return Singleton<StartupCustomizationDocument, |
107 } | 141 DefaultSingletonTraits<StartupCustomizationDocument> >::get(); |
142 } | |
143 | |
144 void StartupCustomizationDocument::Init(SystemAccess* system_access) { | |
145 if (!IsReady()) | |
146 return; | |
108 | 147 |
109 root_->GetString(kInitialLocaleAttr, &initial_locale_); | 148 root_->GetString(kInitialLocaleAttr, &initial_locale_); |
110 root_->GetString(kInitialTimezoneAttr, &initial_timezone_); | 149 root_->GetString(kInitialTimezoneAttr, &initial_timezone_); |
111 root_->GetString(kKeyboardLayoutAttr, &keyboard_layout_); | 150 root_->GetString(kKeyboardLayoutAttr, &keyboard_layout_); |
112 root_->GetString(kRegistrationUrlAttr, ®istration_url_); | 151 root_->GetString(kRegistrationUrlAttr, ®istration_url_); |
113 | 152 |
114 std::string hwid; | 153 std::string hwid; |
115 if (system_access_->GetMachineStatistic(kHwid, &hwid)) { | 154 if (system_access->GetMachineStatistic(kHwid, &hwid)) { |
116 ListValue* hwid_list = NULL; | 155 ListValue* hwid_list = NULL; |
117 if (root_->GetList(kHwidMapAttr, &hwid_list)) { | 156 if (root_->GetList(kHwidMapAttr, &hwid_list)) { |
118 for (size_t i = 0; i < hwid_list->GetSize(); ++i) { | 157 for (size_t i = 0; i < hwid_list->GetSize(); ++i) { |
119 DictionaryValue* hwid_dictionary = NULL; | 158 DictionaryValue* hwid_dictionary = NULL; |
120 std::string hwid_mask; | 159 std::string hwid_mask; |
121 if (hwid_list->GetDictionary(i, &hwid_dictionary) && | 160 if (hwid_list->GetDictionary(i, &hwid_dictionary) && |
122 hwid_dictionary->GetString(kHwidMaskAttr, &hwid_mask)) { | 161 hwid_dictionary->GetString(kHwidMaskAttr, &hwid_mask)) { |
123 if (MatchPattern(hwid, hwid_mask)) { | 162 if (MatchPattern(hwid, hwid_mask)) { |
124 // If HWID for this machine matches some mask, use HWID specific | 163 // If HWID for this machine matches some mask, use HWID specific |
125 // settings. | 164 // settings. |
(...skipping 10 matching lines...) Expand all Loading... | |
136 // Don't break here to allow other entires to be applied if match. | 175 // Don't break here to allow other entires to be applied if match. |
137 } else { | 176 } else { |
138 LOG(ERROR) << "Syntax error in customization manifest"; | 177 LOG(ERROR) << "Syntax error in customization manifest"; |
139 } | 178 } |
140 } | 179 } |
141 } | 180 } |
142 } else { | 181 } else { |
143 LOG(ERROR) << "HWID is missing in machine statistics"; | 182 LOG(ERROR) << "HWID is missing in machine statistics"; |
144 } | 183 } |
145 | 184 |
146 system_access_->GetMachineStatistic(kInitialLocaleAttr, &initial_locale_); | 185 system_access->GetMachineStatistic(kInitialLocaleAttr, &initial_locale_); |
147 system_access_->GetMachineStatistic(kInitialTimezoneAttr, &initial_timezone_); | 186 system_access->GetMachineStatistic(kInitialTimezoneAttr, &initial_timezone_); |
148 system_access_->GetMachineStatistic(kKeyboardLayoutAttr, &keyboard_layout_); | 187 system_access->GetMachineStatistic(kKeyboardLayoutAttr, &keyboard_layout_); |
149 | |
150 // system_access_ is no longer used. | |
151 system_access_ = NULL; | |
152 | |
153 return true; | |
154 } | 188 } |
155 | 189 |
156 std::string StartupCustomizationDocument::GetHelpPage( | 190 std::string StartupCustomizationDocument::GetHelpPage( |
157 const std::string& locale) const { | 191 const std::string& locale) const { |
158 return GetLocaleSpecificString(locale, kSetupContentAttr, kHelpPageAttr); | 192 return GetLocaleSpecificString(locale, kSetupContentAttr, kHelpPageAttr); |
159 } | 193 } |
160 | 194 |
161 std::string StartupCustomizationDocument::GetEULAPage( | 195 std::string StartupCustomizationDocument::GetEULAPage( |
162 const std::string& locale) const { | 196 const std::string& locale) const { |
163 return GetLocaleSpecificString(locale, kSetupContentAttr, kEulaPageAttr); | 197 return GetLocaleSpecificString(locale, kSetupContentAttr, kEulaPageAttr); |
164 } | 198 } |
165 | 199 |
166 // ServicesCustomizationDocument implementation. | 200 // ServicesCustomizationDocument implementation. |
201 ServicesCustomizationDocument::ServicesCustomizationDocument() | |
202 : url_(kServicesCustomizationManifestUrl) { | |
203 } | |
204 | |
205 ServicesCustomizationDocument::ServicesCustomizationDocument( | |
206 const std::string& manifest) { | |
207 LoadManifestFromString(manifest); | |
Nikita (slow)
2011/04/27 13:57:07
Instance enters into state when call on StartFetch
Dmitry Polukhin
2011/04/27 14:56:15
Added DCHECK(url_.is_valid());
| |
208 } | |
209 | |
210 // static | |
211 ServicesCustomizationDocument* ServicesCustomizationDocument::GetInstance() { | |
212 return Singleton<ServicesCustomizationDocument, | |
213 DefaultSingletonTraits<ServicesCustomizationDocument> >::get(); | |
214 } | |
215 | |
216 // static | |
217 void ServicesCustomizationDocument::RegisterPrefs(PrefService* local_state) { | |
218 local_state->RegisterBooleanPref(kServicesCustomizationAppliedPref, false); | |
Nikita (slow)
2011/04/27 14:50:02
FYI http://codereview.chromium.org/6905044/
| |
219 } | |
220 | |
221 // static | |
222 bool ServicesCustomizationDocument::WasApplied() { | |
223 PrefService* prefs = g_browser_process->local_state(); | |
224 return prefs->GetBoolean(kServicesCustomizationAppliedPref); | |
225 } | |
226 | |
227 // static | |
228 void ServicesCustomizationDocument::SetApplied(bool val) { | |
229 PrefService* prefs = g_browser_process->local_state(); | |
230 prefs->SetBoolean(kServicesCustomizationAppliedPref, val); | |
231 } | |
232 | |
233 void ServicesCustomizationDocument::StartFetching() { | |
234 if (url_.SchemeIsFile()) { | |
235 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
236 NewRunnableMethod(this, | |
237 &ServicesCustomizationDocument::ReadFileInBackground, | |
238 FilePath(url_.path()))); | |
239 } else { | |
240 StartFileFetch(); | |
241 } | |
242 } | |
243 | |
244 void ServicesCustomizationDocument::ReadFileInBackground(const FilePath& file) { | |
245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
246 | |
247 std::string manifest; | |
248 if (file_util::ReadFileToString(file, &manifest)) { | |
249 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
250 NewRunnableMethod( | |
251 this, | |
252 &ServicesCustomizationDocument::LoadManifestFromString, | |
253 manifest)); | |
254 } else { | |
255 VLOG(1) << "Failed to load services customization manifest from: " | |
256 << file.value(); | |
257 } | |
258 } | |
259 | |
260 void ServicesCustomizationDocument::StartFileFetch() { | |
261 url_fetcher_.reset(new URLFetcher(url_, URLFetcher::GET, this)); | |
262 url_fetcher_->set_request_context( | |
263 ProfileManager::GetDefaultProfile()->GetRequestContext()); | |
264 url_fetcher_->Start(); | |
265 } | |
266 | |
267 void ServicesCustomizationDocument::OnURLFetchComplete( | |
268 const URLFetcher* source, | |
269 const GURL& url, | |
270 const net::URLRequestStatus& status, | |
271 int response_code, | |
272 const ResponseCookies& cookies, | |
273 const std::string& data) { | |
274 if (response_code == 200) { | |
275 LoadManifestFromString(data); | |
276 } else { | |
277 NetworkLibrary* network = CrosLibrary::Get()->GetNetworkLibrary(); | |
278 if (!network->Connected() && num_retries_ < kMaxFetchRetries) { | |
279 num_retries_++; | |
280 retry_timer_.Start(base::TimeDelta::FromSeconds(kRetriesDelayInSec), | |
281 this, &ServicesCustomizationDocument::StartFileFetch); | |
282 return; | |
283 } | |
284 LOG(ERROR) << "URL fetch for services customization failed:" | |
285 << " response code = " << response_code | |
286 << " URL = " << url.spec(); | |
287 } | |
288 } | |
289 | |
290 bool ServicesCustomizationDocument::ApplyCustmization() { | |
291 // TODO(dpolukhin): apply customized apps, exts and support page. | |
292 SetApplied(true); | |
293 return true; | |
294 } | |
295 | |
167 std::string ServicesCustomizationDocument::GetInitialStartPage( | 296 std::string ServicesCustomizationDocument::GetInitialStartPage( |
168 const std::string& locale) const { | 297 const std::string& locale) const { |
169 return GetLocaleSpecificString( | 298 return GetLocaleSpecificString( |
170 locale, kAppContentAttr, kInitialStartPageAttr); | 299 locale, kAppContentAttr, kInitialStartPageAttr); |
171 } | 300 } |
172 | 301 |
173 std::string ServicesCustomizationDocument::GetSupportPage( | 302 std::string ServicesCustomizationDocument::GetSupportPage( |
174 const std::string& locale) const { | 303 const std::string& locale) const { |
175 return GetLocaleSpecificString( | 304 return GetLocaleSpecificString( |
176 locale, kAppContentAttr, kSupportPageAttr); | 305 locale, kAppContentAttr, kSupportPageAttr); |
177 } | 306 } |
178 | 307 |
179 } // namespace chromeos | 308 } // namespace chromeos |
OLD | NEW |