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