• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, BusinessError &businessError, CheckResult &checkResult) 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 &currentVersionInfo,
71         BusinessError &businessError) override;
72 
73     int32_t GetCurrentVersionDescription(const UpgradeInfo &info, const DescriptionOptions &descriptionOptions,
74         VersionDescriptionInfo &currentVersionDescriptionInfo, 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(const SystemAbilityOnDemandReason &startReason) override;
103     int32_t OnIdle(const SystemAbilityOnDemandReason &idleReason) override;
104     void OnStop(const SystemAbilityOnDemandReason &stopReason) override;
105 
106 private:
107     void DumpUpgradeCallback(const int fd);
108 
109 #ifndef UPDATER_UT
110 private:
111 #else
112 public:
113 #endif
114     class ClientDeathRecipient final : public IRemoteObject::DeathRecipient {
115     public:
ClientDeathRecipient(const UpgradeInfo & upgradeInfo)116         ClientDeathRecipient(const UpgradeInfo &upgradeInfo) : upgradeInfo_(upgradeInfo) {}
~ClientDeathRecipient()117         ~ClientDeathRecipient() final {}
118         DISALLOW_COPY_AND_MOVE(ClientDeathRecipient);
119         void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
120     private:
121         UpgradeInfo upgradeInfo_;
122     };
123 
124     class ClientProxy {
125     public:
126         ClientProxy(const UpgradeInfo &info, const sptr<IUpdateCallback> &callback);
127         void AddDeathRecipient();
128         void RemoveDeathRecipient();
129         sptr<IUpdateCallback> Get();
130     private:
131         sptr<IUpdateCallback> proxy_;
132         sptr<IRemoteObject::DeathRecipient> deathRecipient_;
133     };
134 
135 private:
136     std::mutex clientProxyMapLock_;
137     std::map<UpgradeInfo, ClientProxy> clientProxyMap_;
138     static sptr<UpdateService> updateService_;
139     std::shared_ptr<UpdateServiceImplManager> updateImplMgr_ = nullptr;
140 };
141 
142 int32_t HandleOhRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
143 } // namespace UpdateEngine
144 } // namespace OHOS
145 #endif // UPDATE_SERVICE_H