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