Upstream version 9.38.207.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / application_data.cc
1 // Copyright (c) 2013 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/application_data.h"
6
7 #include "base/base64.h"
8 #include "base/basictypes.h"
9 #include "base/command_line.h"
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/i18n/rtl.h"
13 #include "base/logging.h"
14 #include "base/memory/singleton.h"
15 #include "base/stl_util.h"
16 #include "base/strings/string16.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/string_piece.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/values.h"
23 #include "base/version.h"
24 #include "xwalk/application/common/application_manifest_constants.h"
25 #include "xwalk/application/common/id_util.h"
26 #include "xwalk/application/common/constants.h"
27 #include "xwalk/application/common/manifest.h"
28 #include "xwalk/application/common/manifest_handler.h"
29 #include "xwalk/application/common/manifest_handlers/permissions_handler.h"
30 #include "xwalk/application/common/manifest_handlers/widget_handler.h"
31 #include "xwalk/application/common/manifest_handlers/tizen_application_handler.h"
32 #include "xwalk/application/common/permission_policy_manager.h"
33 #include "content/public/common/url_constants.h"
34 #include "url/url_util.h"
35 #include "ui/base/l10n/l10n_util.h"
36
37 namespace keys = xwalk::application_manifest_keys;
38 namespace widget_keys = xwalk::application_widget_keys;
39 namespace errors = xwalk::application_manifest_errors;
40
41 namespace xwalk {
42 namespace application {
43
44 // static
45 scoped_refptr<ApplicationData> ApplicationData::Create(
46     const base::FilePath& path, const std::string& explicit_id,
47     SourceType source_type, scoped_ptr<Manifest> manifest,
48     std::string* error_message) {
49   DCHECK(error_message);
50   base::string16 error;
51   if (!manifest->ValidateManifest(error_message))
52     return NULL;
53
54   scoped_refptr<ApplicationData> app_data =
55       new ApplicationData(path, source_type, manifest.Pass());
56   if (!app_data->Init(explicit_id, &error)) {
57     *error_message = base::UTF16ToUTF8(error);
58     return NULL;
59   }
60
61   ManifestHandlerRegistry* registry =
62       ManifestHandlerRegistry::GetInstance(app_data->manifest_type());
63
64   if (!registry->ValidateAppManifest(app_data, error_message))
65     return NULL;
66
67   return app_data;
68 }
69
70 // static
71 GURL ApplicationData::GetBaseURLFromApplicationId(
72     const std::string& application_id) {
73   return GURL(std::string(kApplicationScheme) +
74       url::kStandardSchemeSeparator + application_id + "/");
75 }
76
77 ApplicationData::ManifestData* ApplicationData::GetManifestData(
78         const std::string& key) const {
79   DCHECK(finished_parsing_manifest_ || thread_checker_.CalledOnValidThread());
80   ManifestDataMap::const_iterator iter = manifest_data_.find(key);
81   if (iter != manifest_data_.end())
82     return iter->second.get();
83   return NULL;
84 }
85
86 void ApplicationData::SetManifestData(const std::string& key,
87                                       ApplicationData::ManifestData* data) {
88   DCHECK(!finished_parsing_manifest_ && thread_checker_.CalledOnValidThread());
89   manifest_data_[key] = linked_ptr<ManifestData>(data);
90 }
91
92 #if defined(OS_TIZEN)
93 std::string ApplicationData::GetPackageID() const {
94   return AppIdToPkgId(application_id_);
95 }
96 #endif
97
98 const std::string ApplicationData::VersionString() const {
99   if (!version_->components().empty())
100     return Version()->GetString();
101
102   return "";
103 }
104
105 bool ApplicationData::IsHostedApp() const {
106   bool hosted = source_type_ == EXTERNAL_URL;
107 #if defined(OS_TIZEN)
108   if (manifest_->HasPath(widget_keys::kContentNamespace)) {
109     std::string ns;
110     if (manifest_->GetString(widget_keys::kContentNamespace, &ns) &&
111         ns == kTizenNamespacePrefix)
112       hosted = true;
113   }
114 #endif
115   return hosted;
116 }
117
118 ApplicationData::ApplicationData(const base::FilePath& path,
119   SourceType source_type, scoped_ptr<Manifest> manifest)
120     : manifest_version_(0),
121       path_(path),
122       manifest_(manifest.release()),
123       finished_parsing_manifest_(false),
124       source_type_(source_type) {
125   DCHECK(path_.empty() || path_.IsAbsolute());
126 }
127
128 ApplicationData::~ApplicationData() {
129 }
130
131 // static
132 GURL ApplicationData::GetResourceURL(const GURL& application_url,
133                                const std::string& relative_path) {
134   DCHECK(application_url.SchemeIs(kApplicationScheme));
135   DCHECK_EQ("/", application_url.path());
136
137   std::string path = relative_path;
138
139   // If the relative path starts with "/", it is "absolute" relative to the
140   // application base directory, but application_url is already specified to
141   // refer to that base directory, so strip the leading "/" if present.
142   if (relative_path.size() > 0 && relative_path[0] == '/')
143     path = relative_path.substr(1);
144
145   GURL ret_val = GURL(application_url.spec() + path);
146   DCHECK(StartsWithASCII(ret_val.spec(), application_url.spec(), false));
147
148   return ret_val;
149 }
150
151 bool ApplicationData::Init(const std::string& explicit_id,
152                            base::string16* error) {
153   DCHECK(error);
154   ManifestHandlerRegistry* registry =
155       ManifestHandlerRegistry::GetInstance(manifest_type());
156   if (!registry->ParseAppManifest(this, error))
157     return false;
158
159   if (!LoadID(explicit_id, error))
160     return false;
161   if (!LoadName(error))
162     return false;
163   if (!LoadVersion(error))
164     return false;
165   if (!LoadDescription(error))
166     return false;
167
168   application_url_ = ApplicationData::GetBaseURLFromApplicationId(ID());
169
170   finished_parsing_manifest_ = true;
171   return true;
172 }
173
174 bool ApplicationData::LoadID(const std::string& explicit_id,
175                              base::string16* error) {
176   std::string application_id;
177 #if defined(OS_TIZEN)
178   if (manifest_type() == Manifest::TYPE_WIDGET) {
179     const TizenApplicationInfo* tizen_app_info =
180         static_cast<TizenApplicationInfo*>(GetManifestData(
181             widget_keys::kTizenApplicationKey));
182     CHECK(tizen_app_info);
183     application_id = tizen_app_info->id();
184   } else if (manifest_->HasKey(keys::kTizenAppIdKey)) {
185     if (!manifest_->GetString(keys::kTizenAppIdKey, &application_id)) {
186       NOTREACHED() << "Could not get Tizen application key";
187       return false;
188     }
189   }
190
191   if (!application_id.empty()) {
192     application_id_ = application_id;
193     return true;
194   }
195 #endif
196
197   if (!explicit_id.empty()) {
198     application_id_ = explicit_id;
199     return true;
200   }
201
202   application_id = GenerateIdForPath(path_);
203   if (application_id.empty()) {
204     NOTREACHED() << "Could not create ID from path.";
205     return false;
206   }
207   application_id_ = application_id;
208   return true;
209 }
210
211 bool ApplicationData::LoadName(base::string16* error) {
212   DCHECK(error);
213   base::string16 localized_name;
214   std::string name_key(GetNameKey(manifest_type()));
215
216   if (!manifest_->GetString(name_key, &localized_name) &&
217       manifest_type() == Manifest::TYPE_MANIFEST) {
218     *error = base::ASCIIToUTF16(errors::kInvalidName);
219     return false;
220   }
221   non_localized_name_ = base::UTF16ToUTF8(localized_name);
222   base::i18n::AdjustStringForLocaleDirection(&localized_name);
223   name_ = base::UTF16ToUTF8(localized_name);
224   return true;
225 }
226
227 bool ApplicationData::LoadVersion(base::string16* error) {
228   DCHECK(error);
229   std::string version_str;
230
231   version_.reset(new base::Version());
232
233   if (manifest_type() == Manifest::TYPE_WIDGET) {
234     bool ok = manifest_->GetString(widget_keys::kVersionKey, &version_str);
235     if (!ok) {
236       *error = base::ASCIIToUTF16(errors::kInvalidVersion);
237       return true;
238     }
239
240     version_.reset(new base::Version(version_str));
241     return true;
242   }
243
244   // W3C Manifest (XPK and hosted):
245
246   bool hasDeprecatedKey = manifest_->HasKey(keys::kDeprecatedVersionKey);
247   bool hasKey = manifest_->HasKey(keys::kXWalkVersionKey);
248
249   if (!hasKey && !hasDeprecatedKey) {
250     // xwalk_version is optional.
251     return true;
252   }
253
254   bool ok = false;
255   if (hasKey) {
256     if (hasDeprecatedKey) {
257       LOG(WARNING) << "Deprecated key '" << keys::kDeprecatedVersionKey
258           << "' found in addition to '" << keys::kXWalkVersionKey
259           << "'. Consider removing.";
260     }
261     ok = manifest_->GetString(keys::kXWalkVersionKey, &version_str);
262   }
263
264   if (!hasKey && hasDeprecatedKey) {
265     LOG(WARNING) << "Deprecated key '" << keys::kDeprecatedVersionKey
266         << "' found. Please migrate to using '" << keys::kXWalkVersionKey
267         << "' instead.";
268     ok = manifest_->GetString(keys::kDeprecatedVersionKey, &version_str);
269   }
270
271   version_.reset(new base::Version(version_str));
272
273   if (!ok || !version_->IsValid() || version_->components().size() > 4) {
274     *error = base::ASCIIToUTF16(errors::kInvalidVersion);
275     version_.reset(new base::Version());
276     return false;
277   }
278
279   return ok;
280 }
281
282 bool ApplicationData::LoadDescription(base::string16* error) {
283   DCHECK(error);
284   // FIXME: Better to assert on use from Widget.
285   if (manifest_type() != Manifest::TYPE_MANIFEST)
286     return true;  // No error.
287
288   bool hasDeprecatedKey = manifest_->HasKey(keys::kDeprecatedDescriptionKey);
289   bool hasKey = manifest_->HasKey(keys::kXWalkDescriptionKey);
290
291   if (hasKey) {
292     if (hasDeprecatedKey) {
293       LOG(WARNING) << "Deprecated key '" << keys::kDeprecatedDescriptionKey
294           << "' found in addition to '" << keys::kXWalkDescriptionKey
295           << "'. Consider removing.";
296     }
297     bool ok = manifest_->GetString(keys::kXWalkDescriptionKey, &description_);
298     if (!ok)
299       *error = base::ASCIIToUTF16(errors::kInvalidDescription);
300     return ok;
301   }
302
303   if (hasDeprecatedKey) {
304     LOG(WARNING) << "Deprecated key '" << keys::kDeprecatedDescriptionKey
305         << "' found. Please migrate to using '" << keys::kXWalkDescriptionKey
306         << "' instead.";
307     bool ok = manifest_->GetString(
308         keys::kDeprecatedDescriptionKey, &description_);
309     if (!ok)
310       *error = base::ASCIIToUTF16(errors::kInvalidDescription);
311     return ok;
312   }
313
314   // No error but also no description found.
315   return true;
316 }
317
318 StoredPermission ApplicationData::GetPermission(
319     const std::string& permission_name) const {
320   StoredPermissionMap::const_iterator iter =
321       permission_map_.find(permission_name);
322   if (iter == permission_map_.end())
323     return UNDEFINED_STORED_PERM;
324   return iter->second;
325 }
326
327 bool ApplicationData::SetPermission(const std::string& permission_name,
328                                     StoredPermission perm) {
329   if (perm != UNDEFINED_STORED_PERM) {
330     permission_map_[permission_name] = perm;
331     return true;
332   }
333   return false;
334 }
335
336 void ApplicationData::ClearPermissions() {
337   permission_map_.clear();
338 }
339
340 PermissionSet ApplicationData::GetManifestPermissions() const {
341   PermissionSet permissions;
342   if (manifest_->value()->HasKey(keys::kPermissionsKey)) {
343     const PermissionsInfo* perm_info = static_cast<PermissionsInfo*>(
344                            GetManifestData(keys::kPermissionsKey));
345     permissions = perm_info->GetAPIPermissions();
346   }
347   return permissions;
348 }
349
350 bool ApplicationData::HasCSPDefined() const {
351 #if defined(OS_TIZEN)
352   return  manifest_->HasPath(GetCSPKey(manifest_type())) ||
353           manifest_->HasPath(widget_keys::kCSPReportOnlyKey) ||
354           manifest_->HasPath(widget_keys::kAllowNavigationKey);
355 #else
356   return manifest_->HasPath(GetCSPKey(manifest_type()));
357 #endif
358 }
359
360 bool ApplicationData::SetApplicationLocale(const std::string& locale,
361                                            base::string16* error) {
362   DCHECK(thread_checker_.CalledOnValidThread());
363   manifest_->SetSystemLocale(locale);
364   if (!LoadName(error))
365     return false;
366   if (!LoadDescription(error))
367     return false;
368
369   // Only update when the package is wgt and we have parsed the widget handler,
370   // otherwise we can not get widget_info.
371   if (WidgetInfo* widget_info = static_cast<WidgetInfo*>(
372           GetManifestData(widget_keys::kWidgetKey))) {
373     std::string string_value;
374     if (manifest_->GetString(widget_keys::kNameKey, &string_value))
375       widget_info->SetName(string_value);
376     if (manifest_->GetString(widget_keys::kShortNameKey, &string_value))
377       widget_info->SetShortName(string_value);
378     if (manifest_->GetString(widget_keys::kDescriptionKey, &string_value))
379       widget_info->SetDescription(string_value);
380   }
381   return true;
382 }
383
384 }   // namespace application
385 }   // namespace xwalk