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