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 "LoadConfigCommonStrategy"
16 #include "load_config_common_strategy.h"
17
18 #include "accesstoken_kit.h"
19 #include "account/account_delegate.h"
20 #include "hap_token_info.h"
21 #include "ipc_skeleton.h"
22 #include "log_print.h"
23 #include "uri_utils.h"
24
25 namespace OHOS::DataShare {
operator ()(std::shared_ptr<Context> context)26 bool LoadConfigCommonStrategy::operator()(std::shared_ptr<Context> context)
27 {
28 if (context->callerTokenId == 0) {
29 context->callerTokenId = IPCSkeleton::GetCallingTokenID();
30 }
31 context->currentUserId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(context->callerTokenId);
32 // sa, userId is in uri, caller token id is from first caller tokenId
33 if (context->currentUserId == 0) {
34 URIUtils::GetInfoFromProxyURI(
35 context->uri, context->currentUserId, context->callerTokenId, context->calledBundleName);
36 FormatUri(context->uri);
37 }
38 if (context->needAutoLoadCallerBundleName && context->callerBundleName.empty()) {
39 Security::AccessToken::HapTokenInfo tokenInfo;
40 auto result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(context->callerTokenId, tokenInfo);
41 if (result != Security::AccessToken::RET_SUCCESS) {
42 ZLOGE("token:0x%{public}x, result:%{public}d", context->callerTokenId, result);
43 return false;
44 }
45 context->callerBundleName = tokenInfo.bundleName;
46 }
47 return true;
48 }
49
FormatUri(std::string & uri)50 void LoadConfigCommonStrategy::FormatUri(std::string &uri)
51 {
52 auto pos = uri.find_last_of('?');
53 if (pos == std::string::npos) {
54 return;
55 }
56
57 uri = uri.substr(0, pos);
58 }
59 } // namespace OHOS::DataShare