• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "filemgmt_libn.h"
21 
22 namespace OHOS::FileManagement::CloudSync {
23 const int32_t ARGS_ONE = 1;
24 
25 class CloudSyncCallbackImpl;
26 class CloudSyncNapi : public LibN::NExporter {
27 public:
28     void SetClassName(const std::string classname);
29     std::string GetClassName() override;
30     bool Export() override;
31     bool ToExport(std::vector<napi_property_descriptor> props);
32     static napi_value Constructor(napi_env env, napi_callback_info info);
33 
34     static napi_value Start(napi_env env, napi_callback_info info);
35     static napi_value Stop(napi_env env, napi_callback_info info);
36     static napi_value OnCallback(napi_env env, napi_callback_info info);
37     static napi_value OffCallback(napi_env env, napi_callback_info info);
38 
CloudSyncNapi(napi_env env,napi_value exports)39     CloudSyncNapi(napi_env env, napi_value exports) : NExporter(env, exports) {};
40     ~CloudSyncNapi() = default;
41 
42 private:
43     static inline std::shared_ptr<CloudSyncCallbackImpl> callback_;
44     std::string className_;
45 };
46 
47 class CloudSyncCallbackImpl : public CloudSyncCallback, public std::enable_shared_from_this<CloudSyncCallbackImpl> {
48 public:
49     CloudSyncCallbackImpl(napi_env env, napi_value fun);
50     ~CloudSyncCallbackImpl() = default;
51     void OnSyncStateChanged(SyncType type, SyncPromptState state) override;
52     void OnSyncStateChanged(CloudSyncState state, ErrorType error) override;
53     void DeleteReference();
54 
55     class UvChangeMsg {
56     public:
UvChangeMsg(std::shared_ptr<CloudSyncCallbackImpl> cloudSyncCallbackIn,CloudSyncState state,ErrorType error)57         UvChangeMsg(std::shared_ptr<CloudSyncCallbackImpl> cloudSyncCallbackIn, CloudSyncState state, ErrorType error)
58             : cloudSyncCallback_(cloudSyncCallbackIn), state_(state), error_(error)
59         {
60         }
~UvChangeMsg()61         ~UvChangeMsg() {}
62         std::weak_ptr<CloudSyncCallbackImpl> cloudSyncCallback_;
63         CloudSyncState state_;
64         ErrorType error_;
65     };
66 
67 private:
68     static void OnComplete(UvChangeMsg *msg);
69     napi_env env_;
70     napi_ref cbOnRef_ = nullptr;
71 };
72 } // namespace OHOS::FileManagement::CloudSync
73 #endif // OHOS_FILEMGMT_CLOUD_SYNC_NAPI_H
74