1 /*
2 * Copyright (c) 2024 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 #define LOG_TAG "NetworkSyncStrategy"
17
18 #include "network_sync_strategy.h"
19
20 #include "device_manager_adapter.h"
21 #include "error/general_error.h"
22 #include "log_print.h"
23 #include "metadata/meta_data_manager.h"
24 #include "utils/constant.h"
25 namespace OHOS::CloudData {
26 using namespace OHOS::DistributedData;
NetworkSyncStrategy()27 NetworkSyncStrategy::NetworkSyncStrategy()
28 {
29 MetaDataManager::GetInstance().Subscribe(
30 StrategyInfo::PREFIX, [this](const std::string &key, const std::string &value, int32_t flag) -> auto {
31 StrategyInfo info;
32 StrategyInfo::Unmarshall(value, info);
33 ZLOGI("flag:%{public}d, value:%{public}s", flag, value.c_str());
34 if (info.user != user_) {
35 return true;
36 }
37 if (flag == MetaDataManager::INSERT || flag == MetaDataManager::UPDATE) {
38 strategies_.InsertOrAssign(info.bundleName, std::move(info));
39 } else if (flag == MetaDataManager::DELETE) {
40 strategies_.Erase(info.bundleName);
41 } else {
42 ZLOGE("ignored operation");
43 }
44 return true;
45 }, true);
46 }
47
~NetworkSyncStrategy()48 NetworkSyncStrategy::~NetworkSyncStrategy()
49 {
50 MetaDataManager::GetInstance().Unsubscribe(StrategyInfo::PREFIX);
51 }
52
CheckSyncAction(const StoreInfo & storeInfo)53 int32_t NetworkSyncStrategy::CheckSyncAction(const StoreInfo &storeInfo)
54 {
55 if (!DeviceManagerAdapter::GetInstance().IsNetworkAvailable()) {
56 return E_NETWORK_ERROR;
57 }
58 if (storeInfo.user != user_) {
59 strategies_.Clear();
60 user_ = storeInfo.user;
61 }
62 StrategyInfo info = GetStrategy(storeInfo.user, storeInfo.bundleName);
63 if (info.user == StrategyInfo::INVALID_USER) {
64 info = GetStrategy(storeInfo.user, GLOBAL_BUNDLE);
65 }
66 if (!Check(info.strategy)) {
67 return E_BLOCKED_BY_NETWORK_STRATEGY;
68 }
69 return next_ ? next_->CheckSyncAction(storeInfo) : E_OK;
70 }
71
Marshal(Serializable::json & node) const72 bool NetworkSyncStrategy::StrategyInfo::Marshal(Serializable::json &node) const
73 {
74 SetValue(node[GET_NAME(user)], user);
75 SetValue(node[GET_NAME(bundleName)], bundleName);
76 SetValue(node[GET_NAME(strategy)], strategy);
77 return true;
78 }
79
Unmarshal(const Serializable::json & node)80 bool NetworkSyncStrategy::StrategyInfo::Unmarshal(const Serializable::json &node)
81 {
82 GetValue(node, GET_NAME(user), user);
83 GetValue(node, GET_NAME(bundleName), bundleName);
84 GetValue(node, GET_NAME(strategy), strategy);
85 return false;
86 }
87
GetKey()88 std::string NetworkSyncStrategy::StrategyInfo::GetKey()
89 {
90 return Constant::Join(StrategyInfo::PREFIX, Constant::KEY_SEPARATOR, { std::to_string(user), bundleName });
91 }
92
GetKey(int32_t user)93 std::string NetworkSyncStrategy::GetKey(int32_t user)
94 {
95 return Constant::Join(StrategyInfo::PREFIX, Constant::KEY_SEPARATOR, { std::to_string(user) });
96 }
97
GetKey(int32_t user,const std::string & bundleName)98 std::string NetworkSyncStrategy::GetKey(int32_t user, const std::string &bundleName)
99 {
100 return Constant::Join(StrategyInfo::PREFIX, Constant::KEY_SEPARATOR, { std::to_string(user), bundleName });
101 }
102
Check(uint32_t strategy)103 bool NetworkSyncStrategy::Check(uint32_t strategy)
104 {
105 auto networkType = DeviceManagerAdapter::GetInstance().GetNetworkType();
106 if (networkType == DeviceManagerAdapter::NONE) {
107 networkType = DeviceManagerAdapter::GetInstance().GetNetworkType(true);
108 }
109 switch (networkType) {
110 case DeviceManagerAdapter::WIFI:
111 case DeviceManagerAdapter::ETHERNET:
112 return (strategy & WIFI) == WIFI;
113 case DeviceManagerAdapter::CELLULAR:
114 return (strategy & CELLULAR) == CELLULAR;
115 default:
116 ZLOGD("verification failed! strategy:%{public}d, networkType:%{public}d", strategy, networkType);
117 return false;
118 }
119 }
120
GetStrategy(int32_t user,const std::string & bundleName)121 NetworkSyncStrategy::StrategyInfo NetworkSyncStrategy::GetStrategy(int32_t user, const std::string &bundleName)
122 {
123 auto [res, info] = strategies_.Find(bundleName);
124 if (res) {
125 return info;
126 }
127 MetaDataManager::GetInstance().LoadMeta(GetKey(user, bundleName), info, true);
128 strategies_.Insert(info.bundleName, info);
129 return info;
130 }
131 }