OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/customization_document.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" |
| 11 #include "base/json/json_reader.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/string_util.h" |
| 14 #include "base/values.h" |
| 15 |
| 16 // Manifest attributes names. |
| 17 |
| 18 namespace { |
| 19 |
| 20 const wchar_t kVersionAttr[] = L"version"; |
| 21 const wchar_t kProductSkuAttr[] = L"product_sku"; |
| 22 const wchar_t kInitialLocaleAttr[] = L"initial_locale"; |
| 23 const wchar_t kBackgroundColorAttr[] = L"background_color"; |
| 24 const wchar_t kRegistrationUrlAttr[] = L"registration_url"; |
| 25 const wchar_t kSetupContentAttr[] = L"setup_content"; |
| 26 const wchar_t kContentLocaleAttr[] = L"content_locale"; |
| 27 const wchar_t kHelpPageAttr[] = L"help_page"; |
| 28 const wchar_t kEulaPageAttr[] = L"eula_page"; |
| 29 const wchar_t kAppMenuAttr[] = L"app_menu"; |
| 30 const wchar_t kInitialStartPageAttr[] = L"initial_start_page"; |
| 31 const wchar_t kSectionTitleAttr[] = L"section_title"; |
| 32 const wchar_t kWebAppsAttr[] = L"web_apps"; |
| 33 const wchar_t kSupportPageAttr[] = L"support_page"; |
| 34 const wchar_t kExtensionsAttr[] = L"extensions"; |
| 35 |
| 36 const char kAcceptedManifestVersion[] = "1.0"; |
| 37 |
| 38 } // anonymous namespace |
| 39 |
| 40 namespace chromeos { |
| 41 |
| 42 // CustomizationDocument implementation. |
| 43 |
| 44 bool CustomizationDocument::LoadManifestFromFile( |
| 45 const FilePath& manifest_path) { |
| 46 std::string manifest; |
| 47 bool read_success = file_util::ReadFileToString(manifest_path, &manifest); |
| 48 if (!read_success) { |
| 49 return false; |
| 50 } |
| 51 return LoadManifestFromString(manifest); |
| 52 } |
| 53 |
| 54 bool CustomizationDocument::LoadManifestFromString( |
| 55 const std::string& manifest) { |
| 56 scoped_ptr<Value> root(base::JSONReader::Read(manifest, true)); |
| 57 DCHECK(root.get() != NULL); |
| 58 if (root.get() == NULL) |
| 59 return false; |
| 60 DCHECK(root->GetType() == Value::TYPE_DICTIONARY); |
| 61 return ParseFromJsonValue(static_cast<DictionaryValue*>(root.get())); |
| 62 } |
| 63 |
| 64 bool CustomizationDocument::ParseFromJsonValue(const DictionaryValue* root) { |
| 65 // Partner customization manifests share only one required field - |
| 66 // version string. |
| 67 bool result = root->GetString(kVersionAttr, &version_); |
| 68 return result && version_ == kAcceptedManifestVersion; |
| 69 } |
| 70 |
| 71 // StartupCustomizationDocument implementation. |
| 72 |
| 73 bool StartupCustomizationDocument::ParseFromJsonValue( |
| 74 const DictionaryValue* root) { |
| 75 if (!CustomizationDocument::ParseFromJsonValue(root)) |
| 76 return false; |
| 77 // Rquired fields. |
| 78 if (!root->GetString(kProductSkuAttr, &product_sku_)) |
| 79 return false; |
| 80 // Optional fields. |
| 81 root->GetString(kInitialLocaleAttr, &initial_locale_); |
| 82 std::string background_color_string; |
| 83 root->GetString(kBackgroundColorAttr, &background_color_string); |
| 84 if (!background_color_string.empty()) { |
| 85 if (background_color_string[0] == '#') { |
| 86 background_color_ = static_cast<SkColor>( |
| 87 0xff000000 | HexStringToInt(background_color_string.substr(1))); |
| 88 } else { |
| 89 // Literal color constants are not supported yet. |
| 90 return false; |
| 91 } |
| 92 } |
| 93 root->GetString(kRegistrationUrlAttr, ®istration_url_); |
| 94 ListValue* setup_content_value = NULL; |
| 95 root->GetList(kSetupContentAttr, &setup_content_value); |
| 96 if (setup_content_value != NULL) { |
| 97 for (ListValue::const_iterator iter = setup_content_value->begin(); |
| 98 iter != setup_content_value->end(); |
| 99 ++iter) { |
| 100 const DictionaryValue* dict = NULL; |
| 101 dict = static_cast<const DictionaryValue*>(*iter); |
| 102 DCHECK(dict->GetType() == Value::TYPE_DICTIONARY); |
| 103 std::string content_locale; |
| 104 if (!dict->GetString(kContentLocaleAttr, &content_locale)) |
| 105 return false; |
| 106 SetupContent content; |
| 107 if (!dict->GetString(kHelpPageAttr, &content.help_page_path)) |
| 108 return false; |
| 109 if (!dict->GetString(kEulaPageAttr, &content.eula_page_path)) |
| 110 return false; |
| 111 setup_content_[content_locale] = content; |
| 112 } |
| 113 } |
| 114 return true; |
| 115 } |
| 116 |
| 117 const StartupCustomizationDocument::SetupContent* |
| 118 StartupCustomizationDocument::GetSetupContent( |
| 119 const std::string& locale) const { |
| 120 SetupContentMap::const_iterator content_iter = setup_content_.find(locale); |
| 121 if (content_iter != setup_content_.end()) { |
| 122 return &content_iter->second; |
| 123 } |
| 124 return NULL; |
| 125 } |
| 126 |
| 127 // ServicesCustomizationDocument implementation. |
| 128 |
| 129 bool ServicesCustomizationDocument::ParseFromJsonValue( |
| 130 const DictionaryValue* root) { |
| 131 return CustomizationDocument::ParseFromJsonValue(root); |
| 132 // TODO(denisromanov): implement. |
| 133 } |
| 134 |
| 135 } // namespace chromeos |
OLD | NEW |