1 /* 2 * Copyright (c) 2023-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 OHOS_ROSEN_SCREEN_SESSION_ABILITY_CONNECTION_H 17 #define OHOS_ROSEN_SCREEN_SESSION_ABILITY_CONNECTION_H 18 19 #include <string> 20 #include <mutex> 21 #include <condition_variable> 22 23 #include "ability_connection.h" 24 #include "iremote_object.h" 25 26 namespace OHOS::Rosen { 27 class ScreenSessionAbilityDeathRecipient : public IRemoteObject::DeathRecipient { 28 public: ScreenSessionAbilityDeathRecipient(std::function<void (void)> deathHandler)29 explicit ScreenSessionAbilityDeathRecipient( 30 std::function<void(void)> deathHandler) : deathHandler_(deathHandler) {} 31 ~ScreenSessionAbilityDeathRecipient() = default; 32 void OnRemoteDied(const wptr<IRemoteObject> &remoteObject) override; 33 34 private: 35 std::function<void(void)> deathHandler_; 36 }; 37 38 class ScreenSessionAbilityConnectionStub : public AAFwk::AbilityConnectionStub { 39 public: 40 explicit ScreenSessionAbilityConnectionStub() = default; 41 virtual ~ScreenSessionAbilityConnectionStub() = default; 42 43 void OnAbilityConnectDone(const AppExecFwk::ElementName &element, 44 const sptr<IRemoteObject> &remoteObject, int32_t resultCode) override; 45 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode) override; 46 int32_t SendMessageSync(int32_t transCode, MessageParcel &data, MessageParcel &reply); 47 int32_t SendMessageSyncBlock(int32_t transCode, MessageParcel &data, MessageParcel &reply); 48 bool IsAbilityConnected(); 49 bool IsAbilityConnectedSync(); 50 int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; 51 int32_t GetScreenId(); 52 int32_t GetLeft(); 53 int32_t GetTop(); 54 int32_t GetWidth(); 55 int32_t GetHeight(); 56 int32_t GetErrCode(); 57 void EraseErrCode(); 58 59 private: 60 bool AddObjectDeathRecipient(); 61 62 private: 63 std::atomic<bool> isConnected_{false}; 64 sptr<IRemoteObject> remoteObject_; 65 std::mutex remoteObjectMutex_; 66 sptr<ScreenSessionAbilityDeathRecipient> deathRecipient_; 67 std::mutex connectedMutex_; 68 std::condition_variable connectedCv_; 69 std::mutex sendMessageMutex_; 70 std::condition_variable blockSendMessageCV_; 71 std::atomic<bool> sendMessageWaitFlag_{false}; 72 int32_t screenId_ = 0; 73 int32_t left_ = 0; 74 int32_t top_ = 0; 75 int32_t width_ = 0; 76 int32_t height_ = 0; 77 uint32_t errCode_ = 0; 78 }; 79 80 class ScreenSessionAbilityConnection { 81 public: 82 explicit ScreenSessionAbilityConnection() = default; 83 ~ScreenSessionAbilityConnection() = default; 84 85 bool ScreenSessionConnectExtension(const std::string &bundleName, const std::string &abilityName); 86 bool ScreenSessionConnectExtension(const std::string &bundleName, const std::string &abilityName, 87 const std::vector<std::pair<std::string, std::string>> ¶mMap); 88 void ScreenSessionDisconnectExtension(); 89 int32_t SendMessage(const int32_t &transCode, MessageParcel &data, MessageParcel &reply); 90 int32_t SendMessageBlock(const int32_t &transCode, MessageParcel &data, MessageParcel &reply); 91 bool IsConnected(); 92 bool IsConnectedSync(); 93 sptr<ScreenSessionAbilityConnectionStub> GetScreenSessionAbilityConnectionStub(); 94 95 private: 96 sptr<ScreenSessionAbilityConnectionStub> abilityConnectionStub_; 97 }; 98 } // namespace OHOS::Rosen 99 100 #endif // OHOS_ROSEN_SCREEN_SESSION_ABILITY_CONNECTION_H