Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / tizen / application_storage_impl.cc
1 // Copyright (c) 2013 Intel Corporation. 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/tizen/application_storage_impl.h"
6
7 #include <ail.h>
8 #include <pkgmgr-info.h>
9
10 #include <string>
11 #include <vector>
12
13 #include "base/file_util.h"
14 #include "third_party/re2/re2/re2.h"
15 #include "xwalk/application/common/application_file_util.h"
16 #include "xwalk/application/common/id_util.h"
17 #include "xwalk/application/common/tizen/application_storage.h"
18 #include "xwalk/application/common/tizen/package_query.h"
19
20 namespace {
21
22 ail_cb_ret_e appinfo_get_app_id_cb(
23     const ail_appinfo_h appinfo, void* user_data, uid_t /*uid*/) {
24   std::vector<std::string>* app_ids =
25     static_cast<std::vector<std::string>*>(user_data);
26   char* app_id;
27   ail_appinfo_get_str(appinfo, AIL_PROP_X_SLP_APPID_STR, &app_id);
28   if (app_id)
29     app_ids->push_back(app_id);
30
31   return AIL_CB_RET_CONTINUE;
32 }
33
34 const char kXWalkPackageType[] = "wgt";
35
36 bool GetPackageType(const std::string& application_id,
37                     xwalk::application::Manifest::Type* package_type) {
38   if (xwalk::application::IsValidWGTID(application_id)) {
39     *package_type = xwalk::application::Manifest::TYPE_WIDGET;
40     return true;
41   }
42
43   if (xwalk::application::IsValidXPKID(application_id)) {
44     *package_type = xwalk::application::Manifest::TYPE_MANIFEST;
45     return true;
46   }
47
48   return false;
49 }
50
51 }  // namespace
52
53 namespace xwalk {
54 namespace application {
55
56 ApplicationStorageImpl::ApplicationStorageImpl(const base::FilePath& path) {
57 }
58
59 ApplicationStorageImpl::~ApplicationStorageImpl() {
60 }
61
62 bool ApplicationStorageImpl::Init() {
63   return true;
64 }
65
66 namespace {
67
68 bool GetManifestType(const std::string& app_id, Manifest::Type* manifest_type) {
69   if (IsValidWGTID(app_id)) {
70     *manifest_type = Manifest::TYPE_WIDGET;
71     return true;
72   }
73
74   if (IsValidXPKID(app_id)) {
75     *manifest_type = Manifest::TYPE_MANIFEST;
76     return true;
77   }
78
79   return false;
80 }
81
82 }  // namespace
83
84 scoped_refptr<ApplicationData> ApplicationStorageImpl::GetApplicationData(
85     const std::string& app_id) {
86   base::FilePath app_path = GetApplicationPath(app_id);
87
88   Manifest::Type manifest_type;
89   if (!GetManifestType(app_id, &manifest_type)) {
90     LOG(ERROR) << "Failed to detect the manifest type from app id "
91                << app_id;
92     return NULL;
93   }
94
95   std::string error;
96   scoped_refptr<ApplicationData> app_data =
97      LoadApplication(
98          app_path, app_id, ApplicationData::INTERNAL, manifest_type, &error);
99   if (!app_data.get())
100     LOG(ERROR) << "Error occurred while trying to load application: " << error;
101
102   return app_data;
103 }
104
105 bool ApplicationStorageImpl::GetInstalledApplicationIDs(
106   std::vector<std::string>& app_ids) {  // NOLINT
107   ail_filter_h filter;
108   int count;
109   uid_t uid = getuid();
110
111   ail_error_e ret = ail_filter_new(&filter);
112   if (ret != AIL_ERROR_OK) {
113     LOG(ERROR) << "Failed to create AIL filter.";
114     return false;
115   }
116   // Filters out web apps (installed from WGT and XPK packages).
117   ret = ail_filter_add_str(
118       filter, AIL_PROP_X_SLP_PACKAGETYPE_STR, kXWalkPackageType);
119   if (ret != AIL_ERROR_OK) {
120     LOG(ERROR) << "Failed to init AIL filter.";
121     ail_filter_destroy(filter);
122     return false;
123   }
124
125   if (uid != GLOBAL_USER)
126     ret = ail_filter_count_usr_appinfo(filter, &count, uid);
127   else
128     ret = ail_filter_count_appinfo(filter, &count);
129
130   if (ret != AIL_ERROR_OK) {
131     LOG(ERROR) << "Failed to count AIL app info.";
132     ail_filter_destroy(filter);
133     return false;
134   }
135
136   if (count > 0) {
137     if (uid != GLOBAL_USER)
138       ail_filter_list_usr_appinfo_foreach(filter, appinfo_get_app_id_cb,
139           &app_ids, uid);
140     else
141       ail_filter_list_appinfo_foreach(filter, appinfo_get_app_id_cb, &app_ids);
142   }
143
144   ail_filter_destroy(filter);
145   return true;
146 }
147
148 bool ApplicationStorageImpl::AddApplication(const ApplicationData* application,
149                                             const base::Time& install_time) {
150   return true;
151 }
152
153 bool ApplicationStorageImpl::UpdateApplication(
154     ApplicationData* application, const base::Time& install_time) {
155   return true;
156 }
157
158 bool ApplicationStorageImpl::RemoveApplication(const std::string& id) {
159   return true;
160 }
161
162 bool ApplicationStorageImpl::ContainsApplication(const std::string& app_id) {
163   return !GetApplicationPath(app_id).empty();
164 }
165
166 }  // namespace application
167 }  // namespace xwalk