• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::vector<uint8_t> buffer(size);
74     auto readRes = read(fd, buffer.data(), size);
75     close(fd);
76     if (readRes == -1) {
77         LOGE("read file fail");
78         return nullptr;
79     }
80 
81     auto result = std::make_unique<DataProviderRes>(std::move(buffer));
82     return result;
83 }
84 
InitHelper()85 void DataProviderManagerStandard::InitHelper()
86 {
87     {
88         std::shared_lock lock(helperMutex_);
89         if (helper_) {
90             return;
91         }
92     }
93     // creating helper_, need exclusive access
94     std::unique_lock lock(helperMutex_);
95     if (!helper_ && dataAbilityHelperImpl_) {
96         helper_ = dataAbilityHelperImpl_();
97     }
98 }
99 
GetDataProviderFile(const std::string & uriStr,const std::string & mode)100 int32_t DataProviderManagerStandard::GetDataProviderFile(const std::string& uriStr, const std::string& mode)
101 {
102     InitHelper();
103     std::shared_lock lock(helperMutex_);
104     CHECK_NULL_RETURN(helper_, -1);
105     return helper_->OpenFile(uriStr, mode);
106 }
107 
108 } // namespace OHOS::Ace
109