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 "PublishStrategy"
16 
17 #include "publish_strategy.h"
18 
19 #include "data_proxy/load_config_from_data_proxy_node_strategy.h"
20 #include "general/cross_permission_strategy.h"
21 #include "general/load_config_common_strategy.h"
22 #include "general/permission_strategy.h"
23 #include "log_print.h"
24 #include "published_data.h"
25 #include "utils/anonymous.h"
26 
27 namespace OHOS::DataShare {
Execute(std::shared_ptr<Context> context,const PublishedDataItem & item)28 int32_t PublishStrategy::Execute(std::shared_ptr<Context> context, const PublishedDataItem &item)
29 {
30     auto &preProcess = GetStrategy();
31     if (preProcess.IsEmpty()) {
32         ZLOGE("get strategy fail, maybe memory not enough");
33         return -1;
34     }
35     if (!preProcess(context)) {
36         ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str());
37         return context->errCode;
38     }
39     auto delegate = KvDBDelegate::GetInstance();
40     if (delegate == nullptr) {
41         ZLOGE("db open failed");
42         return -1;
43     }
44     PublishedDataItem::DataType value = item.GetData();
45     PublishedDataNode node(context->uri, context->calledBundleName, item.subscriberId_, context->currentUserId,
46         PublishedDataNode::MoveTo(value));
47     PublishedData data(node, context->version);
48     int32_t status = delegate->Upsert(KvDBDelegate::DATA_TABLE, data);
49     if (status != E_OK) {
50         ZLOGE("db Upsert failed, %{public}s %{public}s %{public}d", context->calledBundleName.c_str(),
51             DistributedData::Anonymous::Change(context->uri).c_str(), status);
52         return status;
53     }
54     return E_OK;
55 }
56 
GetStrategy()57 SeqStrategy &PublishStrategy::GetStrategy()
58 {
59     std::lock_guard<decltype(mutex_)> lock(mutex_);
60     if (!strategies_.IsEmpty()) {
61         return strategies_;
62     }
63     std::initializer_list<std::string> allowCrossPermissions = { "ohos.permission.WRITE_APP_PUSH_DATA" };
64     std::initializer_list<Strategy *> list = {
65         new (std::nothrow) LoadConfigCommonStrategy(),
66         new (std::nothrow) CrossPermissionStrategy(allowCrossPermissions),
67         new (std::nothrow) LoadConfigFromDataProxyNodeStrategy(),
68         new (std::nothrow) PermissionStrategy()
69     };
70     auto ret = strategies_.Init(list);
71     if (!ret) {
72         std::for_each(list.begin(), list.end(), [](Strategy *item) {
73             delete item;
74         });
75         return strategies_;
76     }
77     return strategies_;
78 }
79 } // namespace OHOS::DataShare