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