fc6f2cc350a9737e3f1fa51fecc7444368bc8a05
[platform/framework/web/crosswalk-tizen.git] / src / common / app_control.cc
1 // Copyright 2015 Samsung Electronics Co, Ltd. 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 "common/app_control.h"
6
7 #include <appsvc.h>
8 #include <algorithm>
9 #include <memory>
10
11 #include "common/logger.h"
12
13 namespace wrt {
14
15 namespace {
16 static bool BundleAddData(bundle* target, const std::string& key,
17                           const std::string& value) {
18   int result = appsvc_add_data(target, key.c_str(), value.c_str());
19   if (result < 0) {
20     LOGGER(ERROR) << "Failed to add data to appsvc.";
21     return false;
22   } else {
23     return true;
24   }
25 }
26
27 static bool BundleAddDataArray(bundle* target, const std::string& key,
28                                const std::vector<std::string>& value_array) {
29   int n = value_array.size();
30   std::vector<const char*> v;
31   std::for_each(value_array.begin(), value_array.end(),
32                 [&v] (std::string str) {
33                   v.push_back(static_cast<const char*>(str.c_str()));
34                 });
35
36   int result = appsvc_add_data_array(target, key.c_str(),
37                                      v.data(), n);
38   if (result < 0) {
39     LOGGER(ERROR) << "Failed to add an array of data to appsvc.";
40     return false;
41   } else {
42     return true;
43   }
44 }
45
46
47 }  // namespace
48
49 AppControl::AppControl(app_control_h app_control) {
50   app_control_clone(&app_control_, app_control);
51   app_control_to_bundle(app_control_, &app_control_bundle_);
52 }
53
54 AppControl:: AppControl() {
55   app_control_create(&app_control_);
56   app_control_to_bundle(app_control_, &app_control_bundle_);
57 }
58
59 AppControl::~AppControl() {
60   if (app_control_ != NULL) {
61     app_control_destroy(app_control_);
62   }
63 }
64
65 std::string AppControl::operation() const {
66   const char* operation = appsvc_get_operation(app_control_bundle_);
67
68   if (operation != NULL) {
69     return std::string(operation);
70   } else {
71     return std::string();
72   }
73 }
74
75 void AppControl::set_operation(const std::string& operation) {
76   appsvc_set_operation(app_control_bundle_, operation.c_str());
77 }
78
79 std::string AppControl::mime() const {
80   const char* mime = appsvc_get_mime(app_control_bundle_);
81
82   if (mime != NULL) {
83     return std::string(mime);
84   } else {
85     return std::string();
86   }
87 }
88
89 void AppControl::set_mime(const std::string& mime) {
90   appsvc_set_mime(app_control_bundle_, mime.c_str());
91 }
92
93 std::string AppControl::uri() const {
94   const char* uri = appsvc_get_uri(app_control_bundle_);
95
96   if (uri != NULL) {
97     return std::string(uri);
98   } else {
99     return std::string();
100   }
101 }
102
103 void AppControl::set_uri(const std::string& uri) {
104   appsvc_set_uri(app_control_bundle_, uri.c_str());
105 }
106
107 std::string AppControl::category() const {
108   const char* category = appsvc_get_category(app_control_bundle_);
109
110   if (category != NULL) {
111     return std::string(category);
112   } else {
113     return std::string();
114   }
115 }
116
117 void AppControl::set_category(const std::string& category) {
118   appsvc_set_category(app_control_bundle_, category.c_str());
119 }
120
121 std::string AppControl::data(const std::string& key) const {
122   const char* data = appsvc_get_data(app_control_bundle_, key.c_str());
123
124   if (data != NULL) {
125     return std::string(data);
126   } else {
127     return std::string();
128   }
129 }
130
131 std::vector<std::string> AppControl::data_array(const std::string& key) const {
132   int data_array_len = 0;
133   const char** data_array = appsvc_get_data_array(app_control_bundle_,
134                                                   key.c_str(), &data_array_len);
135   std::vector<std::string> data_vector;
136
137   if (data_array_len > 0) {
138     for (int i = 0; i < data_array_len; i++) {
139       data_vector.push_back(data_array[i]);
140     }
141   }
142   return data_vector;
143 }
144
145 std::string AppControl::encoded_bundle() {
146   bundle_raw* encoded_data;
147   int len;
148   bundle_encode(app_control_bundle_, &encoded_data, &len);
149   std::unique_ptr<bundle_raw*, decltype(bundle_free_encoded_rawdata)*>
150     ptr { &encoded_data, bundle_free_encoded_rawdata};
151   return std::string(reinterpret_cast<char*>(encoded_data), len);
152 }
153
154 bool AppControl::IsDataArray(const std::string& key) {
155   return appsvc_data_is_array(app_control_bundle_, key.c_str());
156 }
157
158 bool AppControl::AddData(const std::string& key, const std::string& value) {
159   return BundleAddData(app_control_bundle_, key, value);
160 }
161
162
163
164 bool AppControl::AddDataArray(const std::string& key,
165                               const std::vector<std::string>& value_array) {
166   return BundleAddDataArray(app_control_bundle_, key, value_array);
167 }
168
169
170 bool AppControl::Reply(const std::map<std::string,
171                                       std::vector<std::string>>& data) {
172   bundle* result;
173   if (appsvc_create_result_bundle(app_control_bundle_,
174                                   &result) != APPSVC_RET_OK) {
175     LOGGER(ERROR) << "Failed to craete result bundle.";
176     return false;
177   }
178   auto it = data.begin();
179   for ( ; it != data.end(); ++it) {
180     const std::string& key = it->first;
181     if (it->second.size() == 1) {
182       BundleAddData(result, key, it->second[0]);
183     } else {
184       BundleAddDataArray(result, key, it->second);
185     }
186   }
187
188   int ret = appsvc_send_result(result, APPSVC_RES_OK);
189   bundle_free(result);
190
191   return ret == APPSVC_RET_OK ? true : false;
192 }
193
194 bool AppControl::LaunchRequest() {
195   return (app_control_send_launch_request(app_control_, NULL, NULL) ==
196           APP_CONTROL_ERROR_NONE);
197 }
198
199 }  // namespace wrt