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

Side by Side Diff: chrome/browser/chromeos/customization_document.cc

Issue 6899021: HWID and VPD values should be accessible via SystemAccess::GetMachineStatistic (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: unit_test Created 9 years, 8 months 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
1 // Copyright (c) 2010 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_util.h" 7 #include "base/file_util.h"
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/string_tokenizer.h" 10 #include "base/string_tokenizer.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "chrome/browser/chromeos/system_access.h"
12 13
13 // Manifest attributes names. 14 // Manifest attributes names.
14 15
15 namespace { 16 namespace {
16 17
17 const char kVersionAttr[] = "version"; 18 const char kVersionAttr[] = "version";
18 const char kDefaultAttr[] = "default"; 19 const char kDefaultAttr[] = "default";
19 const char kInitialLocaleAttr[] = "initial_locale"; 20 const char kInitialLocaleAttr[] = "initial_locale";
20 const char kInitialTimezoneAttr[] = "initial_timezone"; 21 const char kInitialTimezoneAttr[] = "initial_timezone";
21 const char kKeyboardLayoutAttr[] = "keyboard_layout"; 22 const char kKeyboardLayoutAttr[] = "keyboard_layout";
22 const char kRegistrationUrlAttr[] = "registration_url"; 23 const char kRegistrationUrlAttr[] = "registration_url";
23 const char kHwidMapAttr[] = "hwid_map"; 24 const char kHwidMapAttr[] = "hwid_map";
24 const char kHwidMaskAttr[] = "hwid_mask"; 25 const char kHwidMaskAttr[] = "hwid_mask";
25 const char kSetupContentAttr[] = "setup_content"; 26 const char kSetupContentAttr[] = "setup_content";
26 const char kHelpPageAttr[] = "help_page"; 27 const char kHelpPageAttr[] = "help_page";
27 const char kEulaPageAttr[] = "eula_page"; 28 const char kEulaPageAttr[] = "eula_page";
28 const char kAppContentAttr[] = "app_content"; 29 const char kAppContentAttr[] = "app_content";
29 const char kInitialStartPageAttr[] = "initial_start_page"; 30 const char kInitialStartPageAttr[] = "initial_start_page";
30 const char kSupportPageAttr[] = "support_page"; 31 const char kSupportPageAttr[] = "support_page";
31 32
32 const char kAcceptedManifestVersion[] = "1.0"; 33 const char kAcceptedManifestVersion[] = "1.0";
33 34
34 const char kHWIDPath[] = "/sys/devices/platform/chromeos_acpi/HWID"; 35 const char kHwid[] = "hwid";
35 const char kVPDPath[] = "/var/log/vpd_2.0.txt";
36 36
37 } // anonymous namespace 37 } // anonymous namespace
38 38
39 namespace chromeos { 39 namespace chromeos {
40 40
41 // CustomizationDocument implementation. 41 // CustomizationDocument implementation.
42 bool CustomizationDocument::LoadManifestFromFile( 42 bool CustomizationDocument::LoadManifestFromFile(
43 const FilePath& manifest_path) { 43 const FilePath& manifest_path) {
44 std::string manifest; 44 std::string manifest;
45 if (!file_util::ReadFileToString(manifest_path, &manifest)) 45 if (!file_util::ReadFileToString(manifest_path, &manifest))
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 if (dictionary_content->GetDictionary(kDefaultAttr, &default_dictionary)) { 86 if (dictionary_content->GetDictionary(kDefaultAttr, &default_dictionary)) {
87 std::string result; 87 std::string result;
88 if (default_dictionary->GetString(entry_name, &result)) 88 if (default_dictionary->GetString(entry_name, &result))
89 return result; 89 return result;
90 } 90 }
91 91
92 return std::string(); 92 return std::string();
93 } 93 }
94 94
95 // StartupCustomizationDocument implementation. 95 // StartupCustomizationDocument implementation.
96 StartupCustomizationDocument::StartupCustomizationDocument(
97 SystemAccess* system_access)
98 : system_access_(system_access) {
99 }
100
96 bool StartupCustomizationDocument::LoadManifestFromString( 101 bool StartupCustomizationDocument::LoadManifestFromString(
97 const std::string& manifest) { 102 const std::string& manifest) {
103 DCHECK(system_access_);
104
98 if (!CustomizationDocument::LoadManifestFromString(manifest)) { 105 if (!CustomizationDocument::LoadManifestFromString(manifest)) {
99 return false; 106 return false;
100 } 107 }
101 108
102 root_->GetString(kInitialLocaleAttr, &initial_locale_); 109 root_->GetString(kInitialLocaleAttr, &initial_locale_);
103 root_->GetString(kInitialTimezoneAttr, &initial_timezone_); 110 root_->GetString(kInitialTimezoneAttr, &initial_timezone_);
104 root_->GetString(kKeyboardLayoutAttr, &keyboard_layout_); 111 root_->GetString(kKeyboardLayoutAttr, &keyboard_layout_);
105 root_->GetString(kRegistrationUrlAttr, &registration_url_); 112 root_->GetString(kRegistrationUrlAttr, &registration_url_);
106 113
107 std::string hwid = GetHWID(); 114 std::string hwid;
108 if (!hwid.empty()) { 115 if (system_access_->GetMachineStatistic(kHwid, &hwid)) {
109 ListValue* hwid_list = NULL; 116 ListValue* hwid_list = NULL;
110 if (root_->GetList(kHwidMapAttr, &hwid_list)) { 117 if (root_->GetList(kHwidMapAttr, &hwid_list)) {
111 for (size_t i = 0; i < hwid_list->GetSize(); ++i) { 118 for (size_t i = 0; i < hwid_list->GetSize(); ++i) {
112 DictionaryValue* hwid_dictionary = NULL; 119 DictionaryValue* hwid_dictionary = NULL;
113 std::string hwid_mask; 120 std::string hwid_mask;
114 if (hwid_list->GetDictionary(i, &hwid_dictionary) && 121 if (hwid_list->GetDictionary(i, &hwid_dictionary) &&
115 hwid_dictionary->GetString(kHwidMaskAttr, &hwid_mask)) { 122 hwid_dictionary->GetString(kHwidMaskAttr, &hwid_mask)) {
116 if (MatchPattern(hwid, hwid_mask)) { 123 if (MatchPattern(hwid, hwid_mask)) {
117 // If HWID for this machine matches some mask, use HWID specific 124 // If HWID for this machine matches some mask, use HWID specific
118 // settings. 125 // settings.
119 std::string result; 126 std::string result;
120 if (hwid_dictionary->GetString(kInitialLocaleAttr, &result)) 127 if (hwid_dictionary->GetString(kInitialLocaleAttr, &result))
121 initial_locale_ = result; 128 initial_locale_ = result;
122 129
123 if (hwid_dictionary->GetString(kInitialTimezoneAttr, &result)) 130 if (hwid_dictionary->GetString(kInitialTimezoneAttr, &result))
124 initial_timezone_ = result; 131 initial_timezone_ = result;
125 132
126 if (hwid_dictionary->GetString(kKeyboardLayoutAttr, &result)) 133 if (hwid_dictionary->GetString(kKeyboardLayoutAttr, &result))
127 keyboard_layout_ = result; 134 keyboard_layout_ = result;
128 } 135 }
129 // Don't break here to allow other entires to be applied if match. 136 // Don't break here to allow other entires to be applied if match.
130 } else { 137 } else {
131 LOG(ERROR) << "Syntax error in customization manifest"; 138 LOG(ERROR) << "Syntax error in customization manifest";
132 } 139 }
133 } 140 }
134 } 141 }
135 } else { 142 } else {
136 LOG(ERROR) << "Can't read HWID from " << kHWIDPath; 143 LOG(ERROR) << "HWID is missing in machine statistics";
137 } 144 }
138 145
139 VPDMap vpd_map; 146 system_access_->GetMachineStatistic(kInitialLocaleAttr, &initial_locale_);
140 if (ParseVPD(GetVPD(), &vpd_map)) { 147 system_access_->GetMachineStatistic(kInitialTimezoneAttr, &initial_timezone_);
141 InitFromVPD(vpd_map, kInitialLocaleAttr, &initial_locale_); 148 system_access_->GetMachineStatistic(kKeyboardLayoutAttr, &keyboard_layout_);
142 InitFromVPD(vpd_map, kInitialTimezoneAttr, &initial_timezone_); 149
143 InitFromVPD(vpd_map, kKeyboardLayoutAttr, &keyboard_layout_); 150 // system_access_ is no longer used.
144 } 151 system_access_ = NULL;
145 152
146 return true; 153 return true;
147 } 154 }
148 155
149 std::string StartupCustomizationDocument::GetHWID() const {
150 // TODO(dpolukhin): move to SystemLibrary to be reusable.
151 std::string hwid;
152 FilePath hwid_file_path(kHWIDPath);
153 if (!file_util::ReadFileToString(hwid_file_path, &hwid))
154 LOG(ERROR) << "Can't read HWID from " << kHWIDPath;
155 return hwid;
156 }
157
158 std::string StartupCustomizationDocument::GetVPD() const {
159 // TODO(dpolukhin): move to SystemLibrary to be reusable.
160 std::string vpd;
161 FilePath vpd_file_path(kVPDPath);
162 if (!file_util::ReadFileToString(vpd_file_path, &vpd))
163 LOG(ERROR) << "Can't read VPD from " << kVPDPath;
164 return vpd;
165 }
166
167 bool StartupCustomizationDocument::ParseVPD(const std::string& vpd_string,
168 VPDMap* vpd_map) {
169 // TODO(dpolukhin): move to SystemLibrary to be reusable.
170 const char kDelimiterChars[] = "= \n";
171 const char kQuotaChars[] = "\"\'";
172
173 StringTokenizer tok(vpd_string, kDelimiterChars);
174 tok.set_quote_chars(kQuotaChars);
175 tok.set_options(StringTokenizer::RETURN_DELIMS);
176 bool next_is_equal = false;
177 bool next_is_value = false;
178 std::string name;
179 std::string value;
180 while (tok.GetNext()) {
181 // Skip all delimiters that are not '='.
182 if (tok.token_is_delim() && tok.token() != "=")
183 continue;
184
185 if (next_is_equal) {
186 if (tok.token() != "=")
187 break;
188
189 next_is_equal = false;
190 next_is_value = true;
191 } else if (next_is_value) {
192 TrimString(tok.token(), kQuotaChars, &value);
193 next_is_value = false;
194
195 if (!vpd_map->insert(VPDMap::value_type(name, value)).second) {
196 LOG(ERROR) << "Identical keys in VPD " << name;
197 return false;
198 }
199 } else {
200 TrimString(tok.token(), kQuotaChars, &name);
201 next_is_equal = true;
202 }
203 }
204
205 if (next_is_equal || next_is_value) {
206 LOG(ERROR) << "Syntax error in VPD " << vpd_string;
207 return false;
208 }
209
210 return true;
211 }
212
213 void StartupCustomizationDocument::InitFromVPD(
214 const VPDMap& vpd_map, const char* attr, std::string* value) {
215 VPDMap::const_iterator it = vpd_map.find(attr);
216 if (it != vpd_map.end())
217 *value = it->second;
218 }
219
220 std::string StartupCustomizationDocument::GetHelpPage( 156 std::string StartupCustomizationDocument::GetHelpPage(
221 const std::string& locale) const { 157 const std::string& locale) const {
222 return GetLocaleSpecificString(locale, kSetupContentAttr, kHelpPageAttr); 158 return GetLocaleSpecificString(locale, kSetupContentAttr, kHelpPageAttr);
223 } 159 }
224 160
225 std::string StartupCustomizationDocument::GetEULAPage( 161 std::string StartupCustomizationDocument::GetEULAPage(
226 const std::string& locale) const { 162 const std::string& locale) const {
227 return GetLocaleSpecificString(locale, kSetupContentAttr, kEulaPageAttr); 163 return GetLocaleSpecificString(locale, kSetupContentAttr, kEulaPageAttr);
228 } 164 }
229 165
230 // ServicesCustomizationDocument implementation. 166 // ServicesCustomizationDocument implementation.
231 std::string ServicesCustomizationDocument::GetInitialStartPage( 167 std::string ServicesCustomizationDocument::GetInitialStartPage(
232 const std::string& locale) const { 168 const std::string& locale) const {
233 return GetLocaleSpecificString( 169 return GetLocaleSpecificString(
234 locale, kAppContentAttr, kInitialStartPageAttr); 170 locale, kAppContentAttr, kInitialStartPageAttr);
235 } 171 }
236 172
237 std::string ServicesCustomizationDocument::GetSupportPage( 173 std::string ServicesCustomizationDocument::GetSupportPage(
238 const std::string& locale) const { 174 const std::string& locale) const {
239 return GetLocaleSpecificString( 175 return GetLocaleSpecificString(
240 locale, kAppContentAttr, kSupportPageAttr); 176 locale, kAppContentAttr, kSupportPageAttr);
241 } 177 }
242 178
243 } // namespace chromeos 179 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698