5feaeafa4cb8d71f25b328647097719b3acd424b
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / manifest.cc
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.
4
5 #include "xwalk/application/common/manifest.h"
6
7 #include <list>
8
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/common/xwalk_system_locale.h"
18
19 namespace errors = xwalk::application_manifest_errors;
20 namespace keys   = xwalk::application_manifest_keys;
21 namespace widget_keys = xwalk::application_widget_keys;
22
23 namespace xwalk {
24 namespace application {
25 namespace {
26 const char kLocaleUnlocalized[] = "@unlocalized";
27 #if defined(OS_TIZEN)
28 const char kLocaleAuto[] = "en-gb";
29 #else
30 const char kLocaleAuto[] = "en-us";
31 #endif
32 const char kLocaleFirstOne[] = "*";
33
34 const char kWidgetNamePath[] = "widget.name";
35 const char kWidgetDecriptionPath[] = "widget.description";
36 const char kWidgetLicensePath[] = "widget.license";
37
38 const char kPathConnectSymbol = '.';
39
40 typedef std::list<std::string> List;
41
42 std::string GetLocalizedKey(const std::string& key,
43                             const std::string& local) {
44   std::string lower_local = base::StringToLowerASCII(local);
45   if (lower_local.empty())
46     lower_local = kLocaleUnlocalized;
47   return key + kPathConnectSymbol + lower_local;
48 }
49
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);
54     size_t position;
55     do {
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);
60   }
61   return expansion_list.Pass();
62 }
63
64 }  // namespace
65
66 Manifest::Manifest(SourceType source_type,
67         scoped_ptr<base::DictionaryValue> value)
68     : source_type_(source_type),
69       data_(value.Pass()),
70       i18n_data_(new base::DictionaryValue),
71       type_(TYPE_UNKNOWN) {
72   // FIXME: Hosted apps can contain start_url. Below is wrong.
73   if (data_->Get(keys::kStartURLKey, NULL)) {
74     type_ = TYPE_PACKAGED_APP;
75   } else if (data_->HasKey(keys::kAppKey)) {
76     if (data_->Get(keys::kLaunchWebURLKey, NULL)) {
77       type_ = TYPE_HOSTED_APP;
78     } else if (data_->Get(keys::kLaunchLocalPathKey, NULL)) {
79       type_ = TYPE_PACKAGED_APP;
80     }
81 #if defined(OS_TIZEN)
82   } else if (HasPath(widget_keys::kContentNamespace)) {
83     std::string ns;
84     if (data_->GetString(widget_keys::kContentNamespace, &ns) &&
85         ns == kTizenNamespacePrefix)
86       type_ = TYPE_HOSTED_APP;
87     else
88       type_ = TYPE_PACKAGED_APP;
89 #endif
90   }
91
92   if (data_->HasKey(widget_keys::kWidgetKey) &&
93       data_->Get(widget_keys::kWidgetKey, NULL))
94     ParseWGTI18n();
95
96   // FIXME: Sounds like a setter calling a getter for the same value.
97   SetSystemLocale(GetSystemLocale());
98 }
99
100 Manifest::~Manifest() {
101 }
102
103 bool Manifest::ValidateManifest(
104     std::string* error) const {
105   // TODO(xiang): support features validation
106   return true;
107 }
108
109 bool Manifest::HasKey(const std::string& key) const {
110   return CanAccessKey(key) && data_->HasKey(key);
111 }
112
113 bool Manifest::HasPath(const std::string& path) const {
114   base::Value* ignored = NULL;
115   return CanAccessPath(path) && data_->Get(path, &ignored);
116 }
117
118 bool Manifest::Get(
119     const std::string& path, const base::Value** out_value) const {
120   return CanAccessPath(path) && data_->Get(path, out_value);
121 }
122
123 bool Manifest::Get(
124     const std::string& path, base::Value** out_value) const {
125   return this->Get(
126       path,
127       const_cast<const base::Value**>(out_value));
128 }
129
130 bool Manifest::GetBoolean(
131     const std::string& path, bool* out_value) const {
132   return CanAccessPath(path) && data_->GetBoolean(path, out_value);
133 }
134
135 bool Manifest::GetInteger(
136     const std::string& path, int* out_value) const {
137   return CanAccessPath(path) && data_->GetInteger(path, out_value);
138 }
139
140 bool Manifest::GetString(
141     const std::string& path, std::string* out_value) const {
142   if (!CanAccessPath(path))
143     return false;
144
145   if (i18n_data_->Get(path, NULL)) {
146     List::const_iterator it = user_agent_locales_->begin();
147     for (; it != user_agent_locales_->end(); ++it) {
148       if (i18n_data_->GetString(GetLocalizedKey(path, *it), out_value))
149         return true;
150     }
151     return false;
152   }
153
154   return data_->GetString(path, out_value);
155 }
156
157 bool Manifest::GetString(
158     const std::string& path, base::string16* out_value) const {
159   if (!CanAccessPath(path))
160     return false;
161
162   if (i18n_data_->Get(path, NULL)) {
163     List::const_iterator it = user_agent_locales_->begin();
164     for (; it != user_agent_locales_->end(); ++it) {
165       if (i18n_data_->GetString(GetLocalizedKey(path, *it), out_value))
166         return true;
167     }
168     return false;
169   }
170
171   return data_->GetString(path, out_value);
172 }
173
174 bool Manifest::GetDictionary(
175     const std::string& path, const base::DictionaryValue** out_value) const {
176   return CanAccessPath(path) && data_->GetDictionary(path, out_value);
177 }
178
179 bool Manifest::GetList(
180     const std::string& path, const base::ListValue** out_value) const {
181   return CanAccessPath(path) && data_->GetList(path, out_value);
182 }
183
184 Manifest* Manifest::DeepCopy() const {
185   Manifest* manifest = new Manifest(
186       source_type_, scoped_ptr<base::DictionaryValue>(data_->DeepCopy()));
187   manifest->SetApplicationID(application_id_);
188   return manifest;
189 }
190
191 bool Manifest::Equals(const Manifest* other) const {
192   return other && data_->Equals(other->value());
193 }
194
195 bool Manifest::CanAccessPath(const std::string& path) const {
196   return true;
197 }
198
199 bool Manifest::CanAccessKey(const std::string& key) const {
200   return true;
201 }
202
203 void Manifest::SetSystemLocale(const std::string& locale) {
204   scoped_ptr<List> list_for_expand(new List);
205   list_for_expand->push_back(locale);
206   list_for_expand->push_back(default_locale_);
207   list_for_expand->push_back(kLocaleUnlocalized);
208   list_for_expand->push_back(kLocaleAuto);
209   list_for_expand->push_back(kLocaleFirstOne);
210   user_agent_locales_ = ExpandUserAgentLocalesList(list_for_expand);
211 }
212
213 void Manifest::ParseWGTI18n() {
214   data_->GetString(application_widget_keys::kDefaultLocaleKey,
215                    &default_locale_);
216   default_locale_ = base::StringToLowerASCII(default_locale_);
217
218   ParseWGTI18nEachPath(kWidgetNamePath);
219   ParseWGTI18nEachPath(kWidgetDecriptionPath);
220   ParseWGTI18nEachPath(kWidgetLicensePath);
221 }
222
223 // We might get one element of a list of element from path,
224 // and we parse each element for fast access.
225 // For example config.xml is:
226 // <widget>
227 //   <name>unlocalized name</name>
228 //   <name xml:lang="zh-CN">zh-CN name</name>
229 //   <name xml:lang="en-US" short="en-US short">en-US name</name>
230 // </widget>
231 // The path for value in i18n_data_ are :
232 // "widget.name.#text.@unlocalized" => "unlocalized name".
233 // "widget.name.#text.zh-cn" => "zh-CN name".
234 // "widget.name.#text.en-us" => "en-US name".
235 // "widget.name.@short.en-us" => "en-US short".
236 // "widget.name.#text.*" => "unlocalized name". (the first one)
237 // "widget.name.@short.*" => "". (the first one do not have a short name)
238 void Manifest::ParseWGTI18nEachPath(const std::string& path) {
239   base::Value* value = NULL;
240   if (!data_->Get(path, &value))
241     return;
242
243   if (value->IsType(base::Value::TYPE_DICTIONARY)) {
244     ParseWGTI18nEachElement(value, path);
245     ParseWGTI18nEachElement(value, path, kLocaleFirstOne);
246   } else if (value->IsType(base::Value::TYPE_LIST)) {
247     base::ListValue* list;
248     value->GetAsList(&list);
249
250     bool get_first_one = false;
251     for (base::ListValue::iterator it = list->begin();
252         it != list->end(); ++it) {
253       ParseWGTI18nEachElement(*it, path);
254       if (!get_first_one)
255         get_first_one = ParseWGTI18nEachElement(*it, path, kLocaleFirstOne);
256     }
257   }
258 }
259
260 bool Manifest::ParseWGTI18nEachElement(base::Value* value,
261                                        const std::string& path,
262                                        const std::string& locale) {
263   base::DictionaryValue* dict;
264   if (!value->GetAsDictionary(&dict))
265     return false;
266
267   std::string xml_lang(locale);
268   if (locale.empty())
269     dict->GetString(application_widget_keys::kXmlLangKey, &xml_lang);
270
271   base::DictionaryValue::Iterator iter(*dict);
272   while (!iter.IsAtEnd()) {
273     std::string locale_key(
274         GetLocalizedKey(path + kPathConnectSymbol + iter.key(), xml_lang));
275     if (!i18n_data_->Get(locale_key, NULL))
276       i18n_data_->Set(locale_key, iter.value().DeepCopy());
277
278     iter.Advance();
279   }
280
281   return true;
282 }
283
284 }  // namespace application
285 }  // namespace xwalk