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 #define LOG_TAG "SubscribeStrategy"
16 
17 #include "subscribe_strategy.h"
18 
19 #include "data_proxy/load_config_from_data_proxy_node_strategy.h"
20 #include "general/load_config_common_strategy.h"
21 #include "general/load_config_data_info_strategy.h"
22 #include "general/permission_strategy.h"
23 #include "log_print.h"
24 #include "utils/anonymous.h"
25 
26 namespace OHOS::DataShare {
Execute(std::shared_ptr<Context> context,std::function<int32_t ()> process)27 int32_t SubscribeStrategy::Execute(std::shared_ptr<Context> context, std::function<int32_t()> process)
28 {
29     auto &preProcess = GetStrategy();
30     if (preProcess.IsEmpty()) {
31         ZLOGE("get strategy fail, maybe memory not enough");
32         return -1;
33     }
34     context->isRead = true;
35     context->needAutoLoadCallerBundleName = true;
36     if (!preProcess(context)) {
37         ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str());
38         return context->errCode;
39     }
40     return process();
41 }
42 
GetStrategy()43 SeqStrategy &SubscribeStrategy::GetStrategy()
44 {
45     std::lock_guard<decltype(mutex_)> lock(mutex_);
46     if (!strategies_.IsEmpty()) {
47         return strategies_;
48     }
49     std::initializer_list<Strategy *> list = {
50         new (std::nothrow)LoadConfigCommonStrategy(),
51         new (std::nothrow)LoadConfigFromDataProxyNodeStrategy(),
52         new (std::nothrow)PermissionStrategy()
53     };
54     auto ret = strategies_.Init(list);
55     if (!ret) {
56         std::for_each(list.begin(), list.end(), [](Strategy *item) {
57             delete item;
58         });
59         return strategies_;
60     }
61     return strategies_;
62 }
63 } // namespace OHOS::DataShare