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