• 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     const std::string MEDIA_FUSE_PATH_HEAD = "/data/storage/el2/media";
39     const std::string MEDIA_AUTHORITY = "file://media";
40 }
GetBundleMgrProxy()41 static sptr<BundleMgrProxy> GetBundleMgrProxy()
42 {
43     sptr<ISystemAbilityManager> systemAbilityManager =
44         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45     if (!systemAbilityManager) {
46         LOGE("fail to get system ability mgr.");
47         return nullptr;
48     }
49 
50     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
51     if (!remoteObject) {
52         LOGE("fail to get bundle manager proxy.");
53         return nullptr;
54     }
55 
56     return iface_cast<BundleMgrProxy>(remoteObject);
57 }
58 
GetSelfBundleName()59 string CommonFunc::GetSelfBundleName()
60 {
61     sptr<BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy();
62     if (!bundleMgrProxy) {
63         LOGE("GetSelfBundleName: bundle mgr proxy is nullptr.");
64         return "";
65     }
66 
67     BundleInfo bundleInfo;
68     auto ret = bundleMgrProxy->GetBundleInfoForSelf(0, bundleInfo);
69     if (ret != ERR_OK) {
70         LOGE("GetSelfBundleName: bundleName get fail.");
71         return "";
72     }
73 
74     return bundleInfo.name;
75 }
76 
NormalizePath(string & path)77 static void NormalizePath(string &path)
78 {
79     if (path.size() == 0) {
80         return;
81     }
82 
83     if (path[0] != BACKFLASH) {
84         path.insert(0, 1, BACKFLASH);
85     }
86 }
87 
GetUriFromPath(const string & path)88 string CommonFunc::GetUriFromPath(const string &path)
89 {
90     string realPath = path;
91     NormalizePath(realPath);
92     if (realPath.find(MEDIA_FUSE_PATH_HEAD) == 0) {
93         return realPath.replace(realPath.find(MEDIA_FUSE_PATH_HEAD), MEDIA_FUSE_PATH_HEAD.length(), MEDIA_AUTHORITY);
94     }
95     string packageName = (realPath.find(FILE_MANAGER_URI_HEAD) == 0) ? FILE_MANAGER_AUTHORITY : GetSelfBundleName();
96     realPath = FILE_SCHEME_PREFIX + packageName + SandboxHelper::Encode(realPath);
97     return realPath;
98 }
99 } // namespace AppFileService
100 } // namespace OHOS
101 
102