[Application] Add check for user_data availability in AppControl callback
[platform/core/api/webapi-plugins.git] / src / application / application_manager.h
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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 #ifndef SRC_APPLICATION_APPLICATION_MANAGER_H__
18 #define SRC_APPLICATION_APPLICATION_MANAGER_H__
19
20 #include <app_event.h>
21 #include <app_manager.h>
22 #include <bundle.h>
23 #include <package-manager.h>
24 #include <pkgmgr-info.h>
25 #include <functional>
26 #include <memory>
27 #include <mutex>
28 #include <set>
29 #include <string>
30 #if defined(TIZEN_MOBILE)
31 #include <context-service/context_history.h>
32 #elif defined(TIZEN_WEARABLE)
33 #include <context-service/wearable/context_history.h>
34 #endif
35
36 #include "common/picojson.h"
37 #include "common/platform_result.h"
38
39 typedef std::function<void(picojson::value*)> JsonCallback;
40 namespace extension {
41 namespace application {
42
43 class ApplicationInstance;
44 typedef struct LaunchAppControlCallbackDataStruct {
45   ApplicationInstance* instance;
46   std::shared_ptr<picojson::value> response;
47   std::string reply_callback_id;
48 } LaunchAppControlCallbackData;
49
50 class ApplicationManager {
51  public:
52   explicit ApplicationManager(ApplicationInstance& instance);
53   ~ApplicationManager();
54
55   void GetCurrentApplication(const std::string& app_id, picojson::object* out);
56   void Kill(const picojson::value& args);
57   void Launch(const picojson::value& args);
58   void LaunchAppControl(const picojson::value& args);
59   void FindAppControl(const picojson::value& args);
60   void GetAppsContext(const picojson::value& args);
61   void GetAppContext(const picojson::value& args, picojson::object* out);
62   void GetAppsInfo(const picojson::value& args);
63   void GetAppInfo(const std::string& app_id, picojson::object* out);
64   void GetAppCerts(const std::string& app_id, picojson::object* out);
65   void GetAppSharedUri(const std::string& app_id, picojson::object* out);
66   void GetAppMetaData(const std::string& app_id, picojson::object* out);
67   void GetBatteryUsageInfo(const picojson::value& args, picojson::object* out);
68   void GetAppsUsageInfo(const picojson::value& args, picojson::object* out);
69   void StartAppInfoEventListener(picojson::object* out);
70   void StopAppInfoEventListener();
71   void GetApplicationInformationSize(const picojson::value& args, picojson::object* out);
72   void AsyncResponse(const common::PlatformResult& result,
73                      std::shared_ptr<picojson::value>* response);
74
75   void BroadcastEventHelper(const picojson::value& args, picojson::object& out, bool trusted);
76   common::PlatformResult StartEventListener(const std::string& event_name,
77                                             const JsonCallback& callback);
78   void StopEventListener(const std::string& event_name);
79   common::PlatformResult StartStatusListener(const JsonCallback& callback);
80   common::PlatformResult StopStatusChangeListener();
81
82  private:
83   char* GetPackageId(const std::string& app_id);
84
85   pkgmgr_client* pkgmgr_client_handle_;
86   pkgmgr_client* pkgmgr_client_uninstall_handle_;
87   ApplicationInstance& instance_;
88   app_manager_event_h app_status_handle_;
89
90   JsonCallback event_callback_;
91   JsonCallback status_callback_;
92   std::map<std::string, event_handler_h> event_handler_map_;
93   std::set<LaunchAppControlCallbackData*> launch_app_control_set_;
94   // We use static set of callback_data's pointers to be able to check whether
95   // callback is fired after ApplicationInstance destruction
96   static std::set<LaunchAppControlCallbackData*> launch_app_control_set_global_;
97   static std::mutex launch_app_control_set_mutex_;
98   static void OnEvent(const char* event_name, bundle* event_data, void* user_data);
99   static void OnStatusEvent(const char* type, const char* app_id,
100                             app_manager_event_type_e event_type,
101                             app_manager_event_state_e event_state, app_manager_event_h handle,
102                             void* user_data);
103
104 #if defined(TIZEN_MOBILE) || defined(TIZEN_WEARABLE)
105   static common::PlatformResult GetContextHistory(
106       const picojson::value& args, picojson::object* out,
107       common::PlatformResult (*)(const picojson::value&, const context_history_filter_h,
108                                  context_history_data_e* data_type),
109       common::PlatformResult (*)(const context_history_record_h, picojson::object*));
110
111   static common::PlatformResult BatteryUsageFilter(const picojson::value&,
112                                                    const context_history_filter_h,
113                                                    context_history_data_e* data_type);
114   static common::PlatformResult BatteryUsageAttributes(const context_history_record_h,
115                                                        picojson::object*);
116
117   static common::PlatformResult AppsUsageFilter(const picojson::value&,
118                                                 const context_history_filter_h,
119                                                 context_history_data_e* data_type);
120   static common::PlatformResult AppsUsageAttributes(const context_history_record_h,
121                                                     picojson::object*);
122 #endif
123 };
124
125 }  // namespace application
126 }  // namespace extension
127
128 #endif  // SRC_APPLICATION_APPLICATION_MANAGER_H__