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