Initialize Tizen 2.3
[framework/osp/security-service.git] / src / SecurityService.cpp
1 //
2 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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 /**
18  * @file                SecurityService.cpp
19  * @brief               This is the implementation file for SecurityService class.
20  */
21
22 #include <FAppAppRegistry.h>
23 #include <FBaseLog.h>
24
25 #include "SecurityService.h"
26 #include "PrivilegeService.h"
27 #include "CertificateService.h"
28 #include "DrmService.h"
29
30 using namespace Tizen::App;
31 using namespace Tizen::Base::Collection;
32
33 #define VCONFKEY_APPSERVICE_STATUS "memory/appservice/status"
34
35 static SecurityService* __pSecurityService = null;
36
37 SecurityService::SecurityService(void)
38         : __pPrivilegeService(null)
39         , __pCertificateService(null)
40 {
41
42 }
43
44 SecurityService::~SecurityService(void)
45 {
46
47 }
48
49 Service*
50 SecurityService::CreateInstance(void)
51 {
52         result r = E_SUCCESS;
53
54         ClearLastResult();
55
56         if (__pSecurityService == null)
57         {
58                 __pSecurityService = new (std::nothrow) SecurityService();
59                 if (__pSecurityService == null)
60                 {
61                         AppLogException("[E_OUT_OF_MEMORY] The memory is insufficient.");
62                         r = E_OUT_OF_MEMORY;
63                 }
64         }
65
66         SetLastResult(r);
67
68         return __pSecurityService;
69 }
70
71 bool
72 SecurityService::OnAppInitializing(AppRegistry& appRegistry)
73 {
74         result r = E_SUCCESS;
75         bool ret = true;
76
77         int errorCode = 0;
78         int channelStatus = 0;
79
80         AppLog("Enter");
81
82         __pPrivilegeService = new (std::nothrow) PrivilegeService();
83         TryCatchTag(OSP_SECURITY_SERVICE, __pPrivilegeService != null, r = E_OUT_OF_MEMORY; ret = false, "[E_OUT_OF_MEMORY] The memory is insufficient.");
84
85         r = __pPrivilegeService->Construct();
86         TryCatchTag(OSP_SECURITY_SERVICE, r == E_SUCCESS, r = E_SYSTEM; ret = false, "[E_SYSTEM] An unexpected system error occurred.");
87
88 #if 0
89         __pCertificateService = new (std::nothrow) CertificateService();        //return true even if error occured
90                                                                                                                                                 //because privilege must be running.
91         TryCatchTag(OSP_SECURITY_SERVICE, __pCertificateService != null, r = E_OUT_OF_MEMORY; ret = true, "[E_OUT_OF_MEMORY] The memory is insufficient.");
92
93         r = __pCertificateService->Construct();
94         TryCatchTag(OSP_SECURITY_SERVICE, r == E_SUCCESS, r = E_SYSTEM; ret = true, "[E_SYSTEM] An unexpected system error occurred.");
95 #endif
96
97         errorCode = vconf_get_int(VCONFKEY_APPSERVICE_STATUS, &channelStatus);
98         TryCatchTag(OSP_SECURITY_SERVICE, errorCode == 0, r = E_SYSTEM; ret = true, "[E_SYSTEM] It is failed to get the VCONF_APPSERVICE_STATUS vconf.");
99
100         AppLog("Channel IPC Server init started... VCONF_APPSERVICE_STATUS = %d", errorCode);
101
102         if (channelStatus == 2)
103         {
104                 AppLog("Channel Service already launched. DRM IPC Server started.");
105                 DrmService* pDrmService = DrmService::GetInstance();
106                 TryCatchTag(OSP_SECURITY_SERVICE, pDrmService != null, r = E_OUT_OF_MEMORY; ret = true, "[E_OUT_OF_MEMORY] DrmService::GetInstance() failed.");
107
108                 errorCode = vconf_ignore_key_changed(VCONFKEY_APPSERVICE_STATUS, OnChannelServiceLaunched);
109                 if (errorCode != 0)
110                 {
111                         AppLogException("[E_SYSTEM] It is failed to unregister event listener for vconf change.");
112                 }
113         }
114         else
115         {
116                 AppLog("Channel Service has not been launched. Wait for launching channel service.");
117                 errorCode = vconf_notify_key_changed(VCONFKEY_APPSERVICE_STATUS, OnChannelServiceLaunched, null);
118                 TryCatchTag(OSP_SECURITY_SERVICE, errorCode == 0, r = E_SYSTEM; ret = true, "[E_SYSTEM] Failed to register event listener of VCONF_APPSERVICE_STATUS.");
119         }
120
121         AppLog("Exit");
122
123         return ret;
124
125 CATCH:
126
127         SetLastResult(r);
128
129         AppLog("Exit");
130         return ret;
131 }
132
133 bool
134 SecurityService::OnAppInitialized(void)
135 {
136         result r = E_SUCCESS;
137         bool ret = true;
138
139         AppLog("Enter");
140
141         __pCertificateService = new (std::nothrow) CertificateService();        //return true even if error occured
142                                                                                                                                                 //because privilege must be running.
143         TryCatchTag(OSP_SECURITY_SERVICE, __pCertificateService != null, r = E_OUT_OF_MEMORY; ret = true, "[E_OUT_OF_MEMORY] The memory is insufficient.");
144
145         r = __pCertificateService->Construct();
146         TryCatchTag(OSP_SECURITY_SERVICE, r == E_SUCCESS, r = E_SYSTEM; ret = true, "[E_SYSTEM] An unexpected system error occurred.");
147
148         AppLog("Exit");
149         return ret;
150
151 CATCH:
152         SetLastResult(r);
153
154         AppLog("Exit");
155         return ret;
156 }
157
158 bool
159 SecurityService::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
160 {
161         AppLog("Enter");
162
163         delete __pPrivilegeService;
164         delete __pCertificateService;
165         DrmService::FreeInstance();
166
167         AppLog("Exit");
168         return true;
169 }
170
171 void
172 SecurityService::OnLowMemory(void)
173 {
174
175 }
176
177 void
178 SecurityService::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList *pArgs)
179 {
180
181 }
182
183 void
184 SecurityService::OnChannelServiceLaunched(keynode_t* node, void* userData)
185 {
186         result r = E_SUCCESS;
187
188         int channelStatus = 0;
189         int errorCode = 0;
190
191         AppLog("OnChannelServiceLaunched started. - vconf changed.");
192
193     if(strcmp(VCONFKEY_APPSERVICE_STATUS, vconf_keynode_get_name(node)) == 0)
194     {
195         errorCode = vconf_get_int(VCONFKEY_APPSERVICE_STATUS, &channelStatus);
196         TryCatchTag(OSP_SECURITY_SERVICE, errorCode == 0, r = E_SYSTEM, "[E_SYSTEM] Failed to get the VCONF_APPSERVICE_STATUS.");
197
198         if (channelStatus == 2)
199         {
200                 AppLog("Channel Service launched. DRM IPC Server will be started.");
201
202                 DrmService* pDrmService = DrmService::GetInstance();
203                         TryCatchTag(OSP_SECURITY_SERVICE, pDrmService != null, r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] DrmService::GetInstance() failed.");
204
205                         AppLog("DRM IPC Server succesfully launched.");
206
207                         errorCode = vconf_ignore_key_changed(VCONFKEY_APPSERVICE_STATUS, OnChannelServiceLaunched);
208                         if (errorCode != 0)
209                         {
210                                 AppLogException("[E_SYSTEM] It is failed to unregister event listener for vconf change.");
211                         }
212         }
213     }
214 CATCH:
215         SetLastResult(r);
216 }