wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Application / RequestedApplicationControl.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <Commons/Exception.h>
19 #include <app_service.h>
20 #include <app_manager.h>
21 #include "RequestedApplicationControl.h"
22 #include <Logger.h>
23
24 namespace DeviceAPI {
25 namespace Application {
26
27 using namespace WrtDeviceApis::Commons;
28
29 RequestedApplicationControl::RequestedApplicationControl() :
30         m_appControl(NULL),
31         m_appService(NULL)
32 {
33     LoggerD("entered");
34 }
35
36
37 RequestedApplicationControl::~RequestedApplicationControl()
38 {
39     LoggerD("entered");
40 }
41
42 ApplicationControlPtr RequestedApplicationControl::getAppControl() const
43 {
44         return m_appControl;
45 }
46
47
48 // TODO: Should I add & or not?
49 void RequestedApplicationControl::setAppControl(ApplicationControlPtr &appControl)
50 {
51         m_appControl = appControl;
52 }
53
54 std::string RequestedApplicationControl::getCallerAppId() const
55 {
56         return m_callerAppId;
57 }
58
59 void RequestedApplicationControl::setCallerAppId(const std::string &id)
60 {
61         m_callerAppId = id;
62 }
63
64 void RequestedApplicationControl::replyResult(std::vector<ApplicationControlDataPtr> &appControlDataArray)
65 {
66         service_h reply;
67         service_create(&reply);
68
69         if (!appControlDataArray.empty())
70         {
71                 const char** arr = NULL;
72
73                 LoggerD("appControlDataArray.size() : "<<appControlDataArray.size());
74                 for (size_t i = 0; i < appControlDataArray.size(); ++i) {
75                         std::vector<std::string> valueArray = appControlDataArray.at(i)->getValue();
76                         
77                         arr = (const char**) calloc (sizeof(char*), valueArray.size());
78
79                         if (arr != NULL) {
80                                 for (size_t j = 0; j < valueArray.size(); j++) {
81                                         arr[j] = valueArray.at(j).c_str();
82                                         LoggerD("== value : " << arr[j]);
83                                 }
84                         }
85                         service_add_extra_data_array(reply, appControlDataArray.at(i)->getKey().c_str(), arr, valueArray.size());
86                         if (arr) {
87                                 free (arr);
88                         }
89                 }
90         } else {
91                 LoggerE("[replyResult] appControlDataArray is empty");
92         }
93
94         // temporal code to check caller liveness
95         if (m_callerAppId.empty()) {
96                 LoggerD("m_callerAppId is empty. means caller is dead");
97                 ThrowMsg(NotFoundException, "Cannot find caller");
98         } else {
99                 bool running = false;
100                 int ret = app_manager_is_running(m_callerAppId.c_str(), &running);
101                 if ((ret != APP_MANAGER_ERROR_NONE) || !running) {
102                         LoggerD("caller is not running");
103                         ThrowMsg(NotFoundException, "Cannot find caller");
104                 }
105         }
106
107         if (service_reply_to_launch_request(reply, m_appControl->getService_h(), SERVICE_RESULT_SUCCEEDED) != SERVICE_ERROR_NONE) {
108                 ThrowMsg(NotFoundException, "Cannot find caller");
109         }
110
111         service_destroy(reply);
112 }
113
114 void RequestedApplicationControl::replyFailure()
115 {
116         service_h reply;
117         service_create(&reply);
118
119         LoggerE("[replyFailure] enter RequestedApplicationControl::replyFailure");
120
121         // temporal code to check caller liveness
122         if (m_callerAppId.empty()) {
123                 LoggerD("m_callerAppId is empty. means caller is dead");
124                 ThrowMsg(NotFoundException, "Cannot find caller");
125         } else {
126                 bool running = false;
127                 int ret = app_manager_is_running(m_callerAppId.c_str(), &running);
128                 if ((ret != APP_MANAGER_ERROR_NONE) || !running) {
129                         LoggerD("caller is not running");
130                         ThrowMsg(NotFoundException, "Cannot find caller");
131                 }
132         }
133
134         if (service_reply_to_launch_request(reply, m_appControl->getService_h(), SERVICE_RESULT_FAILED) != SERVICE_ERROR_NONE) {
135                 ThrowMsg(NotFoundException, "Cannot find caller");
136         }
137
138         service_destroy(reply);
139 }
140
141 }
142 }