864082062b8a3b3b760191a99ac109f9742ab3a7
[framework/web/wrt-plugins-tizen.git] / src / Application / ApplicationManager.h
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
19 #ifndef TIZENAPIS_PLATFORM_APPLICATION_H_
20 #define TIZENAPIS_PLATFORM_APPLICATION_H_
21
22 #include <map>
23 #include <set>
24 #include <list>
25 #include <Commons/Emitters.h>
26 #include "IApplicationManager.h"
27 #include "ApplicationFactory.h"
28
29 #include "Application.h"
30 #include "ApplicationContext.h"
31 #include "ApplicationInformation.h"
32 #include "ApplicationCert.h"
33
34 //#include <app_manager.h>
35 #include <app_service.h>
36
37 #include "AppManagerWrapper.h"
38
39 namespace DeviceAPI {
40 namespace Application {
41
42 class ApplicationManager: public IApplicationManager, public IAppManagerAppListChangedCallbacks {
43     friend class ApplicationFactory;
44
45 public:
46         ApplicationManager();
47         virtual ~ApplicationManager();
48         virtual void launch(const EventApplicationLaunchPtr& event);
49         virtual void kill(const EventApplicationKillPtr& event);
50         virtual void launchAppControl(const EventApplicationLaunchAppControlPtr& event);
51         virtual void findAppControl(const EventApplicationFindAppControlPtr& event);
52         virtual void getAppsContext(const EventApplicationGetAppsContextPtr& event);
53         virtual void getAppsInfo(const EventApplicationGetAppsInfoPtr& event);
54         virtual void addAppInfoEventListener(const EventApplicationAddAppInfoEventListenerPtr& event);
55         virtual void removeAppInfoEventListener(const EventApplicationRemoveAppInfoEventListenerPtr& event);
56
57         void invokeManualAnswerLaunchAppControl(service_h request, service_h reply, service_result_e result,
58                         EventApplicationLaunchAppControlReplyPtr &event);
59         void invokeManualAnswerKill(int pid);
60
61         static bool service_extra_data_callback(service_h service, const char *key, void* user_data);
62
63         static ApplicationPtr getCurrentApplication();
64         static ApplicationContextPtr getAppContext(const std::string id);
65         static ApplicationInformationPtr getAppInfo(const std::string id);
66         static ApplicationCertArrayPtr getAppCerts(const std::string id);
67         static std::string getAppSharedURI(const std::string appId);
68
69 protected:
70         virtual void OnRequestReceived(const EventApplicationLaunchPtr& event);
71         virtual void OnRequestReceived(const EventApplicationKillPtr& event);
72         virtual void OnRequestReceived(const EventApplicationLaunchAppControlPtr& event);
73         virtual void OnRequestReceived(const EventApplicationLaunchAppControlReplyPtr& event);
74         virtual void OnRequestReceived(const EventApplicationFindAppControlPtr& event);
75         virtual void OnRequestReceived(const EventApplicationGetAppsContextPtr& event);
76         virtual void OnRequestReceived(const EventApplicationGetAppsInfoPtr& event);
77         virtual void OnRequestReceived(const EventApplicationAddAppInfoEventListenerPtr& event);
78         virtual void OnRequestReceived(const EventApplicationRemoveAppInfoEventListenerPtr& event);
79
80         // inherited from IAppManagerAppListChangedCallbacks
81         virtual void onAppManagerEventInstalled(const char *appId);
82         virtual void onAppManagerEventUninstalled(const char *appId);
83         virtual void onAppManagerEventUpdated(const char *appId);
84
85 private:
86         void initialize();
87
88         DPL::Mutex m_initializationMutex;
89         bool m_initialized;
90
91         std::map<int, EventApplicationKillPtr> m_killEventMap;
92         mutable DPL::Mutex m_killMapLock;
93
94         typedef WrtDeviceApis::Commons::Emitters<EventApplicationAppInfoEventListenerEmitter> EventApplicationAppInfoEventListenerEmitters;
95         typedef std::map<long, EventApplicationAppInfoEventListenerEmitter::IdType> WatchIdMap;
96
97         EventApplicationAppInfoEventListenerEmitters m_installedApplicationsEmitters;
98         WatchIdMap      m_watchIdMap;
99
100 };
101
102 class LaunchAppControlPendingEvent
103 {
104 public:
105         LaunchAppControlPendingEvent(void *thisObject, const EventApplicationLaunchAppControlReplyPtr &event) :
106                 m_thisObject(thisObject),
107                 m_event(event)
108         {
109         }
110
111         virtual ~LaunchAppControlPendingEvent()
112         {
113         }
114
115         void* getThisObject() const { return m_thisObject; }
116         EventApplicationLaunchAppControlReplyPtr getEvent() const { return m_event; }
117
118 private:
119         void *m_thisObject;
120         EventApplicationLaunchAppControlReplyPtr m_event;
121 };
122
123
124 template <class KeyType, class DataType>
125 class KeyMultiMap
126 {
127 public:
128         typedef unsigned int                                DataKeyType;
129         typedef DataType *                                  DataPtrType;
130         typedef std::pair<KeyType, DataPtrType>             KeyDataPairType;
131         typedef std::map<DataKeyType, KeyDataPairType>      DataMapType;
132         typedef std::set<DataKeyType>                       DataKeySetType;
133         typedef std::map<KeyType, DataKeySetType>           KeyMapType;
134         typedef std::list<DataPtrType>                      DataPtrListType;
135
136         KeyMultiMap() :
137                 m_keyAcc(0)
138         {
139         }
140
141         DataKeyType insert(const KeyType &key, const DataPtrType &dataPtr)
142         {
143                 DataKeyType dataKey = ++m_keyAcc;
144
145                 KeyDataPairType pair(key, dataPtr);
146                 m_dataMap.insert(std::pair<DataKeyType, KeyDataPairType>(dataKey, pair));
147
148                 typename KeyMapType::iterator keyMapIter = m_keyMap.find(key);
149                 if(keyMapIter == m_keyMap.end())
150                 {
151                         DataKeySetType newKeySet;
152                         m_keyMap.insert(std::pair<KeyType, DataKeySetType>(key, newKeySet));
153                         keyMapIter = m_keyMap.find(key);
154                 }
155
156                 DataKeySetType &dataKeySet = keyMapIter->second;
157
158                 dataKeySet.insert(dataKey);
159
160                 return dataKey;
161         }
162
163         DataPtrType getData(const DataKeyType &dataKey) const
164         {
165                 typename DataMapType::const_iterator dataMapIter = m_dataMap.find(dataKey);
166
167                 if(dataMapIter == m_dataMap.end())
168                         return static_cast<DataPtrType>(NULL);
169
170                 return dataMapIter->second.second;
171         }
172
173         DataPtrListType getDataPtrList(const KeyType &key)
174         {
175                 DataPtrListType dataPtrList;
176
177                 typename KeyMapType::const_iterator keyMapIter = m_keyMap.find(key);
178                 if(keyMapIter == m_keyMap.end())
179                         return dataPtrList;
180
181                 DataKeySetType keySet = keyMapIter->second;
182                 DataKeySetType::iterator keySetIter = keySet.begin();
183                 for(; keySetIter != keySet.end(); keySetIter++)
184                 {
185                         DataPtrType dataPtr = getData(*keySetIter);
186                         if(dataPtr == NULL)
187                         {
188                                 LoggerD("No data for " << *keySetIter);
189                                 break;
190                         }
191
192                         dataPtrList.push_back(dataPtr);
193                 }
194
195                 return dataPtrList;
196         }
197
198         void eraseData(const DataKeyType &dataKey)
199         {
200                 typename DataMapType::iterator dataKeyIter = m_dataMap.find(dataKey);
201
202                 if(dataKeyIter == m_dataMap.end())
203                 {
204                         LoggerD("No data for " << dataKey);
205                         return;
206                 }
207
208                 KeyType key = dataKeyIter->second.first;
209
210                 m_dataMap.erase(dataKeyIter);
211
212                 typename KeyMapType::iterator keyMapIter = m_keyMap.find(key);
213                 if(keyMapIter == m_keyMap.end())
214                 {
215                         LoggerD("No data for Key");
216                         return;
217                 }
218
219                 DataKeySetType &keySet = keyMapIter->second;
220                 DataKeySetType::iterator keySetIter = keySet.find(dataKey);
221                 if(keySetIter == keySet.end())
222                 {
223                         LoggerD("No data for " << dataKey);
224                         return;
225                 }
226
227                 keySet.erase(keySetIter);
228         }
229
230         void eraseKey(const KeyType &key)
231         {
232                 typename KeyMapType::iterator keyMapIter = m_keyMap.find(key);
233                 if(keyMapIter == m_keyMap.end())
234                 {
235                         LoggerD("No data to erase.");
236                         return;
237                 }
238
239                 DataKeySetType &keySet = keyMapIter->second;
240                 DataKeySetType::iterator keySetIter = keySet.begin();
241                 for(; keySetIter != keySet.end(); keySetIter++)
242                 {
243                         typename DataMapType::iterator dataKeyIter = m_dataMap.find(*keySetIter);
244                         if(dataKeyIter == m_dataMap.end())
245                         {
246                                 LoggerD("No data to erase.");
247                                 break;
248                         }
249
250                         m_dataMap.erase(dataKeyIter);
251                 }
252
253                 m_keyMap.erase(keyMapIter);
254         }
255
256 private:
257         DataKeyType m_keyAcc;
258
259         DataMapType m_dataMap;
260         KeyMapType m_keyMap;
261 };
262
263 }
264 }
265
266 #endif