1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ipc/daemon.h"
17
18 #include <exception>
19 #include <stdexcept>
20
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "iremote_object.h"
24 #include "mountpoint/mount_manager.h"
25 #include "system_ability_definition.h"
26 #include "utils_log.h"
27
28 namespace OHOS {
29 namespace Storage {
30 namespace DistributedFile {
31 using namespace std;
32
33 REGISTER_SYSTEM_ABILITY_BY_ID(Daemon, FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID, true);
34
PublishSA()35 void Daemon::PublishSA()
36 {
37 LOGI("Begin to init");
38 if (!registerToService_) {
39 bool ret = SystemAbility::Publish(this);
40 if (!ret) {
41 throw runtime_error("Failed to publish the daemon");
42 }
43 registerToService_ = true;
44 }
45 LOGI("Init finished successfully");
46 }
47
RegisterOsAccount()48 void Daemon::RegisterOsAccount()
49 {
50 EventFwk::MatchingSkills matchingSkills;
51 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
52 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
53 subScriber_ = std::make_shared<OsAccountObserver>(subscribeInfo);
54 bool subRet = EventFwk::CommonEventManager::SubscribeCommonEvent(subScriber_);
55 if (!subRet) {
56 LOGE("Subscribe common event failed");
57 }
58 }
59
OnStart()60 void Daemon::OnStart()
61 {
62 LOGI("Begin to start service");
63 if (state_ == ServiceRunningState::STATE_RUNNING) {
64 LOGD("Daemon has already started");
65 return;
66 }
67
68 try {
69 PublishSA();
70 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
71 } catch (const exception &e) {
72 LOGE("%{public}s", e.what());
73 }
74
75 state_ = ServiceRunningState::STATE_RUNNING;
76 LOGI("Start service successfully");
77 }
78
OnStop()79 void Daemon::OnStop()
80 {
81 LOGI("Begin to stop");
82 state_ = ServiceRunningState::STATE_NOT_START;
83 registerToService_ = false;
84 bool subRet = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subScriber_);
85 if (!subRet) {
86 LOGE("UnSubscribe common event failed");
87 }
88 subScriber_ = nullptr;
89 LOGI("Stop finished successfully");
90 }
91
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)92 void Daemon::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
93 {
94 (void)systemAbilityId;
95 (void)deviceId;
96 RegisterOsAccount();
97 }
98
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)99 void Daemon::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
100 {
101 (void)deviceId;
102 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
103 LOGE("systemAbilityId is not COMMON_EVENT_SERVICE_ID");
104 return;
105 }
106
107 if (subScriber_ == nullptr) {
108 LOGE("Daemon::OnRemoveSystemAbility subscriberPtr is nullptr");
109 return;
110 }
111
112 bool subscribeResult = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subScriber_);
113 LOGI("Daemon::OnRemoveSystemAbility subscribeResult = %{public}d", subscribeResult);
114 subScriber_ = nullptr;
115 }
116
EchoServerDemo(const string & echoStr)117 int32_t Daemon::EchoServerDemo(const string &echoStr)
118 {
119 (void)echoStr;
120 return 0;
121 }
122 } // namespace DistributedFile
123 } // namespace Storage
124 } // namespace OHOS
125