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 PROGRESS_CALLBACK 17 #define PROGRESS_CALLBACK 18 19 #include <uv.h> 20 #include <atomic> 21 #include "i_progress_callback.h" 22 #include "js_util.h" 23 #include "napi/native_common.h" 24 #include "napi/native_api.h" 25 #include "napi/native_node_api.h" 26 27 namespace OHOS::Request::Upload { 28 using namespace OHOS::Request::UploadNapi; 29 class ProgressCallback : public IProgressCallback, public std::enable_shared_from_this<ProgressCallback> { 30 public: 31 ProgressCallback(napi_env env, napi_value callback); 32 virtual ~ProgressCallback(); 33 void Progress(const int64_t uploadedSize, const int64_t totalSize) override; 34 napi_ref GetCallback() override; 35 napi_env GetEnv(); 36 int64_t GetUploadedSize(); 37 int64_t GetTotalSize(); 38 39 private: 40 napi_ref callback_ = nullptr; 41 napi_env env_; 42 uv_loop_s *loop_ = nullptr; 43 std::atomic<int64_t> uploadedSize_ {0}; 44 std::atomic<int64_t> totalSize_ {0}; 45 }; 46 47 struct ProgressWorker { 48 std::shared_ptr<ProgressCallback> observer = nullptr; 49 }; 50 } // end of OHOS::Request::Upload 51 #endif