• 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 #include "shortcut_info.h"
16 
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <string.h>
21 
22 #include "json_serializer.h"
23 #include "nlohmann/json.hpp"
24 #include "string_ex.h"
25 #include "parcel_macro.h"
26 #include "app_log_wrapper.h"
27 #include "bundle_constants.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 
32 namespace {
33 
34 const std::string JSON_KEY_BUNDLE_ID = "id";
35 const std::string JSON_KEY_BUNDLE_NAME = "bundleName";
36 const std::string JSON_KEY_BUNDLE_HOST_ABILITY = "hostAbility";
37 const std::string JSON_KEY_BUNDLE_ICON = "icon";
38 const std::string JSON_KEY_BUNDLE_LABEL = "label";
39 const std::string JSON_KEY_BUNDLE_DISABLE_MESSAGE = "disableMessage";
40 const std::string JSON_KEY_BUNDLE_IS_STATIC = "isStatic";
41 const std::string JSON_KEY_BUNDLE_IS_HOME_SHORTCUT = "isHomeShortcut";
42 const std::string JSON_KEY_BUNDLE_IS_ENABLES = "isEnables";
43 const std::string JSON_KEY_BUNDLE_INTENTS = "intents";
44 const std::string JSON_KEY_BUNDLE_TARGET_BUNDLE = "targetBundle";
45 const std::string JSON_KEY_BUNDLE_TARGET_CLASS = "targetClass";
46 
47 }  // namespace
48 
ReadFromParcel(Parcel & parcel)49 bool ShortcutInfo::ReadFromParcel(Parcel &parcel)
50 {
51     id = Str16ToStr8(parcel.ReadString16());
52     bundleName = Str16ToStr8(parcel.ReadString16());
53     hostAbility = Str16ToStr8(parcel.ReadString16());
54     icon = Str16ToStr8(parcel.ReadString16());
55     label = Str16ToStr8(parcel.ReadString16());
56     disableMessage = Str16ToStr8(parcel.ReadString16());
57     isStatic = parcel.ReadBool();
58     isHomeShortcut = parcel.ReadBool();
59     isEnables = parcel.ReadBool();
60     int32_t intentsSize;
61     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, intentsSize);
62     for (auto i = 0; i < intentsSize; i++) {
63         ShortcutIntent shortcutIntent;
64         std::string targetBundleName = Str16ToStr8(parcel.ReadString16());
65         std::string targetClassName = Str16ToStr8(parcel.ReadString16());
66         shortcutIntent.targetBundle = targetBundleName;
67         shortcutIntent.targetClass = targetClassName;
68         intents.emplace_back(shortcutIntent);
69     }
70     return true;
71 }
72 
Unmarshalling(Parcel & parcel)73 ShortcutInfo *ShortcutInfo::Unmarshalling(Parcel &parcel)
74 {
75     ShortcutInfo *info = new ShortcutInfo();
76     if (!info->ReadFromParcel(parcel)) {
77         APP_LOGW("read from parcel failed");
78         delete info;
79         info = nullptr;
80     }
81     return info;
82 }
83 
Marshalling(Parcel & parcel) const84 bool ShortcutInfo::Marshalling(Parcel &parcel) const
85 {
86     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(id));
87     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
88     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hostAbility));
89     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(icon));
90     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(label));
91     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(disableMessage));
92     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isStatic);
93     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isHomeShortcut);
94     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isEnables);
95 
96     const auto intentsSize = static_cast<int32_t>(intents.size());
97     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, intentsSize);
98     for (auto i = 0; i < intentsSize; i++) {
99         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(intents[i].targetBundle));
100         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(intents[i].targetClass));
101     }
102     return true;
103 }
104 
to_json(nlohmann::json & jsonObject,const ShortcutIntent & shortcutIntent)105 void to_json(nlohmann::json &jsonObject, const ShortcutIntent &shortcutIntent)
106 {
107     jsonObject = nlohmann::json {
108         {JSON_KEY_BUNDLE_TARGET_BUNDLE, shortcutIntent.targetBundle},
109         {JSON_KEY_BUNDLE_TARGET_CLASS, shortcutIntent.targetClass}};
110 }
111 
to_json(nlohmann::json & jsonObject,const ShortcutInfo & shortcutInfo)112 void to_json(nlohmann::json &jsonObject, const ShortcutInfo &shortcutInfo)
113 {
114     jsonObject = nlohmann::json {
115         {JSON_KEY_BUNDLE_ID, shortcutInfo.id},
116         {JSON_KEY_BUNDLE_NAME, shortcutInfo.bundleName},
117         {JSON_KEY_BUNDLE_HOST_ABILITY, shortcutInfo.hostAbility},
118         {JSON_KEY_BUNDLE_ICON, shortcutInfo.icon},
119         {JSON_KEY_BUNDLE_LABEL, shortcutInfo.label},
120         {JSON_KEY_BUNDLE_DISABLE_MESSAGE, shortcutInfo.disableMessage},
121         {JSON_KEY_BUNDLE_IS_STATIC, shortcutInfo.isStatic},
122         {JSON_KEY_BUNDLE_IS_HOME_SHORTCUT, shortcutInfo.isHomeShortcut},
123         {JSON_KEY_BUNDLE_IS_ENABLES, shortcutInfo.isEnables},
124         {JSON_KEY_BUNDLE_INTENTS, shortcutInfo.intents}};
125 }
126 
from_json(const nlohmann::json & jsonObject,ShortcutIntent & shortcutIntent)127 void from_json(const nlohmann::json &jsonObject, ShortcutIntent &shortcutIntent)
128 {
129     shortcutIntent.targetBundle = jsonObject.at(JSON_KEY_BUNDLE_TARGET_BUNDLE).get<std::string>();
130     shortcutIntent.targetClass = jsonObject.at(JSON_KEY_BUNDLE_TARGET_CLASS).get<std::string>();
131 }
132 
from_json(const nlohmann::json & jsonObject,ShortcutInfo & shortcutInfo)133 void from_json(const nlohmann::json &jsonObject, ShortcutInfo &shortcutInfo)
134 {
135     shortcutInfo.id = jsonObject.at(JSON_KEY_BUNDLE_ID).get<std::string>();
136     shortcutInfo.bundleName = jsonObject.at(JSON_KEY_BUNDLE_NAME).get<std::string>();
137     shortcutInfo.hostAbility = jsonObject.at(JSON_KEY_BUNDLE_HOST_ABILITY).get<std::string>();
138     shortcutInfo.icon = jsonObject.at(JSON_KEY_BUNDLE_ICON).get<std::string>();
139     shortcutInfo.label = jsonObject.at(JSON_KEY_BUNDLE_LABEL).get<std::string>();
140     shortcutInfo.disableMessage = jsonObject.at(JSON_KEY_BUNDLE_DISABLE_MESSAGE).get<std::string>();
141     shortcutInfo.isStatic = jsonObject.at(JSON_KEY_BUNDLE_IS_STATIC).get<bool>();
142     shortcutInfo.isHomeShortcut = jsonObject.at(JSON_KEY_BUNDLE_IS_HOME_SHORTCUT).get<bool>();
143     shortcutInfo.isEnables = jsonObject.at(JSON_KEY_BUNDLE_IS_ENABLES).get<bool>();
144     shortcutInfo.intents = jsonObject.at(JSON_KEY_BUNDLE_INTENTS).get<std::vector<ShortcutIntent>>();
145 }
146 
147 }  // namespace AppExecFwk
148 }  // namespace OHOS
149