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