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 DOWNLOAD_EVENT_H
17 #define DOWNLOAD_EVENT_H
18 
19 #include <string>
20 #include <unordered_set>
21 
22 #include "async_call.h"
23 #include "js_common.h"
24 #include "js_task.h"
25 #include "napi/native_api.h"
26 #include "napi_utils.h"
27 #include "noncopyable.h"
28 #include "notify_interface.h"
29 
30 namespace OHOS::Request {
31 class RequestEvent final {
32 public:
33     RequestEvent() = default;
34     ~RequestEvent() = default;
35     RequestEvent(RequestEvent const &) = delete;
36     void operator=(RequestEvent const &) = delete;
37     RequestEvent(RequestEvent &&) = delete;
38     RequestEvent &operator=(RequestEvent &&) = delete;
39 
40     static napi_value On(napi_env env, napi_callback_info info);
41     static napi_value Off(napi_env env, napi_callback_info info);
42     static napi_value Pause(napi_env env, napi_callback_info info);
43     static napi_value QueryMimeType(napi_env env, napi_callback_info info);
44     static napi_value Query(napi_env env, napi_callback_info info);
45     static napi_value Remove(napi_env env, napi_callback_info info);
46     static napi_value Resume(napi_env env, napi_callback_info info);
47     static napi_value Start(napi_env env, napi_callback_info info);
48     static napi_value Stop(napi_env env, napi_callback_info info);
49     static std::map<Reason, DownloadErrorCode> failMap_;
50 
51 private:
52     struct JsParam {
53         std::string type;
54         SubscribeType subscribeType;
55         napi_value callback;
56         napi_value self;
57         JsTask *task;
58     };
59     enum { BOOL_RES, STR_RES, INFO_RES };
60     struct ExecContext : public AsyncCall::Context {
61         JsTask *task = nullptr;
62         bool boolRes = false;
63         std::string strRes;
64         DownloadInfo infoRes;
65     };
66 
67     using Event = std::function<int32_t(const std::shared_ptr<ExecContext> &)>;
68     static std::map<std::string, SubscribeType> supportEventsV10_;
69     static std::map<std::string, SubscribeType> supportEventsV9_;
70     static std::map<std::string, Event> requestEvent_;
71     static std::map<std::string, uint32_t> resMap_;
72     static std::map<State, DownloadStatus> stateMap_;
73     static napi_value Exec(napi_env env, napi_callback_info info, const std::string &execType);
74 
75     static int32_t StartExec(const std::shared_ptr<ExecContext> &context);
76     static int32_t StopExec(const std::shared_ptr<ExecContext> &context);
77     static int32_t PauseExec(const std::shared_ptr<ExecContext> &context);
78     static int32_t QueryMimeTypeExec(const std::shared_ptr<ExecContext> &context);
79     static int32_t QueryExec(const std::shared_ptr<ExecContext> &context);
80     static int32_t RemoveExec(const std::shared_ptr<ExecContext> &context);
81     static int32_t ResumeExec(const std::shared_ptr<ExecContext> &context);
82 
83     static napi_status ParseInputParameters(
84         napi_env env, size_t argc, napi_value self, const std::shared_ptr<ExecContext> &context);
85     static ExceptionError ParseOnOffParameters(
86         napi_env env, napi_callback_info info, bool IsRequiredParam, JsParam &jsParam);
87     static napi_status GetResult(
88         napi_env env, const std::shared_ptr<ExecContext> &context, const std::string &execType, napi_value &result);
89     static void GetDownloadInfo(const TaskInfo &infoRes, DownloadInfo &info);
90     static NotifyData BuildNotifyData(const std::shared_ptr<TaskInfo> &taskInfo);
91     static SubscribeType StringToSubscribeType(const std::string &type, Version version);
92 };
93 } // namespace OHOS::Request
94 
95 #endif // DOWNLOAD_EVENT_H