• 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 REQUEST_NOTIFY_H
17 #define REQUEST_NOTIFY_H
18 
19 #include <chrono>
20 #include <mutex>
21 #include <string>
22 
23 #include "block_queue.h"
24 #include "constant.h"
25 #include "js_common.h"
26 #include "napi/native_api.h"
27 #include "noncopyable.h"
28 #include "notify_stub.h"
29 #include "uv.h"
30 #include "uv_queue.h"
31 #include "visibility.h"
32 
33 namespace OHOS::Request {
34 struct NotifyEventInfo {
35     int64_t timestamp{};
36     bool operator==(const NotifyEventInfo &info) const
37     {
38         return (timestamp == info.timestamp);
39     }
40 };
41 
42 class RequestNotify : public NotifyStub {
43 public:
44     REQUEST_API explicit RequestNotify(napi_env env, napi_value callback);
45     REQUEST_API explicit RequestNotify() = default;
46     virtual ~RequestNotify();
47     void CallBack(const Notify &notify) override;
48     void Done(const TaskInfo &taskInfo) override;
49     void SetNotify(const Notify &notify);
50     void DeleteCallbackRef();
51     void ConvertCallBackData(uint32_t &paramNumber, napi_value *value, uint32_t valueSize);
52     void ExecCallBack();
53 
54     bool valid_;
55     napi_env env_;
56     std::mutex validMutex_;
57     napi_ref ref_;
58     Notify notify_;
59     std::mutex notifyMutex_;
60     NotifyEventInfo info_;
61     static BlockQueue<NotifyEventInfo> notifyQueue_;
62 };
63 
64 struct NotifyDataPtr {
65     sptr<RequestNotify> callback = nullptr;
66 };
67 } // namespace OHOS::Request
68 
69 #endif // REQUEST_NOTIFY_H