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