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 "data_ability_observer_interface.h" 25 #include "datashare_proxy.h" 26 #include "executor_pool.h" 27 #include "want.h" 28 29 namespace OHOS { 30 namespace DataShare { 31 using namespace AppExecFwk; 32 class DataShareConnection : public AAFwk::AbilityConnectionStub, 33 public std::enable_shared_from_this<DataShareConnection> { 34 public: uri_(uri)35 DataShareConnection(const Uri &uri, const sptr<IRemoteObject> &token, int32_t waitTime = 2) : uri_(uri), 36 token_(token), waitTime_(waitTime) {} 37 virtual ~DataShareConnection(); 38 39 /** 40 * @brief This method is called back to receive the connection result after an ability calls the 41 * ConnectAbility method to connect it to an extension ability. 42 * 43 * @param element: Indicates information about the connected extension ability. 44 * @param remote: Indicates the remote proxy object of the extension ability. 45 * @param resultCode: Indicates the connection result code. The value 0 indicates a successful connection, and any 46 * other value indicates a connection failure. 47 */ 48 void OnAbilityConnectDone( 49 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override; 50 51 /** 52 * @brief This method is called back to receive the disconnection result after the connected extension ability 53 * crashes or is killed. If the extension ability exits unexpectedly, all its connections are disconnected, and 54 * each ability previously connected to it will call onAbilityDisconnectDone. 55 * 56 * @param element: Indicates information about the disconnected extension ability. 57 * @param resultCode: Indicates the disconnection result code. The value 0 indicates a successful disconnection, 58 * and any other value indicates a disconnection failure. 59 */ 60 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; 61 62 /** 63 * @brief disconnect remote ability of DataShareExtAbility. 64 */ 65 void DisconnectDataShareExtAbility(); 66 67 /** 68 * @brief get the proxy of datashare extension ability. 69 * 70 * @return the proxy of datashare extension ability. 71 */ 72 std::shared_ptr<DataShareProxy> GetDataShareProxy(const Uri &uri, const sptr<IRemoteObject> &token); 73 74 void SetConnectInvalid(); 75 76 void UpdateObserverExtsProviderMap(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver, 77 bool isDescendants); 78 79 void DeleteObserverExtsProviderMap(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 80 81 private: 82 struct DataShareConnectionInfo { 83 int count = 0; 84 int64_t firstTime = 0; 85 int64_t prevTime = 0; 86 }; 87 struct ConnectCondition { 88 std::condition_variable condition; 89 }; 90 std::shared_ptr<DataShareProxy> ConnectDataShareExtAbility(const Uri &uri, const sptr<IRemoteObject> &token); 91 std::shared_ptr<DataShareProxy> GetDataShareProxy(); 92 ErrCode Disconnect(); 93 void ReconnectExtAbility(const std::string &uri); 94 void DelayConnectExtAbility(const std::string &uri); 95 void ReRegisterObserverExtProvider(); 96 std::mutex mutex_{}; 97 std::shared_ptr<DataShareProxy> dataShareProxy_; 98 ConnectCondition condition_; 99 Uri uri_; 100 sptr<IRemoteObject> token_ = {}; 101 std::atomic<bool> isInvalid_ = false; 102 static constexpr int MAX_THREADS = 2; 103 static constexpr int MIN_THREADS = 0; 104 static constexpr int MAX_RECONNECT = 6; 105 static constexpr std::chrono::milliseconds RECONNECT_TIME_INTERVAL = std::chrono::milliseconds(10000); 106 static constexpr std::chrono::milliseconds MAX_RECONNECT_TIME_INTERVAL = std::chrono::milliseconds(70000); 107 std::shared_ptr<ExecutorPool> pool_; 108 DataShareConnectionInfo reConnects_; 109 struct Param { ParamParam110 Param(const Uri &uri, bool isDescendants) : uri(uri), isDescendants(isDescendants){}; 111 Uri uri; 112 bool isDescendants; 113 }; 114 // Store observers that have been successfully registered, allowing them to be re-registered during reconnection. 115 ConcurrentMap<sptr<AAFwk::IDataAbilityObserver>, std::list<Param>> observerExtsProvider_; 116 int32_t waitTime_ = 0; 117 // An atomic variable used to identify the re-registration status. 118 std::atomic<bool> isReconnect_ = false; 119 }; 120 } // namespace DataShare 121 } // namespace OHOS 122 #endif // DATASHARE_CONNECTION_H