1 /*
2 * Copyright (c) 2023 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 "cloud/cloud_info.h"
17 #include "utils/constant.h"
18
19 namespace OHOS::DistributedData {
Marshal(Serializable::json & node) const20 bool CloudInfo::Marshal(Serializable::json &node) const
21 {
22 SetValue(node[GET_NAME(user)], user);
23 SetValue(node[GET_NAME(id)], id);
24 SetValue(node[GET_NAME(totalSpace)], totalSpace);
25 SetValue(node[GET_NAME(remainSpace)], remainSpace);
26 SetValue(node[GET_NAME(enableCloud)], enableCloud);
27 SetValue(node[GET_NAME(apps)], apps);
28 return true;
29 }
30
Unmarshal(const Serializable::json & node)31 bool CloudInfo::Unmarshal(const Serializable::json &node)
32 {
33 GetValue(node, GET_NAME(user), user);
34 GetValue(node, GET_NAME(id), id);
35 GetValue(node, GET_NAME(totalSpace), totalSpace);
36 GetValue(node, GET_NAME(remainSpace), remainSpace);
37 GetValue(node, GET_NAME(enableCloud), enableCloud);
38 GetValue(node, GET_NAME(apps), apps);
39 return true;
40 }
41
Marshal(Serializable::json & node) const42 bool CloudInfo::AppInfo::Marshal(Serializable::json &node) const
43 {
44 SetValue(node[GET_NAME(bundleName)], bundleName);
45 SetValue(node[GET_NAME(appId)], appId);
46 SetValue(node[GET_NAME(version)], version);
47 SetValue(node[GET_NAME(instanceId)], instanceId);
48 SetValue(node[GET_NAME(cloudSwitch)], cloudSwitch);
49 return true;
50 }
51
Unmarshal(const Serializable::json & node)52 bool CloudInfo::AppInfo::Unmarshal(const Serializable::json &node)
53 {
54 GetValue(node, GET_NAME(bundleName), bundleName);
55 GetValue(node, GET_NAME(appId), appId);
56 GetValue(node, GET_NAME(version), version);
57 GetValue(node, GET_NAME(instanceId), instanceId);
58 GetValue(node, GET_NAME(cloudSwitch), cloudSwitch);
59 return true;
60 }
61
GetKey() const62 std::string CloudInfo::GetKey() const
63 {
64 return GetKey(INFO_PREFIX, { std::to_string(user) });
65 }
66
GetSchemaKey() const67 std::map<std::string, std::string> CloudInfo::GetSchemaKey() const
68 {
69 std::map<std::string, std::string> keys;
70 for (const auto &[bundle, app] : apps) {
71 const auto key = GetKey(
72 SCHEMA_PREFIX, { std::to_string(user), bundle, std::to_string(app.instanceId) });
73 keys.insert_or_assign(app.bundleName, key);
74 }
75 return keys;
76 }
77
GetSchemaKey(const std::string & bundleName,int32_t instanceId) const78 std::string CloudInfo::GetSchemaKey(const std::string &bundleName, int32_t instanceId) const
79 {
80 return GetKey(SCHEMA_PREFIX, { std::to_string(user), bundleName, std::to_string(instanceId) });
81 }
82
GetSchemaKey(int32_t user,const std::string & bundleName,int32_t instanceId)83 std::string CloudInfo::GetSchemaKey(int32_t user, const std::string &bundleName, int32_t instanceId)
84 {
85 return GetKey(SCHEMA_PREFIX, { std::to_string(user), bundleName, std::to_string(instanceId) });
86 }
87
GetSchemaPrefix(const std::string & bundleName) const88 std::string CloudInfo::GetSchemaPrefix(const std::string &bundleName) const
89 {
90 if (bundleName.empty()) {
91 return GetKey(SCHEMA_PREFIX, { std::to_string(user) });
92 }
93 return GetKey(SCHEMA_PREFIX, { std::to_string(user), bundleName});
94 }
95
GetSchemaKey(const StoreMetaData & meta)96 std::string CloudInfo::GetSchemaKey(const StoreMetaData &meta)
97 {
98 return GetKey(SCHEMA_PREFIX, { meta.user, meta.bundleName, std::to_string(meta.instanceId) });
99 }
100
IsValid() const101 bool CloudInfo::IsValid() const
102 {
103 return !id.empty();
104 }
105
Exist(const std::string & bundleName,int32_t instanceId)106 bool CloudInfo::Exist(const std::string &bundleName, int32_t instanceId)
107 {
108 if (bundleName.empty()) {
109 return false;
110 }
111 auto it = apps.find(bundleName);
112 return it != apps.end() && it->second.bundleName == bundleName && it->second.instanceId == instanceId;
113 }
114
IsOn(const std::string & bundleName,int32_t instanceId)115 bool CloudInfo::IsOn(const std::string &bundleName, int32_t instanceId)
116 {
117 if (bundleName.empty()) {
118 return false;
119 }
120 auto it = apps.find(bundleName);
121 return it != apps.end() && it->second.instanceId == instanceId && it->second.cloudSwitch;
122 }
123
GetPrefix(const std::initializer_list<std::string> & fields)124 std::string CloudInfo::GetPrefix(const std::initializer_list<std::string> &fields)
125 {
126 return GetKey(INFO_PREFIX, fields).append(Constant::KEY_SEPARATOR);
127 }
128
GetKey(const std::string & prefix,const std::initializer_list<std::string> & fields)129 std::string CloudInfo::GetKey(const std::string &prefix, const std::initializer_list<std::string> &fields)
130 {
131 return Constant::Join(prefix, Constant::KEY_SEPARATOR, fields);
132 }
133 } // namespace OHOS::DistributedData