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 BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
51 Constants::BUNDLE_NAME, appQuickFix.bundleName,
52 false, parseResult);
53
54 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd,
55 APP_QUICK_FIX_VERSION_CODE, appQuickFix.versionCode,
56 JsonType::NUMBER, false, parseResult,
57 ArrayType::NOT_ARRAY);
58
59 BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd,
60 APP_QUICK_FIX_VERSION_NAME, appQuickFix.versionName,
61 false, parseResult);
62
63 GetValueIfFindKey<AppqfInfo>(jsonObject, jsonObjectEnd,
64 APP_QUICK_FIX_DEPLOYED_APP_QF_INFO, appQuickFix.deployedAppqfInfo,
65 JsonType::OBJECT, false, parseResult,
66 ArrayType::NOT_ARRAY);
67
68 GetValueIfFindKey<AppqfInfo>(jsonObject, jsonObjectEnd,
69 APP_QUICK_FIX_DEPLOYING_APP_QF_INFO, appQuickFix.deployingAppqfInfo,
70 JsonType::OBJECT, false, parseResult,
71 ArrayType::NOT_ARRAY);
72 if (parseResult != ERR_OK) {
73 LOG_E(BMS_TAG_DEFAULT, "read module appQuickFix from jsonObject error, error code : %{public}d", parseResult);
74 }
75 }
76
ReadFromParcel(Parcel & parcel)77 bool AppQuickFix::ReadFromParcel(Parcel &parcel)
78 {
79 bundleName = Str16ToStr8(parcel.ReadString16());
80 versionCode = parcel.ReadUint32();
81 versionName = Str16ToStr8(parcel.ReadString16());
82 std::unique_ptr<AppqfInfo> deployedAppqfInfoPtr(parcel.ReadParcelable<AppqfInfo>());
83 if (!deployedAppqfInfoPtr) {
84 LOG_E(BMS_TAG_DEFAULT, "ReadParcelable<AppqfInfo> failed");
85 return false;
86 }
87 deployedAppqfInfo = *deployedAppqfInfoPtr;
88
89 std::unique_ptr<AppqfInfo> deployingAppqfInfoPtr(parcel.ReadParcelable<AppqfInfo>());
90 if (!deployingAppqfInfoPtr) {
91 LOG_E(BMS_TAG_DEFAULT, "ReadParcelable<AppqfInfo> failed");
92 return false;
93 }
94 deployingAppqfInfo = *deployingAppqfInfoPtr;
95 return true;
96 }
97
Marshalling(Parcel & parcel) const98 bool AppQuickFix::Marshalling(Parcel &parcel) const
99 {
100 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
101 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, versionCode);
102 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(versionName));
103 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &deployedAppqfInfo);
104 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &deployingAppqfInfo);
105 return true;
106 }
107
Unmarshalling(Parcel & parcel)108 AppQuickFix *AppQuickFix::Unmarshalling(Parcel &parcel)
109 {
110 AppQuickFix *info = new (std::nothrow) AppQuickFix();
111 if (info && !info->ReadFromParcel(parcel)) {
112 LOG_E(BMS_TAG_DEFAULT, "read from parcel failed");
113 delete info;
114 info = nullptr;
115 }
116 return info;
117 }
118 } // AppExecFwk
119 } // OHOS