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