2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef TIZENAPIS_PLATFORM_APPLICATION_H_
20 #define TIZENAPIS_PLATFORM_APPLICATION_H_
25 #include <Commons/Emitters.h>
26 #include "IApplicationManager.h"
27 #include "ApplicationFactory.h"
29 #include "Application.h"
30 #include "ApplicationContext.h"
31 #include "ApplicationInformation.h"
32 #include "ApplicationCert.h"
34 //#include <app_manager.h>
35 #include <app_service.h>
37 #include "AppManagerWrapper.h"
40 namespace Application {
42 class ApplicationManager: public IApplicationManager, public IAppManagerAppListChangedCallbacks {
43 friend class ApplicationFactory;
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);
57 void invokeManualAnswerLaunchAppControl(service_h request, service_h reply, service_result_e result,
58 EventApplicationLaunchAppControlReplyPtr &event);
59 void invokeManualAnswerKill(int pid);
61 static bool service_extra_data_callback(service_h service, const char *key, void* user_data);
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);
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);
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);
88 DPL::Mutex m_initializationMutex;
91 std::map<int, EventApplicationKillPtr> m_killEventMap;
92 mutable DPL::Mutex m_killMapLock;
94 typedef WrtDeviceApis::Commons::Emitters<EventApplicationAppInfoEventListenerEmitter> EventApplicationAppInfoEventListenerEmitters;
95 typedef std::map<long, EventApplicationAppInfoEventListenerEmitter::IdType> WatchIdMap;
97 EventApplicationAppInfoEventListenerEmitters m_installedApplicationsEmitters;
98 WatchIdMap m_watchIdMap;
102 class LaunchAppControlPendingEvent
105 LaunchAppControlPendingEvent(void *thisObject, const EventApplicationLaunchAppControlReplyPtr &event) :
106 m_thisObject(thisObject),
111 virtual ~LaunchAppControlPendingEvent()
115 void* getThisObject() const { return m_thisObject; }
116 EventApplicationLaunchAppControlReplyPtr getEvent() const { return m_event; }
120 EventApplicationLaunchAppControlReplyPtr m_event;
124 template <class KeyType, class DataType>
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;
141 DataKeyType insert(const KeyType &key, const DataPtrType &dataPtr)
143 DataKeyType dataKey = ++m_keyAcc;
145 KeyDataPairType pair(key, dataPtr);
146 m_dataMap.insert(std::pair<DataKeyType, KeyDataPairType>(dataKey, pair));
148 typename KeyMapType::iterator keyMapIter = m_keyMap.find(key);
149 if(keyMapIter == m_keyMap.end())
151 DataKeySetType newKeySet;
152 m_keyMap.insert(std::pair<KeyType, DataKeySetType>(key, newKeySet));
153 keyMapIter = m_keyMap.find(key);
156 DataKeySetType &dataKeySet = keyMapIter->second;
158 dataKeySet.insert(dataKey);
163 DataPtrType getData(const DataKeyType &dataKey) const
165 typename DataMapType::const_iterator dataMapIter = m_dataMap.find(dataKey);
167 if(dataMapIter == m_dataMap.end())
168 return static_cast<DataPtrType>(NULL);
170 return dataMapIter->second.second;
173 DataPtrListType getDataPtrList(const KeyType &key)
175 DataPtrListType dataPtrList;
177 typename KeyMapType::const_iterator keyMapIter = m_keyMap.find(key);
178 if(keyMapIter == m_keyMap.end())
181 DataKeySetType keySet = keyMapIter->second;
182 DataKeySetType::iterator keySetIter = keySet.begin();
183 for(; keySetIter != keySet.end(); keySetIter++)
185 DataPtrType dataPtr = getData(*keySetIter);
188 LoggerD("No data for " << *keySetIter);
192 dataPtrList.push_back(dataPtr);
198 void eraseData(const DataKeyType &dataKey)
200 typename DataMapType::iterator dataKeyIter = m_dataMap.find(dataKey);
202 if(dataKeyIter == m_dataMap.end())
204 LoggerD("No data for " << dataKey);
208 KeyType key = dataKeyIter->second.first;
210 m_dataMap.erase(dataKeyIter);
212 typename KeyMapType::iterator keyMapIter = m_keyMap.find(key);
213 if(keyMapIter == m_keyMap.end())
215 LoggerD("No data for Key");
219 DataKeySetType &keySet = keyMapIter->second;
220 DataKeySetType::iterator keySetIter = keySet.find(dataKey);
221 if(keySetIter == keySet.end())
223 LoggerD("No data for " << dataKey);
227 keySet.erase(keySetIter);
230 void eraseKey(const KeyType &key)
232 typename KeyMapType::iterator keyMapIter = m_keyMap.find(key);
233 if(keyMapIter == m_keyMap.end())
235 LoggerD("No data to erase.");
239 DataKeySetType &keySet = keyMapIter->second;
240 DataKeySetType::iterator keySetIter = keySet.begin();
241 for(; keySetIter != keySet.end(); keySetIter++)
243 typename DataMapType::iterator dataKeyIter = m_dataMap.find(*keySetIter);
244 if(dataKeyIter == m_dataMap.end())
246 LoggerD("No data to erase.");
250 m_dataMap.erase(dataKeyIter);
253 m_keyMap.erase(keyMapIter);
257 DataKeyType m_keyAcc;
259 DataMapType m_dataMap;