1 /*
2  * Copyright (c) 2022 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 
16 #ifndef UDMF_SERVICE_IMPL_H
17 #define UDMF_SERVICE_IMPL_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <vector>
23 
24 #include "error_code.h"
25 #include "store_cache.h"
26 #include "udmf_service_stub.h"
27 #include "unified_data.h"
28 #include "unified_types.h"
29 #include "visibility.h"
30 #include "kv_store_delegate_manager.h"
31 namespace OHOS {
32 namespace UDMF {
33 /*
34  * UDMF server implementation
35  */
36 class API_EXPORT UdmfServiceImpl final : public UdmfServiceStub {
37 public:
38     UdmfServiceImpl();
39     ~UdmfServiceImpl() = default;
40 
41     using DBLaunchParam = DistributedDB::AutoLaunchParam;
42     int32_t SetData(CustomOption &option, UnifiedData &unifiedData, std::string &key) override;
43     int32_t GetData(const QueryOption &query, UnifiedData &unifiedData) override;
44     int32_t GetBatchData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet) override;
45     int32_t UpdateData(const QueryOption &query, UnifiedData &unifiedData) override;
46     int32_t DeleteData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet) override;
47     int32_t GetSummary(const QueryOption &query, Summary &summary) override;
48     int32_t AddPrivilege(const QueryOption &query, Privilege &privilege) override;
49     int32_t Sync(const QueryOption &query, const std::vector<std::string> &devices) override;
50     int32_t IsRemoteData(const QueryOption &query, bool &result) override;
51     int32_t SetAppShareOption(const std::string &intention, int32_t shareOption) override;
52     int32_t GetAppShareOption(const std::string &intention, int32_t &shareOption) override;
53     int32_t RemoveAppShareOption(const std::string &intention) override;
54     int32_t OnInitialize() override;
55     int32_t OnBind(const BindInfo &bindInfo) override;
56     int32_t ResolveAutoLaunch(const std::string &identifier, DBLaunchParam &param) override;
57 
58 private:
59     int32_t SaveData(CustomOption &option, UnifiedData &unifiedData, std::string &key);
60     int32_t RetrieveData(const QueryOption &query, UnifiedData &unifiedData);
61     int32_t QueryDataCommon(const QueryOption &query, std::vector<UnifiedData> &dataSet, std::shared_ptr<Store> &store);
62     int32_t ProcessUri(const QueryOption &query, UnifiedData &unifiedData);
63     void SetRemoteUri(const QueryOption &query, std::vector<std::shared_ptr<UnifiedRecord>> &records);
64     bool IsPermissionInCache(const QueryOption &query);
65     bool IsReadAndKeep(const std::vector<Privilege> &privileges, const QueryOption &query);
66     bool VerifyPermission(const std::string &permission, uint32_t callerTokenId);
67 
68     class Factory {
69     public:
70         Factory();
71         ~Factory();
72 
73     private:
74         std::shared_ptr<UdmfServiceImpl> product_;
75     };
76     static Factory factory_;
77     std::map<std::string, Privilege> privilegeCache_;
78     std::shared_ptr<ExecutorPool> executors_;
79 };
80 } // namespace UDMF
81 } // namespace OHOS
82 #endif // UDMF_SERVICE_IMPL_H
83