预加载目录结构调整

Signed-off-by: fqwert <yanglv2@huawei.com>
Change-Id: Ia3e56720e1800237c265ceeb6929922a765049c6
This commit is contained in:
fqwert 2024-11-16 14:02:34 +08:00
parent 9e631bf297
commit cbd53e09d6
54 changed files with 93 additions and 94 deletions

View File

@ -14,9 +14,9 @@
[workspace]
resolver = "2"
members = [
"pre_download/ffrt_rs",
"pre_download/native",
"pre_download/netstack_rs",
"preload/ffrt_rs",
"preload/native",
"preload/netstack_rs",
"request_utils",
"test/rustest",
]

View File

@ -70,7 +70,7 @@
"base_group": [
"//base/request/request/frameworks/js/napi:request",
"//base/request/request/frameworks/cj/ffi:cj_request_ffi",
"//base/request/request/pre_download/js/napi:preload"
"//base/request/request/preload/js/napi:preload"
],
"fwk_group": [
"//base/request/request/frameworks/native:request_native"
@ -102,7 +102,7 @@
"name": "//base/request/request/frameworks/js/napi:request_static"
},
{
"name": "//base/request/request/pre_download/native:predownload_native",
"name": "//base/request/request/preload/native:preload_native",
"header": {
"header_files": [],
"header_base": "//base/request/request/interfaces/inner_kits/running_count/include"
@ -116,12 +116,12 @@
"//base/request/request/test/unittest/js_test/requestQueryTaskTest:unittest",
"//base/request/request/test/unittest/js_test/requestSystemQueryTest:unittest",
"//base/request/request/test/unittest/cpp_test/saTest:unittest",
"//base/request/request/test/unittest/cpp_test/pre_download:unittest",
"//base/request/request/test/unittest/cpp_test/preload:unittest",
"//base/request/request/test/unittest/cpp_test/fwkTest:unittest",
"//base/request/request/services/tests:unittest",
"//base/request/request/pre_download/netstack_rs:unittest",
"//base/request/request/pre_download/native:unittest",
"//base/request/request/pre_download/ffrt_rs:unittest",
"//base/request/request/preload/netstack_rs:unittest",
"//base/request/request/preload/native:unittest",
"//base/request/request/preload/ffrt_rs:unittest",
"//base/request/request/request_utils:unittest"
]
}

View File

@ -71,7 +71,7 @@ ohos_rust_static_library("ffrt_rs") {
}
ohos_rust_unittest("rust_request_ffrt_rs_ut_test") {
module_out_path = "request/predownload"
module_out_path = "request/preload"
sources = [ "src/lib.rs" ]

View File

@ -28,10 +28,10 @@ ohos_shared_library("preload") {
sources = [
"src/napi_utils.cpp",
"src/pre_download_module.cpp",
"src/preload_module.cpp",
]
deps = [
"../../native:predownload_native",
"../../native:preload_native",
"//third_party/rust/crates/cxx:cxx_cppdeps",
]

View File

@ -13,8 +13,6 @@
* limitations under the License.
*/
#include "pre_download_module.h"
#include <unistd.h>
#include <cstdint>
@ -26,7 +24,8 @@
#include "napi/native_common.h"
#include "napi/native_node_api.h"
#include "napi_utils.h"
#include "request_pre_download.h"
#include "preload_module.h"
#include "request_preload.h"
namespace OHOS::Request {

View File

@ -13,11 +13,11 @@
import("//build/ohos.gni")
import("//build/test.gni")
rust_cxx("predownload_native_rust_gen") {
rust_cxx("preload_native_rust_gen") {
sources = [ "src/wrapper.rs" ]
}
ohos_rust_static_ffi("predownload_native_rust") {
ohos_rust_static_ffi("preload_native_rust") {
sanitize = {
integer_overflow = true
ubsan = true
@ -41,15 +41,15 @@ ohos_rust_static_ffi("predownload_native_rust") {
part_name = "request"
}
config("predownload_native_config") {
config("preload_native_config") {
include_dirs = [
"include",
"${target_gen_dir}/src",
"../../interfaces/inner_kits/predownload/include",
"../../interfaces/inner_kits/preload/include",
]
}
ohos_shared_library("predownload_native") {
ohos_shared_library("preload_native") {
sanitize = {
integer_overflow = true
ubsan = true
@ -60,16 +60,16 @@ ohos_shared_library("predownload_native") {
}
stack_protector_ret = true
public_configs = [ ":predownload_native_config" ]
public_configs = [ ":preload_native_config" ]
sources = [
"src/cxx/pre_download_callback.cpp",
"src/cxx/request_pre_download.cpp",
"src/cxx/preload_callback.cpp",
"src/cxx/request_preload.cpp",
]
sources += get_target_outputs(":predownload_native_rust_gen")
sources += get_target_outputs(":preload_native_rust_gen")
deps = [
":predownload_native_rust",
":predownload_native_rust_gen",
":preload_native_rust",
":preload_native_rust_gen",
"//third_party/rust/crates/cxx:cxx_cppdeps",
]
@ -82,7 +82,7 @@ ohos_shared_library("predownload_native") {
part_name = "request"
}
ohos_static_library("predownload_native_static") {
ohos_static_library("preload_native_static") {
sanitize = {
integer_overflow = true
ubsan = true
@ -93,15 +93,15 @@ ohos_static_library("predownload_native_static") {
}
stack_protector_ret = true
public_configs = [ ":predownload_native_config" ]
public_configs = [ ":preload_native_config" ]
sources = [
"src/cxx/pre_download_callback.cpp",
"src/cxx/request_pre_download.cpp",
"src/cxx/preload_callback.cpp",
"src/cxx/request_preload.cpp",
]
sources += get_target_outputs(":predownload_native_rust_gen")
sources += get_target_outputs(":preload_native_rust_gen")
deps = [
":predownload_native_rust_gen",
":preload_native_rust_gen",
"//third_party/rust/crates/cxx:cxx_cppdeps",
]
@ -114,8 +114,8 @@ ohos_static_library("predownload_native_static") {
part_name = "request"
}
ohos_rust_unittest("rust_predownload_native_ut_test") {
module_out_path = "request/predownload"
ohos_rust_unittest("rust_preload_native_ut_test") {
module_out_path = "request/preload"
sources = [ "src/lib.rs" ]
@ -124,7 +124,7 @@ ohos_rust_unittest("rust_predownload_native_ut_test") {
rustflags = [ "--cfg=feature=\"ohos\"" ]
deps = [
":predownload_native_static",
":preload_native_static",
"../../request_utils:request_utils",
"../ffrt_rs:ffrt_rs",
"../netstack_rs:netstack_rs",
@ -138,5 +138,5 @@ ohos_rust_unittest("rust_predownload_native_ut_test") {
group("unittest") {
testonly = true
deps = [ ":rust_predownload_native_ut_test" ]
deps = [ ":rust_preload_native_ut_test" ]
}

View File

@ -12,7 +12,7 @@
# limitations under the License.
[package]
name = "predownload_native"
name = "preload_native"
version = "0.1.0"
edition = "2021"

View File

@ -17,21 +17,21 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::LazyLock;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use predownload_native::{CustomCallback, DownloadAgent, DownloadRequest};
use preload_native::{CustomCallback, DownloadAgent, DownloadRequest};
use utils::{init, test_server};
struct Callback;
impl CustomCallback for Callback {}
fn agent_predownload(url: &str) {
fn agent_preload(url: &str) {
let agent = DownloadAgent::get_instance();
let request = DownloadRequest::new(&url);
let callback = Box::new(Callback);
agent.pre_download(request, callback, false);
agent.preload(request, callback, false);
}
fn predownload_benchmark_different_url(c: &mut Criterion) {
fn preload_benchmark_different_url(c: &mut Criterion) {
static SERVER: LazyLock<Vec<String>> = LazyLock::new(|| {
let mut v = vec![];
for _ in 0..1000 {
@ -42,21 +42,21 @@ fn predownload_benchmark_different_url(c: &mut Criterion) {
static A: AtomicUsize = AtomicUsize::new(0);
init();
c.bench_function("predownload", |b| {
c.bench_function("preload", |b| {
b.iter(|| {
let a = black_box(A.fetch_add(1, Ordering::SeqCst));
let server = SERVER[a % 1000].clone();
let url = format!("{}/{}", server, a);
agent_predownload(&url)
agent_preload(&url)
});
});
}
fn predownload_benchmark_same_url(c: &mut Criterion) {
fn preload_benchmark_same_url(c: &mut Criterion) {
static SERVER: LazyLock<String> = LazyLock::new(|| test_server(|_| {}));
init();
c.bench_function("predownload", |b| {
b.iter(|| agent_predownload(&SERVER));
c.bench_function("preload", |b| {
b.iter(|| agent_preload(&SERVER));
});
}
@ -64,5 +64,5 @@ fn config() -> Criterion {
Criterion::default().sample_size(1000)
}
criterion_group! {name = agent; config = config();targets = predownload_benchmark_same_url}
criterion_group! {name = agent; config = config();targets = preload_benchmark_same_url}
criterion_main!(agent);

View File

@ -18,7 +18,7 @@
#include <memory>
#include "cxx.h"
#include "request_pre_download.h"
#include "request_preload.h"
namespace OHOS::Request {
@ -27,8 +27,8 @@ public:
PreloadCallbackWrapper(std::unique_ptr<PreloadCallback> callback);
~PreloadCallbackWrapper() = default;
void OnSuccess(const std::shared_ptr<Data> data, rust::str TaskId) const;
void OnFail(rust::Box<DownloadError> error, rust::str TaskId) 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;

View File

@ -123,7 +123,7 @@ impl DownloadAgent {
CacheManager::get_instance().remove(task_id);
}
pub fn pre_download(
pub fn preload(
&self,
request: DownloadRequest,
mut callback: Box<dyn CustomCallback>,
@ -196,7 +196,7 @@ impl DownloadAgent {
}
#[cfg(feature = "ohos")]
pub(crate) fn ffi_pre_download(
pub(crate) fn ffi_preload(
&self,
url: &str,
callback: cxx::UniquePtr<PreloadCallbackWrapper>,
@ -204,7 +204,7 @@ impl DownloadAgent {
options: &FfiPredownloadOptions,
) -> Box<TaskHandle> {
let Some(callback) = FfiCallback::from_ffi(callback) else {
error!("ffi_pre_download callback is null");
error!("ffi_preload callback is null");
return Box::new(TaskHandle::new(TaskId::from_url(url)));
};
@ -218,7 +218,7 @@ impl DownloadAgent {
request.headers(headers);
}
Box::new(self.pre_download(request, Box::new(callback), update))
Box::new(self.preload(request, Box::new(callback), update))
}
pub fn set_file_cache_size(&self, size: u64) {
@ -294,20 +294,20 @@ mod test {
}
#[test]
fn ut_pre_download_success() {
fn ut_preload_success() {
init();
let agent = DownloadAgent::new();
let success_flag = Arc::new(AtomicUsize::new(0));
let callback = Box::new(TestCallbackS {
flag: success_flag.clone(),
});
agent.pre_download(DownloadRequest::new(TEST_URL), callback, false);
agent.preload(DownloadRequest::new(TEST_URL), callback, false);
std::thread::sleep(Duration::from_secs(1));
assert_eq!(success_flag.load(Ordering::SeqCst), 1);
}
#[test]
fn ut_pre_download_success_add_callback() {
fn ut_preload_success_add_callback() {
init();
let agent = DownloadAgent::new();
let success_flag_0 = Arc::new(AtomicUsize::new(0));
@ -320,28 +320,28 @@ mod test {
flag: success_flag_1.clone(),
});
agent.pre_download(DownloadRequest::new(TEST_URL), callback_0, false);
agent.pre_download(DownloadRequest::new(TEST_URL), callback_1, false);
agent.preload(DownloadRequest::new(TEST_URL), callback_0, false);
agent.preload(DownloadRequest::new(TEST_URL), callback_1, false);
std::thread::sleep(Duration::from_secs(1));
assert_eq!(success_flag_0.load(Ordering::SeqCst), 1);
assert_eq!(success_flag_1.load(Ordering::SeqCst), 1);
}
#[test]
fn ut_pre_download_fail() {
fn ut_preload_fail() {
init();
let agent = DownloadAgent::new();
let error = Arc::new(Mutex::new(String::new()));
let callback = Box::new(TestCallbackF {
flag: error.clone(),
});
agent.pre_download(DownloadRequest::new(ERROR_IP), callback, false);
agent.preload(DownloadRequest::new(ERROR_IP), callback, false);
std::thread::sleep(Duration::from_secs(1));
assert!(!error.lock().unwrap().as_str().is_empty());
}
#[test]
fn ut_pre_download_fail_add_callback() {
fn ut_preload_fail_add_callback() {
init();
let agent = DownloadAgent::new();
let error_0 = Arc::new(Mutex::new(String::new()));
@ -353,43 +353,43 @@ mod test {
flag: error_1.clone(),
});
agent.pre_download(DownloadRequest::new(ERROR_IP), callback_0, false);
agent.pre_download(DownloadRequest::new(ERROR_IP), callback_1, false);
agent.preload(DownloadRequest::new(ERROR_IP), callback_0, false);
agent.preload(DownloadRequest::new(ERROR_IP), callback_1, false);
std::thread::sleep(Duration::from_secs(1));
assert!(!error_0.lock().unwrap().as_str().is_empty());
assert!(!error_1.lock().unwrap().as_str().is_empty());
}
#[test]
fn ut_pre_download_cancel_0() {
fn ut_preload_cancel_0() {
init();
let agent = DownloadAgent::new();
let cancel_flag = Arc::new(AtomicUsize::new(0));
let callback = Box::new(TestCallbackC {
flag: cancel_flag.clone(),
});
let handle = agent.pre_download(DownloadRequest::new(TEST_URL), callback, true);
let handle = agent.preload(DownloadRequest::new(TEST_URL), callback, true);
handle.cancel();
std::thread::sleep(Duration::from_secs(1));
assert_eq!(cancel_flag.load(Ordering::SeqCst), 1);
}
#[test]
fn ut_pre_download_cancel_1() {
fn ut_preload_cancel_1() {
init();
let agent = DownloadAgent::new();
let cancel_flag = Arc::new(AtomicUsize::new(0));
let callback = Box::new(TestCallbackC {
flag: cancel_flag.clone(),
});
agent.pre_download(DownloadRequest::new(TEST_URL), callback, true);
agent.preload(DownloadRequest::new(TEST_URL), callback, true);
agent.cancel(TEST_URL);
std::thread::sleep(Duration::from_secs(1));
assert_eq!(cancel_flag.load(Ordering::SeqCst), 1);
}
#[test]
fn ut_pre_download_cancel_add_callback() {
fn ut_preload_cancel_add_callback() {
init();
let agent = DownloadAgent::new();
let cancel_flag_0 = Arc::new(AtomicUsize::new(0));
@ -401,8 +401,8 @@ mod test {
flag: cancel_flag_1.clone(),
});
let handle_0 = agent.pre_download(DownloadRequest::new(TEST_URL), callback_0, false);
agent.pre_download(DownloadRequest::new(TEST_URL), callback_1, false);
let handle_0 = agent.preload(DownloadRequest::new(TEST_URL), callback_0, false);
agent.preload(DownloadRequest::new(TEST_URL), callback_1, false);
handle_0.cancel();
std::thread::sleep(Duration::from_secs(1));
assert_eq!(cancel_flag_0.load(Ordering::SeqCst), 1);
@ -410,10 +410,10 @@ mod test {
}
#[test]
fn ut_pre_download_already_success() {
fn ut_preload_already_success() {
init();
let agent = DownloadAgent::new();
agent.pre_download(
agent.preload(
DownloadRequest::new(TEST_URL),
Box::new(TestCallbackN),
false,
@ -424,13 +424,13 @@ mod test {
let callback = Box::new(TestCallbackS {
flag: success_flag.clone(),
});
agent.pre_download(DownloadRequest::new(TEST_URL), callback, false);
agent.preload(DownloadRequest::new(TEST_URL), callback, false);
std::thread::sleep(Duration::from_millis(500));
assert_eq!(success_flag.load(Ordering::SeqCst), 1);
}
#[test]
fn ut_predownload_local_headers() {
fn ut_preload_local_headers() {
init();
let headers = vec![
("User-Agent", "Mozilla/5.0"),
@ -466,7 +466,7 @@ mod test {
let callback = Box::new(TestCallbackS {
flag: success_flag.clone(),
});
DownloadAgent::new().pre_download(request, callback, true);
DownloadAgent::new().preload(request, callback, true);
std::thread::sleep(Duration::from_millis(200));
assert!(flag.load(Ordering::SeqCst));
assert_eq!(success_flag.load(Ordering::SeqCst), NO_DATA);

View File

@ -38,7 +38,7 @@ static CACHE_DIR_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
#[cfg(not(feature = "ohos"))]
let mut path = PathBuf::from_str("./").unwrap();
path.push("predownload_caches");
path.push("preload_caches");
let _ = fs::create_dir_all(path.as_path());
path
});

View File

@ -27,7 +27,7 @@ impl Updater {
callback: Box<dyn CustomCallback>,
seq: usize,
) -> Self {
info!("new pre_download task {} seq {}", task_id.brief(), seq);
info!("new preload task {} seq {}", task_id.brief(), seq);
let task_handle = download(task_id, request, Some(callback), seq);
Self {
handle: task_handle,

View File

@ -13,12 +13,12 @@
* limitations under the License.
*/
#include "pre_download_callback.h"
#include "preload_callback.h"
#include <memory>
#include "cxx.h"
#include "request_pre_download.h"
#include "request_preload.h"
namespace OHOS::Request {
PreloadCallbackWrapper::PreloadCallbackWrapper(std::unique_ptr<PreloadCallback> callback)
@ -28,7 +28,7 @@ PreloadCallbackWrapper::PreloadCallbackWrapper(std::unique_ptr<PreloadCallback>
}
}
void PreloadCallbackWrapper::OnSuccess(const std::shared_ptr<Data> data, rust::str taskId) 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), std::string(taskId));

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
#include "request_pre_download.h"
#include "request_preload.h"
#include <cstdint>
#include <memory>
@ -77,7 +77,7 @@ std::shared_ptr<PreloadHandle> Preload::load(
ffiOptions.headers.push_back(std::get<1>(header));
}
}
auto taskHandle = this->_agent->ffi_pre_download(rust::str(url), std::move(callback_wrapper), false, ffiOptions);
auto taskHandle = this->_agent->ffi_preload(rust::str(url), std::move(callback_wrapper), false, ffiOptions);
return std::make_shared<PreloadHandle>(std::move(taskHandle));
}

View File

@ -309,7 +309,7 @@ mod test {
}
#[test]
fn ut_pre_download() {
fn ut_preload() {
let success_flag = Arc::new(AtomicBool::new(false));
let request = DownloadRequest::new(TEST_URL);
download(

View File

@ -31,7 +31,7 @@ pub use error::DownloadError;
cfg_ohos! {
mod wrapper;
const TAG: &str = "PreDownloadNative\0";
const TAG: &str = "PreloadNative\0";
const DOMAIN: u32 = 0xD001C50;
#[cfg(not(test))]
use ffrt_rs::ffrt_spawn as spawn;

View File

@ -88,7 +88,7 @@ pub(crate) mod ffi {
type DownloadError;
fn bytes(self: &RustData) -> &[u8];
fn ffi_pre_download(
fn ffi_preload(
self: &DownloadAgent,
url: &str,
mut callback: UniquePtr<PreloadCallbackWrapper>,
@ -113,8 +113,8 @@ pub(crate) mod ffi {
}
unsafe extern "C++" {
include!("pre_download_callback.h");
include!("request_pre_download.h");
include!("preload_callback.h");
include!("request_preload.h");
include!("context.h");
type PreloadCallbackWrapper;

View File

@ -76,7 +76,7 @@ ohos_rust_static_library("request_utils") {
}
ohos_rust_unittest("rust_request_utils_ut_test") {
module_out_path = "request/predownload"
module_out_path = "request/preload"
sources = [ "src/lib.rs" ]
external_deps = [ "hilog:libhilog" ]

View File

@ -14,15 +14,15 @@
import("//build/test.gni")
module_output_path = "request/request"
ohos_unittest("pre_download_test") {
ohos_unittest("preload_test") {
module_out_path = module_output_path
include_dirs = [ "../../../../interfaces/inner_kits/predownload/include" ]
include_dirs = [ "../../../../interfaces/inner_kits/preload/include" ]
sources = [ "src/download.cpp" ]
deps = [
"../../../../pre_download/native:predownload_native",
"../../../../preload/native:preload_native",
"//third_party/rust/crates/cxx:cxx_cppdeps",
]
external_deps = [
@ -33,5 +33,5 @@ ohos_unittest("pre_download_test") {
group("unittest") {
testonly = true
deps = [ ":pre_download_test" ]
deps = [ ":preload_test" ]
}

View File

@ -32,7 +32,7 @@
#include <vector>
#include "gmock/gmock.h"
#include "request_pre_download.h"
#include "request_preload.h"
using namespace testing::ext;
using namespace OHOS::Request;