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()39static 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()57string CommonFunc::GetSelfBundleName() 58 { 59 sptr<BundleMgrProxy> bundleMgrProxy = GetBundleMgrProxy(); 60 if (!bundleMgrProxy) { 61 LOGE("GetSelfBundleName: bundle mgr proxy is nullptr."); 62 return ""; 63 } 64 65 BundleInfo bundleInfo; 66 auto ret = bundleMgrProxy->GetBundleInfoForSelf(0, bundleInfo); 67 if (ret != ERR_OK) { 68 LOGE("GetSelfBundleName: bundleName get fail."); 69 return ""; 70 } 71 72 return bundleInfo.name; 73 } 74 NormalizePath(string & path)75static void NormalizePath(string &path) 76 { 77 if (path.size() == 0) { 78 return; 79 } 80 81 if (path[0] != BACKFLASH) { 82 path.insert(0, 1, BACKFLASH); 83 } 84 } 85 GetUriFromPath(const string & path)86string CommonFunc::GetUriFromPath(const string &path) 87 { 88 string realPath = path; 89 NormalizePath(realPath); 90 91 string packageName = (path.find(FILE_MANAGER_URI_HEAD) == 0) ? FILE_MANAGER_AUTHORITY : GetSelfBundleName(); 92 realPath = FILE_SCHEME_PREFIX + packageName + SandboxHelper::Encode(realPath); 93 return realPath; 94 } 95 } // namespace AppFileService 96 } // namespace OHOS 97 98