1 /*
2 * Copyright (c) 2022 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 "base/resource/data_provider_manager.h"
17
18 #include <shared_mutex>
19 #include <sys/stat.h>
20 #include <unistd.h>
21
22 #include "base/log/log.h"
23 #include "base/utils/utils.h"
24
25 namespace OHOS::Ace {
26
GetDataProviderResFromUri(const std::string & uriStr)27 std::unique_ptr<DataProviderRes> DataProviderManager::GetDataProviderResFromUri(const std::string& uriStr)
28 {
29 LOGD("DataProviderManager::GetDataProviderResFromUri start uri: %{private}s", uriStr.c_str());
30 if (platformImpl_) {
31 return platformImpl_(uriStr);
32 }
33 return nullptr;
34 }
35
GetDataProviderThumbnailResFromUri(const std::string & uriStr)36 void* DataProviderManagerStandard::GetDataProviderThumbnailResFromUri(const std::string& uriStr)
37 {
38 InitHelper();
39 std::shared_lock lock(helperMutex_);
40 if (!helper_) {
41 LOGE("data ability helper is null when try query thumbnail resource, uri: %{private}s", uriStr.c_str());
42 return nullptr;
43 }
44 return helper_->QueryThumbnailResFromDataAbility(uriStr);
45 }
46
GetDataProviderResFromUri(const std::string & uriStr)47 std::unique_ptr<DataProviderRes> DataProviderManagerStandard::GetDataProviderResFromUri(const std::string& uriStr)
48 {
49 LOGD("DataProviderManagerStandard::GetDataProviderResFromUri start uri: %{private}s", uriStr.c_str());
50 InitHelper();
51 std::shared_lock lock(helperMutex_);
52 if (!helper_) {
53 LOGE("data ability helper is null");
54 return nullptr;
55 }
56 auto fd = helper_->OpenFile(uriStr, "r");
57 if (fd == -1) {
58 LOGE("file descriptor is not valid");
59 return nullptr;
60 }
61
62 // get size of file.
63 struct stat statBuf;
64 auto statRes = fstat(fd, &statBuf);
65 if (statRes != 0) {
66 LOGE("get stat fail");
67 close(fd);
68 return nullptr;
69 }
70 auto size = statBuf.st_size;
71
72 // read file content.
73 auto buffer = std::make_unique<uint8_t[]>(size);
74 auto readRes = read(fd, buffer.get(), size);
75 close(fd);
76 if (readRes == -1) {
77 LOGE("read file fail");
78 return nullptr;
79 }
80 auto header = std::unique_ptr<uint8_t[], Deleter>(buffer.release());
81
82 auto result = std::make_unique<DataProviderRes>(std::move(header), size);
83 return result;
84 }
85
InitHelper()86 void DataProviderManagerStandard::InitHelper()
87 {
88 {
89 std::shared_lock lock(helperMutex_);
90 if (helper_) {
91 return;
92 }
93 }
94 // creating helper_, need exclusive access
95 std::unique_lock lock(helperMutex_);
96 if (!helper_ && dataAbilityHelperImpl_) {
97 helper_ = dataAbilityHelperImpl_();
98 }
99 }
100
GetDataProviderFile(const std::string & uriStr,const std::string & mode)101 int32_t DataProviderManagerStandard::GetDataProviderFile(const std::string& uriStr, const std::string& mode)
102 {
103 InitHelper();
104 std::shared_lock lock(helperMutex_);
105 CHECK_NULL_RETURN(helper_, -1);
106 return helper_->OpenFile(uriStr, mode);
107 }
108
109 } // namespace OHOS::Ace
110