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 FOUNDATION_ABILITYRUNTIME_OHOS_JS_DATASHARE_EXT_ABILITY_H 17 #define FOUNDATION_ABILITYRUNTIME_OHOS_JS_DATASHARE_EXT_ABILITY_H 18 19 #include "abs_shared_result_set.h" 20 #include "data_ability_predicates.h" 21 #include "datashare_ext_ability.h" 22 #include "js_runtime.h" 23 #include "native_engine/native_reference.h" 24 #include "native_engine/native_value.h" 25 #include "values_bucket.h" 26 27 namespace OHOS { 28 namespace AbilityRuntime { 29 using RdbValueBucketNewInstance = napi_value (*)(napi_env env, OHOS::NativeRdb::ValuesBucket& valueBucket); 30 using RdbResultSetProxyGetNativeObject = OHOS::NativeRdb::AbsSharedResultSet* (*)(const napi_env& env, 31 const napi_value& arg); 32 using DataAbilityPredicatesNewInstance = napi_value (*)( 33 napi_env env, OHOS::NativeRdb::DataAbilityPredicates* predicates); 34 35 /** 36 * @brief Basic datashare extension ability components. 37 */ 38 class JsDataShareExtAbility : public DataShareExtAbility { 39 public: 40 JsDataShareExtAbility(JsRuntime& jsRuntime); 41 virtual ~JsDataShareExtAbility() override; 42 43 /** 44 * @brief Create JsDataShareExtAbility. 45 * 46 * @param runtime The runtime. 47 * @return The JsDataShareExtAbility instance. 48 */ 49 static JsDataShareExtAbility* Create(const std::unique_ptr<Runtime>& runtime); 50 51 /** 52 * @brief Init the extension. 53 * 54 * @param record the extension record. 55 * @param application the application info. 56 * @param handler the extension handler. 57 * @param token the remote token. 58 */ 59 void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record, 60 const std::shared_ptr<AppExecFwk::OHOSApplication> &application, 61 std::shared_ptr<AppExecFwk::AbilityHandler> &handler, 62 const sptr<IRemoteObject> &token) override; 63 64 /** 65 * @brief Called when this datashare extension ability is started. You must override this function if you want to 66 * perform some initialization operations during extension startup. 67 * 68 * This function can be called only once in the entire lifecycle of an extension. 69 * @param Want Indicates the {@link Want} structure containing startup information about the extension. 70 */ 71 void OnStart(const AAFwk::Want &want) override; 72 73 /** 74 * @brief Called when this datashare extension ability is connected for the first time. 75 * 76 * You can override this function to implement your own processing logic. 77 * 78 * @param want Indicates the {@link Want} structure containing connection information about the datashare 79 * extension. 80 * @return Returns a pointer to the <b>sid</b> of the connected datashare extension ability. 81 */ 82 sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override; 83 84 /** 85 * @brief Obtains the MIME types of files supported. 86 * 87 * @param uri Indicates the path of the files to obtain. 88 * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null. 89 * 90 * @return Returns the matched MIME types. If there is no match, null is returned. 91 */ 92 std::vector<std::string> GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter) override; 93 94 /** 95 * @brief Opens a file in a specified remote path. 96 * 97 * @param uri Indicates the path of the file to open. 98 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access 99 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 100 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data, 101 * or "rwt" for read and write access that truncates any existing file. 102 * 103 * @return Returns the file descriptor. 104 */ 105 int OpenFile(const Uri &uri, const std::string &mode) override; 106 107 /** 108 * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets 109 * inside of their .hap. 110 * 111 * @param uri Indicates the path of the file to open. 112 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access 113 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file, 114 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing 115 * data, or "rwt" for read and write access that truncates any existing file. 116 * 117 * @return Returns the RawFileDescriptor object containing file descriptor. 118 */ 119 int OpenRawFile(const Uri &uri, const std::string &mode) override; 120 121 /** 122 * @brief Inserts a single data record into the database. 123 * 124 * @param uri Indicates the path of the data to operate. 125 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted. 126 * 127 * @return Returns the index of the inserted data record. 128 */ 129 int Insert(const Uri &uri, const NativeRdb::ValuesBucket &value) override; 130 131 /** 132 * @brief Updates data records in the database. 133 * 134 * @param uri Indicates the path of data to update. 135 * @param value Indicates the data to update. This parameter can be null. 136 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 137 * 138 * @return Returns the number of data records updated. 139 */ 140 int Update(const Uri &uri, const NativeRdb::ValuesBucket &value, 141 const NativeRdb::DataAbilityPredicates &predicates) override; 142 143 /** 144 * @brief Deletes one or more data records from the database. 145 * 146 * @param uri Indicates the path of the data to operate. 147 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 148 * 149 * @return Returns the number of data records deleted. 150 */ 151 int Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates) override; 152 153 /** 154 * @brief Deletes one or more data records from the database. 155 * 156 * @param uri Indicates the path of data to query. 157 * @param columns Indicates the columns to query. If this parameter is null, all columns are queried. 158 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null. 159 * 160 * @return Returns the query result. 161 */ 162 std::shared_ptr<NativeRdb::AbsSharedResultSet> Query(const Uri &uri, std::vector<std::string> &columns, 163 const NativeRdb::DataAbilityPredicates &predicates) override; 164 165 /** 166 * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be 167 * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG. 168 * 169 * @param uri Indicates the URI of the data. 170 * 171 * @return Returns the MIME type that matches the data specified by uri. 172 */ 173 std::string GetType(const Uri &uri) override; 174 175 /** 176 * @brief Inserts multiple data records into the database. 177 * 178 * @param uri Indicates the path of the data to operate. 179 * @param values Indicates the data records to insert. 180 * 181 * @return Returns the number of data records inserted. 182 */ 183 int BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values) override; 184 185 /** 186 * @brief Registers an observer to DataObsMgr specified by the given Uri. 187 * 188 * @param uri, Indicates the path of the data to operate. 189 * @param dataObserver, Indicates the IDataAbilityObserver object. 190 */ 191 bool RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver) override; 192 193 /** 194 * @brief Deregisters an observer used for DataObsMgr specified by the given Uri. 195 * 196 * @param uri, Indicates the path of the data to operate. 197 * @param dataObserver, Indicates the IDataAbilityObserver object. 198 */ 199 bool UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver) override; 200 201 /** 202 * @brief Notifies the registered observers of a change to the data resource specified by Uri. 203 * 204 * @param uri, Indicates the path of the data to operate. 205 * 206 * @return Return true if success. otherwise return false. 207 */ 208 bool NotifyChange(const Uri &uri) override; 209 210 /** 211 * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used 212 * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if 213 * the context has changed. If you implement URI normalization for a Data ability, you must also implement 214 * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to 215 * any method that is called on the Data ability must require normalization verification and denormalization. The 216 * default implementation of this method returns null, indicating that this Data ability does not support URI 217 * normalization. 218 * 219 * @param uri Indicates the Uri object to normalize. 220 * 221 * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise. 222 */ 223 Uri NormalizeUri(const Uri &uri) override; 224 225 /** 226 * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one. 227 * The default implementation of this method returns the original URI passed to it. 228 * 229 * @param uri uri Indicates the Uri object to denormalize. 230 * 231 * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri 232 * passed to this method if there is nothing to do; returns null if the data identified by the original Uri cannot 233 * be found in the current environment. 234 */ 235 Uri DenormalizeUri(const Uri &uri) override; 236 237 /** 238 * @brief Performs batch operations on the database. 239 * 240 * @param operations Indicates a list of database operations on the database. 241 * @return Returns the result of each operation, in array. 242 */ 243 std::vector<std::shared_ptr<AppExecFwk::DataAbilityResult>> ExecuteBatch( 244 const std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> &operations) override; 245 private: 246 NativeValue* CallObjectMethod(const char* name, NativeValue* const* argv = nullptr, size_t argc = 0); 247 void GetSrcPath(std::string &srcPath); 248 void LoadLibrary(); 249 void UnloadLibrary(); 250 bool CheckCallingPermission(const std::string &permission); 251 napi_value MakePredicates(napi_env env, const NativeRdb::DataAbilityPredicates &predicates); 252 253 JsRuntime& jsRuntime_; 254 std::unique_ptr<NativeReference> jsObj_; 255 void* libRdbHandle_ = nullptr; 256 void* libDataAbilityHandle_ = nullptr; 257 RdbValueBucketNewInstance rdbValueBucketNewInstance_ = nullptr; 258 RdbResultSetProxyGetNativeObject rdbResultSetProxyGetNativeObject_ = nullptr; 259 DataAbilityPredicatesNewInstance dataAbilityPredicatesNewInstance_ = nullptr; 260 }; 261 } // namespace AbilityRuntime 262 } // namespace OHOS 263 #endif // FOUNDATION_ABILITYRUNTIME_OHOS_JS_DATASHARE_EXT_ABILITY_H