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 30 namespace OHOS::testserver { 31 class TestServerService : public SystemAbility, public TestServerInterfaceStub { 32 DECLARE_SYSTEM_ABILITY(TestServerService); 33 34 public: 35 TestServerService(int32_t saId, bool runOnCreate); 36 ~TestServerService(); 37 38 void StartCallerDetectTimer(); 39 void KillProcess(const std::string& processName); 40 ErrCode CreateSession(const SessionToken &sessionToken) override; 41 42 // Declare the logical interfaces here 43 ErrCode SetPasteData(const std::string& text) override; 44 45 ErrCode PublishCommonEvent(const EventFwk::CommonEventData &event, bool &re) override; 46 47 ErrCode FrequencyLock() override; 48 49 ErrCode SpDaemonProcess(int daemonCommand) override; 50 51 protected: 52 void OnStart() override; 53 void OnStop() override; 54 virtual bool IsRootVersion(); 55 virtual bool IsDeveloperMode(); 56 void AddCaller(); 57 void RemoveCaller(); 58 int GetCallerCount(); 59 void DestorySession(); 60 bool RemoveTestServer(); 61 62 private: 63 std::atomic<int> callerCount_{0}; 64 65 class TestServerProxyDeathRecipient : public IRemoteObject::DeathRecipient { 66 public: TestServerProxyDeathRecipient(const sptr<TestServerService> & testServerService)67 explicit TestServerProxyDeathRecipient(const sptr<TestServerService> &testServerService) 68 : testServerService_(testServerService){}; 69 ~TestServerProxyDeathRecipient() = default; 70 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 71 72 private: 73 sptr<TestServerService> testServerService_; 74 }; 75 76 class CallerDetectTimer { 77 public: CallerDetectTimer(const sptr<TestServerService> & testServerService)78 explicit CallerDetectTimer(const sptr<TestServerService> &testServerService) 79 : testServerService_(testServerService), testServerExit_(false){}; 80 void Start(); 81 void Cancel(); 82 83 private: 84 std::thread thread_; 85 sptr<TestServerService> testServerService_; 86 bool testServerExit_; 87 }; 88 89 CallerDetectTimer* callerDetectTimer_; 90 }; 91 } // namespace OHOS::testserver 92 93 #endif // TEST_SERVER_SERVICE_H