• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TEST_SERVER_SERVICE_H
17 #define TEST_SERVER_SERVICE_H
18 
19 #include <string>
20 #include <mutex>
21 #include <thread>
22 #include <chrono>
23 #include "test_server_interface_stub.h"
24 #include "refbase.h"
25 #include "system_ability.h"
26 #include "session_token.h"
27 #include <common_event_manager.h>
28 #include <common_event_subscribe_info.h>
29 #include "cpu_collector.h"
30 
31 namespace OHOS::testserver {
32     class TestServerService : public SystemAbility, public TestServerInterfaceStub {
33         DECLARE_SYSTEM_ABILITY(TestServerService);
34 
35     public:
36         TestServerService(int32_t saId, bool runOnCreate);
37         ~TestServerService();
38 
39         void StartCallerDetectTimer();
40         void KillProcess(const std::string& processName);
41         bool RetailDemoAppVerify();
42         ErrCode CreateSession(const SessionToken &sessionToken) override;
43 
44         // Declare the logical interfaces here
45         ErrCode SetPasteData(const std::string& text) override;
46 
47         ErrCode ChangeWindowMode(int windowId, uint32_t mode) override;
48 
49         ErrCode TerminateWindow(int windowId) override;
50 
51         ErrCode MinimizeWindow(int windowId) override;
52 
53         ErrCode PublishCommonEvent(const EventFwk::CommonEventData &event, bool &re) override;
54 
55         ErrCode FrequencyLock() override;
56 
57         ErrCode SpDaemonProcess(int daemonCommand, const std::string& extraInfo) override;
58 
59         ErrCode CollectProcessMemory(const int32_t pid, ProcessMemoryInfo &processMemoryInfo) override;
60 
61         ErrCode CollectProcessCpu(const int32_t pid, bool isNeedUpdate, ProcessCpuInfo &processCpuInfo) override;
62 
63     protected:
64         void OnStart() override;
65         void OnStop() override;
66         virtual bool IsRootVersion();
67         virtual bool IsDeveloperMode();
68         void AddCaller();
69         void RemoveCaller();
70         int GetCallerCount();
71         void DestorySession();
72         bool RemoveTestServer();
73 
74     private:
75         std::atomic<int> callerCount_{0};
76 
77         class TestServerProxyDeathRecipient : public IRemoteObject::DeathRecipient {
78         public:
TestServerProxyDeathRecipient(const sptr<TestServerService> & testServerService)79             explicit TestServerProxyDeathRecipient(const sptr<TestServerService> &testServerService)
80                 : testServerService_(testServerService){};
81             ~TestServerProxyDeathRecipient() = default;
82             void OnRemoteDied(const wptr<IRemoteObject> &object) override;
83 
84         private:
85             sptr<TestServerService> testServerService_;
86         };
87 
88         class CallerDetectTimer {
89         public:
CallerDetectTimer(const sptr<TestServerService> & testServerService)90             explicit CallerDetectTimer(const sptr<TestServerService> &testServerService)
91                 : testServerService_(testServerService), testServerExit_(false){};
92             void Start();
93             void Cancel();
94 
95         private:
96             std::thread thread_;
97             sptr<TestServerService> testServerService_;
98             bool testServerExit_;
99         };
100 
101         CallerDetectTimer* callerDetectTimer_;
102         std::shared_ptr<HiviewDFX::UCollectUtil::CpuCollector> cpuCollector_;
103     };
104 } // namespace OHOS::testserver
105 
106 #endif // TEST_SERVER_SERVICE_H