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 #ifndef PPSOPENHARMONYSA_OPEN_CONNECT_ADS_STUB_H 16 #define PPSOPENHARMONYSA_OPEN_CONNECT_ADS_STUB_H 17 18 #include "extension_manager_client.h" 19 #include "ability_connect_callback_stub.h" 20 #include "oaid_common.h" 21 #include "oaid_service_define.h" 22 #include "config_policy_utils.h" 23 #include "distributed_kv_data_manager.h" 24 #include "oaid_service.h" 25 #include "iremote_broker.h" 26 #include "iremote_stub.h" 27 #include "message_parcel.h" 28 #include "message_option.h" 29 #include "oaid_file_operator.h" 30 #include "cJSON.h" 31 #include <mutex> 32 #include <queue> 33 #include <unordered_set> 34 35 namespace OHOS { 36 namespace Cloud { 37 38 // 连接状态枚举 39 enum class ConnectionState { 40 DISCONNECTED, 41 CONNECTING, 42 CONNECTED 43 }; 44 class AdsCallback : public IRemoteBroker { 45 public: 46 DECLARE_INTERFACE_DESCRIPTOR(u"ohos.cloud.oaid.AdsCallback"); 47 }; 48 49 class ADSCallbackStub : public IRemoteStub<AdsCallback> { 50 public: 51 DISALLOW_COPY_AND_MOVE(ADSCallbackStub); 52 ADSCallbackStub() = default; 53 virtual ~ADSCallbackStub() = default; 54 55 int OnRemoteRequest( 56 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; 57 }; 58 59 class ConnectAdsStub : public AAFwk::AbilityConnectionStub { 60 public: 61 void OnAbilityConnectDone(const AppExecFwk::ElementName &element, 62 const sptr<IRemoteObject> &remoteObject, 63 int resultCode) override; 64 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, 65 int resultCode) override; 66 67 sptr<IRemoteObject> GetRemoteObject(); 68 ConnectionState GetConnectionState() const; 69 void SetConnectionState(ConnectionState state); 70 sptr<IRemoteObject> GetProxy() const; 71 void SetProxy(const sptr<IRemoteObject> &remoteObject); 72 void AddMessageToQueue(int32_t code); 73 void ProcessMessageQueue(); 74 void DisconnectIfIdle(); 75 void SendMessage(int32_t code); 76 77 static void setToken(std::u16string token); 78 static void setCodeOaid(std::int32_t code); 79 80 private: 81 sptr<IRemoteObject> proxy_; 82 ConnectionState connectionState_ = ConnectionState::DISCONNECTED; 83 std::queue<int32_t> messageQueue_; 84 std::unordered_set<int32_t> messageSet_; 85 static std::u16string OAID_INFO_TOKEN; 86 static std::mutex queueMutex_; 87 static std::int32_t CODE_OAID; 88 static std::mutex setTokenMutex_; 89 static std::mutex setOaidMutex_; 90 static std::mutex stateMutex_; 91 static std::mutex proxyMutex_; 92 }; 93 94 class ConnectAdsManager { 95 public: 96 static ConnectAdsManager* GetInstance(); 97 int32_t DisconnectService(); 98 AAFwk::Want getWantInfo(); 99 bool checkAllowGetOaid(); 100 void notifyKit(int32_t code); 101 sptr<ConnectAdsStub> getConnection(); 102 103 private: 104 ConnectAdsManager(); 105 ~ConnectAdsManager(); 106 ConnectAdsManager(const ConnectAdsManager&) = delete; 107 ConnectAdsManager& operator=(const ConnectAdsManager&) = delete; 108 109 static std::mutex connectMutex_; 110 sptr<ConnectAdsStub> connectObject_; 111 int32_t DEFAULT_VALUE = -1; 112 }; 113 114 } // namespace Cloud 115 } // namespace OHOS 116 #endif //PPSOPENHARMONYSA_OPEN_CONNECT_ADS_STUB_H 117