1 /*
2  * Copyright (c) 2023 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 SYS_INSTALLER_IMODULE_UPDATE_H
17 #define SYS_INSTALLER_IMODULE_UPDATE_H
18 
19 #include "iremote_broker.h"
20 #include "isys_installer_callback.h"
21 #include "module_ipc_helper.h"
22 #include "parcel.h"
23 #include "string_ex.h"
24 
25 namespace OHOS {
26 namespace SysInstaller {
27 struct HmpVersionInfo : public Parcelable {
28     std::string name {};
29     std::string laneCode {};
30     std::string compatibleVersion {};
31     std::string version {};
32 
MarshallingHmpVersionInfo33     virtual bool Marshalling(Parcel &parcel) const override
34     {
35         if (!parcel.WriteString16(Str8ToStr16(name))) {
36             return false;
37         }
38         if (!parcel.WriteString16(Str8ToStr16(laneCode))) {
39             return false;
40         }
41         if (!parcel.WriteString16(Str8ToStr16(compatibleVersion))) {
42             return false;
43         }
44         if (!parcel.WriteString16(Str8ToStr16(version))) {
45             return false;
46         }
47         return true;
48     }
49 
ReadFromParcelHmpVersionInfo50     bool ReadFromParcel(Parcel &parcel)
51     {
52         name = Str16ToStr8(parcel.ReadString16());
53         laneCode = Str16ToStr8(parcel.ReadString16());
54         compatibleVersion = Str16ToStr8(parcel.ReadString16());
55         version = Str16ToStr8(parcel.ReadString16());
56         return true;
57     }
58 
UnmarshallingHmpVersionInfo59     static HmpVersionInfo *Unmarshalling(Parcel &parcel)
60     {
61         HmpVersionInfo *obj = new (std::nothrow) HmpVersionInfo();
62         if (obj != nullptr && !obj->ReadFromParcel(parcel)) {
63             delete obj;
64             obj = nullptr;
65         }
66         return obj;
67     }
68 };
69 
70 struct HmpUpdateInfo : public Parcelable {
71     std::string path {};
72     int32_t result = 0; // 0 means success
73     std::string resultMsg {};
74 
MarshallingHmpUpdateInfo75     virtual bool Marshalling(Parcel &parcel) const override
76     {
77         if (!parcel.WriteString16(Str8ToStr16(path))) {
78             return false;
79         }
80         if (!parcel.WriteInt32(result)) {
81             return false;
82         }
83         if (!parcel.WriteString16(Str8ToStr16(resultMsg))) {
84             return false;
85         }
86         return true;
87     }
88 
ReadFromParcelHmpUpdateInfo89     bool ReadFromParcel(Parcel &parcel)
90     {
91         path = Str16ToStr8(parcel.ReadString16());
92         result = parcel.ReadInt32();
93         resultMsg = Str16ToStr8(parcel.ReadString16());
94         return true;
95     }
96 
UnmarshallingHmpUpdateInfo97     static HmpUpdateInfo *Unmarshalling(Parcel &parcel)
98     {
99         HmpUpdateInfo *obj = new (std::nothrow) HmpUpdateInfo();
100         if (obj != nullptr && !obj->ReadFromParcel(parcel)) {
101             delete obj;
102             obj = nullptr;
103         }
104         return obj;
105     }
106 };
107 
108 class IModuleUpdate : public OHOS::IRemoteBroker {
109 public:
110     DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Updater.IModuleUpdate");
111 
112     virtual int32_t InstallModulePackage(const std::string &pkgPath) = 0;
113     virtual int32_t UninstallModulePackage(const std::string &hmpName) = 0;
114     virtual int32_t GetModulePackageInfo(const std::string &hmpName,
115         std::list<ModulePackageInfo> &modulePackageInfos) = 0;
116     virtual int32_t ExitModuleUpdate() = 0;
117 
118     virtual std::vector<HmpVersionInfo> GetHmpVersionInfo() = 0;
119     virtual int32_t StartUpdateHmpPackage(const std::string &path,
120         const sptr<ISysInstallerCallback> &updateCallback) = 0;
121     virtual std::vector<HmpUpdateInfo> GetHmpUpdateResult() = 0;
122 };
123 } // namespace SysInstaller
124 } // namespace OHOS
125 #endif // SYS_INSTALLER_IMODULE_UPDATE_H
126