summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/customization_document.cc
blob: 04e446057c4acaa3479901287d43e988f350756f (plain)
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
// Copyright (c) 2010 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/file_util.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/string_tokenizer.h"
#include "base/string_util.h"

// 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 kHWIDPath[] = "/sys/devices/platform/chromeos_acpi/HWID";
const char kVPDPath[] = "/var/log/vpd_2.0.txt";

}  // anonymous namespace

namespace chromeos {

// CustomizationDocument implementation.
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) {
  scoped_ptr<Value> root(base::JSONReader::Read(manifest, true));
  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 == kAcceptedManifestVersion)
      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_->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.
bool StartupCustomizationDocument::LoadManifestFromString(
    const std::string& manifest) {
  if (!CustomizationDocument::LoadManifestFromString(manifest)) {
    return false;
  }

  root_->GetString(kInitialLocaleAttr, &initial_locale_);
  root_->GetString(kInitialTimezoneAttr, &initial_timezone_);
  root_->GetString(kKeyboardLayoutAttr, &keyboard_layout_);
  root_->GetString(kRegistrationUrlAttr, &registration_url_);

  std::string hwid = GetHWID();
  if (!hwid.empty()) {
    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) << "Can't read HWID from " << kHWIDPath;
  }

  VPDMap vpd_map;
  if (ParseVPD(GetVPD(), &vpd_map)) {
    InitFromVPD(vpd_map, kInitialLocaleAttr, &initial_locale_);
    InitFromVPD(vpd_map, kInitialTimezoneAttr, &initial_timezone_);
    InitFromVPD(vpd_map, kKeyboardLayoutAttr, &keyboard_layout_);
  }

  return true;
}

std::string StartupCustomizationDocument::GetHWID() const {
  // TODO(dpolukhin): move to SystemLibrary to be reusable.
  std::string hwid;
  FilePath hwid_file_path(kHWIDPath);
  if (!file_util::ReadFileToString(hwid_file_path, &hwid))
    LOG(ERROR) << "Can't read HWID from " << kHWIDPath;
  return hwid;
}

std::string StartupCustomizationDocument::GetVPD() const {
  // TODO(dpolukhin): move to SystemLibrary to be reusable.
  std::string vpd;
  FilePath vpd_file_path(kVPDPath);
  if (!file_util::ReadFileToString(vpd_file_path, &vpd))
    LOG(ERROR) << "Can't read VPD from " << kVPDPath;
  return vpd;
}

bool StartupCustomizationDocument::ParseVPD(const std::string& vpd_string,
                                            VPDMap* vpd_map) {
  // TODO(dpolukhin): move to SystemLibrary to be reusable.
  const char kDelimiterChars[] = "= \n";
  const char kQuotaChars[] = "\"\'";

  StringTokenizer tok(vpd_string, kDelimiterChars);
  tok.set_quote_chars(kQuotaChars);
  tok.set_options(StringTokenizer::RETURN_DELIMS);
  bool next_is_equal = false;
  bool next_is_value = false;
  std::string name;
  std::string value;
  while (tok.GetNext()) {
    // Skip all delimiters that are not '='.
    if (tok.token_is_delim() && tok.token() != "=")
      continue;

    if (next_is_equal) {
      if (tok.token() != "=")
        break;

      next_is_equal = false;
      next_is_value = true;
    } else if (next_is_value) {
      TrimString(tok.token(), kQuotaChars, &value);
      next_is_value = false;

      if (!vpd_map->insert(VPDMap::value_type(name, value)).second) {
        LOG(ERROR) << "Identical keys in VPD " << name;
        return false;
      }
    } else {
      TrimString(tok.token(), kQuotaChars, &name);
      next_is_equal = true;
    }
  }

  if (next_is_equal || next_is_value) {
    LOG(ERROR) << "Syntax error in VPD " << vpd_string;
    return false;
  }

  return true;
}

void StartupCustomizationDocument::InitFromVPD(
    const VPDMap& vpd_map, const char* attr, std::string* value) {
  VPDMap::const_iterator it = vpd_map.find(attr);
  if (it != vpd_map.end())
    *value = it->second;
}

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.
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