wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Application / Application.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 "Application.h"
19
20 #include <bundle.h>
21 #include <app.h>
22 #include <app_service.h>
23
24 #include "ApplicationControlData.h"
25 #include "ApplicationControl.h"
26 #include "ApplicationManager.h"
27 #include <Logger.h>
28
29 namespace DeviceAPI {
30 namespace Application { 
31
32 using namespace WrtDeviceApis;
33 using namespace WrtDeviceApis::Commons;
34
35 extern "C" int service_create_event(bundle *data, struct service_s **service);
36         
37 Application::Application()
38 {
39 }
40
41 Application::~Application()
42 {
43 }
44
45 std::string Application::getContextId() const
46 {
47         return m_contextId;
48 }
49
50 void Application::setContextId(const std::string &id)
51 {
52         m_contextId = id;
53 }
54
55 ApplicationInformationPtr Application::getAppInfo() const
56 {
57         return m_appInfo;
58 }
59
60 void Application::setAppInfo(ApplicationInformationPtr& appInfo)
61 {
62         m_appInfo = appInfo;
63 }
64
65 void Application::getRequestedAppControl(const EventApplicationGetRequestedAppControlPtr& event)
66 {
67         LoggerD("entered");
68
69         Try
70         {
71                 int ret = 0;
72
73                 std::string bundle_str = event->getEncodedBundle();
74                 LoggerD("bundle str : " << bundle_str);
75
76                 service_h service = NULL;
77                 char* tmpStr = NULL;
78
79                 bundle *request_bundle = bundle_decode((bundle_raw*)bundle_str.c_str(), bundle_str.length());
80                 ret = service_create_event(request_bundle, &service);
81                 if(ret != SERVICE_ERROR_NONE)
82                 {
83                         LoggerE("Fail to create event");
84                         event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
85                         bundle_free(request_bundle);
86                         return;
87                 }
88                 bundle_free(request_bundle);
89
90                 ApplicationControlPtr appControl(new ApplicationControl());
91                 appControl->setService_h(service);
92
93                 ret = service_get_operation(service, &tmpStr);
94                 if(ret == SERVICE_ERROR_NONE && tmpStr != NULL)
95                 {
96                         LoggerD(" operation : " << tmpStr);
97                         appControl->setOperation(tmpStr);
98                         free(tmpStr);
99                         tmpStr = NULL;
100                 }
101
102                 ret = service_get_uri(service, &tmpStr);
103                 if(ret == SERVICE_ERROR_NONE && tmpStr != NULL)
104                 {
105                         LoggerD(" uri       : " << tmpStr);
106                         appControl->setUri(tmpStr);
107                         free(tmpStr);
108                         tmpStr = NULL;
109                 }
110
111                 ret = service_get_mime(service, &tmpStr);
112                 if(ret == SERVICE_ERROR_NONE && tmpStr != NULL)
113                 {
114                         LoggerD(" mime      : " << tmpStr);
115                         appControl->setMime(tmpStr);
116                         free(tmpStr);
117                         tmpStr = NULL;
118                 }
119
120                 ret = service_get_category(service, &tmpStr);
121                 if(ret == SERVICE_ERROR_NONE && tmpStr != NULL)
122                 {
123                         LoggerD(" category      : " << tmpStr);
124                         appControl->setCategory(tmpStr);
125                         free(tmpStr);
126                         tmpStr = NULL;
127                 }
128
129                 std::vector<ApplicationControlDataPtr> appControlDataArray;
130                 ret = service_foreach_extra_data(service, ApplicationManager::service_extra_data_callback, &appControlDataArray);
131                 if (ret != SERVICE_ERROR_NONE)
132                 {
133                         LoggerE("service_foreach_extra_data fail");
134                         event->setExceptionCode(Commons::ExceptionCodes::UnknownException);
135                 }
136                 else
137                 {
138                         appControl->setAppControlDataArray(appControlDataArray);
139                 }
140                 
141                 RequestedApplicationControlPtr appCtrMgr(new RequestedApplicationControl());
142                 appCtrMgr->setAppControl(appControl);
143
144                 // add caller id
145                 ret = service_get_caller(service, &tmpStr);
146                 if(ret == SERVICE_ERROR_NONE && tmpStr != NULL)
147                 {
148                         LoggerD(" caller App Id   : " << tmpStr);
149                         appCtrMgr->setCallerAppId(tmpStr);
150                         free(tmpStr);
151                         tmpStr = NULL;
152                 } else {
153                         LoggerE("caller id fail");
154                         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
155                 }
156                 event->setRequestedAppControl(appCtrMgr);               
157                 
158         }
159         Catch (WrtDeviceApis::Commons::Exception)
160         {
161                 LoggerE("Error on getAppControl : " << _rethrown_exception.GetMessage());
162                 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
163         }
164 }
165
166 }
167 }