1 /* 2 * Copyright (c) 2021 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 <memory> 20 #include <mutex> 21 #include <string> 22 #include <vector> 23 24 #include "iupdater.h" 25 #include "iupdate_service.h" 26 #include "iupdate_session.h" 27 28 namespace OHOS { 29 namespace UpdateEngine { 30 class UpdateSession : public IUpdateSession { 31 public: 32 UpdateSession(IUpdater *client, SessionParams &sessionParams, size_t argc, size_t callbackNumber); 33 ~UpdateSession()34 virtual ~UpdateSession() {} 35 36 napi_value StartWork(napi_env env, const napi_value *args, DoWorkFunction worker, void *context) override; 37 GetUpdateClient()38 IUpdater* GetUpdateClient() const 39 { 40 return client_; 41 } 42 GetType()43 SessionType GetType() const override 44 { 45 return sessionParams_.type; 46 } 47 GetSessionId()48 uint32_t GetSessionId() const override 49 { 50 return sessionId; 51 } 52 CompleteWork(napi_env env,napi_status status)53 virtual void CompleteWork(napi_env env, napi_status status) {} 54 virtual void ExecuteWork(napi_env env); 55 virtual napi_value StartWork(napi_env env, size_t startIndex, const napi_value *args) = 0; 56 57 static void CompleteWork(napi_env env, napi_status status, void *data); 58 59 static void ExecuteWork(napi_env env, void *data); 60 IsAsyncCompleteWork()61 bool IsAsyncCompleteWork() override 62 { 63 return false; 64 } 65 66 protected: 67 napi_value CreateWorkerName(napi_env env) const; 68 GetUpdateResult(UpdateResult & result)69 void GetUpdateResult(UpdateResult &result) 70 { 71 result.businessError = businessError_; 72 client_->GetUpdateResult(sessionParams_.type, result); 73 } 74 IsWorkExecuteSuccess()75 bool IsWorkExecuteSuccess() const 76 { 77 return workResult_ == INT_CALL_SUCCESS; 78 } 79 BuildWorkBusinessErr(BusinessError & businessError)80 void BuildWorkBusinessErr(BusinessError &businessError) 81 { 82 std::string msg = "execute error"; 83 switch (workResult_) { 84 case INT_APP_NOT_GRANTED: 85 msg = "permission not granted"; 86 break; 87 case INT_CALL_IPC_ERR: 88 msg = "ipc error"; 89 break; 90 case INT_UN_SUPPORT: 91 msg = "api unsupport"; 92 break; 93 case INT_PARAM_ERR: 94 msg = "param error"; 95 break; 96 default: 97 break; 98 } 99 businessError.Build(static_cast<CallResult>(workResult_), msg); 100 } 101 GetBusinessError(BusinessError & businessError,const UpdateResult & result)102 void GetBusinessError(BusinessError &businessError, const UpdateResult &result) 103 { 104 if (IsWorkExecuteSuccess()) { 105 businessError = result.businessError; 106 } else { 107 BuildWorkBusinessErr(businessError); 108 } 109 } 110 IsNeedWaitAsyncCallback()111 bool IsNeedWaitAsyncCallback() 112 { 113 return IsAsyncCompleteWork() && IsWorkExecuteSuccess(); 114 } 115 116 uint32_t sessionId {0}; 117 IUpdater *client_ = nullptr; 118 BusinessError businessError_ {}; 119 SessionParams sessionParams_ {}; 120 int32_t workResult_ = INT_CALL_SUCCESS; 121 size_t totalArgc_ = 0; 122 size_t callbackNumber_ = 0; 123 void* context_ {}; 124 DoWorkFunction doWorker_ {}; 125 }; 126 127 class UpdateAsyncSession : public UpdateSession { 128 public: 129 UpdateAsyncSession(IUpdater *client, SessionParams &sessionParams, size_t argc, size_t callbackNumber = 1) UpdateSession(client,sessionParams,argc,callbackNumber)130 : UpdateSession(client, sessionParams, argc, callbackNumber) 131 { 132 callbackRef_.resize(callbackNumber); 133 } 134 ~UpdateAsyncSession()135 ~UpdateAsyncSession() override 136 { 137 callbackRef_.clear(); 138 } 139 140 void CompleteWork(napi_env env, napi_status status) override; 141 napi_value StartWork(napi_env env, size_t startIndex, const napi_value *args) override; 142 void NotifyJS(napi_env env, napi_value thisVar, const UpdateResult &result) override; 143 144 private: 145 napi_async_work worker_ = nullptr; 146 std::vector<napi_ref> callbackRef_ = {0}; 147 }; 148 149 class UpdateAsyncSessionNoCallback : public UpdateAsyncSession { 150 public: 151 UpdateAsyncSessionNoCallback( 152 IUpdater *client, SessionParams &sessionParams, size_t argc, size_t callbackNumber = 0) UpdateAsyncSession(client,sessionParams,argc,callbackNumber)153 : UpdateAsyncSession(client, sessionParams, argc, callbackNumber) {} 154 ~UpdateAsyncSessionNoCallback()155 ~UpdateAsyncSessionNoCallback() override {} 156 157 void CompleteWork(napi_env env, napi_status status) override; 158 }; 159 160 class UpdatePromiseSession : public UpdateSession { 161 public: 162 UpdatePromiseSession(IUpdater *client, SessionParams &sessionParams, size_t argc, size_t callbackNumber = 0) UpdateSession(client,sessionParams,argc,callbackNumber)163 : UpdateSession(client, sessionParams, argc, callbackNumber) {} 164 ~UpdatePromiseSession()165 ~UpdatePromiseSession() override {} 166 167 void CompleteWork(napi_env env, napi_status status) override; 168 napi_value StartWork(napi_env env, size_t startIndex, const napi_value *args) override; 169 void NotifyJS(napi_env env, napi_value thisVar, const UpdateResult &result) override; 170 171 private: 172 napi_async_work worker_ = nullptr; 173 napi_deferred deferred_ = nullptr; 174 }; 175 176 class UpdateListener : public UpdateSession { 177 public: 178 UpdateListener( 179 IUpdater *client, SessionParams &sessionParams, size_t argc, bool isOnce, size_t callbackNumber = 1) UpdateSession(client,sessionParams,argc,callbackNumber)180 : UpdateSession(client, sessionParams, argc, callbackNumber), isOnce_(isOnce) {} 181 ~UpdateListener()182 ~UpdateListener() override {} 183 184 napi_value StartWork(napi_env env, size_t startIndex, const napi_value *args) override; 185 186 void NotifyJS(napi_env env, napi_value thisVar, const UpdateResult &result) override; 187 188 void NotifyJS(napi_env env, napi_value thisVar, const EventInfo &eventInfo); 189 IsOnce()190 bool IsOnce() const 191 { 192 return isOnce_; 193 } 194 GetEventType()195 std::string GetEventType() const 196 { 197 return eventType_; 198 } 199 200 bool CheckEqual(napi_env env, napi_value handler, const std::string &type); 201 IsSubscribeEvent(const EventClassifyInfo & eventClassifyInfo)202 bool IsSubscribeEvent(const EventClassifyInfo &eventClassifyInfo) const 203 { 204 return eventClassifyInfo_.eventClassify == eventClassifyInfo.eventClassify; 205 } 206 207 bool IsSameListener(napi_env env, const EventClassifyInfo &eventClassifyInfo, napi_value handler); 208 209 void RemoveHandlerRef(napi_env env); 210 private: 211 bool isOnce_ = false; 212 std::string eventType_; 213 napi_ref handlerRef_ = nullptr; 214 std::mutex mutex_; 215 EventClassifyInfo eventClassifyInfo_; 216 }; 217 } // namespace UpdateEngine 218 } // namespace OHOS 219 #endif // UPDATE_SESSION_H 220