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 #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_NETWORK_SYNC_STRATEGY_H
16 #define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_NETWORK_SYNC_STRATEGY_H
17 #include "cloud/sync_strategy.h"
18 #include "concurrent_map.h"
19 #include "serializable/serializable.h"
20 namespace OHOS::CloudData {
21 class NetworkSyncStrategy : public DistributedData::SyncStrategy {
22 public:
23     using StoreInfo = DistributedData::StoreInfo;
24     enum Strategy : uint32_t {
25         // should equal to NetWorkStrategy::WIFI in cloud_types.h
26         WIFI = 0x01,
27         // should equal to NetWorkStrategy::CELLULAR in cloud_types.h
28         CELLULAR = 0x02,
29         BUTT
30     };
31     struct StrategyInfo : public DistributedData::Serializable {
32         int32_t user = INVALID_USER;
33         std::string bundleName;
34         uint32_t strategy = DEFAULT_STRATEGY;
35         bool Marshal(json &node) const override;
36         bool Unmarshal(const json &node) override;
37         std::string GetKey();
38         static constexpr int32_t INVALID_USER = -1;
39         static constexpr const char *PREFIX = "NETWORK_SYNC_STRATEGY";
40     };
41     NetworkSyncStrategy();
42     ~NetworkSyncStrategy();
43     int32_t CheckSyncAction(const StoreInfo &storeInfo) override;
44 
45     static std::string GetKey(int32_t user);
46     static std::string GetKey(int32_t user, const std::string &bundleName);
47 
48 private:
49     StrategyInfo GetStrategy(int32_t user, const std::string &bundleName);
50 
51     static bool Check(uint32_t strategy);
52     static constexpr uint32_t DEFAULT_STRATEGY = WIFI | CELLULAR;
53     std::shared_ptr<SyncStrategy> next_;
54     int32_t user_ = -1;
55     ConcurrentMap<std::string, StrategyInfo> strategies_;
56 };
57 } // namespace OHOS::CloudData
58 #endif //OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_NETWORK_SYNC_STRATEGY_H