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
16 #include "pasteboard_permission.h"
17
18
19 namespace OHOS {
20 namespace MiscServices {
21 namespace {
22 const std::int32_t USER_ID_CHANGE_VALUE = 1000000;
23 }
24 std::mutex PasteboardPermission::instanceLock_;
25 sptr<PasteboardPermission> PasteboardPermission::instance_;
26 sptr<AppExecFwk::IBundleMgr> PasteboardPermission::bundleMgrProxy_;
27
PasteboardPermission()28 PasteboardPermission::PasteboardPermission()
29 {
30 }
31
~PasteboardPermission()32 PasteboardPermission::~PasteboardPermission()
33 {
34 }
35
GetInstance()36 sptr<PasteboardPermission> PasteboardPermission::GetInstance()
37 {
38 if (instance_ == nullptr) {
39 std::lock_guard<std::mutex> autoLock(instanceLock_);
40 if (instance_ == nullptr) {
41 instance_ = new PasteboardPermission;
42 }
43 }
44 return instance_;
45 }
46
CheckSelfPermission(std::string permName)47 bool PasteboardPermission::CheckSelfPermission(std::string permName)
48 {
49 return true;
50 }
51
CheckCallingPermission(int32_t uid,std::string permName)52 bool PasteboardPermission::CheckCallingPermission(int32_t uid, std::string permName)
53 {
54 if (bundleMgrProxy_ == nullptr) {
55 bundleMgrProxy_ = GetBundleManager();
56 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_COMMON, "get bundle mgr");
57 }
58
59 if (bundleMgrProxy_ == nullptr) {
60 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_COMMON, "redo get bundle mgr failed");
61 return false;
62 }
63 std::string bundleName;
64 auto ret = bundleMgrProxy_->GetBundleNameForUid(uid, bundleName);
65 if (!ret) {
66 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_COMMON, "get bundle name failed");
67 return false;
68 }
69 auto userId = uid / USER_ID_CHANGE_VALUE;
70 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_COMMON, "VerifyPermission bundleName %{public}s, permission %{public}s",
71 bundleName.c_str(), permName.c_str());
72 return MockPermission::VerifyPermission(bundleName, permName, userId);
73 }
74
GetBundleManager()75 sptr<AppExecFwk::IBundleMgr> PasteboardPermission::GetBundleManager()
76 {
77 if (bundleMgrProxy_ == nullptr) {
78 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
79 if (systemManager != nullptr) {
80 bundleMgrProxy_ =
81 iface_cast<AppExecFwk::IBundleMgr>(systemManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID));
82 } else {
83 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_COMMON, "fail to get SAMGR");
84 }
85 }
86 return bundleMgrProxy_;
87 }
88 } // namespace MiscServices
89 } // namespace OHOS
90