• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include <fstream>
16 #include <mutex>
17 #include "data_share_config.h"
18 #include "datashare_log.h"
19 namespace OHOS {
20 namespace DataShare {
ConfigFactory()21 ConfigFactory::ConfigFactory() : file_(std::string(CONF_PATH) + "/config.json")
22 {
23 }
24 
~ConfigFactory()25 ConfigFactory::~ConfigFactory()
26 {
27 }
28 
GetInstance()29 ConfigFactory &ConfigFactory::GetInstance()
30 {
31     static std::mutex mutex;
32     static ConfigFactory factory;
33     if (factory.isInited) {
34         return factory;
35     }
36     std::lock_guard<std::mutex> lock(mutex);
37     factory.Initialize();
38     return factory;
39 }
40 
Initialize()41 int32_t ConfigFactory::Initialize()
42 {
43     std::string jsonStr;
44     std::ifstream fin(file_);
45     if (!fin.is_open()) {
46         LOG_ERROR("ConfigFactory open file failed");
47         return -1;
48     }
49     while (fin.good()) {
50         std::string line;
51         std::getline(fin, line);
52         jsonStr += line;
53     }
54     config_.Unmarshall(jsonStr);
55     isInited = true;
56     return 0;
57 }
58 
GetDataShareConfig()59 DataShareConfig *ConfigFactory::GetDataShareConfig()
60 {
61     return config_.dataShare;
62 }
63 
Marshal(Serializable::json & node) const64 bool DataShareConfig::Marshal(Serializable::json &node) const
65 {
66     SetValue(node[GET_NAME(dataShareExtNames)], dataShareExtNames);
67     SetValue(node[GET_NAME(uriTrusts)], uriTrusts);
68     SetValue(node[GET_NAME(extensionObsTrusts)], extensionObsTrusts);
69     return true;
70 }
71 
Unmarshal(const Serializable::json & node)72 bool DataShareConfig::Unmarshal(const Serializable::json &node)
73 {
74     GetValue(node, GET_NAME(dataShareExtNames), dataShareExtNames);
75     GetValue(node, GET_NAME(uriTrusts), uriTrusts);
76     GetValue(node, GET_NAME(extensionObsTrusts), extensionObsTrusts);
77     return true;
78 }
79 
Marshal(Serializable::json & node) const80 bool DataShareConfig::ConsumerProvider::Marshal(Serializable::json &node) const
81 {
82     SetValue(node[GET_NAME(consumer)], consumer);
83     SetValue(node[GET_NAME(provider)], provider);
84     return true;
85 }
86 
Unmarshal(const Serializable::json & node)87 bool DataShareConfig::ConsumerProvider::Unmarshal(const Serializable::json &node)
88 {
89     GetValue(node, GET_NAME(consumer), consumer);
90     GetValue(node, GET_NAME(provider), provider);
91     return true;
92 }
93 
Marshal(Serializable::json & node) const94 bool DataShareConfig::Bundle::Marshal(Serializable::json &node) const
95 {
96     SetValue(node[GET_NAME(name)], name);
97     SetValue(node[GET_NAME(appIdentifier)], appIdentifier);
98     return true;
99 }
100 
Unmarshal(const Serializable::json & node)101 bool DataShareConfig::Bundle::Unmarshal(const Serializable::json &node)
102 {
103     GetValue(node, GET_NAME(name), name);
104     GetValue(node, GET_NAME(appIdentifier), appIdentifier);
105     return true;
106 }
107 
Marshal(json & node) const108 bool GlobalConfig::Marshal(json &node) const
109 {
110     SetValue(node[GET_NAME(dataShare)], dataShare);
111     return true;
112 }
113 
Unmarshal(const json & node)114 bool GlobalConfig::Unmarshal(const json &node)
115 {
116     GetValue(node, GET_NAME(dataShare), dataShare);
117     return true;
118 }
119 
~GlobalConfig()120 GlobalConfig::~GlobalConfig()
121 {
122     if (dataShare != nullptr) {
123         delete dataShare;
124     }
125 }
126 } // namespace DistributedData
127 } // namespace OHOS
128