2 * Copyright (c) 2015 - 2021 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <bundle_cpp.h>
18 #include <bundle_internal.h>
26 #include "include/aul.h"
27 #include "include/aul_app_com.h"
28 #include "include/aul_cmd.h"
33 class AppComConnection {
35 AppComConnection(std::string endpoint, app_com_cb cb, void* user_data)
36 : endpoint_(std::move(endpoint)),
38 user_data_(user_data) {
41 ~AppComConnection() = default;
43 const std::string& GetEndpoint() const {
47 void Invoke(aul_app_com_result_e result, bundle* envelope) {
49 cb_(endpoint_.c_str(), result, envelope, user_data_);
53 std::string endpoint_;
58 class AppComPermission {
60 AppComPermission() : option_(AUL_APP_COM_PUBLIC) {}
61 ~AppComPermission() {}
63 void SetPrivilege(std::string privilege) {
64 privilege_ = std::move(privilege);
67 void SetPropagationOption(aul_app_com_propagate_option_e option) {
71 const std::string& GetPrivilege() const {
75 aul_app_com_propagate_option_e GetPropagationOption() const {
80 std::string privilege_;
81 aul_app_com_propagate_option_e option_;
89 int Receive(bundle* b) {
90 const char* val = bundle_get_val(b, AUL_K_COM_ENDPOINT);
94 size_t result_size = 0;
95 aul_app_com_result_e* result = nullptr;
96 int ret = bundle_get_byte(b, AUL_K_COM_RESULT,
97 reinterpret_cast<void**>(&result), &result_size);
98 if (ret != BUNDLE_ERROR_NONE)
101 std::string endpoint(val);
103 std::lock_guard<std::recursive_mutex> lock(mutex_);
104 auto iter = conns_.begin();
105 while (iter != conns_.end()) {
108 if (conn->GetEndpoint() == endpoint)
109 conn->Invoke(*result, b);
115 AppComConnection* AddConnection(std::string endpoint, app_com_cb cb,
117 auto* conn = new (std::nothrow) AppComConnection(endpoint, cb, user_data);
121 std::lock_guard<std::recursive_mutex> lock(mutex_);
122 conns_.emplace_back(conn);
126 void RemoveConnection(AppComConnection* conn) {
127 std::lock_guard<std::recursive_mutex> lock(mutex_);
128 auto iter = conns_.begin();
129 while (iter != conns_.end()) {
130 if ((*iter).get() == conn) {
131 iter = conns_.erase(iter);
139 bool ExistConnection(const std::string& endpoint) {
140 std::lock_guard<std::recursive_mutex> lock(mutex_);
141 for (auto& conn : conns_) {
142 if (conn->GetEndpoint() == endpoint)
149 bool ExistConnection(AppComConnection* conn) {
150 std::lock_guard<std::recursive_mutex> lock(mutex_);
151 for (auto& i : conns_) {
160 std::list<std::unique_ptr<AppComConnection>> conns_;
161 mutable std::recursive_mutex mutex_;
168 int app_com_recv(bundle* b) {
172 return context.Receive(b);
175 extern "C" API aul_app_com_permission_h aul_app_com_permission_create() {
176 auto* handle = new (std::nothrow) AppComPermission();
177 return static_cast<aul_app_com_permission_h>(handle);
180 extern "C" API void aul_app_com_permission_destroy(
181 aul_app_com_permission_h permission) {
182 if (permission == nullptr)
185 auto* handle = static_cast<AppComPermission*>(permission);
189 extern "C" API int aul_app_com_permission_set_propagation(
190 aul_app_com_permission_h permission,
191 aul_app_com_propagate_option_e option) {
192 if (permission == nullptr)
195 auto* handle = static_cast<AppComPermission*>(permission);
196 handle->SetPropagationOption(option);
200 extern "C" API int aul_app_com_permission_set_privilege(
201 aul_app_com_permission_h permission, const char* privilege) {
202 if (permission == nullptr || privilege == nullptr)
205 auto* handle = static_cast<AppComPermission*>(permission);
206 handle->SetPrivilege(privilege);
210 static int AppComCreate(const char* endpoint,
211 aul_app_com_permission_h permission, app_com_cb callback, void* user_data,
212 bool sync, aul_app_com_connection_h* connection) {
213 if (endpoint == nullptr || callback == nullptr || connection == nullptr) {
214 _E("Invalid parameter");
218 if (!aul_is_initialized()) {
219 if (aul_launch_init(nullptr, nullptr) < 0)
220 return AUL_R_ENOINIT;
223 tizen_base::Bundle b;
224 b.Add(AUL_K_COM_ENDPOINT, endpoint);
227 auto* handle = static_cast<AppComPermission*>(permission);
228 auto option = handle->GetPropagationOption();
230 auto* p = reinterpret_cast<unsigned char*>(&option);
231 std::vector<unsigned char> bytes;
232 std::copy(p, p + sizeof(option), std::back_inserter(bytes));
233 b.Add(AUL_K_COM_PROPAGATE, bytes);
236 if (!handle->GetPrivilege().empty())
237 b.Add(AUL_K_COM_PRIVILEGE, handle->GetPrivilege());
242 ret = app_send_cmd(AUL_UTIL_PID, APP_COM_CREATE, b.GetHandle());
244 ret = app_send_cmd_with_noreply(AUL_UTIL_PID, APP_COM_CREATE,
249 *connection = static_cast<aul_app_com_connection_h>(
250 context.AddConnection(endpoint, callback, user_data));
256 extern "C" API int aul_app_com_create(const char* endpoint,
257 aul_app_com_permission_h permission, app_com_cb callback,
258 void* user_data, aul_app_com_connection_h* connection) {
259 return AppComCreate(endpoint, permission, callback, user_data,
263 extern "C" API int aul_app_com_create_async(const char* endpoint,
264 aul_app_com_permission_h permission, app_com_cb callback,
265 void* user_data, aul_app_com_connection_h* connection) {
266 return AppComCreate(endpoint, permission, callback, user_data,
270 extern "C" API int aul_app_com_join(const char* endpoint, const char* filter,
271 app_com_cb callback, void* user_data,
272 aul_app_com_connection_h* connection) {
273 if (endpoint == nullptr || callback == nullptr || connection == nullptr) {
274 _E("Invalid parameter");
278 if (!aul_is_initialized()) {
279 if (aul_launch_init(nullptr, nullptr) < 0)
280 return AUL_R_ENOINIT;
283 tizen_base::Bundle b;
284 b.Add(AUL_K_COM_ENDPOINT, endpoint);
286 b.Add(AUL_K_COM_FILTER, filter);
288 int ret = app_send_cmd(AUL_UTIL_PID, APP_COM_JOIN, b.GetHandle());
290 *connection = static_cast<aul_app_com_connection_h>(
291 context.AddConnection(endpoint, callback, user_data));
297 extern "C" API int aul_app_com_send(const char* endpoint, bundle* envelope) {
298 if (endpoint == nullptr || envelope == nullptr)
301 bundle_del(envelope, AUL_K_COM_ENDPOINT);
302 bundle_add(envelope, AUL_K_COM_ENDPOINT, endpoint);
303 return app_send_cmd(AUL_UTIL_PID, APP_COM_SEND, envelope);
306 extern "C" API int aul_app_com_leave(aul_app_com_connection_h connection) {
307 if (connection == nullptr)
310 auto* conn = static_cast<AppComConnection*>(connection);
311 if (!context.ExistConnection(conn)) {
312 _E("Invalid parameter");
316 std::string endpoint = conn->GetEndpoint();
317 context.RemoveConnection(conn);
319 if (context.ExistConnection(endpoint))
322 tizen_base::Bundle b;
323 b.Add(AUL_K_COM_ENDPOINT, endpoint);
324 return app_send_cmd(AUL_UTIL_PID, APP_COM_LEAVE, b.GetHandle());