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_SERVICE_H 17 #define UPDATE_SERVICE_H 18 19 #include <iostream> 20 #include <thread> 21 22 #include "if_system_ability_manager.h" 23 #include "ipc_skeleton.h" 24 #include "iremote_stub.h" 25 #include "iservice_local_updater.h" 26 #include "iservice_online_updater.h" 27 #include "iservice_restorer.h" 28 #include "system_ability.h" 29 30 #include "update_service_impl_manager.h" 31 #include "update_service_stub.h" 32 33 namespace OHOS { 34 namespace UpdateService { 35 class UpdateService : public SystemAbility, public UpdateServiceStub { 36 public: 37 DECLARE_SYSTEM_ABILITY(UpdateService); 38 DISALLOW_COPY_AND_MOVE(UpdateService); 39 explicit UpdateService(int32_t systemAbilityId, bool runOnCreate = true); 40 ~UpdateService() override; 41 42 int32_t RegisterUpdateCallback(const UpgradeInfo &info, const sptr<IUpdateCallback> &updateCallback, 43 int32_t &funcResutl) override; 44 45 int32_t UnregisterUpdateCallback(const UpgradeInfo &info, int32_t &funcResutl) override; 46 47 int32_t CheckNewVersion(const UpgradeInfo &info, BusinessError &businessError, CheckResult &checkResult, 48 int32_t &funcResult) override; 49 50 int32_t Download(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo, 51 const DownloadOptions &downloadOptions, BusinessError &businessError, int32_t &funcResutl) override; 52 53 int32_t PauseDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo, 54 const PauseDownloadOptions &pauseDownloadOptions, BusinessError &businessError, int32_t &funcResutl) override; 55 56 int32_t ResumeDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo, 57 const ResumeDownloadOptions &resumeDownloadOptions, BusinessError &businessError, int32_t &funcResutl) override; 58 59 int32_t Upgrade(const UpgradeInfo &info, const VersionDigestInfo &versionDigest, 60 const UpgradeOptions &upgradeOptions, BusinessError &businessError, int32_t &funcResutl) override; 61 62 int32_t ClearError(const UpgradeInfo &info, const VersionDigestInfo &versionDigest, 63 const ClearOptions &clearOptions, BusinessError &businessError, int32_t &funcResutl) override; 64 65 int32_t TerminateUpgrade(const UpgradeInfo &info, BusinessError &businessError, int32_t &funcResutl) override; 66 67 int32_t GetNewVersionInfo( 68 const UpgradeInfo &info, NewVersionInfo &newVersionInfo, BusinessError &businessError, 69 int32_t &funcResutl) override; 70 71 int32_t GetNewVersionDescription(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo, 72 const DescriptionOptions &descriptionOptions, VersionDescriptionInfo &newVersionDescriptionInfo, 73 BusinessError &businessError, int32_t &funcResult) override; 74 75 int32_t GetCurrentVersionInfo(const UpgradeInfo &info, CurrentVersionInfo ¤tVersionInfo, 76 BusinessError &businessError, int32_t &funcResult) override; 77 78 int32_t GetCurrentVersionDescription(const UpgradeInfo &info, const DescriptionOptions &descriptionOptions, 79 VersionDescriptionInfo ¤tVersionDescriptionInfo, BusinessError &businessError, 80 int32_t &funcResult) override; 81 82 int32_t GetTaskInfo(const UpgradeInfo &info, TaskInfo &taskInfo, BusinessError &businessError, 83 int32_t &funcResutl) override; 84 85 int32_t SetUpgradePolicy(const UpgradeInfo &info, const UpgradePolicy &policy, 86 BusinessError &businessError, int32_t &funcResult) override; 87 88 int32_t GetUpgradePolicy(const UpgradeInfo &info, UpgradePolicy &policy, BusinessError &businessError, 89 int32_t &funcResult) override; 90 91 int32_t Cancel(const UpgradeInfo &info, int32_t service, BusinessError &businessError, 92 int32_t &funcResult) override; 93 94 int32_t FactoryReset(BusinessError &businessError, int32_t &funcResult) override; 95 96 int32_t ApplyNewVersion(const UpgradeInfo &info, const std::string &miscFile, 97 const std::vector<std::string> &packageNames, BusinessError &businessError, int32_t &funcResult) override; 98 99 int32_t VerifyUpgradePackage(const std::string &packagePath, const std::string &keyPath, 100 BusinessError &businessError, int32_t &funcResult) override; 101 102 int Dump(int fd, const std::vector<std::u16string> &args) override; 103 104 int32_t CallbackEnter(uint32_t code) override; 105 106 int32_t CallbackExit(uint32_t code, int32_t result) override; 107 108 int32_t CallbackParcel(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 109 110 static sptr<UpdateService> GetInstance(); 111 112 sptr<IUpdateCallback> GetUpgradeCallback(const UpgradeInfo &info, int32_t &funcResutl); 113 #ifdef UPDATE_SERVICE_ENABLE_RUN_ON_DEMAND_QOS 114 private: 115 void SetThreadPrio(int priority); 116 #endif 117 118 #ifndef UPDATER_UT 119 protected: 120 #endif 121 void OnStart(const SystemAbilityOnDemandReason &startReason) override; 122 int32_t OnIdle(const SystemAbilityOnDemandReason &idleReason) override; 123 void OnStop(const SystemAbilityOnDemandReason &stopReason) override; 124 125 private: 126 void DumpUpgradeCallback(const int fd); 127 bool IsCallerValid(); 128 bool IsPermissionGranted(uint32_t code); 129 int32_t PermissionCheck(uint32_t code); 130 131 #ifndef UPDATER_UT 132 private: 133 #else 134 public: 135 #endif 136 class ClientDeathRecipient final : public IRemoteObject::DeathRecipient { 137 public: ClientDeathRecipient(const UpgradeInfo & upgradeInfo)138 ClientDeathRecipient(const UpgradeInfo &upgradeInfo) : upgradeInfo_(upgradeInfo) {} ~ClientDeathRecipient()139 ~ClientDeathRecipient() final {} 140 DISALLOW_COPY_AND_MOVE(ClientDeathRecipient); 141 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 142 private: 143 UpgradeInfo upgradeInfo_; 144 }; 145 146 class ClientProxy { 147 public: 148 ClientProxy(const UpgradeInfo &info, const sptr<IUpdateCallback> &callback); 149 void AddDeathRecipient(); 150 void RemoveDeathRecipient(); 151 sptr<IUpdateCallback> Get(); 152 private: 153 sptr<IUpdateCallback> proxy_; 154 sptr<IRemoteObject::DeathRecipient> deathRecipient_; 155 }; 156 157 private: 158 std::mutex clientProxyMapLock_; 159 std::map<UpgradeInfo, ClientProxy> clientProxyMap_; 160 static sptr<UpdateService> updateService_; 161 std::shared_ptr<UpdateServiceImplManager> updateImplMgr_ = nullptr; 162 }; 163 164 } // namespace UpdateService 165 } // namespace OHOS 166 #endif // UPDATE_SERVICE_H