1 /*
2  * Copyright (c) 2021 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 OHOS_ABILITY_RUNTIME_DATA_ABILITY_OPERATION_H
17 #define OHOS_ABILITY_RUNTIME_DATA_ABILITY_OPERATION_H
18 
19 #include <map>
20 #include <memory>
21 #include "data_ability_operation_builder.h"
22 #include "uri.h"
23 #include "parcel.h"
24 
25 using Uri = OHOS::Uri;
26 
27 namespace OHOS {
28 namespace NativeRdb {
29 class DataAbilityPredicates;
30 class ValuesBucket;
31 }
32 namespace AppExecFwk {
33 class DataAbilityOperationBuilder;
34 class DataAbilityOperation final : public Parcelable, public std::enable_shared_from_this<DataAbilityOperation> {
35 public:
36     ~DataAbilityOperation();
37 
38     DataAbilityOperation(
39         const std::shared_ptr<DataAbilityOperation> &dataAbilityOperation, const std::shared_ptr<Uri> &withUri);
40     explicit DataAbilityOperation(Parcel &in);
41     explicit DataAbilityOperation(const std::shared_ptr<DataAbilityOperationBuilder> &builder);
42     DataAbilityOperation();
43     /**
44      * @brief Creates an operation for inserting data.
45      * @param uri Indicates the path of data to operate.
46      * @return Returns an insert DataAbilityOperationBuilder object.
47      */
48     static std::shared_ptr<DataAbilityOperationBuilder> NewInsertBuilder(const std::shared_ptr<Uri> &uri);
49     /**
50      * @brief Creates an operation for updating data.
51      * @param uri Indicates the path of data to operate.
52      * @return Returns an update DataAbilityOperationBuilder object.
53      */
54     static std::shared_ptr<DataAbilityOperationBuilder> NewUpdateBuilder(const std::shared_ptr<Uri> &uri);
55     /**
56      * @brief Creates an operation for deleting data.
57      * @param uri Indicates the path of data to operate.
58      * @return Returns an delete DataAbilityOperationBuilder object.
59      */
60     static std::shared_ptr<DataAbilityOperationBuilder> NewDeleteBuilder(const std::shared_ptr<Uri> &uri);
61     /**
62      * @brief Creates an operation for asserting data.
63      * @param uri Indicates the path of data to operate.
64      * @return Returns an assert DataAbilityOperationBuilder object.
65      */
66     static std::shared_ptr<DataAbilityOperationBuilder> NewAssertBuilder(const std::shared_ptr<Uri> &uri);
67     /**
68      * @brief Obtains the value of the type attribute included in this DataAbilityOperation.
69      * @return Returns the type included in this DataAbilityOperation.
70      */
71     int GetType() const;
72     /**
73      * @brief Obtains the value of the uri attribute included in this DataAbilityOperation.
74      * @return Returns the uri included in this DataAbilityOperation.
75      */
76     std::shared_ptr<Uri> GetUri() const;
77     /**
78      * @brief Obtains the value of the valuesBucket attribute included in this DataAbilityOperation.
79      * @return Returns the valuesBucket included in this DataAbilityOperation.
80      */
81     std::shared_ptr<NativeRdb::ValuesBucket> GetValuesBucket() const;
82     /**
83      * @brief Obtains the value of the expectedCount attribute included in this DataAbilityOperation.
84      * @return Returns the expectedCount included in this DataAbilityOperation.
85      */
86     int GetExpectedCount() const;
87     /**
88      * @brief Obtains the value of the dataAbilityPredicates attribute included in this DataAbilityOperation.
89      * @return Returns the dataAbilityPredicates included in this DataAbilityOperation.
90      */
91     std::shared_ptr<NativeRdb::DataAbilityPredicates> GetDataAbilityPredicates() const;
92     /**
93      * @brief Obtains the value of the valuesBucketReferences attribute included in this DataAbilityOperation.
94      * @return Returns the valuesBucketReferences included in this DataAbilityOperation.
95      */
96     std::shared_ptr<NativeRdb::ValuesBucket> GetValuesBucketReferences() const;
97     /**
98      * @brief Obtains the value of the dataAbilityPredicatesBackReferences attribute included in this
99      * DataAbilityOperation.
100      * @return Returns the dataAbilityPredicatesBackReferences included in this DataAbilityOperation.
101      */
102     std::map<int, int> GetDataAbilityPredicatesBackReferences() const;
103     /**
104      * @brief Checks whether an insert operation is created.
105      * @return Returns true if it is an insert operation; returns false otherwise.
106      */
107     bool IsValidOperation() const;
108     /**
109      * @brief Checks whether an operation is valid.
110      * @return Returns true if it is an insert or delete or update or assert operation; returns false otherwise.
111      */
112     bool IsInsertOperation() const;
113     /**
114      * @brief Checks whether an delete operation is created.
115      * @return Returns true if it is an delete operation; returns false otherwise.
116      */
117     bool IsDeleteOperation() const;
118     /**
119      * @brief Checks whether an update operation is created.
120      * @return Returns true if it is an update operation; returns false otherwise.
121      */
122     bool IsUpdateOperation() const;
123     /**
124      * @brief Checks whether an assert operation is created.
125      * @return Returns true if it is an assert operation; returns false otherwise.
126      */
127     bool IsAssertOperation() const;
128     /**
129      * @brief Checks whether an operation can be interrupted.
130      * @return Returns true if the operation can be interrupted; returns false otherwise.
131      */
132     bool IsInterruptionAllowed() const;
133 
134     bool operator==(const DataAbilityOperation &other) const;
135     DataAbilityOperation &operator=(const DataAbilityOperation &other);
136     bool Marshalling(Parcel &out) const;
137     static DataAbilityOperation *Unmarshalling(Parcel &in);
138 
139     /**
140      * @brief Creates a DataAbilityOperation instance based on the given Parcel object
141      * @param in Indicates the Parcel object.
142      * @return Returns the DataAbilityOperation object.
143      */
144     static std::shared_ptr<DataAbilityOperation> CreateFromParcel(Parcel &in);
145 
146 public:
147     static constexpr int TYPE_INSERT = 1;
148     static constexpr int TYPE_UPDATE = 2;
149     static constexpr int TYPE_DELETE = 3;
150     static constexpr int TYPE_ASSERT = 4;
151 
152 private:
153     void PutMap(Parcel &in);
154     bool ReadFromParcel(Parcel &in);
155     bool ReadUriFromParcel(Parcel &in);
156     bool ReadValuesBucketFromParcel(Parcel &in);
157     bool ReadDataAbilityPredicatesFromParcel(Parcel &in);
158     bool ReadValuesBucketReferencesFromParcel(Parcel &in);
159     bool WriteUri(Parcel &out) const;
160     bool WriteValuesBucket(Parcel &out) const;
161     bool WritePredicates(Parcel &out) const;
162     bool WriteValuesBucketReferences(Parcel &out) const;
163 
164 private:
165     // no object in parcel
166     static constexpr int VALUE_NULL = 0;
167     // object exist in parcel
168     static constexpr int VALUE_OBJECT = 1;
169     static constexpr int REFERENCE_THRESHOLD = 3 * 1024 * 1024;
170     int type_ = -1;
171     int expectedCount_ = 0;
172     bool interrupted_ = false;
173     std::shared_ptr<Uri> uri_ = nullptr;
174     std::shared_ptr<NativeRdb::ValuesBucket> valuesBucket_ = nullptr;
175     std::shared_ptr<NativeRdb::DataAbilityPredicates> dataAbilityPredicates_ = nullptr;
176     std::shared_ptr<NativeRdb::ValuesBucket> valuesBucketReferences_ = nullptr;
177     std::map<int, int> dataAbilityPredicatesBackReferences_;
178 };
179 }  // namespace AppExecFwk
180 }  // namespace OHOS
181 #endif  // OHOS_ABILITY_RUNTIME_DATA_ABILITY_OPERATION_H
182