• 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_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 #ifdef UPDATE_SERVICE_ENABLE_RUN_ON_DEMAND_QOS
101 private:
102     void SetThreadPrio(int priority);
103 #endif
104 
105 #ifndef UPDATER_UT
106 protected:
107 #endif
108     void OnStart(const SystemAbilityOnDemandReason &startReason) override;
109     int32_t OnIdle(const SystemAbilityOnDemandReason &idleReason) override;
110     void OnStop(const SystemAbilityOnDemandReason &stopReason) override;
111 
112 private:
113     void DumpUpgradeCallback(const int fd);
114 
115 #ifndef UPDATER_UT
116 private:
117 #else
118 public:
119 #endif
120     class ClientDeathRecipient final : public IRemoteObject::DeathRecipient {
121     public:
ClientDeathRecipient(const UpgradeInfo & upgradeInfo)122         ClientDeathRecipient(const UpgradeInfo &upgradeInfo) : upgradeInfo_(upgradeInfo) {}
~ClientDeathRecipient()123         ~ClientDeathRecipient() final {}
124         DISALLOW_COPY_AND_MOVE(ClientDeathRecipient);
125         void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
126     private:
127         UpgradeInfo upgradeInfo_;
128     };
129 
130     class ClientProxy {
131     public:
132         ClientProxy(const UpgradeInfo &info, const sptr<IUpdateCallback> &callback);
133         void AddDeathRecipient();
134         void RemoveDeathRecipient();
135         sptr<IUpdateCallback> Get();
136     private:
137         sptr<IUpdateCallback> proxy_;
138         sptr<IRemoteObject::DeathRecipient> deathRecipient_;
139     };
140 
141 private:
142     std::mutex clientProxyMapLock_;
143     std::map<UpgradeInfo, ClientProxy> clientProxyMap_;
144     static sptr<UpdateService> updateService_;
145     std::shared_ptr<UpdateServiceImplManager> updateImplMgr_ = nullptr;
146 };
147 
148 int32_t HandleOhRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
149 } // namespace UpdateEngine
150 } // namespace OHOS
151 #endif // UPDATE_SERVICE_H