1 // Copyright (c) 2012 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.
5 #include "xwalk/application/common/manifest.h"
9 #include "base/basictypes.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "xwalk/application/common/application_manifest_constants.h"
17 #include "xwalk/runtime/browser/xwalk_runner.h"
19 namespace errors = xwalk::application_manifest_errors;
20 namespace keys = xwalk::application_manifest_keys;
21 namespace widget_keys = xwalk::application_widget_keys;
24 namespace application {
26 const char kLocaleUnlocalized[] = "@unlocalized";
28 const char kLocaleAuto[] = "en-gb";
30 const char kLocaleAuto[] = "en-us";
32 const char kLocaleFirstOne[] = "*";
34 const char kWidgetNamePath[] = "widget.name";
35 const char kWidgetDecriptionPath[] = "widget.description";
36 const char kWidgetLicensePath[] = "widget.license";
38 const char kPathConnectSymbol = '.';
40 typedef std::list<std::string> List;
42 std::string GetLocalizedKey(const std::string& key,
43 const std::string& local) {
44 std::string lower_local = StringToLowerASCII(local);
45 if (lower_local.empty())
46 lower_local = kLocaleUnlocalized;
47 return key + kPathConnectSymbol + lower_local;
50 scoped_ptr<List> ExpandUserAgentLocalesList(const scoped_ptr<List>& list) {
51 scoped_ptr<List> expansion_list(new List);
52 for (List::const_iterator it = list->begin(); it != list->end(); ++it) {
53 std::string copy_locale(*it);
56 expansion_list->push_back(copy_locale);
57 position = copy_locale.find_last_of("-");
58 copy_locale = copy_locale.substr(0, position);
59 } while (position != std::string::npos);
61 return expansion_list.Pass();
66 Manifest::Manifest(SourceType source_type,
67 scoped_ptr<base::DictionaryValue> value)
68 : source_type_(source_type),
70 i18n_data_(new base::DictionaryValue),
72 if (data_->Get(keys::kStartURLKey, NULL)) {
73 type_ = TYPE_PACKAGED_APP;
74 } else if (data_->HasKey(keys::kAppKey)) {
75 if (data_->Get(keys::kWebURLsKey, NULL) ||
76 data_->Get(keys::kLaunchWebURLKey, NULL)) {
77 type_ = TYPE_HOSTED_APP;
78 } else if (data_->Get(keys::kLaunchLocalPathKey, NULL)) {
79 type_ = TYPE_PACKAGED_APP;
83 if (data_->HasKey(widget_keys::kWidgetKey) &&
84 data_->Get(widget_keys::kWidgetKey, NULL))
87 // Unittest may not have an xwalkrunner, so we should check here.
88 if (XWalkRunner* xwalk_runner = XWalkRunner::GetInstance())
89 SetSystemLocale(xwalk_runner->GetLocale());
91 SetSystemLocale(kLocaleUnlocalized);
94 Manifest::~Manifest() {
97 bool Manifest::ValidateManifest(
99 std::vector<InstallWarning>* warnings) const {
100 // TODO(changbin): field 'manifest_version' of manifest.json is not clearly
101 // defined at present. Temporarily disable check of this field.
104 if (type_ == Manifest::TYPE_PACKAGED_APP && GetManifestVersion() < 2) {
105 *error = errors::kPlatformAppNeedsManifestVersion2;
110 // TODO(xiang): support features validation
114 bool Manifest::HasKey(const std::string& key) const {
115 return CanAccessKey(key) && data_->HasKey(key);
118 bool Manifest::HasPath(const std::string& path) const {
119 base::Value* ignored = NULL;
120 return CanAccessPath(path) && data_->Get(path, &ignored);
124 const std::string& path, const base::Value** out_value) const {
125 return CanAccessPath(path) && data_->Get(path, out_value);
129 const std::string& path, base::Value** out_value) const {
132 const_cast<const base::Value**>(out_value));
135 bool Manifest::GetBoolean(
136 const std::string& path, bool* out_value) const {
137 return CanAccessPath(path) && data_->GetBoolean(path, out_value);
140 bool Manifest::GetInteger(
141 const std::string& path, int* out_value) const {
142 return CanAccessPath(path) && data_->GetInteger(path, out_value);
145 bool Manifest::GetString(
146 const std::string& path, std::string* out_value) const {
147 if (!CanAccessPath(path))
150 if (i18n_data_->Get(path, NULL)) {
151 List::const_iterator it = user_agent_locales_->begin();
152 for (; it != user_agent_locales_->end(); ++it) {
153 if (i18n_data_->GetString(GetLocalizedKey(path, *it), out_value))
159 return data_->GetString(path, out_value);
162 bool Manifest::GetString(
163 const std::string& path, base::string16* out_value) const {
164 if (!CanAccessPath(path))
167 if (i18n_data_->Get(path, NULL)) {
168 List::const_iterator it = user_agent_locales_->begin();
169 for (; it != user_agent_locales_->end(); ++it) {
170 if (i18n_data_->GetString(GetLocalizedKey(path, *it), out_value))
176 return data_->GetString(path, out_value);
179 bool Manifest::GetDictionary(
180 const std::string& path, const base::DictionaryValue** out_value) const {
181 return CanAccessPath(path) && data_->GetDictionary(path, out_value);
184 bool Manifest::GetList(
185 const std::string& path, const base::ListValue** out_value) const {
186 return CanAccessPath(path) && data_->GetList(path, out_value);
189 Manifest* Manifest::DeepCopy() const {
190 Manifest* manifest = new Manifest(
191 source_type_, scoped_ptr<base::DictionaryValue>(data_->DeepCopy()));
192 manifest->SetApplicationID(application_id_);
196 bool Manifest::Equals(const Manifest* other) const {
197 return other && data_->Equals(other->value());
200 int Manifest::GetManifestVersion() const {
201 int manifest_version = 1;
202 data_->GetInteger(keys::kManifestVersionKey, &manifest_version);
203 return manifest_version;
206 bool Manifest::CanAccessPath(const std::string& path) const {
210 bool Manifest::CanAccessKey(const std::string& key) const {
214 void Manifest::SetSystemLocale(const std::string& locale) {
215 scoped_ptr<List> list_for_expand(new List);
216 list_for_expand->push_back(locale);
217 list_for_expand->push_back(default_locale_);
218 list_for_expand->push_back(kLocaleUnlocalized);
219 list_for_expand->push_back(kLocaleAuto);
220 list_for_expand->push_back(kLocaleFirstOne);
221 user_agent_locales_ = ExpandUserAgentLocalesList(list_for_expand);
224 void Manifest::ParseWGTI18n() {
225 data_->GetString(application_widget_keys::kDefaultLocaleKey,
227 default_locale_ = StringToLowerASCII(default_locale_);
229 ParseWGTI18nEachPath(kWidgetNamePath);
230 ParseWGTI18nEachPath(kWidgetDecriptionPath);
231 ParseWGTI18nEachPath(kWidgetLicensePath);
234 // We might get one element of a list of element from path,
235 // and we parse each element for fast access.
236 // For example config.xml is:
238 // <name>unlocalized name</name>
239 // <name xml:lang="zh-CN">zh-CN name</name>
240 // <name xml:lang="en-US" short="en-US short">en-US name</name>
242 // The path for value in i18n_data_ are :
243 // "widget.name.#text.@unlocalized" => "unlocalized name".
244 // "widget.name.#text.zh-cn" => "zh-CN name".
245 // "widget.name.#text.en-us" => "en-US name".
246 // "widget.name.@short.en-us" => "en-US short".
247 // "widget.name.#text.*" => "unlocalized name". (the first one)
248 // "widget.name.@short.*" => "". (the first one do not have a short name)
249 void Manifest::ParseWGTI18nEachPath(const std::string& path) {
250 base::Value* value = NULL;
251 if (!data_->Get(path, &value))
254 if (value->IsType(base::Value::TYPE_DICTIONARY)) {
255 ParseWGTI18nEachElement(value, path);
256 ParseWGTI18nEachElement(value, path, kLocaleFirstOne);
257 } else if (value->IsType(base::Value::TYPE_LIST)) {
258 base::ListValue* list;
259 value->GetAsList(&list);
261 bool get_first_one = false;
262 for (base::ListValue::iterator it = list->begin();
263 it != list->end(); ++it) {
264 ParseWGTI18nEachElement(*it, path);
266 get_first_one = ParseWGTI18nEachElement(*it, path, kLocaleFirstOne);
271 bool Manifest::ParseWGTI18nEachElement(base::Value* value,
272 const std::string& path,
273 const std::string& locale) {
274 base::DictionaryValue* dict;
275 if (!value->GetAsDictionary(&dict))
278 std::string xml_lang(locale);
280 dict->GetString(application_widget_keys::kXmlLangKey, &xml_lang);
282 base::DictionaryValue::Iterator iter(*dict);
283 while (!iter.IsAtEnd()) {
284 std::string locale_key(
285 GetLocalizedKey(path + kPathConnectSymbol + iter.key(), xml_lang));
286 if (!i18n_data_->Get(locale_key, NULL))
287 i18n_data_->Set(locale_key, iter.value().DeepCopy());
295 } // namespace application