• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 NAPI_DATASHARE_HELPER_H
17 #define NAPI_DATASHARE_HELPER_H
18 
19 #include <memory>
20 
21 #include "napi_subscriber_manager.h"
22 #include "async_call.h"
23 #include "data_share_common.h"
24 #include "datashare_helper.h"
25 #include "napi_datashare_observer.h"
26 
27 namespace OHOS {
28 namespace DataShare {
29 class NapiDataShareHelper {
30 public:
31     static napi_value Napi_CreateDataShareHelper(napi_env env, napi_callback_info info);
32     static napi_value Napi_OpenFile(napi_env env, napi_callback_info info);
33     static napi_value Napi_On(napi_env env, napi_callback_info info);
34     static napi_value Napi_Off(napi_env env, napi_callback_info info);
35     static napi_value Napi_Insert(napi_env env, napi_callback_info info);
36     static napi_value Napi_Delete(napi_env env, napi_callback_info info);
37     static napi_value Napi_Query(napi_env env, napi_callback_info info);
38     static napi_value Napi_Update(napi_env env, napi_callback_info info);
39     static napi_value Napi_BatchInsert(napi_env env, napi_callback_info info);
40     static napi_value Napi_GetType(napi_env env, napi_callback_info info);
41     static napi_value Napi_GetFileTypes(napi_env env, napi_callback_info info);
42     static napi_value Napi_NormalizeUri(napi_env env, napi_callback_info info);
43     static napi_value Napi_DenormalizeUri(napi_env env, napi_callback_info info);
44     static napi_value Napi_NotifyChange(napi_env env, napi_callback_info info);
45     static napi_value Napi_AddTemplate(napi_env env, napi_callback_info info);
46     static napi_value Napi_DelTemplate(napi_env env, napi_callback_info info);
47     static napi_value Napi_Publish(napi_env env, napi_callback_info info);
48     static napi_value Napi_GetPublishedData(napi_env env, napi_callback_info info);
49 
50 private:
51     static napi_value GetConstructor(napi_env env);
52     static napi_value Initialize(napi_env env, napi_callback_info info);
53     static napi_value Napi_SubscribeRdbObserver(napi_env env, size_t argc, napi_value *argv, napi_value self);
54     static napi_value Napi_UnsubscribeRdbObserver(napi_env env, size_t argc, napi_value *argv, napi_value self);
55     static napi_value Napi_SubscribePublishedObserver(napi_env env, size_t argc, napi_value *argv, napi_value self);
56     static napi_value Napi_UnsubscribePublishedObserver(napi_env env, size_t argc, napi_value *argv, napi_value self);
57 
58     bool HasRegisteredObserver(napi_env env, std::list<sptr<NAPIDataShareObserver>> &list, napi_value callback);
59     void RegisteredObserver(napi_env env, const std::string &uri, napi_value callback);
60     void UnRegisteredObserver(napi_env env, const std::string &uri, napi_value callback);
61     void UnRegisteredObserver(napi_env env, const std::string &uri);
62     static bool GetOptions(napi_env env, napi_value jsValue, CreateOptions &options);
63     std::shared_ptr<DataShareHelper> datashareHelper_ = nullptr;
64     std::map<std::string, std::list<sptr<NAPIDataShareObserver>>> observerMap_;
65     std::mutex listMutex_{};
66     std::shared_ptr<NapiRdbSubscriberManager> jsRdbObsManager_ = nullptr;
67     std::shared_ptr<NapiPublishedSubscriberManager> jsPublishedObsManager_ = nullptr;
68 
69     struct CreateContextInfo : public AsyncCall::Context {
70         napi_env env = nullptr;
71         napi_ref ref = nullptr;
72         bool isStageMode = true;
73         CreateOptions options;
74         std::string strUri;
75         std::shared_ptr<AppExecFwk::Context> contextF = nullptr;
76         std::shared_ptr<OHOS::AbilityRuntime::Context> contextS = nullptr;
77         std::shared_ptr<DataShareHelper> dataShareHelper = nullptr;
CreateContextInfoCreateContextInfo78         CreateContextInfo() : Context(nullptr, nullptr) {};
CreateContextInfoCreateContextInfo79         CreateContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
~CreateContextInfoCreateContextInfo80         ~CreateContextInfo()
81         {
82             if (env != nullptr && ref != nullptr) {
83                 napi_delete_reference(env, ref);
84             }
85         }
86     };
87 
88     struct ContextInfo : public AsyncCall::Context {
89         NapiDataShareHelper *proxy = nullptr;
90         napi_status status = napi_generic_failure;
91         int resultNumber = 0;
92         std::shared_ptr<DataShareResultSet> resultObject = nullptr;
93         std::string resultString = "";
94         std::vector<std::string> resultStrArr;
95         std::string uri;
96         std::string mode;
97         DataShareValuesBucket valueBucket;
98         DataSharePredicates predicates;
99         std::vector<std::string> columns;
100         std::vector<DataShareValuesBucket> values;
101         std::string mimeTypeFilter;
102         DatashareBusinessError businessError;
103         Data publishData;
104         std::string bundleName;
105         std::vector<OperationResult> results;
106 
ContextInfoContextInfo107         ContextInfo() : Context(nullptr, nullptr) {};
ContextInfoContextInfo108         ContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
~ContextInfoContextInfo109         virtual ~ContextInfo() {};
110 
operatorContextInfo111         napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
112         {
113             NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
114             NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&proxy)), napi_invalid_arg);
115             NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native upload task", napi_invalid_arg);
116             return Context::operator()(env, argc, argv, self);
117         }
operatorContextInfo118         napi_status operator()(napi_env env, napi_value *result) override
119         {
120             if (status != napi_ok) {
121                 return status;
122             }
123             return Context::operator()(env, result);
124         }
125     };
126 };
127 } // namespace DataShare
128 } // namespace OHOS
129 #endif /* NAPI_DATASHARE_HELPER_H */
130