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_GALLERY_SYNC_NAPI_H 17 #define OHOS_FILEMGMT_GALLERY_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 GallerySyncNapi final : public LibN::NExporter { 27 public: 28 bool Export() override; 29 std::string GetClassName() override; 30 31 static napi_value Constructor(napi_env env, napi_callback_info info); 32 33 static napi_value Start(napi_env env, napi_callback_info info); 34 static napi_value Stop(napi_env env, napi_callback_info info); 35 static napi_value OnCallback(napi_env env, napi_callback_info info); 36 static napi_value OffCallback(napi_env env, napi_callback_info info); 37 GallerySyncNapi(napi_env env,napi_value exports)38 GallerySyncNapi(napi_env env, napi_value exports) : NExporter(env, exports) {}; 39 ~GallerySyncNapi() = default; 40 41 private: 42 static inline std::shared_ptr<CloudSyncCallbackImpl> callback_; 43 inline static const std::string className_ = "GallerySync"; 44 }; 45 46 class CloudSyncCallbackImpl : public CloudSyncCallback, public std::enable_shared_from_this<CloudSyncCallbackImpl> { 47 public: 48 CloudSyncCallbackImpl(napi_env env, napi_value fun); 49 ~CloudSyncCallbackImpl() = default; 50 void OnSyncStateChanged(SyncType type, SyncPromptState state) override; 51 void OnSyncStateChanged(CloudSyncState state, ErrorType error) override; 52 void DeleteReference(); 53 54 class UvChangeMsg { 55 public: UvChangeMsg(std::shared_ptr<CloudSyncCallbackImpl> cloudSyncCallbackIn,CloudSyncState state,ErrorType error)56 UvChangeMsg(std::shared_ptr<CloudSyncCallbackImpl> cloudSyncCallbackIn, CloudSyncState state, ErrorType error) 57 : cloudSyncCallback_(cloudSyncCallbackIn), state_(state), error_(error) 58 { 59 } ~UvChangeMsg()60 ~UvChangeMsg() {} 61 std::weak_ptr<CloudSyncCallbackImpl> cloudSyncCallback_; 62 CloudSyncState state_; 63 ErrorType error_; 64 }; 65 66 private: 67 static void OnComplete(UvChangeMsg *msg); 68 napi_env env_; 69 napi_ref cbOnRef_ = nullptr; 70 }; 71 } // namespace OHOS::FileManagement::CloudSync 72 #endif // OHOS_FILEMGMT_GALLERY_SYNC_NAPI_H 73