image网络下载优化

Signed-off-by: yujiadong <yujiadong3@huawei.com>
Change-Id: Ia4a1178a6376d289d3f8b2f5e01f221858edc810
This commit is contained in:
yujiadong 2024-05-29 01:50:58 +00:00
parent 8d078b2409
commit 588a8d86c1
8 changed files with 46 additions and 3 deletions

View File

@ -114,6 +114,10 @@ public:
{
return false;
}
bool RemoveDownloadTask(const std::string& url) override
{
return false;
}
private:
struct ProxyInfo {

View File

@ -48,6 +48,7 @@ public:
virtual bool Download(const std::string& url, std::vector<uint8_t>& dataOut);
virtual bool DownloadAsync(DownloadCallback&& downloadCallback, const std::string& url, int32_t instanceId);
virtual bool DownloadSync(DownloadCallback&& downloadCallback, const std::string& url, int32_t instanceId);
virtual bool RemoveDownloadTask(const std::string& url);
private:
static std::unique_ptr<DownloadManager> instance_;

View File

@ -59,6 +59,8 @@ constexpr int32_t MAXIMUM_WAITING_PERIOD = 2800;
using NetStackRequest = NetStack::HttpClient::HttpClientRequest;
using NetStackResponse = NetStack::HttpClient::HttpClientResponse;
using NetStackError = NetStack::HttpClient::HttpClientError;
using NetStackTask = NetStack::HttpClient::HttpClientTask;
using NetStackTaskStatus = NetStack::HttpClient::TaskStatus;
class ACE_FORCE_EXPORT DownloadManagerImpl : public DownloadManager {
public:
@ -171,10 +173,10 @@ public:
onProgressCallback(dlTotal, dlNow, true, instanceId);
});
}
httpTaskMap_.emplace(url, task);
auto result = task->Start();
LOGI("Task of netstack with src [%{private}s] [%{public}s]", url.c_str(),
result ? " started on another thread successfully"
: " failed to start on another thread, please check netStack log");
LOGI("download src [%{private}s] [%{public}s]", url.c_str(),
result ? " successfully" : " failed to download, please check netStack log");
return result;
}
@ -233,16 +235,33 @@ public:
onProgressCallback(dlTotal, dlNow, false, instanceId);
});
}
httpTaskMap_.emplace(url, task);
auto result = task->Start();
return HandleDownloadResult(result, std::move(downloadCallback), downloadCondition, instanceId, url);
}
bool RemoveDownloadTask(const std::string& url) override
{
auto iter = httpTaskMap_.find(url);
if (iter != httpTaskMap_.end()) {
auto task = iter->second;
if (task->GetStatus() == NetStackTaskStatus::RUNNING) {
LOGI("AceImage RemoveDownloadTask, url:%{private}s", url.c_str());
task->Cancel();
}
httpTaskMap_.erase(url);
return true;
}
return false;
}
private:
struct ProxyInfo {
std::string host;
int32_t port = 0;
std::string exclusions;
};
std::unordered_map<std::string, std::shared_ptr<NetStackTask>> httpTaskMap_;
bool HandleDownloadResult(bool result, DownloadCallback&& downloadCallback,
const std::shared_ptr<DownloadCondition>& downloadCondition, int32_t instanceId, const std::string& url)

View File

@ -85,6 +85,9 @@ ImageLoadingContext::ImageLoadingContext(const ImageSourceInfo& src, LoadNotifie
ImageLoadingContext::~ImageLoadingContext()
{
// cancel background task
if (Downloadable()) {
RemoveDownloadTask(src_.GetSrc());
}
if (!syncLoad_) {
auto state = stateManager_->GetCurrentState();
if (state == ImageLoadingState::DATA_LOADING) {
@ -269,6 +272,11 @@ void ImageLoadingContext::PerformDownload()
NetworkImageLoader::DownloadImage(std::move(downloadCallback), src_.GetSrc(), syncLoad_);
}
bool ImageLoadingContext::RemoveDownloadTask(const std::string& src)
{
return DownloadManager::GetInstance()->RemoveDownloadTask(src);
}
void ImageLoadingContext::CacheDownloadedImage()
{
CHECK_NULL_VOID(Downloadable());

View File

@ -126,6 +126,7 @@ public:
void OnDataReadyOnCompleteCallBack();
void SetOnProgressCallback(std::function<void(const uint32_t& dlNow, const uint32_t& dlTotal)>&& onProgress);
bool RemoveDownloadTask(const std::string& src);
private:
#define DEFINE_SET_NOTIFY_TASK(loadResult) \

View File

@ -35,6 +35,10 @@ public:
{
return false;
}
bool RemoveDownloadTask(const std::string& /* url */) override
{
return false;
}
};
DownloadManager* DownloadManager::GetInstance()

View File

@ -171,4 +171,9 @@ void ImageLoadingContext::PerformDownload() {}
void ImageLoadingContext::DownloadImageSuccess(const std::string& imageData) {}
void ImageLoadingContext::DownloadImageFailed(const std::string& errorMessage) {}
bool ImageLoadingContext::RemoveDownloadTask(const std::string& src)
{
return false;
}
} // namespace OHOS::Ace::NG

View File

@ -28,6 +28,7 @@ ohos_unittest("image_provider_test_ng") {
"$ace_root/frameworks/core/components_ng/image_provider/svg_image_object.cpp",
"$ace_root/test/mock/base/mock_ace_performance_check.cpp",
"$ace_root/test/mock/base/mock_ace_performance_monitor.cpp",
"$ace_root/test/mock/base/mock_download_manager.cpp",
"$ace_root/test/mock/base/mock_engine_helper.cpp",
"$ace_root/test/mock/base/mock_frame_trace_adapter.cpp",
"$ace_root/test/mock/base/mock_localization.cpp",