1190978c3Sopenharmony_ci/*
2190978c3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3190978c3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4190978c3Sopenharmony_ci * you may not use this file except in compliance with the License.
5190978c3Sopenharmony_ci * You may obtain a copy of the License at
6190978c3Sopenharmony_ci *
7190978c3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8190978c3Sopenharmony_ci *
9190978c3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10190978c3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11190978c3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12190978c3Sopenharmony_ci * See the License for the specific language governing permissions and
13190978c3Sopenharmony_ci * limitations under the License.
14190978c3Sopenharmony_ci */
15190978c3Sopenharmony_ci
16190978c3Sopenharmony_ci#ifndef UPDATE_SERVICE_H
17190978c3Sopenharmony_ci#define UPDATE_SERVICE_H
18190978c3Sopenharmony_ci
19190978c3Sopenharmony_ci#include <iostream>
20190978c3Sopenharmony_ci#include <thread>
21190978c3Sopenharmony_ci
22190978c3Sopenharmony_ci#include "if_system_ability_manager.h"
23190978c3Sopenharmony_ci#include "ipc_skeleton.h"
24190978c3Sopenharmony_ci#include "iremote_stub.h"
25190978c3Sopenharmony_ci#include "system_ability.h"
26190978c3Sopenharmony_ci
27190978c3Sopenharmony_ci#include "update_service_impl_manager.h"
28190978c3Sopenharmony_ci#include "update_service_stub.h"
29190978c3Sopenharmony_ci
30190978c3Sopenharmony_cinamespace OHOS {
31190978c3Sopenharmony_cinamespace UpdateEngine {
32190978c3Sopenharmony_ciclass UpdateService : public SystemAbility, public UpdateServiceStub {
33190978c3Sopenharmony_cipublic:
34190978c3Sopenharmony_ci    DECLARE_SYSTEM_ABILITY(UpdateService);
35190978c3Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(UpdateService);
36190978c3Sopenharmony_ci    explicit UpdateService(int32_t systemAbilityId, bool runOnCreate = true);
37190978c3Sopenharmony_ci    ~UpdateService() override;
38190978c3Sopenharmony_ci
39190978c3Sopenharmony_ci    int32_t RegisterUpdateCallback(const UpgradeInfo &info, const sptr<IUpdateCallback> &updateCallback) override;
40190978c3Sopenharmony_ci
41190978c3Sopenharmony_ci    int32_t UnregisterUpdateCallback(const UpgradeInfo &info) override;
42190978c3Sopenharmony_ci
43190978c3Sopenharmony_ci    int32_t CheckNewVersion(const UpgradeInfo &info, BusinessError &businessError, CheckResult &checkResult) override;
44190978c3Sopenharmony_ci
45190978c3Sopenharmony_ci    int32_t Download(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
46190978c3Sopenharmony_ci        const DownloadOptions &downloadOptions, BusinessError &businessError) override;
47190978c3Sopenharmony_ci
48190978c3Sopenharmony_ci    int32_t PauseDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
49190978c3Sopenharmony_ci        const PauseDownloadOptions &pauseDownloadOptions, BusinessError &businessError) override;
50190978c3Sopenharmony_ci
51190978c3Sopenharmony_ci    int32_t ResumeDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
52190978c3Sopenharmony_ci        const ResumeDownloadOptions &resumeDownloadOptions, BusinessError &businessError) override;
53190978c3Sopenharmony_ci
54190978c3Sopenharmony_ci    int32_t Upgrade(const UpgradeInfo &info, const VersionDigestInfo &versionDigest,
55190978c3Sopenharmony_ci        const UpgradeOptions &upgradeOptions, BusinessError &businessError) override;
56190978c3Sopenharmony_ci
57190978c3Sopenharmony_ci    int32_t ClearError(const UpgradeInfo &info, const VersionDigestInfo &versionDigest,
58190978c3Sopenharmony_ci        const ClearOptions &clearOptions, BusinessError &businessError) override;
59190978c3Sopenharmony_ci
60190978c3Sopenharmony_ci    int32_t TerminateUpgrade(const UpgradeInfo &info, BusinessError &businessError) override;
61190978c3Sopenharmony_ci
62190978c3Sopenharmony_ci    int32_t GetNewVersionInfo(
63190978c3Sopenharmony_ci        const UpgradeInfo &info, NewVersionInfo &newVersionInfo, BusinessError &businessError) override;
64190978c3Sopenharmony_ci
65190978c3Sopenharmony_ci    int32_t GetNewVersionDescription(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
66190978c3Sopenharmony_ci        const DescriptionOptions &descriptionOptions, VersionDescriptionInfo &newVersionDescriptionInfo,
67190978c3Sopenharmony_ci        BusinessError &businessError) override;
68190978c3Sopenharmony_ci
69190978c3Sopenharmony_ci    int32_t GetCurrentVersionInfo(const UpgradeInfo &info, CurrentVersionInfo &currentVersionInfo,
70190978c3Sopenharmony_ci        BusinessError &businessError) override;
71190978c3Sopenharmony_ci
72190978c3Sopenharmony_ci    int32_t GetCurrentVersionDescription(const UpgradeInfo &info, const DescriptionOptions &descriptionOptions,
73190978c3Sopenharmony_ci        VersionDescriptionInfo &currentVersionDescriptionInfo, BusinessError &businessError) override;
74190978c3Sopenharmony_ci
75190978c3Sopenharmony_ci    int32_t GetTaskInfo(const UpgradeInfo &info, TaskInfo &taskInfo, BusinessError &businessError) override;
76190978c3Sopenharmony_ci
77190978c3Sopenharmony_ci    int32_t SetUpgradePolicy(const UpgradeInfo &info, const UpgradePolicy &policy,
78190978c3Sopenharmony_ci        BusinessError &businessError) override;
79190978c3Sopenharmony_ci
80190978c3Sopenharmony_ci    int32_t GetUpgradePolicy(const UpgradeInfo &info, UpgradePolicy &policy, BusinessError &businessError) override;
81190978c3Sopenharmony_ci
82190978c3Sopenharmony_ci    int32_t Cancel(const UpgradeInfo &info, int32_t service, BusinessError &businessError) override;
83190978c3Sopenharmony_ci
84190978c3Sopenharmony_ci    int32_t FactoryReset(BusinessError &businessError) override;
85190978c3Sopenharmony_ci
86190978c3Sopenharmony_ci    int32_t ApplyNewVersion(const UpgradeInfo &info, const std::string &miscFile,
87190978c3Sopenharmony_ci        const std::vector<std::string> &packageNames, BusinessError &businessError) override;
88190978c3Sopenharmony_ci
89190978c3Sopenharmony_ci    int32_t VerifyUpgradePackage(const std::string &packagePath, const std::string &keyPath,
90190978c3Sopenharmony_ci        BusinessError &businessError) override;
91190978c3Sopenharmony_ci
92190978c3Sopenharmony_ci    int Dump(int fd, const std::vector<std::u16string> &args) override;
93190978c3Sopenharmony_ci
94190978c3Sopenharmony_ci    void RegisterOhFunc();
95190978c3Sopenharmony_ci
96190978c3Sopenharmony_ci    static sptr<UpdateService> GetInstance();
97190978c3Sopenharmony_ci
98190978c3Sopenharmony_ci    sptr<IUpdateCallback> GetUpgradeCallback(const UpgradeInfo &info);
99190978c3Sopenharmony_ci
100190978c3Sopenharmony_ci#ifndef UPDATER_UT
101190978c3Sopenharmony_ciprotected:
102190978c3Sopenharmony_ci#endif
103190978c3Sopenharmony_ci    void OnStart(const SystemAbilityOnDemandReason &startReason) override;
104190978c3Sopenharmony_ci    int32_t OnIdle(const SystemAbilityOnDemandReason &idleReason) override;
105190978c3Sopenharmony_ci    void OnStop(const SystemAbilityOnDemandReason &stopReason) override;
106190978c3Sopenharmony_ci
107190978c3Sopenharmony_ciprivate:
108190978c3Sopenharmony_ci    void DumpUpgradeCallback(const int fd);
109190978c3Sopenharmony_ci
110190978c3Sopenharmony_ci#ifndef UPDATER_UT
111190978c3Sopenharmony_ciprivate:
112190978c3Sopenharmony_ci#else
113190978c3Sopenharmony_cipublic:
114190978c3Sopenharmony_ci#endif
115190978c3Sopenharmony_ci    class ClientDeathRecipient final : public IRemoteObject::DeathRecipient {
116190978c3Sopenharmony_ci    public:
117190978c3Sopenharmony_ci        ClientDeathRecipient(const UpgradeInfo &upgradeInfo) : upgradeInfo_(upgradeInfo) {}
118190978c3Sopenharmony_ci        ~ClientDeathRecipient() final {}
119190978c3Sopenharmony_ci        DISALLOW_COPY_AND_MOVE(ClientDeathRecipient);
120190978c3Sopenharmony_ci        void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
121190978c3Sopenharmony_ci    private:
122190978c3Sopenharmony_ci        UpgradeInfo upgradeInfo_;
123190978c3Sopenharmony_ci    };
124190978c3Sopenharmony_ci
125190978c3Sopenharmony_ci    class ClientProxy {
126190978c3Sopenharmony_ci    public:
127190978c3Sopenharmony_ci        ClientProxy(const UpgradeInfo &info, const sptr<IUpdateCallback> &callback);
128190978c3Sopenharmony_ci        void AddDeathRecipient();
129190978c3Sopenharmony_ci        void RemoveDeathRecipient();
130190978c3Sopenharmony_ci        sptr<IUpdateCallback> Get();
131190978c3Sopenharmony_ci    private:
132190978c3Sopenharmony_ci        sptr<IUpdateCallback> proxy_;
133190978c3Sopenharmony_ci        sptr<IRemoteObject::DeathRecipient> deathRecipient_;
134190978c3Sopenharmony_ci    };
135190978c3Sopenharmony_ci
136190978c3Sopenharmony_ciprivate:
137190978c3Sopenharmony_ci    std::mutex clientProxyMapLock_;
138190978c3Sopenharmony_ci    std::map<UpgradeInfo, ClientProxy> clientProxyMap_;
139190978c3Sopenharmony_ci    static sptr<UpdateService> updateService_;
140190978c3Sopenharmony_ci    std::shared_ptr<UpdateServiceImplManager> updateImplMgr_ = nullptr;
141190978c3Sopenharmony_ci};
142190978c3Sopenharmony_ci
143190978c3Sopenharmony_ciint32_t HandleOhRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option);
144190978c3Sopenharmony_ci} // namespace UpdateEngine
145190978c3Sopenharmony_ci} // namespace OHOS
146190978c3Sopenharmony_ci#endif // UPDATE_SERVICE_H