• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "UpdateStrategy"
16 
17 #include "update_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 "general/permission_strategy.h"
23 #include "general/process_single_app_user_cross_strategy.h"
24 #include "log_print.h"
25 #include "utils/anonymous.h"
26 
27 namespace OHOS::DataShare {
Execute(std::shared_ptr<Context> context,const DataSharePredicates & predicate,const DataShareValuesBucket & valuesBucket)28 int64_t UpdateStrategy::Execute(
29     std::shared_ptr<Context> context, const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket)
30 {
31     auto &preProcess = GetStrategy();
32     if (preProcess.IsEmpty()) {
33         ZLOGE("get strategy fail, maybe memory not enough");
34         return -1;
35     }
36     if (!preProcess(context)) {
37         ZLOGE("pre process fail, uri: %{public}s", DistributedData::Anonymous::Change(context->uri).c_str());
38         return -1;
39     }
40     auto delegate = DBDelegate::Create(context->calledSourceDir, context->version, true,
41         context->isEncryptDb, context->secretMetaKey);
42     if (delegate == nullptr) {
43         ZLOGE("malloc fail %{public}s %{public}s", context->calledBundleName.c_str(), context->calledTableName.c_str());
44         return -1;
45     }
46     return delegate->Update(context->calledTableName, predicate, valuesBucket);
47 }
48 
GetStrategy()49 SeqStrategy &UpdateStrategy::GetStrategy()
50 {
51     std::lock_guard<decltype(mutex_)> lock(mutex_);
52     if (!strategies_.IsEmpty()) {
53         return strategies_;
54     }
55     std::initializer_list<Strategy *> list = {
56         new (std::nothrow)LoadConfigCommonStrategy(),
57         new (std::nothrow)LoadConfigFromBundleInfoStrategy(),
58         new (std::nothrow)PermissionStrategy(),
59         new (std::nothrow)LoadConfigDataInfoStrategy(),
60         new (std::nothrow)ProcessSingleAppUserCrossStrategy()
61     };
62     auto ret = strategies_.Init(list);
63     if (!ret) {
64         std::for_each(list.begin(), list.end(), [](Strategy *item) {
65             delete item;
66         });
67         return strategies_;
68     }
69     return strategies_;
70 }
71 } // namespace OHOS::DataShare