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 "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:
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 private:
77     struct DataShareConnectionInfo {
78         int count = 0;
79         int64_t firstTime = 0;
80         int64_t prevTime = 0;
81     };
82     struct ConnectCondition {
83         std::condition_variable condition;
84         std::mutex mutex;
85     };
86     std::shared_ptr<DataShareProxy> ConnectDataShareExtAbility(const Uri &uri, const sptr<IRemoteObject> &token);
87     std::shared_ptr<DataShareProxy> GetDataShareProxy();
88     ErrCode Disconnect();
89     void ReconnectExtAbility(const std::string &uri);
90     void DelayConnectExtAbility(const std::string &uri);
91     std::mutex mutex_{};
92     std::shared_ptr<DataShareProxy> dataShareProxy_;
93     ConnectCondition condition_;
94     Uri uri_;
95     sptr<IRemoteObject> token_ = {};
96     std::atomic<bool> isInvalid_ = false;
97     static constexpr int MAX_THREADS = 2;
98     static constexpr int MIN_THREADS = 0;
99     static constexpr int MAX_RECONNECT = 6;
100     static constexpr std::chrono::milliseconds RECONNECT_TIME_INTERVAL = std::chrono::milliseconds(10000);
101     static constexpr std::chrono::milliseconds MAX_RECONNECT_TIME_INTERVAL = std::chrono::milliseconds(70000);
102     std::shared_ptr<ExecutorPool> pool_;
103     DataShareConnectionInfo reConnects_;
104     int32_t waitTime_ = 0;
105 };
106 }  // namespace DataShare
107 }  // namespace OHOS
108 #endif  // DATASHARE_CONNECTION_H