1 /* 2 * Copyright (C) 2022-2024 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 INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_USER_FILE_CLIENT_H 17 #define INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_USER_FILE_CLIENT_H 18 19 #include "datashare_helper.h" 20 #include "datashare_predicates.h" 21 #include "napi_base_context.h" 22 #include "napi_error.h" 23 #include "napi/native_api.h" 24 #include "napi/native_node_api.h" 25 #include "napi_remote_object.h" 26 #include "rdb_store.h" 27 #include "uri.h" 28 #include "safe_map.h" 29 30 namespace OHOS { 31 namespace Media { 32 #define EXPORT __attribute__ ((visibility ("default"))) 33 class UserFileClient { 34 public: UserFileClient()35 EXPORT UserFileClient() {} ~UserFileClient()36 EXPORT virtual ~UserFileClient() {} 37 EXPORT static bool IsValid(const int32_t userId = -1); 38 EXPORT static napi_status CheckIsStage(napi_env env, napi_callback_info info, bool &result); 39 EXPORT static sptr<IRemoteObject> ParseTokenInStageMode(napi_env env, napi_callback_info info); 40 EXPORT static sptr<IRemoteObject> ParseTokenInAbility(napi_env env, napi_callback_info info); 41 42 EXPORT static void Init(const sptr<IRemoteObject> &token, bool isSetHelper = false, const int32_t userId = -1); 43 EXPORT static void Init(napi_env env, napi_callback_info info, const int32_t userId = -1); 44 EXPORT static std::shared_ptr<DataShare::DataShareResultSet> Query(Uri &uri, 45 const DataShare::DataSharePredicates &predicates, std::vector<std::string> &columns, 46 int &errCode, const int32_t userId = -1); 47 EXPORT static int Insert(Uri &uri, const DataShare::DataShareValuesBucket &value, const int32_t userId = -1); 48 EXPORT static int InsertExt(Uri &uri, const DataShare::DataShareValuesBucket &value, std::string &result, 49 const int32_t userId = -1); 50 EXPORT static int BatchInsert(Uri &uri, const std::vector<DataShare::DataShareValuesBucket> &values); 51 EXPORT static int Delete(Uri &uri, const DataShare::DataSharePredicates &predicates); 52 EXPORT static void NotifyChange(const Uri &uri); 53 EXPORT static void RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 54 EXPORT static void UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 55 EXPORT static int OpenFile(Uri &uri, const std::string &mode, const int32_t userId = -1); 56 EXPORT static int Update(Uri &uri, const DataShare::DataSharePredicates &predicates, 57 const DataShare::DataShareValuesBucket &value, const int32_t userId = -1); 58 EXPORT static void RegisterObserverExt(const Uri &uri, 59 std::shared_ptr<DataShare::DataShareObserver> dataObserver, bool isDescendants); 60 EXPORT static void UnregisterObserverExt(const Uri &uri, 61 std::shared_ptr<DataShare::DataShareObserver> dataObserver); 62 EXPORT static void Clear(); 63 EXPORT static std::shared_ptr<NativeRdb::ResultSet> QueryRdb(Uri &uri, 64 const DataShare::DataSharePredicates &predicates, std::vector<std::string> &columns); 65 EXPORT static std::string GetType(Uri &uri); 66 EXPORT static void SetUserId(const int32_t userId); 67 EXPORT static int32_t GetUserId(); 68 EXPORT static std::shared_ptr<DataShare::DataShareHelper> GetDataShareHelperByUser(const int32_t userId); 69 private: 70 static inline std::shared_ptr<DataShare::DataShareHelper> sDataShareHelper_ = nullptr; 71 static std::shared_ptr<DataShare::DataShareHelper> GetDataShareHelper(napi_env env, napi_callback_info info, 72 const int32_t userId = -1); 73 static int32_t userId_; 74 static SafeMap<int32_t, std::shared_ptr<DataShare::DataShareHelper>> dataShareHelperMap_; 75 }; 76 } 77 } 78 79 #endif // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_USER_FILE_CLIENT_H