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 
16 #define LOG_TAG "DataShareSilentConfig"
17 
18 #include "data_share_silent_config.h"
19 
20 #include "accesstoken_kit.h"
21 #include "account/account_delegate.h"
22 #include "data_share_service_impl.h"
23 #include "hap_token_info.h"
24 #include "ipc_skeleton.h"
25 #include "log_print.h"
26 #include "uri_utils.h"
27 #include "utils/anonymous.h"
28 
29 namespace OHOS::DataShare {
30 const std::string ALL_URI = "*";
IsSilentProxyEnable(uint32_t callerTokenId,int32_t currentUserId,const std::string & calledBundleName,const std::string & originUriStr)31 bool DataShareSilentConfig::IsSilentProxyEnable(uint32_t callerTokenId, int32_t currentUserId,
32     const std::string &calledBundleName, const std::string &originUriStr)
33 {
34     std::string uri = originUriStr;
35     URIUtils::FormatUri(uri);
36     bool isEnable = false;
37     if (CheckExistEnableSilentUris(callerTokenId, uri, isEnable) == E_OK) {
38         return isEnable;
39     }
40     std::map<std::string, ProfileInfo> profileInfos;
41     if (!DataShareProfileConfig::GetProfileInfo(calledBundleName, currentUserId, profileInfos)) {
42         return true;
43     }
44     for (const auto &[key, value] : profileInfos) {
45         if (!value.isSilentProxyEnable) {
46             ZLOGI("Is silent proxy enable end, profileInfo file isSilentProxyEnable is false");
47             return false;
48         }
49     }
50     return true;
51 }
52 
EnableSilentProxy(uint32_t callerTokenId,const std::string & originUriStr,bool enable)53 bool DataShareSilentConfig::EnableSilentProxy(uint32_t callerTokenId, const std::string &originUriStr, bool enable)
54 {
55     std::string uri = originUriStr;
56     URIUtils::FormatUri(uri);
57     if (uri.empty()) {
58         enableSilentUris_.Erase(callerTokenId);
59         uri = ALL_URI;
60     }
61     ZLOGI("Enable silent proxy, callerTokenId:%{public}u, enable:%{public}d, uri:%{public}s",
62           callerTokenId, enable, DistributedData::Anonymous::Change(uri).c_str());
63     enableSilentUris_.Compute(callerTokenId, [&enable, &uri](const uint32_t &key,
64         std::map<std::string, bool> &uris) {
65         uris[uri] = enable;
66         return !uris.empty();
67     });
68     return true;
69 }
70 
CheckExistEnableSilentUris(uint32_t callerTokenId,const std::string & uri,bool & isEnable)71 int DataShareSilentConfig::CheckExistEnableSilentUris(uint32_t callerTokenId,
72     const std::string &uri, bool &isEnable)
73 {
74     int status = ERROR;
75     enableSilentUris_.ComputeIfPresent(callerTokenId, [&isEnable, &status, &uri](const uint32_t &key,
76         const std::map<std::string, bool> &uris) {
77         auto iter = uris.find(uri);
78         if (iter != uris.end()) {
79             isEnable = iter->second;
80             status = E_OK;
81             return true;
82         }
83         iter = uris.find(ALL_URI);
84         if (iter != uris.end()) {
85             isEnable = iter->second;
86             status = E_OK;
87             return true;
88         }
89         return !uris.empty();
90     });
91     return status;
92 }
93 } // namespace OHOS::DataShare
94