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 "system_ability.h" 26 27 #include "update_helper.h" 28 #include "update_service_impl_manager.h" 29 #include "update_service_stub.h" 30 31 namespace OHOS { 32 namespace UpdateEngine { 33 class UpdateService : public SystemAbility, public UpdateServiceStub { 34 public: 35 DECLARE_SYSTEM_ABILITY(UpdateService); 36 DISALLOW_COPY_AND_MOVE(UpdateService); 37 explicit UpdateService(int32_t systemAbilityId, bool runOnCreate = true); 38 ~UpdateService() override; 39 40 int32_t RegisterUpdateCallback(const UpgradeInfo &info, const sptr<IUpdateCallback> &updateCallback) override; 41 42 int32_t UnregisterUpdateCallback(const UpgradeInfo &info) override; 43 44 int32_t CheckNewVersion(const UpgradeInfo &info) override; 45 46 int32_t Download(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo, 47 const DownloadOptions &downloadOptions, BusinessError &businessError) override; 48 49 int32_t PauseDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo, 50 const PauseDownloadOptions &pauseDownloadOptions, BusinessError &businessError) override; 51 52 int32_t ResumeDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo, 53 const ResumeDownloadOptions &resumeDownloadOptions, BusinessError &businessError) override; 54 55 int32_t Upgrade(const UpgradeInfo &info, const VersionDigestInfo &versionDigest, 56 const UpgradeOptions &upgradeOptions, BusinessError &businessError) override; 57 58 int32_t ClearError(const UpgradeInfo &info, const VersionDigestInfo &versionDigest, 59 const ClearOptions &clearOptions, BusinessError &businessError) override; 60 61 int32_t TerminateUpgrade(const UpgradeInfo &info, BusinessError &businessError) override; 62 63 int32_t GetNewVersionInfo( 64 const UpgradeInfo &info, NewVersionInfo &newVersionInfo, BusinessError &businessError) override; 65 66 int32_t GetNewVersionDescription(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo, 67 const DescriptionOptions &descriptionOptions, VersionDescriptionInfo &newVersionDescriptionInfo, 68 BusinessError &businessError) override; 69 70 int32_t GetCurrentVersionInfo(const UpgradeInfo &info, CurrentVersionInfo ¤tVersionInfo, 71 BusinessError &businessError) override; 72 73 int32_t GetCurrentVersionDescription(const UpgradeInfo &info, const DescriptionOptions &descriptionOptions, 74 VersionDescriptionInfo ¤tVersionDescriptionInfo, BusinessError &businessError) override; 75 76 int32_t GetTaskInfo(const UpgradeInfo &info, TaskInfo &taskInfo, BusinessError &businessError) override; 77 78 int32_t SetUpgradePolicy(const UpgradeInfo &info, const UpgradePolicy &policy, 79 BusinessError &businessError) override; 80 81 int32_t GetUpgradePolicy(const UpgradeInfo &info, UpgradePolicy &policy, BusinessError &businessError) override; 82 83 int32_t Cancel(const UpgradeInfo &info, int32_t service, BusinessError &businessError) override; 84 85 int32_t FactoryReset(BusinessError &businessError) override; 86 87 int32_t ApplyNewVersion(const UpgradeInfo &info, const std::string &miscFile, const std::string &packageName, 88 BusinessError &businessError) override; 89 90 int32_t VerifyUpgradePackage(const std::string &packagePath, const std::string &keyPath, 91 BusinessError &businessError) override; 92 93 int Dump(int fd, const std::vector<std::u16string> &args) override; 94 95 static sptr<UpdateService> GetInstance(); 96 97 sptr<IUpdateCallback> GetUpgradeCallback(const UpgradeInfo &info); 98 99 #ifndef UPDATER_UT 100 protected: 101 #endif 102 void OnStart() override; 103 void OnStop() override; 104 105 private: 106 void DumpUpgradeCallback(const int fd); 107 108 #ifndef UPDATER_UT 109 private: 110 #else 111 public: 112 #endif 113 class ClientDeathRecipient final : public IRemoteObject::DeathRecipient { 114 public: ClientDeathRecipient(const UpgradeInfo & upgradeInfo)115 ClientDeathRecipient(const UpgradeInfo &upgradeInfo) : upgradeInfo_(upgradeInfo) {} ~ClientDeathRecipient()116 ~ClientDeathRecipient() final {} 117 DISALLOW_COPY_AND_MOVE(ClientDeathRecipient); 118 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 119 private: 120 UpgradeInfo upgradeInfo_; 121 }; 122 123 class ClientProxy { 124 public: 125 ClientProxy(const UpgradeInfo &info, const sptr<IUpdateCallback> &callback); 126 void AddDeathRecipient(); 127 void RemoveDeathRecipient(); 128 sptr<IUpdateCallback> Get(); 129 private: 130 sptr<IUpdateCallback> proxy_; 131 sptr<IRemoteObject::DeathRecipient> deathRecipient_; 132 }; 133 134 private: 135 std::mutex clientProxyMapLock_; 136 std::map<UpgradeInfo, ClientProxy> clientProxyMap_; 137 static sptr<UpdateService> updateService_; 138 std::shared_ptr<UpdateServiceImplManager> updateImplMgr_; 139 }; 140 } // namespace UpdateEngine 141 } // namespace OHOS 142 #endif // UPDATE_SERVICE_H