1eace7efcSopenharmony_ci
2eace7efcSopenharmony_ci/*
3eace7efcSopenharmony_ci* Copyright (c) 2024 Huawei Device Co., Ltd.
4eace7efcSopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License");
5eace7efcSopenharmony_ci* you may not use this file except in compliance with the License.
6eace7efcSopenharmony_ci* You may obtain a copy of the License at
7eace7efcSopenharmony_ci*
8eace7efcSopenharmony_ci*     http://www.apache.org/licenses/LICENSE-2.0
9eace7efcSopenharmony_ci*
10eace7efcSopenharmony_ci* Unless required by applicable law or agreed to in writing, software
11eace7efcSopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS,
12eace7efcSopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13eace7efcSopenharmony_ci* See the License for the specific language governing permissions and
14eace7efcSopenharmony_ci* limitations under the License.
15eace7efcSopenharmony_ci*/
16eace7efcSopenharmony_ci
17eace7efcSopenharmony_ci#include "media_permission_manager.h"
18eace7efcSopenharmony_ci
19eace7efcSopenharmony_ci#include <sys/types.h>
20eace7efcSopenharmony_ci#include <vector>
21eace7efcSopenharmony_ci
22eace7efcSopenharmony_ci#include "ability_manager_errors.h"
23eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h"
24eace7efcSopenharmony_ci#include "hitrace_meter.h"
25eace7efcSopenharmony_ci#include "in_process_call_wrapper.h"
26eace7efcSopenharmony_ci#include "iservice_registry.h"
27eace7efcSopenharmony_ci#include "system_ability_definition.h"
28eace7efcSopenharmony_ci#include "uri_permission_utils.h"
29eace7efcSopenharmony_ci#include "want.h"
30eace7efcSopenharmony_ci
31eace7efcSopenharmony_cinamespace OHOS {
32eace7efcSopenharmony_cinamespace AAFwk {
33eace7efcSopenharmony_ci
34eace7efcSopenharmony_ciMediaPermissionManager& MediaPermissionManager::GetInstance()
35eace7efcSopenharmony_ci{
36eace7efcSopenharmony_ci    static MediaPermissionManager mediaPermissionManager;
37eace7efcSopenharmony_ci    return mediaPermissionManager;
38eace7efcSopenharmony_ci}
39eace7efcSopenharmony_ci
40eace7efcSopenharmony_ciMediaPermissionManager::MediaPermissionManager()
41eace7efcSopenharmony_ci{
42eace7efcSopenharmony_ci    TAG_LOGI(AAFwkTag::URIPERMMGR, "MediaPermissionManager init.");
43eace7efcSopenharmony_ci    mediaLibraryManager_ = GetMediaLibraryManager();
44eace7efcSopenharmony_ci}
45eace7efcSopenharmony_ci
46eace7efcSopenharmony_ciMedia::MediaLibraryManager *MediaPermissionManager::GetMediaLibraryManager()
47eace7efcSopenharmony_ci{
48eace7efcSopenharmony_ci    TAG_LOGI(AAFwkTag::URIPERMMGR, "GetMediaLibraryManager.");
49eace7efcSopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
50eace7efcSopenharmony_ci    if (mediaLibraryManager_) {
51eace7efcSopenharmony_ci        return mediaLibraryManager_;
52eace7efcSopenharmony_ci    }
53eace7efcSopenharmony_ci    mediaLibraryManager_ = Media::MediaLibraryManager::GetMediaLibraryManager();
54eace7efcSopenharmony_ci    if (mediaLibraryManager_ == nullptr) {
55eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::URIPERMMGR, "GetMediaLibraryManager failed.");
56eace7efcSopenharmony_ci        return mediaLibraryManager_;
57eace7efcSopenharmony_ci    }
58eace7efcSopenharmony_ci    mediaLibraryManager_->InitMediaLibraryManager();
59eace7efcSopenharmony_ci    TAG_LOGI(AAFwkTag::URIPERMMGR, "InitMediaLibraryManager success!");
60eace7efcSopenharmony_ci    return mediaLibraryManager_;
61eace7efcSopenharmony_ci}
62eace7efcSopenharmony_ci
63eace7efcSopenharmony_cistd::vector<bool> MediaPermissionManager::CheckUriPermission(const std::vector<Uri> &uriVec,
64eace7efcSopenharmony_ci    uint32_t callerTokenId, uint32_t flag)
65eace7efcSopenharmony_ci{
66eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
67eace7efcSopenharmony_ci    std::vector<std::string> uriStrVec;
68eace7efcSopenharmony_ci    std::vector<bool> results = std::vector<bool>(uriVec.size(), false);
69eace7efcSopenharmony_ci    for (auto &uri: uriVec) {
70eace7efcSopenharmony_ci        uriStrVec.emplace_back(uri.ToString());
71eace7efcSopenharmony_ci    }
72eace7efcSopenharmony_ci    flag &= (Want::FLAG_AUTH_READ_URI_PERMISSION | Want::FLAG_AUTH_WRITE_URI_PERMISSION);
73eace7efcSopenharmony_ci    std::string bundleName = "";
74eace7efcSopenharmony_ci    if (!UPMSUtils::GetBundleNameByTokenId(callerTokenId, bundleName)) {
75eace7efcSopenharmony_ci        TAG_LOGW(AAFwkTag::URIPERMMGR, "Get bundle name failed.");
76eace7efcSopenharmony_ci        return results;
77eace7efcSopenharmony_ci    }
78eace7efcSopenharmony_ci    std::string appId = "";
79eace7efcSopenharmony_ci    if (UPMSUtils::GetAppIdByBundleName(bundleName, appId) != ERR_OK) {
80eace7efcSopenharmony_ci        TAG_LOGW(AAFwkTag::URIPERMMGR, "Get appId by bundle failed.");
81eace7efcSopenharmony_ci        return results;
82eace7efcSopenharmony_ci    }
83eace7efcSopenharmony_ci    auto mediaLibraryManager = GetMediaLibraryManager();
84eace7efcSopenharmony_ci    if (mediaLibraryManager == nullptr) {
85eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::URIPERMMGR, "GetMediaLibraryManager failed.");
86eace7efcSopenharmony_ci        return results;
87eace7efcSopenharmony_ci    }
88eace7efcSopenharmony_ci    TAG_LOGI(AAFwkTag::URIPERMMGR, "CheckPhotoUriPermission start.");
89eace7efcSopenharmony_ci    auto ret = IN_PROCESS_CALL(mediaLibraryManager->CheckPhotoUriPermission(callerTokenId, appId, uriStrVec,
90eace7efcSopenharmony_ci        results, flag));
91eace7efcSopenharmony_ci    TAG_LOGI(AAFwkTag::URIPERMMGR, "CheckPhotoUriPermission finished.");
92eace7efcSopenharmony_ci    if (ret != ERR_OK) {
93eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::URIPERMMGR, "Check photo uri permission failed, ret is %{pubic}d", ret);
94eace7efcSopenharmony_ci        results = std::vector<bool>(uriStrVec.size(), false);
95eace7efcSopenharmony_ci        return results;
96eace7efcSopenharmony_ci    }
97eace7efcSopenharmony_ci    if (results.size() != uriStrVec.size()) {
98eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::URIPERMMGR, "size of results is unexpected: %{public}zu", results.size());
99eace7efcSopenharmony_ci        results = std::vector<bool>(uriStrVec.size(), false);
100eace7efcSopenharmony_ci        return results;
101eace7efcSopenharmony_ci    }
102eace7efcSopenharmony_ci    return results;
103eace7efcSopenharmony_ci}
104eace7efcSopenharmony_ci
105eace7efcSopenharmony_ci} // OHOS
106eace7efcSopenharmony_ci} // AAFwk