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