• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 OHOS_FILEMGMT_CLOUD_SYNC_NAPI_H
17 #define OHOS_FILEMGMT_CLOUD_SYNC_NAPI_H
18 
19 #include "cloud_sync_callback.h"
20 #include "cloud_optimize_callback.h"
21 #include "cloud_sync_common.h"
22 #include "data_ability_observer_interface.h"
23 #include "data_ability_observer_stub.h"
24 #include "dataobs_mgr_client.h"
25 #include "filemgmt_libn.h"
26 #include "uv.h"
27 
28 namespace OHOS::FileManagement::CloudSync {
29 const int32_t ARGS_ONE = 1;
30 
31 struct BundleEntity {
32     std::string bundleName_;
BundleEntityBundleEntity33     explicit BundleEntity(const std::string bundleName): bundleName_(bundleName){};
34 };
35 
36 struct CloudChangeListener {
37     NotifyType notifyType;
38     AAFwk::ChangeInfo changeInfo;
39     std::string strUri;
40 };
41 
42 struct RegisterParams {
43     std::string uri;
44     bool recursion;
45     napi_ref cbOnRef;
46 };
47 
48 class CloudSyncCallbackImpl;
49 class ChangeListenerNapi;
50 class CloudNotifyObserver;
51 class CloudSyncNapi : public LibN::NExporter {
52 public:
53     void SetClassName(const std::string classname);
54     std::string GetClassName() override;
55     bool Export() override;
56     bool ToExport(std::vector<napi_property_descriptor> props);
57     static napi_value Constructor(napi_env env, napi_callback_info info);
58 
59     static napi_value Start(napi_env env, napi_callback_info info);
60     static napi_value Stop(napi_env env, napi_callback_info info);
61     static napi_value OnCallback(napi_env env, napi_callback_info info);
62     static napi_value OffCallback(napi_env env, napi_callback_info info);
63     static napi_value GetFileSyncState(napi_env env, napi_callback_info info);
64     static napi_value RegisterChange(napi_env env, napi_callback_info info);
65     static napi_value UnregisterChange(napi_env env, napi_callback_info info);
66     static napi_value OptimizeStorage(napi_env env, napi_callback_info info);
67     static napi_value StartOptimizeStorage(napi_env env, napi_callback_info info);
68     static napi_value StopOptimizeStorage(napi_env env, napi_callback_info info);
69 
70     static std::string GetBundleName(const napi_env &env, const LibN::NFuncArg &funcArg);
71     static bool InitArgsOnCallback(const napi_env &env, LibN::NFuncArg &funcArg);
72     static bool InitArgsOffCallback(const napi_env &env, LibN::NFuncArg &funcArg);
CloudSyncNapi(napi_env env,napi_value exports)73     CloudSyncNapi(napi_env env, napi_value exports) : NExporter(env, exports) {};
74     ~CloudSyncNapi() = default;
75 
76 protected:
77     static inline std::shared_ptr<CloudSyncCallbackImpl> callback_;
78 
79 private:
80     static bool CheckRef(napi_env env, napi_ref ref, ChangeListenerNapi &listObj, const std::string &uri);
81     static int32_t RegisterToObs(napi_env env, const RegisterParams &registerParams);
82     static napi_value UnregisterFromObs(napi_env env, const std::string &uri);
83     static int32_t HandOptimizeStorageParams(napi_env env, napi_callback_info info, LibN::NFuncArg &funcArg,
84         OptimizeSpaceOptions &optimizeOptions);
85     std::string className_;
86     static std::mutex sOnOffMutex_;
87 };
88 
89 class CloudSyncCallbackImpl : public CloudSyncCallback, public std::enable_shared_from_this<CloudSyncCallbackImpl> {
90 public:
91     CloudSyncCallbackImpl(napi_env env, napi_value fun);
92     ~CloudSyncCallbackImpl() = default;
93     void OnSyncStateChanged(SyncType type, SyncPromptState state) override;
94     void OnSyncStateChanged(CloudSyncState state, ErrorType error) override;
95     void OnDeathRecipient() override;
96     void DeleteReference();
97 
98     class UvChangeMsg {
99     public:
UvChangeMsg(std::shared_ptr<CloudSyncCallbackImpl> cloudSyncCallbackIn,CloudSyncState state,ErrorType error)100         UvChangeMsg(std::shared_ptr<CloudSyncCallbackImpl> cloudSyncCallbackIn, CloudSyncState state, ErrorType error)
101             : cloudSyncCallback_(cloudSyncCallbackIn), state_(state), error_(error)
102         {
103         }
~UvChangeMsg()104         ~UvChangeMsg() {}
105         std::weak_ptr<CloudSyncCallbackImpl> cloudSyncCallback_;
106         CloudSyncState state_;
107         ErrorType error_;
108     };
109 
110 private:
111     static void OnComplete(UvChangeMsg *msg);
112     napi_env env_;
113     napi_ref cbOnRef_ = nullptr;
114     static CloudSyncState preState_;
115     static ErrorType preError_;
116 };
117 
118 class ChangeListenerNapi {
119 public:
120     class UvChangeMsg {
121     public:
UvChangeMsg(napi_env env,napi_ref ref,AAFwk::ChangeInfo & changeInfo,std::string strUri)122         UvChangeMsg(napi_env env, napi_ref ref, AAFwk::ChangeInfo &changeInfo, std::string strUri)
123             : env_(env), ref_(ref), changeInfo_(changeInfo), strUri_(std::move(strUri))
124         {
125             data_ = nullptr;
126         }
~UvChangeMsg()127         ~UvChangeMsg() {}
128         napi_env env_;
129         napi_ref ref_;
130         AAFwk::ChangeInfo changeInfo_;
131         uint8_t *data_;
132         std::string strUri_;
133     };
134 
ChangeListenerNapi(napi_env env)135     explicit ChangeListenerNapi(napi_env env) : env_(env) {}
136 
ChangeListenerNapi(const ChangeListenerNapi & listener)137     ChangeListenerNapi(const ChangeListenerNapi &listener)
138     {
139         this->env_ = listener.env_;
140         this->cbOnRef_ = listener.cbOnRef_;
141         this->cbOffRef_ = listener.cbOffRef_;
142         this->observers_ = listener.observers_;
143     }
144 
145     ChangeListenerNapi &operator=(const ChangeListenerNapi &listener)
146     {
147         this->env_ = listener.env_;
148         this->cbOnRef_ = listener.cbOnRef_;
149         this->cbOffRef_ = listener.cbOffRef_;
150         this->observers_ = listener.observers_;
151         return *this;
152     }
153 
~ChangeListenerNapi()154     ~ChangeListenerNapi(){};
155     void OnChange(CloudChangeListener &listener, const napi_ref cbRef);
156     int32_t UvQueueWork(uv_loop_s *loop, uv_work_t *work);
157     static napi_value SolveOnChange(napi_env env, UvChangeMsg *msg);
158     napi_ref cbOnRef_ = nullptr;
159     napi_ref cbOffRef_ = nullptr;
160     std::vector<std::shared_ptr<CloudNotifyObserver>> observers_;
161 
162 private:
163     napi_env env_ = nullptr;
164 };
165 
166 class CloudNotifyObserver {
167 public:
CloudNotifyObserver(const ChangeListenerNapi & listObj,std::string uri,napi_ref ref)168     CloudNotifyObserver(const ChangeListenerNapi &listObj, std::string uri, napi_ref ref)
169         : listObj_(listObj), uri_(uri), ref_(ref)
170     {
171     }
172     ~CloudNotifyObserver() = default;
173 
174     void OnChange();
175     void OnchangeExt(const AAFwk::ChangeInfo &changeInfo);
176 
177     ChangeListenerNapi listObj_;
178     std::string uri_;
179     napi_ref ref_;
180 };
181 
182 class CloudOptimizeCallbackImpl : public CloudOptimizeCallback,
183                                   public std::enable_shared_from_this<CloudOptimizeCallbackImpl> {
184 public:
CloudOptimizeCallbackImpl(napi_env env,LibN::NVal callbcakVal)185     CloudOptimizeCallbackImpl(napi_env env, LibN::NVal callbcakVal) : env_(env), cbOnRef_(callbcakVal) {}
186     ~CloudOptimizeCallbackImpl() = default;
187     void OnOptimizeProcess(const OptimizeState state, const int32_t progress) override;
188 
189 private:
190     napi_env env_ = nullptr;;
191     LibN::NRef cbOnRef_;
192 };
193 } // namespace OHOS::FileManagement::CloudSync
194 #endif // OHOS_FILEMGMT_CLOUD_SYNC_NAPI_H
195