• 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     if (platformImpl_) {
30         return platformImpl_(uriStr);
31     }
32     return nullptr;
33 }
34 
GetDataProviderThumbnailResFromUri(const std::string & uriStr)35 void* DataProviderManagerStandard::GetDataProviderThumbnailResFromUri(const std::string& uriStr)
36 {
37     InitHelper();
38     std::shared_lock lock(helperMutex_);
39     if (!helper_) {
40         return nullptr;
41     }
42     return helper_->QueryThumbnailResFromDataAbility(uriStr);
43 }
44 
GetDataProviderResFromUri(const std::string & uriStr)45 std::unique_ptr<DataProviderRes> DataProviderManagerStandard::GetDataProviderResFromUri(const std::string& uriStr)
46 {
47     InitHelper();
48     std::shared_lock lock(helperMutex_);
49     if (!helper_) {
50         return nullptr;
51     }
52     auto fd = helper_->OpenFile(uriStr, "r");
53     if (fd == -1) {
54         return nullptr;
55     }
56 
57     // get size of file.
58     struct stat statBuf;
59     auto statRes = fstat(fd, &statBuf);
60     if (statRes != 0) {
61         LOGW("Get stat fail");
62         close(fd);
63         return nullptr;
64     }
65     auto size = statBuf.st_size;
66 
67     // read file content.
68     auto buffer = std::unique_ptr<void, decltype(&std::free)>(std::malloc(size), std::free);
69     auto readRes = read(fd, buffer.get(), size);
70     close(fd);
71     if (readRes == -1) {
72         LOGW("Read file fail");
73         return nullptr;
74     }
75 
76     auto result = std::make_unique<DataProviderRes>(std::move(buffer), size);
77     return result;
78 }
79 
InitHelper()80 void DataProviderManagerStandard::InitHelper()
81 {
82     {
83         std::shared_lock lock(helperMutex_);
84         if (helper_) {
85             return;
86         }
87     }
88     // creating helper_, need exclusive access
89     std::unique_lock lock(helperMutex_);
90     if (!helper_ && dataAbilityHelperImpl_) {
91         helper_ = dataAbilityHelperImpl_();
92     }
93 }
94 
GetDataProviderFile(const std::string & uriStr,const std::string & mode)95 int32_t DataProviderManagerStandard::GetDataProviderFile(const std::string& uriStr, const std::string& mode)
96 {
97     InitHelper();
98     std::shared_lock lock(helperMutex_);
99     CHECK_NULL_RETURN(helper_, -1);
100     return helper_->OpenFile(uriStr, mode);
101 }
102 
103 } // namespace OHOS::Ace
104