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