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