• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 MEDIA_SOURCE_LOADING_REQUEST_NAPI_H
17 #define MEDIA_SOURCE_LOADING_REQUEST_NAPI_H
18 
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 #include "common_napi.h"
22 #include "loading_request.h"
23 
24 namespace OHOS {
25 namespace Media {
26 
27 class MediaSourceLoadingRequestNapi {
28 public:
29     __attribute__((visibility("default"))) static napi_value Init(napi_env env, napi_value exports);
30     static napi_value CreateLoadingRequest(napi_env env, std::shared_ptr<LoadingRequest> request);
31 private:
32     static napi_value Constructor(napi_env env, napi_callback_info info);
33     static void Destructor(napi_env env, void *nativeObject, void *finalize);
34     /**
35      * function RespondData(uuid:number, offset:number, buffer: ArrayBuffer): number;
36      */
37     static napi_value JsRespondData(napi_env env, napi_callback_info info);
38     /**
39      * function RespondHeader(uuid:number, header?: Record<string,string>, redirectUrl?: string): void;
40      */
41     static napi_value JsRespondHeader(napi_env env, napi_callback_info info);
42     /**
43      * function FinishLoading(uuid:number, state: LoadingRequestError): void;
44      */
45     static napi_value JsFinishLoading(napi_env env, napi_callback_info info);
46     /**
47      * url: string
48      */
49     static napi_value JsGetUrl(napi_env env, napi_callback_info info);
50     /**
51      * header?: Record<string, string>
52      */
53     static napi_value JsGetHeader(napi_env env, napi_callback_info info);
54 
55     MediaSourceLoadingRequestNapi();
56     virtual ~MediaSourceLoadingRequestNapi();
57 
58     static MediaSourceLoadingRequestNapi* GetJsInstance(napi_env env, napi_callback_info info);
59     static MediaSourceLoadingRequestNapi* GetJsInstanceWithParameter(napi_env env, napi_callback_info info,
60         size_t &argc, napi_value *argv);
61 
62 private:
63     static thread_local napi_ref constructor_;
64     napi_env env_ = nullptr;
65     std::shared_ptr<LoadingRequest> request_;
66 };
67 
68 class RequestContainer {
69 public:
GetInstance()70     static RequestContainer &GetInstance()
71     {
72         static RequestContainer* inst = nullptr;
73         static std::once_flag once;
74         std::call_once(once, [&] { inst = new RequestContainer(); });
75         return *inst;
76     }
77 
IsEmpty()78     bool IsEmpty()
79     {
80         std::lock_guard<std::mutex> lock(mutex_);
81         return data_.empty();
82     }
83 
Insert(uint64_t key,std::shared_ptr<LoadingRequest> value)84     void Insert(uint64_t key, std::shared_ptr<LoadingRequest> value)
85     {
86         std::lock_guard<std::mutex> lock(mutex_);
87         auto result = data_.insert({key, value});
88         if (!result.second) {
89             data_[key] = value;
90         }
91     }
92 
Find(uint64_t key)93     std::shared_ptr<LoadingRequest> Find(uint64_t key)
94     {
95         std::lock_guard<std::mutex> lock(mutex_);
96         auto it = data_.find(key);
97         if (it != data_.end()) {
98             return it->second;
99         }
100         return nullptr;
101     }
102 
Erase(uint64_t key)103     void Erase(uint64_t key)
104     {
105         std::lock_guard<std::mutex> lock(mutex_);
106         data_.erase(key);
107     }
108 
Clear()109     void Clear()
110     {
111         std::lock_guard<std::mutex> lock(mutex_);
112         data_.clear();
113     }
114 private:
115     RequestContainer() = default;
116     ~RequestContainer() = default;
117     RequestContainer(const RequestContainer&) = delete;
118     RequestContainer& operator=(const RequestContainer&) = delete;
119 
120     std::mutex mutex_;
121     std::map<uint64_t, std::shared_ptr<LoadingRequest>> data_;
122 };
123 } // Media
124 } // OHOS
125 #endif // MEDIA_SOURCE_LOADING_REQUEST_NAPI_H