1 /*
2  * Copyright (c) 2021-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 DATA_STORAGE_OPKEY_ABILITY_H
17 #define DATA_STORAGE_OPKEY_ABILITY_H
18 
19 #include <functional>
20 
21 #include "__mutex_base"
22 #include "ability.h"
23 #include "ability_lifecycle.h"
24 #include "abs_shared_result_set.h"
25 #include "datashare_ext_ability.h"
26 #include "datashare_ext_ability_context.h"
27 #include "datashare_values_bucket.h"
28 #include "iosfwd"
29 #include "map"
30 #include "memory"
31 #include "rdb_opkey_helper.h"
32 #include "rdb_predicates.h"
33 #include "string"
34 #include "vector"
35 #include "want.h"
36 
37 namespace OHOS {
38 namespace NativeRdb {
39 class AbsRdbPredicates;
40 class ResultSet;
41 class DataAbilityPredicates;
42 class ValuesBucket;
43 }
44 namespace Telephony {
45 enum class OpKeyUriType {
46     UNKNOW, OPKEY_INFO, OPKEY_INIT
47 };
48 class OpKeyAbility : public DataShare::DataShareExtAbility {
49 public:
50     OpKeyAbility();
51     virtual ~OpKeyAbility() override;
52     static OpKeyAbility* Create();
53     void DoInit();
54     sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
55     virtual void OnStart(const AppExecFwk::Want &want) override;
56     virtual int BatchInsert(const Uri &uri, const std::vector<DataShare::DataShareValuesBucket> &values) override;
57     virtual int Insert(const Uri &uri, const DataShare::DataShareValuesBucket &value) override;
58     virtual std::shared_ptr<DataShare::DataShareResultSet> Query(const Uri &uri,
59         const DataShare::DataSharePredicates &predicates, std::vector<std::string> &columns,
60         DataShare::DatashareBusinessError &businessError) override;
61     virtual int Update(const Uri &uri, const DataShare::DataSharePredicates &predicates,
62         const DataShare::DataShareValuesBucket &value) override;
63     virtual int Delete(const Uri &uri, const DataShare::DataSharePredicates &predicates) override;
64     virtual std::string GetType(const Uri &uri) override;
65     virtual int OpenFile(const Uri &uri, const std::string &mode) override;
66 
67 private:
68     /**
69      * Parse Uri Type
70      *
71      * @param uri Resource address
72      * @return return OpKeyUriType
73      */
74     OpKeyUriType ParseUriType(Uri &uri);
75 
76     /**
77      * Convert DataSharePredicates to RdbPredicates
78      *
79      * @param tableName table name of the predicates
80      * @param predicates DataSharePredicates
81      */
82     OHOS::NativeRdb::RdbPredicates ConvertPredicates(
83         const std::string &tableName, const DataShare::DataSharePredicates &predicates);
84 
85     /**
86     * Check whether the initialization succeeds
87     *
88     * @return true : succeed ,false : failed
89     */
90     bool IsInitOk();
91 
92 private:
93     RdbOpKeyHelper helper_;
94     std::mutex lock_;
95     bool initDatabaseDir_ = false;
96     bool initRdbStore_ = false;
97 };
98 } // namespace Telephony
99 } // namespace OHOS
100 #endif // DATA_STORAGE_OPKEY_ABILITY_H
101