Add job-scheduler service
[platform/core/context/context-service.git] / src / ServiceLoader.cpp
1 /*
2  * Copyright (c) 2017 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 <AppHistoryService.h>
18 #include <SensorRecorderService.h>
19 #include <ContextStoreService.h>
20 #include <JobSchedulerService.h>
21
22 #include "ServiceLoader.h"
23
24 #define ROOT_UID 0
25
26 using namespace ctx;
27
28 ServiceLoader::ServiceLoader() :
29         __activeUser(ROOT_UID)
30 {
31 }
32
33 ServiceLoader::~ServiceLoader()
34 {
35         for (auto& svc : __userServices) {
36                 delete svc;
37         }
38         for (auto& svc : __systemServices) {
39                 delete svc;
40         }
41 }
42
43 bool ServiceLoader::load(GDBusConnection* conn)
44 {
45         __create<AppHistoryService>(conn);
46         __create<SensorRecorderService>(conn);
47         __create<ContextStoreService>(conn);
48         __create<JobSchedulerService>(conn);
49
50         return (!__systemServices.empty() || !__userServices.empty());
51 }
52
53 void ServiceLoader::startSystem()
54 {
55         for (auto& svc : __systemServices) {
56                 svc->start();
57         }
58 }
59
60 void ServiceLoader::stopSystem()
61 {
62         for (auto& svc : __systemServices) {
63                 svc->stop();
64         }
65 }
66
67 void ServiceLoader::startUser(uid_t uid)
68 {
69         IF_FAIL_VOID(__activeUser != uid);
70         _I("Starting services for %u", static_cast<unsigned int>(uid));
71
72         ServiceBase::setActiveUser(uid);
73
74         for (auto& svc : __userServices) {
75                 svc->start();
76         }
77
78         for (auto& svc : __systemServices) {
79                 svc->notifyUserNew();
80         }
81
82         __activeUser = uid;
83 }
84
85 void ServiceLoader::stopUser()
86 {
87         IF_FAIL_VOID(__activeUser != ROOT_UID);
88         _I("Stopping services for %u", static_cast<unsigned int>(__activeUser));
89
90         for (auto& svc : __userServices) {
91                 svc->stop();
92         }
93
94         ServiceBase::setActiveUser(ROOT_UID);
95
96         for (auto& svc : __systemServices) {
97                 svc->notifyUserRemoved();
98         }
99
100         __activeUser = ROOT_UID;
101 }