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 UPDATE_SESSION_H
17 #define UPDATE_SESSION_H
18 
19 #include <condition_variable>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 
25 #include "base_async_session.h"
26 #include "base_promise_session.h"
27 #include "iupdater.h"
28 #include "napi_session.h"
29 
30 namespace OHOS::UpdateEngine {
31 class BaseUpdateSession : public BaseAsyncSession<UpdateResult> {
32 public:
BaseUpdateSession(BaseClient * client,SessionParams & sessionParams,size_t argc,size_t callbackNumber)33     BaseUpdateSession(BaseClient *client, SessionParams &sessionParams, size_t argc, size_t callbackNumber)
34         : BaseAsyncSession<UpdateResult>(client, sessionParams, argc, callbackNumber) {}
35 
36     ~BaseUpdateSession() override = default;
37 
GetUpdateResult(UpdateResult & result)38     void GetUpdateResult(UpdateResult &result)
39     {
40         result.businessError = businessError_;
41         IUpdater *migrateClient = static_cast<IUpdater *>(client_);
42         migrateClient->GetUpdateResult(sessionParams_.type, result);
43     }
44 
45     std::string GetFunctionName() override;
46 
GetFunctionPermissionName()47     std::string GetFunctionPermissionName() override
48     {
49         if (sessionParams_.type == SessionType::SESSION_FACTORY_RESET) {
50             return "ohos.permission.FACTORY_RESET";
51         } else {
52             return "ohos.permission.UPDATE_SYSTEM";
53         }
54     }
55 
IsAsyncCompleteWork()56     bool IsAsyncCompleteWork() override
57     {
58         return sessionParams_.type == SessionType::SESSION_CHECK_VERSION;
59     }
60 
CompleteWork(napi_env env,napi_status status)61     virtual void CompleteWork(napi_env env, napi_status status) override
62     {
63         ENGINE_LOGI("BaseUpdateSession::CompleteWork callbackNumber_: %d", static_cast<int32_t>(callbackNumber_));
64     }
65 };
66 
67 
68 class BaseMigratePromiseSession : public BasePromiseSession<UpdateResult> {
69 public:
70     BaseMigratePromiseSession(BaseClient *client, SessionParams &sessionParams, size_t argc,
71         size_t callbackNumber = 0)
72         : BasePromiseSession<UpdateResult>(client, sessionParams, argc, callbackNumber) {}
73 
GetUpdateResult(UpdateResult & result)74     void GetUpdateResult(UpdateResult &result)
75     {
76         result.businessError = businessError_;
77         IUpdater *migrateClient = static_cast<IUpdater *>(client_);
78         migrateClient->GetUpdateResult(sessionParams_.type, result);
79     }
80 
81     std::string GetFunctionName() override;
82 
GetFunctionPermissionName()83     std::string GetFunctionPermissionName() override
84     {
85         if (sessionParams_.type == SessionType::SESSION_FACTORY_RESET) {
86             return "ohos.permission.FACTORY_RESET";
87         } else {
88             return "ohos.permission.UPDATE_SYSTEM";
89         }
90     }
91 
IsAsyncCompleteWork()92     bool IsAsyncCompleteWork() override
93     {
94         return sessionParams_.type == SessionType::SESSION_CHECK_VERSION;
95     }
96 
CompleteWork(napi_env env,napi_status status)97     void CompleteWork(napi_env env, napi_status status) override {}
98 };
99 
100 class UpdateAsyncession : public BaseUpdateSession {
101 public:
102     UpdateAsyncession(IUpdater *client, SessionParams &sessionParams, size_t argc, size_t callbackNumber = 1)
103         : BaseUpdateSession(client, sessionParams, argc, callbackNumber) {}
104 
105     ~UpdateAsyncession() override = default;
106 
107     void CompleteWork(napi_env env, napi_status status) override;
108 };
109 
110 class UpdateAsyncessionNoCallback : public UpdateAsyncession {
111 public:
112     UpdateAsyncessionNoCallback(IUpdater *client, SessionParams &sessionParams, size_t argc, size_t callbackNumber = 0)
113         : UpdateAsyncession(client, sessionParams, argc, callbackNumber) {}
114 
115     ~UpdateAsyncessionNoCallback() override = default;
116 };
117 
118 class UpdatePromiseSession : public BaseMigratePromiseSession {
119 public:
120     UpdatePromiseSession(IUpdater *client, SessionParams &sessionParams, size_t argc, size_t callbackNumber = 0)
121         : BaseMigratePromiseSession(client, sessionParams, argc, callbackNumber) {}
122 
123     ~UpdatePromiseSession() override = default;
124 
125     void CompleteWork(napi_env env, napi_status status) override;
126 };
127 
128 class UpdateListener : public NapiSession {
129 public:
130     UpdateListener(BaseClient *client, SessionParams &sessionParams, size_t argc, bool isOnce,
131         size_t callbackNumber = 1)
132         : NapiSession(client, sessionParams, argc, callbackNumber), isOnce_(isOnce) {}
133 
134     ~UpdateListener() override = default;
135 
136     napi_value StartWork(napi_env env, size_t startIndex, const napi_value *args) override;
137 
138     void NotifyJS(napi_env env, napi_value thisVar, const UpdateResult &result);
139 
140     void NotifyJS(napi_env env, napi_value thisVar, const EventInfo &eventInfo);
141 
IsOnce()142     bool IsOnce() const
143     {
144         return isOnce_;
145     }
146 
GetEventType()147     std::string GetEventType() const
148     {
149         return eventType_;
150     }
151 
152     bool CheckEqual(napi_env env, napi_value handler, const std::string &type);
153 
IsSubscribeEvent(const EventClassifyInfo & eventClassifyInfo)154     bool IsSubscribeEvent(const EventClassifyInfo &eventClassifyInfo) const
155     {
156         return eventClassifyInfo_.eventClassify == eventClassifyInfo.eventClassify;
157     }
158 
159     bool IsSameListener(napi_env env, const EventClassifyInfo &eventClassifyInfo, napi_value handler);
160 
161     void RemoveHandlerRef(napi_env env);
162 
163 private:
164     bool isOnce_ = false;
165     std::string eventType_;
166     napi_ref handlerRef_ = nullptr;
167     std::mutex mutex_;
168     EventClassifyInfo eventClassifyInfo_;
169 };
170 
171 class SessionFuncHelper {
172 public:
173     static std::string GetFuncName(uint32_t sessionType);
174 
175 private:
176     static std::map<uint32_t, std::string> sessionFuncMap_;
177 };
178 } // namespace OHOS::UpdateEngine
179 #endif // UPDATE_SESSION_H