• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "network_set_manager.h"
16 
17 #include "accesstoken_kit.h"
18 #include "cloud_file_kit.h"
19 #include "datashare_errno.h"
20 #include "datashare_result_set.h"
21 #include "dfs_error.h"
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #include "parameters.h"
25 #include "system_ability_definition.h"
26 #include "utils_log.h"
27 
28 namespace OHOS::FileManagement::CloudSync {
29 const std::string QUERY_URI = "datashareproxy://";
30 const std::string PHOTO_NET_CONT = "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
31 const std::string SWITCH_STATUS_KEY = "useMobileNetworkData";
32 const std::string CLOUDDRIVE_KEY = "persist.kernel.bundle_name.clouddrive";
33 const std::string PHOTOS_BUNDLE_NAME = "com.ohos.photos";
34 const std::string BUNDLE_NAME_KEY = "key";
35 const std::string BUNDLE_NAME_VALUE = "value";
36 auto clouddriveBundleName = system::GetParameter(CLOUDDRIVE_KEY, "");
37 auto photoQueryUri = PHOTO_NET_CONT + "&key=photo_network_connection_status";
38 auto queryUri = QUERY_URI + clouddriveBundleName + "/cloud_sp?key=" + SWITCH_STATUS_KEY;
39 DataShare::CreateOptions options;
40 
QueryCellularConnect(int32_t userId,const std::string & bundleName)41 int32_t NetworkSetManager::QueryCellularConnect(int32_t userId, const std::string &bundleName)
42 {
43     LOGI("QueryCellularConnect bundleName: %{public}s", bundleName.c_str());
44     DataShare::DataSharePredicates predicates;
45     predicates.EqualTo(BUNDLE_NAME_KEY, SWITCH_STATUS_KEY);
46     std::shared_ptr<DataShare::DataShareResultSet> resultSet;
47     std::vector<std::string> columns;
48     DataShare::CreateOptions options;
49     options.enabled_ = true;
50     auto clouddriveBundleName = system::GetParameter(CLOUDDRIVE_KEY, "");
51     auto queryUri = QUERY_URI + clouddriveBundleName + "/cloud_sp?user=" + std::to_string(userId);
52     Uri uri(queryUri);
53     auto dataShareHelper = DataShare::DataShareHelper::Creator(QUERY_URI, options);
54     if (dataShareHelper == nullptr) {
55         LOGE("dataShareHelper = nullptr");
56         return E_RDB;
57     }
58     resultSet = dataShareHelper->Query(uri, predicates, columns);
59     ReleaseDataShareHelper(dataShareHelper);
60     if (resultSet == nullptr) {
61         return E_RDB;
62     }
63     if (resultSet->GoToFirstRow() != E_OK) {
64         return E_RDB;
65     }
66     int32_t columnIndex = 0;
67     if (resultSet->GetColumnIndex(BUNDLE_NAME_VALUE, columnIndex) != E_OK) {
68         return E_RDB;
69     }
70     int64_t status = -1;
71     if (resultSet->GetLong(columnIndex, status) != E_OK) {
72         return E_RDB;
73     }
74     std::string queryKey = std::to_string(userId) + "/" + bundleName;
75     if (status == 1) {
76         cellularNetMap_.EnsureInsert(queryKey, true);
77         LOGI("QueryCellularConnect on");
78     } else {
79         cellularNetMap_.EnsureInsert(queryKey, false);
80         LOGI("QueryCellularConnect off");
81     }
82     return E_OK;
83 }
84 
QueryNetConnect(int32_t userId,const std::string & bundleName)85 int32_t NetworkSetManager::QueryNetConnect(int32_t userId, const std::string &bundleName)
86 {
87     LOGI("QueryNetConnect bundleName: %{public}s", bundleName.c_str());
88     DataShare::DataSharePredicates predicates;
89     predicates.EqualTo("KEYWORD", "photo_network_connection_status");
90     std::vector<std::string> columns = {"VALUE"};
91     std::shared_ptr<DataShare::DataShareResultSet> resultSet;
92     DataShare::CreateOptions options;
93     options.enabled_ = true;
94     auto queryUri = PHOTO_NET_CONT + "&key=photo_network_connection_status";
95     auto dataShareHelper = DataShare::DataShareHelper::Creator(QUERY_URI, options);
96     Uri uri(queryUri);
97     if (dataShareHelper == nullptr) {
98         LOGE("dataShareHelper = nullptr");
99         return E_RDB;
100     }
101     resultSet = dataShareHelper->Query(uri, predicates, columns);
102     ReleaseDataShareHelper(dataShareHelper);
103     if (resultSet == nullptr) {
104         return E_RDB;
105     }
106     if (resultSet->GoToFirstRow() != E_OK) {
107         return E_RDB;
108     }
109     int32_t columnIndex = 0;
110     if (resultSet->GetColumnIndex(BUNDLE_NAME_VALUE, columnIndex) != E_OK) {
111         return E_RDB;
112     }
113     std::string val;
114     if (resultSet->GetString(columnIndex, val) != E_OK) {
115         return E_RDB;
116     }
117     std::string queryKey = std::to_string(userId) + "/" + bundleName;
118     if (val == "on") {
119         netMap_.EnsureInsert(queryKey, true);
120         LOGI("QueryNetConnect on");
121     } else {
122         netMap_.EnsureInsert(queryKey, false);
123         LOGI("QueryNetConnect off");
124     }
125     return E_OK;
126 }
127 
GetCellularConnect(const std::string & bundleName,const int32_t userId)128 void NetworkSetManager::GetCellularConnect(const std::string &bundleName, const int32_t userId)
129 {
130     if (QueryCellularConnect(userId, bundleName) != E_OK) {
131         cellularNetMap_.EnsureInsert(std::to_string(userId) + "/" +
132                                      bundleName, GetConfigParams(bundleName, userId));
133     }
134 }
135 
136 
GetNetConnect(const std::string & bundleName,const int32_t userId)137 void NetworkSetManager::GetNetConnect(const std::string &bundleName, const int32_t userId)
138 {
139     if (QueryNetConnect(userId, bundleName) != E_OK) {
140         netMap_.EnsureInsert(std::to_string(userId) + "/" + bundleName, true);
141     }
142 }
143 
GetConfigParams(const std::string & bundleName,int32_t userId)144 bool NetworkSetManager::GetConfigParams(const std::string &bundleName, int32_t userId)
145 {
146     auto driveKit = CloudFile::CloudFileKit::GetInstance();
147     std::map<std::string, std::string> param;
148     auto err = driveKit->GetAppConfigParams(userId, bundleName, param);
149     if (err != E_OK || param.empty()) {
150         LOGE("GetAppConfigParams failed");
151         return false;
152     }
153     int32_t networkData = std::stoi(param["useMobileNetworkData"]);
154     if (networkData == 0) {
155         return false;
156     }
157     return true;
158 }
159 
UnregisterObserver(const std::string & bundleName,const int32_t userId,const int32_t type)160 void NetworkSetManager::UnregisterObserver(const std::string &bundleName, const int32_t userId, const int32_t type)
161 {
162     LOGI("UnregisterObserver start");
163     options.enabled_ = true;
164     auto dataShareHelper = DataShare::DataShareHelper::Creator(QUERY_URI, options);
165     if (dataShareHelper == nullptr) {
166         LOGE("dataShareHelper = nullptr");
167         return;
168     }
169     sptr<MobileNetworkObserver> dataObserver(new (std::nothrow) MobileNetworkObserver(bundleName,
170     userId, type));
171     if (type == CELLULARCONNECT) {
172         Uri observerUri(queryUri);
173         dataShareHelper->UnregisterObserver(observerUri, dataObserver);
174     } else {
175         Uri observerUri(photoQueryUri);
176         dataShareHelper->UnregisterObserver(observerUri, dataObserver);
177     }
178     ReleaseDataShareHelper(dataShareHelper);
179     LOGI("UnregisterObserver type:%{public}d, finish", type);
180 }
181 
RegisterObserver(const std::string & bundleName,const int32_t userId,const int32_t type)182 void NetworkSetManager::RegisterObserver(const std::string &bundleName, const int32_t userId, const int32_t type)
183 {
184     options.enabled_ = true;
185     auto dataShareHelper = DataShare::DataShareHelper::Creator(QUERY_URI, options);
186     if (dataShareHelper == nullptr) {
187         LOGE("dataShareHelper = nullptr");
188         return;
189     }
190     sptr<MobileNetworkObserver> dataObserver(new (std::nothrow) MobileNetworkObserver(bundleName, userId, type));
191     if (type == CELLULARCONNECT) {
192         Uri observerUri(queryUri);
193         dataShareHelper->RegisterObserver(observerUri, dataObserver);
194     } else {
195         Uri observerUri(photoQueryUri);
196         dataShareHelper->RegisterObserver(observerUri, dataObserver);
197     }
198     ReleaseDataShareHelper(dataShareHelper);
199     LOGI("RegisterObserver type:%{public}d, finish", type);
200 }
201 
ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> & helper)202 void NetworkSetManager::ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> &helper)
203 {
204     if (helper == nullptr) {
205         LOGI("helper is nullptr");
206         return;
207     }
208     if (!helper->Release()) {
209         LOGI("Release data share helper failed");
210         return;
211     }
212     LOGI("Release data share helper finish");
213 }
214 
OnChange()215 void MobileNetworkObserver::OnChange()
216 {
217     LOGI("MobileNetworkObserver OnChange network status");
218     if (observerCallback_ != nullptr) {
219         observerCallback_();
220     }
221     if (type_ == NetworkSetManager::CELLULARCONNECT) {
222         NetworkSetManager::GetCellularConnect(bundleName_, userId_);
223     } else {
224         NetworkSetManager::GetNetConnect(bundleName_, userId_);
225     }
226 }
227 
IsAllowCellularConnect(const std::string & bundleName,const int32_t userId)228 bool NetworkSetManager::IsAllowCellularConnect(const std::string &bundleName, const int32_t userId)
229 {
230     LOGI("IsAllowCellularConnect bundleName: %{public}s", bundleName.c_str());
231     bool checkSwitch = false;
232     bool getCellularConnect = cellularNetMap_.Find(std::to_string(userId) + "/" + bundleName, checkSwitch);
233     if (getCellularConnect == false) {
234         GetCellularConnect(bundleName, userId);
235         return cellularNetMap_.ReadVal(std::to_string(userId) + "/" + bundleName);
236     }
237     return checkSwitch;
238 }
239 
IsAllowNetConnect(const std::string & bundleName,const int32_t userId)240 bool NetworkSetManager::IsAllowNetConnect(const std::string &bundleName, const int32_t userId)
241 {
242     LOGI("IsAllowNetConnect bundleName: %{public}s", bundleName.c_str());
243     bool checkSwitch = true;
244     bool getNetConnect = netMap_.Find(std::to_string(userId) + "/" + bundleName, checkSwitch);
245     if (getNetConnect == false) {
246         GetNetConnect(bundleName, userId);
247         return netMap_.ReadVal(std::to_string(userId) + "/" + bundleName);
248     }
249     return checkSwitch;
250 }
251 
InitNetworkSetManager(const std::string & bundleName,const int32_t userId)252 void NetworkSetManager::InitNetworkSetManager(const std::string &bundleName, const int32_t userId)
253 {
254     LOGI("InitNetworkSetManager bundleName: %{public}s", bundleName.c_str());
255     if (bundleName != PHOTOS_BUNDLE_NAME) {
256         LOGE("InitNetworkSetManager bundleName is illegals");
257         return;
258     }
259     RegisterObserver(bundleName, userId, CELLULARCONNECT);
260     RegisterObserver(bundleName, userId, NETCONNECT);
261     GetCellularConnect(bundleName, userId);
262     GetNetConnect(bundleName, userId);
263 }
264 }