1 /*
2  * Copyright (c) 2024 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 "test_server_service.h"
17 
18 #include "iremote_object.h"
19 #include "system_ability_definition.h"
20 #include "hilog/log.h"
21 #include "parameters.h"
22 #include "iservice_registry.h"
23 #include "test_server_error_code.h"
24 #include "pasteboard_client.h"
25 
26 namespace OHOS::testserver {
27     // TEST_SERVER_SA_ID
28     REGISTER_SYSTEM_ABILITY_BY_ID(TestServerService, TEST_SERVER_SA_ID, false); // SA run on demand
29 
30     using namespace std;
31     using namespace OHOS::HiviewDFX;
32     static constexpr OHOS::HiviewDFX::HiLogLabel LABEL_SERVICE = {LOG_CORE, 0xD003110, "TestServerService"};
33     static constexpr OHOS::HiviewDFX::HiLogLabel LABEL_TIMER = {LOG_CORE, 0xD003110, "CallerDetectTimer"};
34     static const int CALLER_DETECT_DURING = 10000;
35 
TestServerService(int32_t saId, bool runOnCreate)36     TestServerService::TestServerService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
37     {
38         HiLog::Info(LABEL_SERVICE, "%{public}s called. saId=%{public}d, runOnCreate=%{public}d",
39             __func__, saId, runOnCreate);
40         StartCallerDetectTimer();
41     }
42 
~TestServerService()43     TestServerService::~TestServerService()
44     {
45         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
46         if (callerDetectTimer_ == nullptr) {
47             HiLog::Error(LABEL_SERVICE, "%{public}s. callerDetectTimer_ is nullptr.", __func__);
48             return;
49         }
50         callerDetectTimer_->Cancel();
51     }
52 
OnStart()53     void TestServerService::OnStart()
54     {
55         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
56         if (!IsRootVersion() && !IsDeveloperMode()) {
57             HiLog::Error(LABEL_SERVICE, "%{public}s. System mode is unsatisfied.", __func__);
58             return;
59         }
60         bool res = Publish(this);
61         if (!res) {
62             HiLog::Error(LABEL_SERVICE, "%{public}s. Publish failed", __func__);
63         }
64     }
65 
OnStop()66     void TestServerService::OnStop()
67     {
68         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
69         IsDeveloperMode();
70     }
71 
IsRootVersion()72     bool TestServerService::IsRootVersion()
73     {
74         bool debugmode = OHOS::system::GetBoolParameter("const.debuggable", false);
75         HiLog::Info(LABEL_SERVICE, "%{public}s. debugmode=%{public}d", __func__, debugmode);
76         return debugmode;
77     }
78 
IsDeveloperMode()79     bool TestServerService::IsDeveloperMode()
80     {
81         bool developerMode = OHOS::system::GetBoolParameter("const.security.developermode.state", false);
82         HiLog::Info(LABEL_SERVICE, "%{public}s. developerMode=%{public}d", __func__, developerMode);
83         return developerMode;
84     }
85 
StartCallerDetectTimer()86     void TestServerService::StartCallerDetectTimer()
87     {
88         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
89         callerDetectTimer_ = new CallerDetectTimer(this);
90         callerDetectTimer_->Start();
91     }
92 
CreateSession(const SessionToken &sessionToken)93     ErrCode TestServerService::CreateSession(const SessionToken &sessionToken)
94     {
95         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
96         bool result = true;
97         try {
98             result = sessionToken.AddDeathRecipient(
99                 sptr<TestServerProxyDeathRecipient>(new TestServerProxyDeathRecipient(this)));
100         } catch(...) {
101             result = false;
102         }
103         if (!result) {
104             HiLog::Error(LABEL_SERVICE, "%{public}s. AddDeathRecipient FAILD.", __func__);
105             DestorySession();
106             return TEST_SERVER_ADD_DEATH_RECIPIENT_FAILED;
107         }
108         AddCaller();
109         HiLog::Info(LABEL_SERVICE, "%{public}s. Create session SUCCESS. callerCount=%{public}d",
110             __func__, GetCallerCount());
111         return TEST_SERVER_OK;
112     }
113 
SetPasteData(const std::string& text)114     ErrCode TestServerService::SetPasteData(const std::string& text)
115     {
116         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
117         auto pasteBoardMgr = MiscServices::PasteboardClient::GetInstance();
118         pasteBoardMgr->Clear();
119         auto pasteData = pasteBoardMgr->CreatePlainTextData(text);
120         if (pasteData == nullptr) {
121             return TEST_SERVER_CREATE_PASTE_DATA_FAILED;
122         }
123         int32_t ret = pasteBoardMgr->SetPasteData(*pasteData);
124         int32_t successErrCode = 27787264;
125         return ret == successErrCode ? TEST_SERVER_OK : TEST_SERVER_SET_PASTE_DATA_FAILED;
126     }
127 
PublishCommonEvent(const EventFwk::CommonEventData &event, bool &re)128     ErrCode TestServerService::PublishCommonEvent(const EventFwk::CommonEventData &event, bool &re)
129     {
130         if (!EventFwk::CommonEventManager::PublishCommonEvent(event)) {
131             HiLog::Info(LABEL_SERVICE, "%{public}s Pulbish commonEvent.", __func__);
132             re = false;
133         }
134         re = true;
135         int32_t ret = re ? TEST_SERVER_OK : TEST_SERVER_PUBLISH_EVENT_FAILED;
136         return ret;
137     }
138 
AddCaller()139     void TestServerService::AddCaller()
140     {
141         callerCount_++;
142     }
143 
RemoveCaller()144     void TestServerService::RemoveCaller()
145     {
146         callerCount_--;
147     }
148 
GetCallerCount()149     int TestServerService::GetCallerCount()
150     {
151         return callerCount_.load();
152     }
153 
DestorySession()154     void TestServerService::DestorySession()
155     {
156         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
157         if (callerCount_ == 0) {
158             HiLog::Info(LABEL_SERVICE, "%{public}s. No proxy exists. Remove the TestServer", __func__);
159             RemoveTestServer();
160         } else {
161             HiLog::Info(LABEL_SERVICE, "%{public}s. Other proxys exist. Can not remove the TestServer", __func__);
162         }
163     }
164 
RemoveTestServer()165     bool TestServerService::RemoveTestServer()
166     {
167         HiLog::Info(LABEL_SERVICE, "%{public}s called. ", __func__);
168         sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
169         if (samgr == nullptr) {
170             HiLog::Error(LABEL_SERVICE, "%{public}s. Get SystemAbility Manager failed!", __func__);
171             return false;
172         }
173         auto res = samgr->UnloadSystemAbility(TEST_SERVER_SA_ID);
174         return res == ERR_OK;
175     }
176 
OnRemoteDied(const wptr<IRemoteObject> &object)177     void TestServerService::TestServerProxyDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
178     {
179         HiLog::Info(LABEL_SERVICE, "%{public}s called.", __func__);
180         if (object == nullptr) {
181             HiLog::Error(LABEL_SERVICE, "%{public}s. IRemoteObject is NULL.", __func__);
182             return;
183         }
184         testServerService_->RemoveCaller();
185         testServerService_->DestorySession();
186     }
187 
Start()188     void TestServerService::CallerDetectTimer::Start()
189     {
190         HiLog::Info(LABEL_TIMER, "%{public}s called.", __func__);
191         thread_ = thread([this] {
192             this_thread::sleep_for(chrono::milliseconds(CALLER_DETECT_DURING));
193             HiLog::Info(LABEL_TIMER, "%{public}s. Timer is done.", __func__);
194             if (!testServerExit_) {
195                 testServerService_->DestorySession();
196             }
197         });
198         thread_.detach();
199     }
200 
Cancel()201     void TestServerService::CallerDetectTimer::Cancel()
202     {
203         HiLog::Info(LABEL_TIMER, "%{public}s called.", __func__);
204         testServerExit_ = true;
205     }
206 
207 } // namespace OHOS::testserver