• 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     if (parseResult != ERR_OK) {
74         APP_LOGE("read module appQuickFix from jsonObject error, error code : %{public}d", parseResult);
75     }
76 }
77 
ReadFromParcel(Parcel & parcel)78 bool AppQuickFix::ReadFromParcel(Parcel &parcel)
79 {
80     bundleName = Str16ToStr8(parcel.ReadString16());
81     versionCode = parcel.ReadUint32();
82     versionName = Str16ToStr8(parcel.ReadString16());
83     std::unique_ptr<AppqfInfo> deployedAppqfInfoPtr(parcel.ReadParcelable<AppqfInfo>());
84     if (!deployedAppqfInfoPtr) {
85         APP_LOGE("ReadParcelable<AppqfInfo> failed");
86         return false;
87     }
88     deployedAppqfInfo = *deployedAppqfInfoPtr;
89 
90     std::unique_ptr<AppqfInfo> deployingAppqfInfoPtr(parcel.ReadParcelable<AppqfInfo>());
91     if (!deployingAppqfInfoPtr) {
92         APP_LOGE("ReadParcelable<AppqfInfo> failed");
93         return false;
94     }
95     deployingAppqfInfo = *deployingAppqfInfoPtr;
96     return true;
97 }
98 
Marshalling(Parcel & parcel) const99 bool AppQuickFix::Marshalling(Parcel &parcel) const
100 {
101     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
102     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, versionCode);
103     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(versionName));
104     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &deployedAppqfInfo);
105     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &deployingAppqfInfo);
106     return true;
107 }
108 
Unmarshalling(Parcel & parcel)109 AppQuickFix *AppQuickFix::Unmarshalling(Parcel &parcel)
110 {
111     AppQuickFix *info = new (std::nothrow) AppQuickFix();
112     if (info && !info->ReadFromParcel(parcel)) {
113         APP_LOGE("read from parcel failed");
114         delete info;
115         info = nullptr;
116     }
117     return info;
118 }
119 } // AppExecFwk
120 } // OHOS