mirror of
https://gitee.com/openharmony/request_request
synced 2024-11-27 09:01:05 +00:00
fix on response and codecheck.
Change-Id: Ic2145fc1d7010e408744f377781361f74276efe4 Signed-off-by: huaxin <huaxin5@huawei.com>
This commit is contained in:
parent
9d1ca6475c
commit
e266194526
@ -17,6 +17,7 @@
|
||||
#define OHOS_REQUEST_JS_RESPONSE_LISTENER_H
|
||||
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include "i_response_listener.h"
|
||||
@ -42,7 +43,9 @@ private:
|
||||
private:
|
||||
const napi_env env_;
|
||||
const std::string taskId_;
|
||||
std::list<napi_ref> allCb_;
|
||||
std::list<std::pair<bool, napi_ref>> allCb_;
|
||||
std::recursive_mutex allCbMutex_;
|
||||
std::atomic<uint32_t> validCbNum{ 0 };
|
||||
};
|
||||
|
||||
} // namespace OHOS::Request
|
||||
|
@ -21,6 +21,7 @@ namespace OHOS::Request {
|
||||
|
||||
napi_status JSResponseListener::AddListener(napi_value cb)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(allCbMutex_);
|
||||
if (this->IsListenerAdded(cb)) {
|
||||
return napi_ok;
|
||||
}
|
||||
@ -31,8 +32,9 @@ napi_status JSResponseListener::AddListener(napi_value cb)
|
||||
return status;
|
||||
}
|
||||
|
||||
this->allCb_.push_back(ref);
|
||||
if (this->allCb_.size() == 1) {
|
||||
this->allCb_.push_back(std::make_pair(true, ref));
|
||||
++this->validCbNum;
|
||||
if (this->validCbNum == 1) {
|
||||
RequestManager::GetInstance()->Subscribe(this->taskId_, shared_from_this());
|
||||
}
|
||||
|
||||
@ -41,34 +43,36 @@ napi_status JSResponseListener::AddListener(napi_value cb)
|
||||
|
||||
napi_status JSResponseListener::RemoveListener(napi_value cb)
|
||||
{
|
||||
if (this->allCb_.empty()) {
|
||||
std::lock_guard<std::recursive_mutex> lock(allCbMutex_);
|
||||
if (this->validCbNum == 0) {
|
||||
return napi_ok;
|
||||
}
|
||||
|
||||
if (cb == nullptr) {
|
||||
RequestManager::GetInstance()->Unsubscribe(this->taskId_, shared_from_this());
|
||||
while (!this->allCb_.empty()) {
|
||||
napi_ref ref = this->allCb_.front();
|
||||
napi_delete_reference(this->env_, ref);
|
||||
this->allCb_.pop_front();
|
||||
for (auto it = this->allCb_.begin(); it != this->allCb_.end(); it++) {
|
||||
it->first = false;
|
||||
}
|
||||
this->validCbNum = 0;
|
||||
return napi_ok;
|
||||
}
|
||||
|
||||
for (auto it = this->allCb_.begin(); it != this->allCb_.end(); it++) {
|
||||
napi_value copyValue = nullptr;
|
||||
napi_get_reference_value(this->env_, *it, ©Value);
|
||||
napi_get_reference_value(this->env_, it->second, ©Value);
|
||||
|
||||
bool isEquals = false;
|
||||
napi_strict_equals(this->env_, cb, copyValue, &isEquals);
|
||||
if (isEquals) {
|
||||
napi_delete_reference(this->env_, *it);
|
||||
this->allCb_.erase(it);
|
||||
if (it->first == true) {
|
||||
it->first = false;
|
||||
--this->validCbNum;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (this->allCb_.empty()) {
|
||||
if (this->validCbNum == 0) {
|
||||
RequestManager::GetInstance()->Unsubscribe(this->taskId_, shared_from_this());
|
||||
}
|
||||
|
||||
@ -77,17 +81,24 @@ napi_status JSResponseListener::RemoveListener(napi_value cb)
|
||||
|
||||
void JSResponseListener::OnResponseReceive(const std::shared_ptr<Response> &response)
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(allCbMutex_);
|
||||
napi_value value = NapiUtils::Convert2JSValue(this->env_, response);
|
||||
for (auto it = this->allCb_.begin(); it != this->allCb_.end(); it++) {
|
||||
for (auto it = this->allCb_.begin(); it != this->allCb_.end();) {
|
||||
if (it->first == false) {
|
||||
napi_delete_reference(this->env_, it->second);
|
||||
it = this->allCb_.erase(it);
|
||||
continue;
|
||||
}
|
||||
napi_handle_scope scope = nullptr;
|
||||
napi_open_handle_scope(this->env_, &scope);
|
||||
napi_value callbackFunc = nullptr;
|
||||
napi_get_reference_value(this->env_, *it, &callbackFunc);
|
||||
napi_get_reference_value(this->env_, it->second, &callbackFunc);
|
||||
|
||||
napi_value callbackResult = nullptr;
|
||||
uint32_t paramNumber = 1;
|
||||
napi_call_function(this->env_, nullptr, callbackFunc, paramNumber, &value, &callbackResult);
|
||||
napi_close_handle_scope(this->env_, scope);
|
||||
it++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -98,12 +109,12 @@ bool JSResponseListener::IsListenerAdded(napi_value cb)
|
||||
}
|
||||
for (auto it = this->allCb_.begin(); it != this->allCb_.end(); it++) {
|
||||
napi_value copyValue = nullptr;
|
||||
napi_get_reference_value(this->env_, *it, ©Value);
|
||||
napi_get_reference_value(this->env_, it->second, ©Value);
|
||||
|
||||
bool isEquals = false;
|
||||
napi_strict_equals(this->env_, cb, copyValue, &isEquals);
|
||||
if (isEquals) {
|
||||
return true;
|
||||
return it->first;
|
||||
}
|
||||
}
|
||||
|
||||
@ -112,6 +123,7 @@ bool JSResponseListener::IsListenerAdded(napi_value cb)
|
||||
|
||||
bool JSResponseListener::HasListener()
|
||||
{
|
||||
return !this->allCb_.empty();
|
||||
return this->validCbNum != 0;
|
||||
}
|
||||
|
||||
} // namespace OHOS::Request
|
@ -861,7 +861,7 @@ void JsTask::ReloadListenerByTaskId(const std::string &id)
|
||||
{
|
||||
REQUEST_HILOGD("ReloadListenerByTaskId in");
|
||||
std::lock_guard<std::mutex> lockGuard(JsTask::taskMutex_);
|
||||
RequestManager::GetInstance()->ReopenChannel();
|
||||
|
||||
const auto it = taskMap_.find(id);
|
||||
std::string tid = it->first;
|
||||
if (it->second->responseListener_->HasListener()) {
|
||||
|
@ -13,9 +13,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "js_initialize.h"
|
||||
#include "request_event.h"
|
||||
|
||||
#include "js_initialize.h"
|
||||
#include "log.h"
|
||||
#include "request_manager.h"
|
||||
|
||||
@ -320,7 +320,7 @@ napi_status RequestEvent::GetResult(
|
||||
int32_t RequestEvent::StartExec(const std::shared_ptr<ExecContext> &context)
|
||||
{
|
||||
REQUEST_HILOGD("RequestEvent::StartExec in");
|
||||
JsTask* task = context->task;
|
||||
JsTask *task = context->task;
|
||||
Config config = task->config_;
|
||||
|
||||
// Rechecks file path.
|
||||
|
@ -70,7 +70,11 @@ public:
|
||||
|
||||
void OnResponseReceive(const std::shared_ptr<Response> &response)
|
||||
{
|
||||
std::vector<std::shared_ptr<IResponseListener>> responseListenerVec;
|
||||
for (auto responseListener : responseListeners_) {
|
||||
responseListenerVec.push_back(responseListener);
|
||||
}
|
||||
for (auto responseListener : responseListenerVec) {
|
||||
responseListener->OnResponseReceive(response);
|
||||
}
|
||||
}
|
||||
|
@ -115,7 +115,7 @@ int32_t RequestManagerImpl::Start(const std::string &tid)
|
||||
{
|
||||
REQUEST_HILOGD("Start in");
|
||||
auto proxy = GetRequestServiceProxy();
|
||||
if (proxy == nullptr ) {
|
||||
if (proxy == nullptr) {
|
||||
if (!RequestManager::GetInstance()->LoadRequestServer()) {
|
||||
return E_SERVICE_ERROR;
|
||||
}
|
||||
@ -317,7 +317,6 @@ std::shared_ptr<Request> RequestManagerImpl::GetTask(const std::string &taskId)
|
||||
}
|
||||
|
||||
auto retPair = this->tasks_.emplace(taskId, std::make_shared<Request>(Request(taskId)));
|
||||
|
||||
if (retPair.second) {
|
||||
return retPair.first->second;
|
||||
}
|
||||
|
@ -15,9 +15,9 @@
|
||||
|
||||
#include "response_message_receiver.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
@ -157,6 +157,7 @@ int QueryRequestTaskConfig(const OHOS::NativeRdb::RdbPredicates &rdbPredicates,
|
||||
CTaskConfig **BuildCTaskConfigs(const std::vector<TaskConfig> &taskConfigs);
|
||||
bool CleanTaskConfigTable(uint32_t taskId, uint64_t uid);
|
||||
void RequestDBRemoveRecordsFromTime(uint64_t time);
|
||||
uint64_t QueryTaskTokenId(uint32_t taskId);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -1154,4 +1154,29 @@ void RequestDBRemoveRecordsFromTime(uint64_t time)
|
||||
}
|
||||
REQUEST_HILOGE("request_task table deletes records before one week failed");
|
||||
return;
|
||||
}
|
||||
|
||||
uint64_t QueryTaskTokenId(uint32_t taskId)
|
||||
{
|
||||
OHOS::NativeRdb::RdbPredicates rdbPredicates("request_task");
|
||||
rdbPredicates.EqualTo("task_id", std::to_string(taskId));
|
||||
auto resultSet = OHOS::Request::RequestDataBase::GetInstance().Query(rdbPredicates, { "token_id" });
|
||||
int rowCount = 0;
|
||||
if (resultSet == nullptr) {
|
||||
REQUEST_HILOGE("result set is nullptr");
|
||||
return -1;
|
||||
}
|
||||
if (resultSet->GetRowCount(rowCount) != OHOS::NativeRdb::E_OK) {
|
||||
REQUEST_HILOGE("TaskConfig result count row failed");
|
||||
return -1;
|
||||
}
|
||||
if (rowCount == 0) {
|
||||
REQUEST_HILOGE("TaskConfig result count row is 0");
|
||||
return -1;
|
||||
}
|
||||
if (resultSet->GoToRow(0) != OHOS::NativeRdb::E_OK) {
|
||||
REQUEST_HILOGE("TaskConfig result set go to 0 row failed");
|
||||
return -1;
|
||||
}
|
||||
return (GetLong(resultSet, 0));
|
||||
}
|
@ -36,7 +36,10 @@ impl TaskManager {
|
||||
self.start_inner(task);
|
||||
ErrorCode::ErrOk
|
||||
} else if self.has_task_config_record(task_id) {
|
||||
info!("Has found a failed task in database, task_id:{}, try to continue download", task_id);
|
||||
info!(
|
||||
"Has found a failed task in database, task_id:{}, try to continue download",
|
||||
task_id
|
||||
);
|
||||
self.continue_single_failed_task(self.recording_rdb_num.clone(), task_id);
|
||||
ErrorCode::ErrOk
|
||||
} else {
|
||||
|
@ -26,6 +26,7 @@ use super::scheduled;
|
||||
use crate::error::ErrorCode;
|
||||
use crate::service::ability::PANIC_INFO;
|
||||
use crate::task::config::Version;
|
||||
use crate::task::ffi::HasRequestTaskRecord;
|
||||
use crate::task::info::{ApplicationState, State};
|
||||
use crate::task::reason::Reason;
|
||||
use crate::task::request_task::RequestTask;
|
||||
@ -188,15 +189,15 @@ impl TaskManager {
|
||||
}
|
||||
return;
|
||||
}
|
||||
for task in &self.restoring_tasks {
|
||||
if task.conf.common_data.task_id == task_id {
|
||||
if task.conf.common_data.token_id == token_id {
|
||||
let _ = tx.send(ErrorCode::ErrOk);
|
||||
} else {
|
||||
let _ = tx.send(ErrorCode::Permission);
|
||||
}
|
||||
return;
|
||||
|
||||
// get token_id from db
|
||||
if unsafe { HasRequestTaskRecord(task_id) } {
|
||||
if unsafe { QueryTaskTokenId(task_id) } == token_id {
|
||||
let _ = tx.send(ErrorCode::ErrOk);
|
||||
} else {
|
||||
let _ = tx.send(ErrorCode::Permission);
|
||||
}
|
||||
return;
|
||||
}
|
||||
let _ = tx.send(ErrorCode::TaskNotFound);
|
||||
}
|
||||
@ -523,4 +524,5 @@ extern "C" {
|
||||
pub(crate) fn GetHost() -> CStringWrapper;
|
||||
pub(crate) fn GetPort() -> CStringWrapper;
|
||||
pub(crate) fn GetExclusionList() -> CStringWrapper;
|
||||
pub(crate) fn QueryTaskTokenId(task_id: u32) -> u64;
|
||||
}
|
||||
|
@ -112,10 +112,14 @@ impl TaskManager {
|
||||
}
|
||||
|
||||
pub(crate) fn has_task_config_record(&self, task_id: u32) -> bool {
|
||||
unsafe{ HasTaskConfigRecord(task_id) }
|
||||
unsafe { HasTaskConfigRecord(task_id) }
|
||||
}
|
||||
|
||||
pub(crate) fn continue_single_failed_task(&mut self, recording_rdb_num: Arc<AtomicU32>, task_id: u32) {
|
||||
pub(crate) fn continue_single_failed_task(
|
||||
&mut self,
|
||||
recording_rdb_num: Arc<AtomicU32>,
|
||||
task_id: u32,
|
||||
) {
|
||||
if let Some(config) = self.query_single_failed_task_config(task_id) {
|
||||
debug!("RSA query single failed task config is {:?}", config);
|
||||
let uid = config.common_data.uid;
|
||||
@ -238,9 +242,12 @@ impl TaskManager {
|
||||
|
||||
pub(crate) fn query_single_failed_task_config(&self, task_id: u32) -> Option<TaskConfig> {
|
||||
debug!("query single failed task config in database");
|
||||
let c_task_config = unsafe {QuerySingleFailedTaskConfig(task_id)};
|
||||
let c_task_config = unsafe { QuerySingleFailedTaskConfig(task_id) };
|
||||
if c_task_config.is_null() {
|
||||
debug!("can not find the failed task in database, which task id is {}", task_id);
|
||||
debug!(
|
||||
"can not find the failed task in database, which task id is {}",
|
||||
task_id
|
||||
);
|
||||
None
|
||||
} else {
|
||||
let task_config = TaskConfig::from_c_struct(unsafe { &*c_task_config });
|
||||
|
@ -43,7 +43,7 @@ impl ClientManagerEntry {
|
||||
match rx.get() {
|
||||
Some(ret) => ret,
|
||||
None => {
|
||||
error!("open_channel failed");
|
||||
error!("open channel fail");
|
||||
Err(ErrorCode::Other)
|
||||
}
|
||||
}
|
||||
|
@ -29,21 +29,21 @@ impl OpenChannel {
|
||||
_data: &BorrowedMsgParcel,
|
||||
reply: &mut BorrowedMsgParcel,
|
||||
) -> IpcResult<()> {
|
||||
info!("open channnel");
|
||||
info!("open channel");
|
||||
let pid = get_calling_pid();
|
||||
let uid = get_calling_uid();
|
||||
let token_id = get_calling_token_id();
|
||||
match RequestAbility::client_manager().open_channel(pid, uid, token_id) {
|
||||
Ok(fd) => {
|
||||
debug!("open channel ok, fd is {}", fd);
|
||||
let file = unsafe { File::from_raw_fd(fd) };
|
||||
let file = FileDesc::new(file);
|
||||
reply.write(&(ErrorCode::ErrOk as i32))?;
|
||||
reply.write(&file)?;
|
||||
info!("open channnel ok ");
|
||||
Ok(())
|
||||
}
|
||||
Err(_) => {
|
||||
error!("open_channel failed");
|
||||
error!("open channel fail");
|
||||
reply.write(&(ErrorCode::ParameterCheck as i32))?;
|
||||
Err(IpcStatusCode::Failed)
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ impl Unsubscribe {
|
||||
data: &BorrowedMsgParcel,
|
||||
reply: &mut BorrowedMsgParcel,
|
||||
) -> IpcResult<()> {
|
||||
info!("subscribe");
|
||||
info!("unsubscribe");
|
||||
let tid: String = data.read()?;
|
||||
debug!("Service unsubscribe: task_id is {}", tid);
|
||||
match tid.parse::<u32>() {
|
||||
@ -32,8 +32,8 @@ impl Unsubscribe {
|
||||
reply.write(&(ErrorCode::ErrOk as i32))?;
|
||||
Ok(())
|
||||
} else {
|
||||
error!("unsubscribe failed");
|
||||
reply.write(&(ErrorCode::TaskNotFound as i32))?; // 错误码待统一处理
|
||||
debug!("unsubscribe failed");
|
||||
reply.write(&(ErrorCode::TaskNotFound as i32))?;
|
||||
Err(IpcStatusCode::Failed)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user