• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "common_func.h"
17 
18 #include <vector>
19 
20 #include "bundle_mgr_proxy.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 
25 #include "log.h"
26 #include "sandbox_helper.h"
27 
28 using namespace std;
29 
30 namespace OHOS {
31 namespace AppFileService {
32 using namespace OHOS::AppExecFwk;
33 namespace {
34     const std::string FILE_SCHEME_PREFIX = "file://";
35     const char BACKFLASH = '/';
36     const std::string FILE_MANAGER_URI_HEAD = "/storage/";
37     const std::string FILE_MANAGER_AUTHORITY = "docs";
38 }
GetBundleMgrProxy()39 static sptr<BundleMgrProxy> GetBundleMgrProxy()
40 {
41     sptr<ISystemAbilityManager> systemAbilityManager =
42         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
43     if (!systemAbilityManager) {
44         LOGE("fail to get system ability mgr.");
45         return nullptr;
46     }
47 
48     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
49     if (!remoteObject) {
50         LOGE("fail to get bundle manager proxy.");
51         return nullptr;
52     }
53 
54     return iface_cast<BundleMgrProxy>(remoteObject);
55 }
56 
GetSelfBundleName()57 string CommonFunc::GetSelfBundleName()
58 {
59     int uid = -1;
60     uid = IPCSkeleton::GetCallingUid();
61 
62     sptr<BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
63     if (!bundleMgrProxy) {
64         LOGE("GetSelfBundleName: bundle mgr proxy is nullptr.");
65         return "";
66     }
67 
68     BundleInfo bundleInfo;
69     auto ret = bundleMgrProxy->GetBundleInfoForSelf(uid, bundleInfo);
70     if (ret != ERR_OK) {
71         LOGE("GetSelfBundleName: bundleName get fail. uid is %{public}d", uid);
72         return "";
73     }
74 
75     return bundleInfo.name;
76 }
77 
NormalizePath(string & path)78 static void NormalizePath(string &path)
79 {
80     if (path.size() == 0) {
81         return;
82     }
83 
84     if (path[0] != BACKFLASH) {
85         path.insert(0, 1, BACKFLASH);
86     }
87 }
88 
GetUriFromPath(const string & path)89 string CommonFunc::GetUriFromPath(const string &path)
90 {
91     string realPath = path;
92     NormalizePath(realPath);
93 
94     string packageName = (path.find(FILE_MANAGER_URI_HEAD) == 0) ? FILE_MANAGER_AUTHORITY : GetSelfBundleName();
95     realPath = FILE_SCHEME_PREFIX + packageName + realPath;
96     return SandboxHelper::Encode(realPath);
97 }
98 } // namespace AppFileService
99 } // namespace OHOS
100 
101