1 /* 2 * Copyright (c) 2022 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 DATASHARE_CONNECTION_H 17 #define DATASHARE_CONNECTION_H 18 19 #include <condition_variable> 20 #include <memory> 21 #include <mutex> 22 23 #include "ability_connect_callback_stub.h" 24 #include "datashare_proxy.h" 25 #include "event_handler.h" 26 #include "want.h" 27 28 namespace OHOS { 29 namespace DataShare { 30 using namespace AppExecFwk; 31 class DataShareConnection : public AAFwk::AbilityConnectionStub { 32 public: DataShareConnection(const Uri & uri,const sptr<IRemoteObject> & token)33 DataShareConnection(const Uri &uri, const sptr<IRemoteObject> &token) : uri_(uri), token_(token) {} 34 virtual ~DataShareConnection(); 35 36 /** 37 * @brief This method is called back to receive the connection result after an ability calls the 38 * ConnectAbility method to connect it to an extension ability. 39 * 40 * @param element: Indicates information about the connected extension ability. 41 * @param remote: Indicates the remote proxy object of the extension ability. 42 * @param resultCode: Indicates the connection result code. The value 0 indicates a successful connection, and any 43 * other value indicates a connection failure. 44 */ 45 void OnAbilityConnectDone( 46 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override; 47 48 /** 49 * @brief This method is called back to receive the disconnection result after the connected extension ability 50 * crashes or is killed. If the extension ability exits unexpectedly, all its connections are disconnected, and 51 * each ability previously connected to it will call onAbilityDisconnectDone. 52 * 53 * @param element: Indicates information about the disconnected extension ability. 54 * @param resultCode: Indicates the disconnection result code. The value 0 indicates a successful disconnection, 55 * and any other value indicates a disconnection failure. 56 */ 57 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; 58 59 /** 60 * @brief disconnect remote ability of DataShareExtAbility. 61 */ 62 void DisconnectDataShareExtAbility(); 63 64 /** 65 * @brief get the proxy of datashare extension ability. 66 * 67 * @return the proxy of datashare extension ability. 68 */ 69 std::shared_ptr<DataShareProxy> GetDataShareProxy(const Uri &uri, const sptr<IRemoteObject> &token); 70 71 private: 72 struct ConnectCondition { 73 std::condition_variable condition; 74 std::mutex mutex; 75 }; 76 std::shared_ptr<DataShareProxy> ConnectDataShareExtAbility(const Uri &uri, const sptr<IRemoteObject> &token); 77 std::mutex mutex_{}; 78 std::shared_ptr<DataShareProxy> dataShareProxy_; 79 ConnectCondition condition_; 80 Uri uri_; 81 sptr<IRemoteObject> token_ = {}; 82 }; 83 } // namespace DataShare 84 } // namespace OHOS 85 #endif // DATASHARE_CONNECTION_H