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 <sys/stat.h>
19 #include <unistd.h>
20 #include "base/utils/utils.h"
21 
22 namespace OHOS::Ace {
23 
GetDataProviderResFromUri(const std::string & uriStr)24 std::unique_ptr<DataProviderRes> DataProviderManager::GetDataProviderResFromUri(const std::string& uriStr)
25 {
26     if (platformImpl_) {
27         return platformImpl_(uriStr);
28     }
29     return nullptr;
30 }
31 
GetDataProviderThumbnailResFromUri(const std::string & uriStr)32 void* DataProviderManagerStandard::GetDataProviderThumbnailResFromUri(const std::string& uriStr)
33 {
34     InitHelper();
35     std::shared_lock lock(helperMutex_);
36     if (!helper_) {
37         return nullptr;
38     }
39     return helper_->QueryThumbnailResFromDataAbility(uriStr);
40 }
41 
GetDataProviderResFromUri(const std::string & uriStr)42 std::unique_ptr<DataProviderRes> DataProviderManagerStandard::GetDataProviderResFromUri(const std::string& uriStr)
43 {
44     InitHelper();
45     std::shared_lock lock(helperMutex_);
46     if (!helper_) {
47         return nullptr;
48     }
49     auto fd = helper_->OpenFile(uriStr, "r");
50     if (fd == -1) {
51         LOGW("open file %{public}s fail.", uriStr.c_str());
52         return nullptr;
53     }
54 
55     // get size of file.
56     struct stat statBuf;
57     auto statRes = fstat(fd, &statBuf);
58     if (statRes != 0) {
59         LOGW("get file %{public}s stat fail.", uriStr.c_str());
60         close(fd);
61         return nullptr;
62     }
63     auto size = statBuf.st_size;
64 
65     // read file content.
66     auto buffer = std::unique_ptr<void, decltype(&std::free)>(std::malloc(size), std::free);
67     auto readRes = read(fd, buffer.get(), size);
68     close(fd);
69     if (readRes == -1) {
70         LOGW("read file %{public}s fail.", uriStr.c_str());
71         return nullptr;
72     }
73 
74     auto result = std::make_unique<DataProviderRes>(std::move(buffer), size);
75     return result;
76 }
77 
InitHelper()78 void DataProviderManagerStandard::InitHelper()
79 {
80     {
81         std::shared_lock lock(helperMutex_);
82         if (helper_) {
83             return;
84         }
85     }
86     // creating helper_, need exclusive access
87     std::unique_lock lock(helperMutex_);
88     if (!helper_ && dataAbilityHelperImpl_) {
89         helper_ = dataAbilityHelperImpl_();
90     }
91 }
92 
GetDataProviderFile(const std::string & uriStr,const std::string & mode)93 int32_t DataProviderManagerStandard::GetDataProviderFile(const std::string& uriStr, const std::string& mode)
94 {
95     InitHelper();
96     std::shared_lock lock(helperMutex_);
97     CHECK_NULL_RETURN(helper_, -1);
98     return helper_->OpenFile(uriStr, mode);
99 }
100 
ReadMovingPhotoVideo(const std::string & uri)101 int32_t DataProviderManagerStandard::ReadMovingPhotoVideo(const std::string &uri)
102 {
103     InitHelper();
104     std::shared_lock lock(helperMutex_);
105     CHECK_NULL_RETURN(helper_, -1);
106     return helper_->ReadMovingPhotoVideo(uri);
107 }
108 
GetMovingPhotoImageUri(const std::string & uri)109 std::string DataProviderManagerStandard::GetMovingPhotoImageUri(const std::string& uri)
110 {
111     InitHelper();
112     std::shared_lock lock(helperMutex_);
113     CHECK_NULL_RETURN(helper_, "");
114     return helper_->GetMovingPhotoImageUri(uri);
115 }
116 
GetMovingPhotoDateModified(const std::string & uri)117 int64_t DataProviderManagerStandard::GetMovingPhotoDateModified(const std::string& uri)
118 {
119     InitHelper();
120     std::shared_lock lock(helperMutex_);
121     CHECK_NULL_RETURN(helper_, -1);
122     return helper_->GetMovingPhotoDateModified(uri);
123 }
124 } // namespace OHOS::Ace
125