• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "module_usage_record.h"
17 
18 #include "string_ex.h"
19 
20 #include "json_util.h"
21 #include "nlohmann/json.hpp"
22 #include "parcel_macro.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
ReadFromParcel(Parcel & parcel)26 bool ModuleUsageRecord::ReadFromParcel(Parcel &parcel)
27 {
28     name = Str16ToStr8(parcel.ReadString16());
29     bundleName = Str16ToStr8(parcel.ReadString16());
30     abilityName = Str16ToStr8(parcel.ReadString16());
31     appLabelId = static_cast<uint32_t>(parcel.ReadInt32());
32     labelId = static_cast<uint32_t>(parcel.ReadInt32());
33     descriptionId = static_cast<uint32_t>(parcel.ReadInt32());
34     abilityLabelId = static_cast<uint32_t>(parcel.ReadInt32());
35     abilityDescriptionId = static_cast<uint32_t>(parcel.ReadInt32());
36     abilityIconId = static_cast<uint32_t>(parcel.ReadInt32());
37     launchedCount = static_cast<uint32_t>(parcel.ReadInt32());
38     lastLaunchTime = parcel.ReadInt64();
39     removed = parcel.ReadBool();
40     installationFreeSupported = parcel.ReadBool();
41     return true;
42 }
43 
Unmarshalling(Parcel & parcel)44 ModuleUsageRecord *ModuleUsageRecord::Unmarshalling(Parcel &parcel)
45 {
46     ModuleUsageRecord *record = new ModuleUsageRecord();
47     if (record && !record->ReadFromParcel(parcel)) {
48         APP_LOGW("read ModuleUsageRecord from parcel failed");
49         delete record;
50         record = nullptr;
51     }
52     return record;
53 }
54 
Marshalling(Parcel & parcel) const55 bool ModuleUsageRecord::Marshalling(Parcel &parcel) const
56 {
57     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(name));
58     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
59     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName));
60     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appLabelId);
61     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, labelId);
62     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, descriptionId);
63     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, abilityLabelId);
64     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, abilityDescriptionId);
65     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, abilityIconId);
66     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, launchedCount);
67     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int64, parcel, lastLaunchTime);
68     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, removed);
69     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, installationFreeSupported);
70     return true;
71 }
72 
ToString() const73 std::string ModuleUsageRecord::ToString() const
74 {
75     nlohmann::json json;
76     json[UsageRecordKey::LAUNCHED_COUNT] = launchedCount;
77     json[UsageRecordKey::LAST_LAUNCH_TIME] = lastLaunchTime;
78     json[UsageRecordKey::IS_REMOVED] = removed;
79     json[UsageRecordKey::ABILITY_NAME] = abilityName;
80     return json.dump();
81 }
82 
FromJsonString(const std::string & jsonString)83 bool ModuleUsageRecord::FromJsonString(const std::string &jsonString)
84 {
85     nlohmann::json jsonObject;
86 
87     jsonObject = nlohmann::json::parse(jsonString);
88     if (jsonObject.is_discarded()) {
89         APP_LOGE("failed to parse module usage record: %{public}s.", jsonString.c_str());
90         return false;
91     }
92 
93     const auto &jsonObjectEnd = jsonObject.end();
94     int32_t parseResult = ERR_OK;
95     GetValueIfFindKey<uint32_t>(jsonObject,
96         jsonObjectEnd,
97         UsageRecordKey::LAUNCHED_COUNT,
98         launchedCount,
99         JsonType::NUMBER,
100         false,
101         parseResult,
102         ArrayType::NOT_ARRAY);
103     GetValueIfFindKey<int64_t>(jsonObject,
104         jsonObjectEnd,
105         UsageRecordKey::LAST_LAUNCH_TIME,
106         lastLaunchTime,
107         JsonType::NUMBER,
108         false,
109         parseResult,
110         ArrayType::NOT_ARRAY);
111     GetValueIfFindKey<bool>(jsonObject,
112         jsonObjectEnd,
113         UsageRecordKey::IS_REMOVED,
114         removed,
115         JsonType::BOOLEAN,
116         false,
117         parseResult,
118         ArrayType::NOT_ARRAY);
119     GetValueIfFindKey<std::string>(jsonObject,
120         jsonObjectEnd,
121         UsageRecordKey::ABILITY_NAME,
122         abilityName,
123         JsonType::STRING,
124         false,
125         parseResult,
126         ArrayType::NOT_ARRAY);
127     return parseResult == ERR_OK;
128 }
129 }  // namespace AppExecFwk
130 }  // namespace OHOS