mirror of
https://gitee.com/openharmony/request_request
synced 2024-11-23 06:49:58 +00:00
预下载功能接口更改,适配JS接口
Signed-off-by: fqwert <yanglv2@huawei.com> Change-Id: I3178657a3eb39f3cc3ad9c982589e5f863bcbaf1
This commit is contained in:
parent
ab0f7a99f8
commit
054a2f00be
@ -30,7 +30,7 @@ struct TaskHandle;
|
||||
struct DownloadAgent;
|
||||
struct DownloadError;
|
||||
|
||||
enum class PreDownloadState {
|
||||
enum class PreloadState {
|
||||
INIT,
|
||||
RUNNING,
|
||||
SUCCESS,
|
||||
@ -56,11 +56,11 @@ enum ErrorKind {
|
||||
CACHE,
|
||||
};
|
||||
|
||||
class PreDownloadError {
|
||||
class PreloadError {
|
||||
public:
|
||||
PreDownloadError(rust::Box<DownloadError> error);
|
||||
PreDownloadError &operator=(const PreDownloadError &) = delete;
|
||||
~PreDownloadError();
|
||||
PreloadError(rust::Box<DownloadError> error);
|
||||
PreloadError &operator=(const PreloadError &) = delete;
|
||||
~PreloadError();
|
||||
|
||||
int32_t GetCode() const;
|
||||
std::string GetMessage() const;
|
||||
@ -70,45 +70,45 @@ private:
|
||||
DownloadError *_error;
|
||||
};
|
||||
|
||||
struct DownloadCallback {
|
||||
std::function<void(const std::shared_ptr<Data> &&)> OnSuccess;
|
||||
std::function<void(const PreDownloadError &)> OnFail;
|
||||
struct PreloadCallback {
|
||||
std::function<void(const std::shared_ptr<Data> &&, const std::string &TaskId)> OnSuccess;
|
||||
std::function<void(const PreloadError &, const std::string &TaskId)> OnFail;
|
||||
std::function<void()> OnCancel;
|
||||
std::function<void(uint64_t current, uint64_t total)> OnProgress;
|
||||
};
|
||||
|
||||
class PreDownloadHandle {
|
||||
class PreloadHandle {
|
||||
public:
|
||||
PreDownloadHandle(rust::Box<TaskHandle>);
|
||||
PreDownloadError &operator=(const PreDownloadError &) = delete;
|
||||
PreloadHandle(rust::Box<TaskHandle>);
|
||||
PreloadError &operator=(const PreloadError &) = delete;
|
||||
|
||||
~PreDownloadHandle();
|
||||
~PreloadHandle();
|
||||
void Cancel();
|
||||
std::string GetTaskId();
|
||||
bool IsFinish();
|
||||
PreDownloadState GetState();
|
||||
PreloadState GetState();
|
||||
|
||||
private:
|
||||
TaskHandle *_handle;
|
||||
};
|
||||
|
||||
struct PreDownloadOptions {
|
||||
struct PreloadOptions {
|
||||
std::vector<std::tuple<std::string, std::string>> headers;
|
||||
};
|
||||
|
||||
class PreDownloadAgent {
|
||||
class Preload {
|
||||
public:
|
||||
PreDownloadAgent();
|
||||
static PreDownloadAgent *GetInstance();
|
||||
virtual ~PreDownloadAgent() = default;
|
||||
Preload();
|
||||
static Preload *GetInstance();
|
||||
virtual ~Preload() = default;
|
||||
void Cancel(std::string const &url);
|
||||
void Remove(std::string const &url);
|
||||
|
||||
void SetRamCacheSize(uint64_t size);
|
||||
void SetFileCacheSize(uint64_t size);
|
||||
|
||||
std::shared_ptr<PreDownloadHandle> Download(std::string const &url, std::unique_ptr<DownloadCallback>,
|
||||
std::unique_ptr<PreDownloadOptions> options = nullptr);
|
||||
std::shared_ptr<PreloadHandle> load(
|
||||
std::string const &url, std::unique_ptr<PreloadCallback>, std::unique_ptr<PreloadOptions> options = nullptr);
|
||||
|
||||
private:
|
||||
const DownloadAgent *_agent;
|
||||
|
@ -48,9 +48,9 @@ napi_value preload(napi_env env, napi_callback_info info)
|
||||
|
||||
std::string url = GetValueString(env, args[0]);
|
||||
|
||||
std::unique_ptr<PreDownloadOptions> options = nullptr;
|
||||
std::unique_ptr<PreloadOptions> options = nullptr;
|
||||
if (valuetype1 == napi_object) {
|
||||
options = std::make_unique<PreDownloadOptions>();
|
||||
options = std::make_unique<PreloadOptions>();
|
||||
napi_value headers = nullptr;
|
||||
NAPI_CALL(env, napi_get_named_property(env, args[1], "headers", &headers));
|
||||
if (headers != nullptr) {
|
||||
@ -61,7 +61,7 @@ napi_value preload(napi_env env, napi_callback_info info)
|
||||
}
|
||||
}
|
||||
}
|
||||
PreDownloadAgent::GetInstance()->Download(std::string(url), nullptr, std::move(options));
|
||||
Preload::GetInstance()->load(std::string(url), nullptr, std::move(options));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -80,7 +80,7 @@ napi_value cancel(napi_env env, napi_callback_info info)
|
||||
}
|
||||
|
||||
std::string url = GetValueString(env, args[0]);
|
||||
PreDownloadAgent::GetInstance()->Cancel(std::string(url));
|
||||
Preload::GetInstance()->Cancel(std::string(url));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -99,7 +99,7 @@ napi_value setMemoryCacheSize(napi_env env, napi_callback_info info)
|
||||
}
|
||||
|
||||
uint32_t size = GetValueNum(env, args[0]);
|
||||
PreDownloadAgent::GetInstance()->SetRamCacheSize(size);
|
||||
Preload::GetInstance()->SetRamCacheSize(size);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -118,7 +118,7 @@ napi_value setFileCacheSize(napi_env env, napi_callback_info info)
|
||||
}
|
||||
|
||||
uint32_t size = GetValueNum(env, args[0]);
|
||||
PreDownloadAgent::GetInstance()->SetFileCacheSize(size);
|
||||
Preload::GetInstance()->SetFileCacheSize(size);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,8 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
default = [
|
||||
]
|
||||
|
||||
ohos = [
|
||||
"netstack_rs",
|
||||
|
@ -22,18 +22,18 @@
|
||||
|
||||
namespace OHOS::Request {
|
||||
|
||||
class DownloadCallbackWrapper {
|
||||
class PreloadCallbackWrapper {
|
||||
public:
|
||||
DownloadCallbackWrapper(std::unique_ptr<DownloadCallback> callback);
|
||||
~DownloadCallbackWrapper() = default;
|
||||
PreloadCallbackWrapper(std::unique_ptr<PreloadCallback> callback);
|
||||
~PreloadCallbackWrapper() = default;
|
||||
|
||||
void OnSuccess(const std::shared_ptr<Data> data) const;
|
||||
void OnFail(rust::Box<DownloadError> error) const;
|
||||
void OnSuccess(const std::shared_ptr<Data> data, rust::str TaskId) const;
|
||||
void OnFail(rust::Box<DownloadError> error, rust::str TaskId) const;
|
||||
void OnCancel() const;
|
||||
void OnProgress(uint64_t current, uint64_t total) const;
|
||||
|
||||
private:
|
||||
std::unique_ptr<DownloadCallback> _callback;
|
||||
std::unique_ptr<PreloadCallback> _callback;
|
||||
};
|
||||
|
||||
std::shared_ptr<Data> BuildSharedData(rust::Box<RustData> data);
|
||||
|
@ -23,14 +23,14 @@ use crate::utils::url_hash;
|
||||
use crate::DownloadError;
|
||||
|
||||
cfg_ohos! {
|
||||
use crate::wrapper::ffi::{FfiPredownloadOptions,DownloadCallbackWrapper};
|
||||
use crate::wrapper::ffi::{FfiPredownloadOptions,PreloadCallbackWrapper};
|
||||
use crate::wrapper::FfiCallback;
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub trait CustomCallback: Send {
|
||||
fn on_success(&mut self, data: Arc<RamCache>) {}
|
||||
fn on_fail(&mut self, error: DownloadError) {}
|
||||
fn on_success(&mut self, data: Arc<RamCache>, task_id: &str) {}
|
||||
fn on_fail(&mut self, error: DownloadError, task_id: &str) {}
|
||||
fn on_cancel(&mut self) {}
|
||||
fn on_progress(&mut self, progress: u64, total: u64) {}
|
||||
}
|
||||
@ -100,7 +100,8 @@ impl DownloadAgent {
|
||||
|
||||
pub fn get_instance() -> &'static Self {
|
||||
static DOWNLOAD_AGENT: LazyLock<DownloadAgent> = LazyLock::new(|| {
|
||||
crate::spawn(|| CacheManager::get_instance().init());
|
||||
#[cfg(not(test))]
|
||||
CacheManager::get_instance().init();
|
||||
DownloadAgent::new()
|
||||
});
|
||||
|
||||
@ -198,7 +199,7 @@ impl DownloadAgent {
|
||||
pub(crate) fn ffi_pre_download(
|
||||
&self,
|
||||
url: &str,
|
||||
callback: cxx::UniquePtr<DownloadCallbackWrapper>,
|
||||
callback: cxx::UniquePtr<PreloadCallbackWrapper>,
|
||||
update: bool,
|
||||
options: &FfiPredownloadOptions,
|
||||
) -> Box<TaskHandle> {
|
||||
@ -263,7 +264,7 @@ mod test {
|
||||
}
|
||||
|
||||
impl CustomCallback for TestCallbackS {
|
||||
fn on_success(&mut self, data: Arc<RamCache>) {
|
||||
fn on_success(&mut self, data: Arc<RamCache>, _task_id: &str) {
|
||||
if data.size() != 0 {
|
||||
self.flag.fetch_add(1, Ordering::SeqCst);
|
||||
} else {
|
||||
@ -277,7 +278,7 @@ mod test {
|
||||
}
|
||||
|
||||
impl CustomCallback for TestCallbackF {
|
||||
fn on_fail(&mut self, error: DownloadError) {
|
||||
fn on_fail(&mut self, error: DownloadError, _task_id: &str) {
|
||||
*self.flag.lock().unwrap() = error.message().to_string();
|
||||
}
|
||||
}
|
||||
|
5
pre_download/native/src/cache/data/file.rs
vendored
5
pre_download/native/src/cache/data/file.rs
vendored
@ -76,6 +76,7 @@ impl Drop for FileCache {
|
||||
}
|
||||
|
||||
impl FileCache {
|
||||
#[cfg(not(test))]
|
||||
pub(crate) fn try_restore(task_id: TaskId, handle: &'static CacheManager) -> Option<Self> {
|
||||
let metadata = fs::metadata(Self::path(&task_id)).ok()?;
|
||||
if !CacheManager::apply_cache(
|
||||
@ -151,6 +152,7 @@ impl FileCache {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub(crate) fn restore_files() -> impl Iterator<Item = TaskId> {
|
||||
restore_files_inner(CACHE_DIR_PATH.as_path())
|
||||
}
|
||||
@ -196,9 +198,6 @@ fn filter_map_entry(
|
||||
impl CacheManager {
|
||||
pub(super) fn update_file_cache(&'static self, task_id: TaskId, cache: Arc<RamCache>) {
|
||||
self.update_from_file_once.lock().unwrap().remove(&task_id);
|
||||
if self.files.lock().unwrap().contains_key(&task_id) {
|
||||
return;
|
||||
}
|
||||
spawn(move || {
|
||||
self.backup_rams
|
||||
.lock()
|
||||
|
5
pre_download/native/src/cache/data/mod.rs
vendored
5
pre_download/native/src/cache/data/mod.rs
vendored
@ -15,6 +15,9 @@ mod file;
|
||||
mod ram;
|
||||
mod space;
|
||||
|
||||
pub(crate) use file::{restore_files, FileCache};
|
||||
cfg_not_test! {
|
||||
pub(crate) use file::restore_files;
|
||||
}
|
||||
pub(crate) use file::FileCache;
|
||||
pub(crate) use ram::RamCache;
|
||||
pub(crate) use space::Handle;
|
||||
|
3
pre_download/native/src/cache/fetch.rs
vendored
3
pre_download/native/src/cache/fetch.rs
vendored
@ -27,8 +27,9 @@ impl<'a> Fetcher<'a> {
|
||||
mut callback: Box<dyn CustomCallback>,
|
||||
) -> Result<(), Box<dyn CustomCallback>> {
|
||||
if let Some(cache) = CacheManager::get_instance().get_cache(self.task_id) {
|
||||
let task_id = self.task_id.clone();
|
||||
crate::spawn(move || {
|
||||
callback.on_success(cache);
|
||||
callback.on_success(cache, task_id.brief());
|
||||
});
|
||||
Ok(())
|
||||
} else {
|
||||
|
4
pre_download/native/src/cache/manage.rs
vendored
4
pre_download/native/src/cache/manage.rs
vendored
@ -17,12 +17,13 @@ use std::sync::{Arc, LazyLock, Mutex, OnceLock, Weak};
|
||||
|
||||
use request_utils::queue_map::QueueMap;
|
||||
|
||||
use super::data::{self, restore_files, FileCache, RamCache};
|
||||
use super::data::{self, FileCache, RamCache};
|
||||
use crate::agent::TaskId;
|
||||
|
||||
cfg_not_test! {
|
||||
const DEFAULT_RAM_CACHE_SIZE: u64 = 1024 * 1024 * 20;
|
||||
const DEFAULT_FILE_CACHE_SIZE: u64 = 1024 * 1024 * 100;
|
||||
use super::data::restore_files;
|
||||
}
|
||||
|
||||
cfg_test! {
|
||||
@ -59,6 +60,7 @@ impl CacheManager {
|
||||
&CACHE_MANAGER
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub(crate) fn init(&'static self) {
|
||||
for task_id in restore_files() {
|
||||
let Some(file_cache) = FileCache::try_restore(task_id.clone(), self) else {
|
||||
|
@ -21,36 +21,36 @@
|
||||
#include "request_pre_download.h"
|
||||
namespace OHOS::Request {
|
||||
|
||||
DownloadCallbackWrapper::DownloadCallbackWrapper(std::unique_ptr<DownloadCallback> callback)
|
||||
PreloadCallbackWrapper::PreloadCallbackWrapper(std::unique_ptr<PreloadCallback> callback)
|
||||
{
|
||||
if (callback != nullptr) {
|
||||
this->_callback = std::move(callback);
|
||||
}
|
||||
}
|
||||
|
||||
void DownloadCallbackWrapper::OnSuccess(const std::shared_ptr<Data> data) const
|
||||
void PreloadCallbackWrapper::OnSuccess(const std::shared_ptr<Data> data, rust::str taskId) const
|
||||
{
|
||||
if (this->_callback != nullptr && this->_callback->OnSuccess != nullptr) {
|
||||
this->_callback->OnSuccess(std::move(data));
|
||||
this->_callback->OnSuccess(std::move(data), std::string(taskId));
|
||||
}
|
||||
}
|
||||
|
||||
void DownloadCallbackWrapper::OnFail(rust::Box<DownloadError> error) const
|
||||
void PreloadCallbackWrapper::OnFail(rust::Box<DownloadError> error, rust::str taskId) const
|
||||
{
|
||||
if (this->_callback != nullptr && this->_callback->OnFail != nullptr) {
|
||||
PreDownloadError preDownloadError(std::move(error));
|
||||
this->_callback->OnFail(preDownloadError);
|
||||
PreloadError preloadError(std::move(error));
|
||||
this->_callback->OnFail(preloadError, std::string(taskId));
|
||||
}
|
||||
}
|
||||
|
||||
void DownloadCallbackWrapper::OnCancel() const
|
||||
void PreloadCallbackWrapper::OnCancel() const
|
||||
{
|
||||
if (this->_callback != nullptr && this->_callback->OnCancel != nullptr) {
|
||||
this->_callback->OnCancel();
|
||||
}
|
||||
}
|
||||
|
||||
void DownloadCallbackWrapper::OnProgress(uint64_t current, uint64_t total) const
|
||||
void PreloadCallbackWrapper::OnProgress(uint64_t current, uint64_t total) const
|
||||
{
|
||||
if (this->_callback != nullptr && this->_callback->OnProgress != nullptr) {
|
||||
this->_callback->OnProgress(current, total);
|
||||
|
@ -36,40 +36,40 @@ rust::Slice<const uint8_t> Data::bytes()
|
||||
return this->_data->bytes();
|
||||
}
|
||||
|
||||
PreDownloadError::PreDownloadError(rust::Box<DownloadError> error)
|
||||
PreloadError::PreloadError(rust::Box<DownloadError> error)
|
||||
{
|
||||
this->_error = error.into_raw();
|
||||
}
|
||||
|
||||
PreDownloadError::~PreDownloadError()
|
||||
PreloadError::~PreloadError()
|
||||
{
|
||||
rust::Box<DownloadError>::from_raw(this->_error);
|
||||
}
|
||||
|
||||
int32_t PreDownloadError::GetCode() const
|
||||
int32_t PreloadError::GetCode() const
|
||||
{
|
||||
return this->_error->code();
|
||||
}
|
||||
|
||||
std::string PreDownloadError::GetMessage() const
|
||||
std::string PreloadError::GetMessage() const
|
||||
{
|
||||
return std::string(this->_error->message());
|
||||
}
|
||||
|
||||
ErrorKind PreDownloadError::GetErrorKind() const
|
||||
ErrorKind PreloadError::GetErrorKind() const
|
||||
{
|
||||
return static_cast<ErrorKind>(this->_error->ffi_kind());
|
||||
}
|
||||
|
||||
PreDownloadAgent::PreDownloadAgent()
|
||||
Preload::Preload()
|
||||
{
|
||||
this->_agent = download_agent();
|
||||
}
|
||||
|
||||
std::shared_ptr<PreDownloadHandle> PreDownloadAgent::Download(
|
||||
std::string const &url, std::unique_ptr<DownloadCallback> callback, std::unique_ptr<PreDownloadOptions> options)
|
||||
std::shared_ptr<PreloadHandle> Preload::load(
|
||||
std::string const &url, std::unique_ptr<PreloadCallback> callback, std::unique_ptr<PreloadOptions> options)
|
||||
{
|
||||
auto callback_wrapper = std::make_unique<DownloadCallbackWrapper>(std::move(callback));
|
||||
auto callback_wrapper = std::make_unique<PreloadCallbackWrapper>(std::move(callback));
|
||||
FfiPredownloadOptions ffiOptions = { .headers = rust::Vec<rust::str>() };
|
||||
if (options != nullptr) {
|
||||
for (auto header : options->headers) {
|
||||
@ -78,62 +78,62 @@ std::shared_ptr<PreDownloadHandle> PreDownloadAgent::Download(
|
||||
}
|
||||
}
|
||||
auto taskHandle = this->_agent->ffi_pre_download(rust::str(url), std::move(callback_wrapper), false, ffiOptions);
|
||||
return std::make_shared<PreDownloadHandle>(std::move(taskHandle));
|
||||
return std::make_shared<PreloadHandle>(std::move(taskHandle));
|
||||
}
|
||||
|
||||
void PreDownloadAgent::SetRamCacheSize(uint64_t size)
|
||||
void Preload::SetRamCacheSize(uint64_t size)
|
||||
{
|
||||
this->_agent->set_ram_cache_size(size);
|
||||
}
|
||||
void PreDownloadAgent::SetFileCacheSize(uint64_t size)
|
||||
void Preload::SetFileCacheSize(uint64_t size)
|
||||
{
|
||||
this->_agent->set_file_cache_size(size);
|
||||
}
|
||||
|
||||
void PreDownloadAgent::Cancel(std::string const &url)
|
||||
void Preload::Cancel(std::string const &url)
|
||||
{
|
||||
this->_agent->cancel(rust::str(url));
|
||||
}
|
||||
|
||||
void PreDownloadAgent::Remove(std::string const &url)
|
||||
void Preload::Remove(std::string const &url)
|
||||
{
|
||||
this->_agent->remove(rust::str(url));
|
||||
}
|
||||
|
||||
PreDownloadAgent *PreDownloadAgent::GetInstance()
|
||||
Preload *Preload::GetInstance()
|
||||
{
|
||||
static PreDownloadAgent agent;
|
||||
static Preload agent;
|
||||
return &agent;
|
||||
}
|
||||
|
||||
PreDownloadHandle::PreDownloadHandle(rust::Box<TaskHandle> handle)
|
||||
PreloadHandle::PreloadHandle(rust::Box<TaskHandle> handle)
|
||||
{
|
||||
this->_handle = handle.into_raw();
|
||||
}
|
||||
|
||||
PreDownloadHandle::~PreDownloadHandle()
|
||||
PreloadHandle::~PreloadHandle()
|
||||
{
|
||||
rust::Box<TaskHandle>::from_raw(this->_handle);
|
||||
}
|
||||
|
||||
void PreDownloadHandle::Cancel()
|
||||
void PreloadHandle::Cancel()
|
||||
{
|
||||
this->_handle->cancel();
|
||||
}
|
||||
|
||||
std::string PreDownloadHandle::GetTaskId()
|
||||
std::string PreloadHandle::GetTaskId()
|
||||
{
|
||||
return std::string(this->_handle->task_id());
|
||||
}
|
||||
|
||||
bool PreDownloadHandle::IsFinish()
|
||||
bool PreloadHandle::IsFinish()
|
||||
{
|
||||
return this->_handle->is_finish();
|
||||
}
|
||||
|
||||
PreDownloadState PreDownloadHandle::GetState()
|
||||
PreloadState PreloadHandle::GetState()
|
||||
{
|
||||
return static_cast<PreDownloadState>(this->_handle->state());
|
||||
return static_cast<PreloadState>(this->_handle->state());
|
||||
}
|
||||
|
||||
} // namespace OHOS::Request
|
@ -81,7 +81,7 @@ impl DownloadCallback {
|
||||
self.finish.store(true, Ordering::Release);
|
||||
let mut callbacks = self.callbacks.lock().unwrap();
|
||||
while let Some(mut callback) = callbacks.pop() {
|
||||
callback.on_success(cache.clone());
|
||||
callback.on_success(cache.clone(), self.task_id.brief());
|
||||
}
|
||||
drop(callbacks);
|
||||
self.notify_agent_finish();
|
||||
@ -93,7 +93,7 @@ impl DownloadCallback {
|
||||
self.finish.store(true, Ordering::Release);
|
||||
let mut callbacks = self.callbacks.lock().unwrap();
|
||||
while let Some(mut callback) = callbacks.pop() {
|
||||
callback.on_fail(DownloadError::from(&error));
|
||||
callback.on_fail(DownloadError::from(&error), self.task_id.brief());
|
||||
}
|
||||
drop(callbacks);
|
||||
self.notify_agent_finish();
|
||||
@ -301,7 +301,7 @@ mod test {
|
||||
}
|
||||
|
||||
impl CustomCallback for TestCallback {
|
||||
fn on_success(&mut self, data: Arc<RamCache>) {
|
||||
fn on_success(&mut self, data: Arc<RamCache>, _task_id: &str) {
|
||||
if data.size() != 0 {
|
||||
self.flag.store(true, Ordering::Release);
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use cxx::UniquePtr;
|
||||
use ffi::DownloadCallbackWrapper;
|
||||
use ffi::PreloadCallbackWrapper;
|
||||
|
||||
use crate::agent::DownloadAgent;
|
||||
use crate::cache::RamCache;
|
||||
@ -22,13 +22,13 @@ use crate::download::TaskHandle;
|
||||
use crate::{CustomCallback, DownloadError};
|
||||
|
||||
pub(super) struct FfiCallback {
|
||||
inner: UniquePtr<DownloadCallbackWrapper>,
|
||||
inner: UniquePtr<PreloadCallbackWrapper>,
|
||||
}
|
||||
|
||||
unsafe impl Send for FfiCallback {}
|
||||
|
||||
impl FfiCallback {
|
||||
pub(crate) fn from_ffi(ffi: UniquePtr<DownloadCallbackWrapper>) -> Option<Self> {
|
||||
pub(crate) fn from_ffi(ffi: UniquePtr<PreloadCallbackWrapper>) -> Option<Self> {
|
||||
if ffi.is_null() {
|
||||
None
|
||||
} else {
|
||||
@ -52,14 +52,14 @@ impl RustData {
|
||||
}
|
||||
|
||||
impl CustomCallback for FfiCallback {
|
||||
fn on_success(&mut self, data: Arc<RamCache>) {
|
||||
fn on_success(&mut self, data: Arc<RamCache>, task_id: &str) {
|
||||
let rust_data = RustData::new(data);
|
||||
let shared_data = ffi::BuildSharedData(Box::new(rust_data));
|
||||
self.inner.OnSuccess(shared_data);
|
||||
self.inner.OnSuccess(shared_data, task_id);
|
||||
}
|
||||
|
||||
fn on_fail(&mut self, error: DownloadError) {
|
||||
self.inner.OnFail(Box::new(error));
|
||||
fn on_fail(&mut self, error: DownloadError, task_id: &str) {
|
||||
self.inner.OnFail(Box::new(error), task_id);
|
||||
}
|
||||
|
||||
fn on_cancel(&mut self) {
|
||||
@ -77,7 +77,6 @@ fn download_agent() -> *const DownloadAgent {
|
||||
|
||||
#[cxx::bridge(namespace = "OHOS::Request")]
|
||||
pub(crate) mod ffi {
|
||||
|
||||
struct FfiPredownloadOptions<'a> {
|
||||
headers: Vec<&'a str>,
|
||||
}
|
||||
@ -92,7 +91,7 @@ pub(crate) mod ffi {
|
||||
fn ffi_pre_download(
|
||||
self: &DownloadAgent,
|
||||
url: &str,
|
||||
mut callback: UniquePtr<DownloadCallbackWrapper>,
|
||||
mut callback: UniquePtr<PreloadCallbackWrapper>,
|
||||
update: bool,
|
||||
options: &FfiPredownloadOptions,
|
||||
) -> Box<TaskHandle>;
|
||||
@ -118,13 +117,13 @@ pub(crate) mod ffi {
|
||||
include!("request_pre_download.h");
|
||||
include!("context.h");
|
||||
|
||||
type DownloadCallbackWrapper;
|
||||
type PreloadCallbackWrapper;
|
||||
type Data;
|
||||
|
||||
fn BuildSharedData(data: Box<RustData>) -> SharedPtr<Data>;
|
||||
fn OnSuccess(self: &DownloadCallbackWrapper, data: SharedPtr<Data>);
|
||||
fn OnFail(self: &DownloadCallbackWrapper, error: Box<DownloadError>);
|
||||
fn OnCancel(self: &DownloadCallbackWrapper);
|
||||
fn OnProgress(self: &DownloadCallbackWrapper, progress: u64, total: u64);
|
||||
fn OnSuccess(self: &PreloadCallbackWrapper, data: SharedPtr<Data>, task_id: &str);
|
||||
fn OnFail(self: &PreloadCallbackWrapper, error: Box<DownloadError>, task_id: &str);
|
||||
fn OnCancel(self: &PreloadCallbackWrapper);
|
||||
fn OnProgress(self: &PreloadCallbackWrapper, progress: u64, total: u64);
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,7 @@
|
||||
using namespace testing::ext;
|
||||
using namespace OHOS::Request;
|
||||
|
||||
class PreDownloadTest : public testing::Test {
|
||||
class PreloadTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase(void);
|
||||
static void TearDownTestCase(void);
|
||||
@ -53,22 +53,22 @@ constexpr size_t INTERVAL = 500;
|
||||
constexpr uint64_t TEST_SIZE = 1042003;
|
||||
constexpr uint64_t TEST_SIZE_4 = 318465;
|
||||
|
||||
void PreDownloadTest::SetUpTestCase(void)
|
||||
void PreloadTest::SetUpTestCase(void)
|
||||
{
|
||||
// input testsuit setup step,setup invoked before all testcases
|
||||
}
|
||||
|
||||
void PreDownloadTest::TearDownTestCase(void)
|
||||
void PreloadTest::TearDownTestCase(void)
|
||||
{
|
||||
// input testsuit teardown step,teardown invoked after all testcases
|
||||
}
|
||||
|
||||
void PreDownloadTest::SetUp(void)
|
||||
void PreloadTest::SetUp(void)
|
||||
{
|
||||
// input testcase setup step,setup invoked before each testcases
|
||||
}
|
||||
|
||||
void PreDownloadTest::TearDown(void)
|
||||
void PreloadTest::TearDown(void)
|
||||
{
|
||||
// input testcase teardown step,teardown invoked after each testcases
|
||||
}
|
||||
@ -76,83 +76,84 @@ void PreDownloadTest::TearDown(void)
|
||||
void DownloadSuccessTest(std::string url, uint64_t size)
|
||||
{
|
||||
auto flagS = std::make_shared<std::atomic_uint64_t>(0);
|
||||
PreDownloadOptions options = { .headers = std::vector<std::tuple<std::string, std::string>>() };
|
||||
PreloadOptions options = { .headers = std::vector<std::tuple<std::string, std::string>>() };
|
||||
options.headers.push_back(std::tuple<std::string, std::string>("Accept", "text/html"));
|
||||
|
||||
auto flagP = std::make_shared<std::atomic_int64_t>(0);
|
||||
auto callback = DownloadCallback{
|
||||
.OnSuccess = [flagS](const std::shared_ptr<Data> &&data) { flagS->store(data->bytes().size()); },
|
||||
auto callback = PreloadCallback{
|
||||
.OnSuccess = [flagS](const std::shared_ptr<Data> &&data,
|
||||
const std::string &taskId) { flagS->store(data->bytes().size()); },
|
||||
.OnCancel = []() {},
|
||||
.OnFail = [](const PreDownloadError &error) {},
|
||||
.OnFail = [](const PreloadError &error, const std::string &taskId) {},
|
||||
.OnProgress = [flagP](uint64_t current, uint64_t total) { flagP->fetch_add(1); },
|
||||
};
|
||||
auto handle = PreDownloadAgent::GetInstance()->Download(TEST_URL_0, std::make_unique<DownloadCallback>(callback));
|
||||
auto handle = Preload::GetInstance()->load(TEST_URL_0, std::make_unique<PreloadCallback>(callback));
|
||||
EXPECT_FALSE(handle->IsFinish());
|
||||
EXPECT_EQ(handle->GetState(), PreDownloadState::RUNNING);
|
||||
EXPECT_EQ(handle->GetState(), PreloadState::RUNNING);
|
||||
|
||||
while (!handle->IsFinish()) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(INTERVAL));
|
||||
}
|
||||
EXPECT_TRUE(flagP->load() > 0);
|
||||
EXPECT_EQ(flagS->load(), TEST_SIZE);
|
||||
EXPECT_EQ(handle->GetState(), PreDownloadState::SUCCESS);
|
||||
PreDownloadAgent::GetInstance()->Remove(TEST_URL_0);
|
||||
EXPECT_EQ(handle->GetState(), PreloadState::SUCCESS);
|
||||
Preload::GetInstance()->Remove(TEST_URL_0);
|
||||
}
|
||||
|
||||
// test success and progress callback
|
||||
HWTEST_F(PreDownloadTest, PreDownloadTest_001, TestSize.Level1)
|
||||
HWTEST_F(PreloadTest, PreloadTest_001, TestSize.Level1)
|
||||
{
|
||||
DownloadSuccessTest(TEST_URL_0, TEST_SIZE);
|
||||
DownloadSuccessTest(TEST_URL_4, TEST_SIZE_4);
|
||||
}
|
||||
|
||||
// test cancel callback
|
||||
HWTEST_F(PreDownloadTest, PreDownloadTest_002, TestSize.Level1)
|
||||
HWTEST_F(PreloadTest, PreloadTest_002, TestSize.Level1)
|
||||
{
|
||||
auto flag = std::make_shared<std::atomic_uint64_t>(0);
|
||||
auto callback = DownloadCallback{
|
||||
.OnSuccess = [](const std::shared_ptr<Data> &&data) {},
|
||||
auto callback = PreloadCallback{
|
||||
.OnSuccess = [](const std::shared_ptr<Data> &&data, const std::string &taskId) {},
|
||||
.OnCancel = [flag]() { flag->fetch_add(1); },
|
||||
.OnFail = [](const PreDownloadError &error) {},
|
||||
.OnFail = [](const PreloadError &error, const std::string &taskId) {},
|
||||
.OnProgress = [](uint64_t current, uint64_t total) {},
|
||||
};
|
||||
|
||||
auto handle = PreDownloadAgent::GetInstance()->Download(TEST_URL_1, std::make_unique<DownloadCallback>(callback));
|
||||
auto handle = Preload::GetInstance()->load(TEST_URL_1, std::make_unique<PreloadCallback>(callback));
|
||||
handle->Cancel();
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
EXPECT_EQ(flag->load(), 1);
|
||||
EXPECT_TRUE(handle->IsFinish());
|
||||
EXPECT_EQ(handle->GetState(), PreDownloadState::CANCEL);
|
||||
PreDownloadAgent::GetInstance()->Remove(TEST_URL_1);
|
||||
EXPECT_EQ(handle->GetState(), PreloadState::CANCEL);
|
||||
Preload::GetInstance()->Remove(TEST_URL_1);
|
||||
}
|
||||
|
||||
// test fail callback
|
||||
HWTEST_F(PreDownloadTest, PreDownloadTest_003, TestSize.Level1)
|
||||
HWTEST_F(PreloadTest, PreloadTest_003, TestSize.Level1)
|
||||
{
|
||||
auto flag = std::make_shared<std::atomic_uint64_t>(0);
|
||||
auto callback = DownloadCallback{
|
||||
.OnSuccess = [](const std::shared_ptr<Data> &&data) {},
|
||||
auto callback = PreloadCallback{
|
||||
.OnSuccess = [](const std::shared_ptr<Data> &&data, const std::string &taskId) {},
|
||||
.OnCancel = []() {},
|
||||
.OnFail = [flag](const PreDownloadError &error) { flag->fetch_add(1); },
|
||||
.OnFail = [flag](const PreloadError &error, const std::string &taskId) { flag->fetch_add(1); },
|
||||
.OnProgress = [](uint64_t current, uint64_t total) {},
|
||||
};
|
||||
|
||||
auto handle = PreDownloadAgent::GetInstance()->Download(TEST_URL_2, std::make_unique<DownloadCallback>(callback));
|
||||
auto handle = Preload::GetInstance()->load(TEST_URL_2, std::make_unique<PreloadCallback>(callback));
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
EXPECT_EQ(flag->load(), 1);
|
||||
EXPECT_TRUE(handle->IsFinish());
|
||||
EXPECT_EQ(handle->GetState(), PreDownloadState::FAIL);
|
||||
EXPECT_EQ(handle->GetState(), PreloadState::FAIL);
|
||||
}
|
||||
|
||||
// test nullptr callback
|
||||
HWTEST_F(PreDownloadTest, PreDownloadTest_004, TestSize.Level1)
|
||||
HWTEST_F(PreloadTest, PreloadTest_004, TestSize.Level1)
|
||||
{
|
||||
auto callback = DownloadCallback{
|
||||
auto callback = PreloadCallback{
|
||||
.OnSuccess = nullptr,
|
||||
.OnCancel = nullptr,
|
||||
.OnFail = nullptr,
|
||||
.OnProgress = nullptr,
|
||||
};
|
||||
|
||||
auto handle = PreDownloadAgent::GetInstance()->Download(TEST_URL_1, std::make_unique<DownloadCallback>(callback));
|
||||
auto handle = Preload::GetInstance()->load(TEST_URL_1, std::make_unique<PreloadCallback>(callback));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user