• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "message_parcel.h"
22 #include "napi_base_context.h"
23 #include "message_parcel.h"
24 #include "napi_error.h"
25 #include "napi/native_api.h"
26 #include "napi/native_node_api.h"
27 #include "napi_remote_object.h"
28 #include "rdb_store.h"
29 #include "uri.h"
30 #include "safe_map.h"
31 #include "bundle_mgr_interface.h"
32 #include <mutex>
33 
34 namespace OHOS {
35 namespace Media {
36 #define EXPORT __attribute__ ((visibility ("default")))
37 class UserFileClient {
38 public:
UserFileClient()39     EXPORT UserFileClient() {}
~UserFileClient()40     EXPORT virtual ~UserFileClient() {}
41     EXPORT static bool IsValid(const int32_t userId = -1);
42     EXPORT static napi_status CheckIsStage(napi_env env, napi_callback_info info, bool &result);
43     EXPORT static sptr<IRemoteObject> ParseTokenInStageMode(napi_env env, napi_callback_info info);
44     EXPORT static sptr<IRemoteObject> ParseTokenInAbility(napi_env env, napi_callback_info info);
45 
46     EXPORT static void Init(const sptr<IRemoteObject> &token, bool isSetHelper = false, const int32_t userId = -1);
47     EXPORT static void Init(napi_env env, napi_callback_info info, const int32_t userId = -1);
48     EXPORT static std::shared_ptr<DataShare::DataShareResultSet> Query(Uri &uri,
49         const DataShare::DataSharePredicates &predicates, std::vector<std::string> &columns,
50         int &errCode, const int32_t userId = -1);
51     EXPORT static std::pair<bool, std::shared_ptr<DataShare::DataShareResultSet>> QueryAccessibleViaSandBox(Uri &uri,
52         const DataShare::DataSharePredicates &predicates, std::vector<std::string> &columns, int &errCode,
53         const int32_t userId = -1);
54     EXPORT static int Insert(Uri &uri, const DataShare::DataShareValuesBucket &value, const int32_t userId = -1);
55     EXPORT static int InsertExt(Uri &uri, const DataShare::DataShareValuesBucket &value, std::string &result,
56         const int32_t userId = -1);
57     EXPORT static int BatchInsert(Uri &uri, const std::vector<DataShare::DataShareValuesBucket> &values);
58     EXPORT static int Delete(Uri &uri, const DataShare::DataSharePredicates &predicates);
59     EXPORT static void NotifyChange(const Uri &uri);
60     EXPORT static void RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver);
61     EXPORT static void UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver);
62     EXPORT static int OpenFile(Uri &uri, const std::string &mode, const int32_t userId = -1);
63     EXPORT static int OpenFileWithErrCode(
64         Uri &uri, const std::string &mode, int32_t &realErr, const int32_t userId = -1);
65     EXPORT static int Update(Uri &uri, const DataShare::DataSharePredicates &predicates,
66         const DataShare::DataShareValuesBucket &value, const int32_t userId = -1);
67     EXPORT static void RegisterObserverExt(const Uri &uri,
68         std::shared_ptr<DataShare::DataShareObserver> dataObserver, bool isDescendants);
69     EXPORT static void UnregisterObserverExt(const Uri &uri,
70         std::shared_ptr<DataShare::DataShareObserver> dataObserver);
71     EXPORT static void Clear();
72     EXPORT static std::shared_ptr<NativeRdb::ResultSet> QueryRdb(Uri &uri,
73         const DataShare::DataSharePredicates &predicates, std::vector<std::string> &columns);
74     EXPORT static std::shared_ptr<NativeRdb::ResultSet> QueryByStep(const std::string &sql);
75     EXPORT static std::string GetType(Uri &uri);
76     EXPORT static int32_t UserDefineFunc(MessageParcel &data, MessageParcel &reply, MessageOption &option);
77     EXPORT static int32_t UserDefineFunc(const int32_t &userId, MessageParcel &data, MessageParcel &reply,
78         MessageOption &option);
79     EXPORT static void SetUserId(const int32_t userId);
80     EXPORT static int32_t GetUserId();
81     EXPORT static std::shared_ptr<DataShare::DataShareHelper> GetDataShareHelperByUser(const int32_t userId);
82     EXPORT static std::string GetBundleName();
83     EXPORT static int32_t RegisterObserverExtProvider(const Uri &uri,
84         std::shared_ptr<DataShare::DataShareObserver> dataObserver, bool isDescendants);
85     EXPORT static int32_t UnregisterObserverExtProvider(const Uri &uri,
86         std::shared_ptr<DataShare::DataShareObserver> dataObserver);
87 
88 private:
89     static inline std::shared_ptr<DataShare::DataShareHelper> sDataShareHelper_ = nullptr;
90     static std::shared_ptr<DataShare::DataShareHelper> GetDataShareHelper(napi_env env, napi_callback_info info,
91         const int32_t userId = -1);
92     static int32_t userId_;
93     static std::string bundleName_;
94     static SafeMap<int32_t, std::shared_ptr<DataShare::DataShareHelper>> dataShareHelperMap_;
95     static sptr<AppExecFwk::IBundleMgr> GetSysBundleManager();
96     static sptr<AppExecFwk::IBundleMgr> bundleMgr_;
97     static std::mutex bundleMgrMutex_;
98 };
99 }
100 }
101 
102 #endif // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_USER_FILE_CLIENT_H