Upstream version 11.39.260.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / manifest_handlers / tizen_application_handler.cc
1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include "xwalk/application/common/manifest_handlers/tizen_application_handler.h"
7
8 #include <map>
9 #include <utility>
10
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/version.h"
14 #include "third_party/re2/re2/re2.h"
15 #include "xwalk/application/common/application_manifest_constants.h"
16 #include "xwalk/application/common/constants.h"
17
18 namespace xwalk {
19
20 namespace keys = application_widget_keys;
21
22 namespace application {
23
24 TizenApplicationInfo::TizenApplicationInfo() {
25 }
26
27 TizenApplicationInfo::~TizenApplicationInfo() {
28 }
29
30 TizenApplicationHandler::TizenApplicationHandler() {}
31
32 TizenApplicationHandler::~TizenApplicationHandler() {}
33
34 bool TizenApplicationHandler::Parse(scoped_refptr<ApplicationData> application,
35                                     base::string16* error) {
36   scoped_ptr<TizenApplicationInfo> app_info(new TizenApplicationInfo);
37   const Manifest* manifest = application->GetManifest();
38   DCHECK(manifest);
39
40   base::Value* app_value = NULL;
41   manifest->Get(keys::kTizenApplicationKey, &app_value);
42   // Find an application element with tizen namespace
43   base::DictionaryValue* app_dict;
44   std::string value;
45   bool find = false;
46   if (app_value && app_value->IsType(base::Value::TYPE_DICTIONARY)) {
47     app_value->GetAsDictionary(&app_dict);
48     find = app_dict->GetString(keys::kNamespaceKey, &value);
49     find = find && (value == keys::kTizenNamespacePrefix);
50   } else if (app_value && app_value->IsType(base::Value::TYPE_LIST)) {
51     base::ListValue* list;
52     app_value->GetAsList(&list);
53     for (base::ListValue::iterator it = list->begin();
54          it != list->end(); ++it) {
55       (*it)->GetAsDictionary(&app_dict);
56       find = app_dict->GetString(keys::kNamespaceKey, &value);
57       find = find && (value == keys::kTizenNamespacePrefix);
58       if (find)
59         break;
60     }
61   }
62
63   if (!find) {
64     *error = base::ASCIIToUTF16(
65         "Cannot find application element with tizen namespace"
66         " or the tizen namespace prefix is incorrect.\n");
67     return false;
68   }
69   if (app_dict->GetString(keys::kTizenApplicationIdKey, &value))
70     app_info->set_id(value);
71   if (app_dict->GetString(keys::kTizenApplicationPackageKey, &value))
72     app_info->set_package(value);
73   if (app_dict->GetString(keys::kTizenApplicationRequiredVersionKey, &value))
74     app_info->set_required_version(value);
75
76   application->SetManifestData(keys::kTizenApplicationKey,
77                                app_info.release());
78   return true;
79 }
80
81 bool TizenApplicationHandler::Validate(
82     scoped_refptr<const ApplicationData> application,
83     std::string* error) const {
84   const TizenApplicationInfo* app_info =
85       static_cast<const TizenApplicationInfo*>(
86           application->GetManifestData(keys::kTizenApplicationKey));
87
88   const char kIdPattern[] = "\\A[0-9a-zA-Z]{10}[.][0-9a-zA-Z]{1,52}\\z";
89   const char kPackagePattern[] = "\\A[0-9a-zA-Z]{10}\\z";
90   if (!RE2::PartialMatch(app_info->id(), kIdPattern)) {
91     *error = std::string("The id property of application element"
92                          " does not match the format\n");
93     return false;
94   }
95   if (!RE2::PartialMatch(app_info->package(), kPackagePattern)) {
96     *error = std::string("The package property of application element"
97                          " does not match the format\n");
98     return false;
99   }
100   if (app_info->id().find(app_info->package()) != 0) {
101     *error = std::string("The application element property id"
102                          " does not start with package.\n");
103     return false;
104   }
105   if (app_info->required_version().empty()) {
106     *error = std::string("The required_version property of application"
107                          " element does not exist.\n");
108     return false;
109   }
110
111   const base::Version supported_version = Version(kTizenWebAPIVersion);
112   if (supported_version.IsOlderThan(app_info->required_version())) {
113     *error = std::string("The required_version of Tizen Web API"
114                          " is not supported.\n");
115     return false;
116   }
117
118   return true;
119 }
120
121 std::vector<std::string> TizenApplicationHandler::Keys() const {
122   return std::vector<std::string>(1, keys::kTizenApplicationKey);
123 }
124
125 bool TizenApplicationHandler::AlwaysParseForType(Manifest::Type type) const {
126   return true;
127 }
128
129 }  // namespace application
130 }  // namespace xwalk