Store the dbus connection while launching the service
[platform/core/context/context-service.git] / src / agent / legacy / policy / PolicyManager.cpp
1 /*
2  * Copyright (c) 2016 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 #include <Types.h>
18 #include <DBusTypes.h>
19 #include <ProviderTypes.h>
20 #include <SensorRecorderTypes.h>
21 #include "../ContextManager.h"
22 #include "PolicyRequest.h"
23 #include "PolicyManager.h"
24
25 using namespace ctx;
26
27 PolicyManager::PolicyManager(ContextManager *contextMgr) :
28         __contextMgr(contextMgr)
29 {
30         __subscribe(SUBJ_CUSTOM);
31
32 //      __subscribe(SUBJ_APP_LOGGER);
33 //      __subscribe(SUBJ_BATTERY_LOGGER);
34 //      __subscribe(SUBJ_MEDIA_LOGGER);
35 //      __subscribe(SUBJ_PLACE_DETECTION);
36 //      __subscribe(SUBJ_STATE_WIFI);
37
38 //      __subscribe(SUBJ_SENSOR_PEDOMETER);
39 //      __subscribe(SUBJ_SENSOR_PRESSURE);
40 //      __subscribe(SUBJ_SENSOR_SLEEP_MONITOR);
41 //      __subscribe(SUBJ_SENSOR_HEART_RATE);
42 }
43
44 PolicyManager::~PolicyManager()
45 {
46         for (auto &it : __subscriptionMap) {
47                 PolicyRequest *req = new(std::nothrow) PolicyRequest(REQ_UNSUBSCRIBE, it.first, it.second, NULL);
48                 if (!req) {
49                         _E("Memory allocation failed");
50                         continue;
51                 }
52                 __contextMgr->assignRequest(req);
53         }
54
55         __subscriptionMap.clear();
56 }
57
58 void PolicyManager::__subscribe(const char *subject)
59 {
60         static int rid = 0;
61         ++rid;
62
63         PolicyRequest *req = new(std::nothrow) PolicyRequest(REQ_SUBSCRIBE, rid, subject, NULL);
64         IF_FAIL_VOID_TAG(req, _E, "Memory allocation failed");
65
66         __contextMgr->assignRequest(req);
67
68         __subscriptionMap[rid] = subject;
69 }