1 /*
2 * Copyright (c) 2022 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 #include "app_quick_fix.h"
17
18 #include "app_log_tag_wrapper.h"
19 #include "app_log_wrapper.h"
20 #include "bundle_constants.h"
21 #include "json_util.h"
22 #include "nlohmann/json.hpp"
23 #include "parcel_macro.h"
24 #include "string_ex.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 const char* APP_QUICK_FIX_VERSION_CODE = "versionCode";
30 const char* APP_QUICK_FIX_VERSION_NAME = "versionName";
31 const char* APP_QUICK_FIX_DEPLOYED_APP_QF_INFO = "deployedAppqfInfo";
32 const char* APP_QUICK_FIX_DEPLOYING_APP_QF_INFO = "deployingAppqfInfo";
33 }
34
to_json(nlohmann::json & jsonObject,const AppQuickFix & appQuickFix)35 void to_json(nlohmann::json &jsonObject, const AppQuickFix &appQuickFix)
36 {
37 jsonObject = nlohmann::json {
38 {Constants::BUNDLE_NAME, appQuickFix.bundleName},
39 {APP_QUICK_FIX_VERSION_CODE, appQuickFix.versionCode},
40 {APP_QUICK_FIX_VERSION_NAME, appQuickFix.versionName},
41 {APP_QUICK_FIX_DEPLOYED_APP_QF_INFO, appQuickFix.deployedAppqfInfo},
42 {APP_QUICK_FIX_DEPLOYING_APP_QF_INFO, appQuickFix.deployingAppqfInfo}
43 };
44 }
45
from_json(const nlohmann::json & jsonObject,AppQuickFix & appQuickFix)46 void from_json(const nlohmann::json &jsonObject, AppQuickFix &appQuickFix)
47 {
48 const auto &jsonObjectEnd = jsonObject.end();
49 int32_t parseResult = ERR_OK;
50 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd,
51 Constants::BUNDLE_NAME, appQuickFix.bundleName,
52 JsonType::STRING, false, parseResult,
53 ArrayType::NOT_ARRAY);
54
55 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd,
56 APP_QUICK_FIX_VERSION_CODE, appQuickFix.versionCode,
57 JsonType::NUMBER, false, parseResult,
58 ArrayType::NOT_ARRAY);
59
60 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd,
61 APP_QUICK_FIX_VERSION_NAME, appQuickFix.versionName,
62 JsonType::STRING, false, parseResult,
63 ArrayType::NOT_ARRAY);
64
65 GetValueIfFindKey<AppqfInfo>(jsonObject, jsonObjectEnd,
66 APP_QUICK_FIX_DEPLOYED_APP_QF_INFO, appQuickFix.deployedAppqfInfo,
67 JsonType::OBJECT, false, parseResult,
68 ArrayType::NOT_ARRAY);
69
70 GetValueIfFindKey<AppqfInfo>(jsonObject, jsonObjectEnd,
71 APP_QUICK_FIX_DEPLOYING_APP_QF_INFO, appQuickFix.deployingAppqfInfo,
72 JsonType::OBJECT, false, parseResult,
73 ArrayType::NOT_ARRAY);
74 if (parseResult != ERR_OK) {
75 LOG_E(BMS_TAG_DEFAULT, "read module appQuickFix from jsonObject error, error code : %{public}d", parseResult);
76 }
77 }
78
ReadFromParcel(Parcel & parcel)79 bool AppQuickFix::ReadFromParcel(Parcel &parcel)
80 {
81 bundleName = Str16ToStr8(parcel.ReadString16());
82 versionCode = parcel.ReadUint32();
83 versionName = Str16ToStr8(parcel.ReadString16());
84 std::unique_ptr<AppqfInfo> deployedAppqfInfoPtr(parcel.ReadParcelable<AppqfInfo>());
85 if (!deployedAppqfInfoPtr) {
86 LOG_E(BMS_TAG_DEFAULT, "ReadParcelable<AppqfInfo> failed");
87 return false;
88 }
89 deployedAppqfInfo = *deployedAppqfInfoPtr;
90
91 std::unique_ptr<AppqfInfo> deployingAppqfInfoPtr(parcel.ReadParcelable<AppqfInfo>());
92 if (!deployingAppqfInfoPtr) {
93 LOG_E(BMS_TAG_DEFAULT, "ReadParcelable<AppqfInfo> failed");
94 return false;
95 }
96 deployingAppqfInfo = *deployingAppqfInfoPtr;
97 return true;
98 }
99
Marshalling(Parcel & parcel) const100 bool AppQuickFix::Marshalling(Parcel &parcel) const
101 {
102 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
103 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, versionCode);
104 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(versionName));
105 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &deployedAppqfInfo);
106 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &deployingAppqfInfo);
107 return true;
108 }
109
Unmarshalling(Parcel & parcel)110 AppQuickFix *AppQuickFix::Unmarshalling(Parcel &parcel)
111 {
112 AppQuickFix *info = new (std::nothrow) AppQuickFix();
113 if (info && !info->ReadFromParcel(parcel)) {
114 LOG_E(BMS_TAG_DEFAULT, "read from parcel failed");
115 delete info;
116 info = nullptr;
117 }
118 return info;
119 }
120 } // AppExecFwk
121 } // OHOS