add network check for agent

Signed-off-by: woohoa <wanghuan36@huawei.com>
This commit is contained in:
15950533375 2024-09-06 18:27:25 +08:00
parent c21301ccc9
commit f774426066
13 changed files with 116 additions and 99 deletions

View File

@ -47,7 +47,8 @@
"access_token",
"eventhandler",
"hiappevent",
"hicollie"
"hicollie",
"netmanager_base"
],
"third_party": [
"openssl"

View File

@ -22,7 +22,7 @@
"ohos.permission.INTERNET",
"ohos.permission.MANAGE_SECURE_SETTINGS",
"ohos.permission.GET_BUNDLE_INFO",
"ohos.permission.CONNECTIVITY_INTERNAL"
"ohos.permission.GET_NETWORK_INFO"
],
"secon" : "u:r:app_domain_verify_agent:s0",
"sandbox" : 0

View File

@ -15,7 +15,6 @@
#ifndef APP_DOMAIN_VERIFY_TASK_H
#define APP_DOMAIN_VERIFY_TASK_H
#include <deque>
#include <memory>
#include <queue>
#include <shared_mutex>
@ -49,7 +48,7 @@ private:
ffrt::mutex mutex_;
ffrt::condition_variable cond_;
bool stop_ = false;
std::vector<std::function<void()>> tasks_;
std::queue<std::function<void()>> tasks_;
SafeMap<uint32_t, std::shared_ptr<IHttpTask>> taskMap_;
};
}

View File

@ -95,8 +95,8 @@ bool AppDomainVerifyTaskMgr::Init()
std::unique_lock<ffrt::mutex> lock(mutex_);
while (!this->stop_) {
if (!tasks_.empty()) {
auto task = std::move(tasks_.back());
tasks_.pop_back();
auto task = std::move(tasks_.front());
tasks_.pop();
lock.unlock();
task();
lock.lock();
@ -171,7 +171,7 @@ bool AppDomainVerifyTaskMgr::AddTask(const std::shared_ptr<IHttpTask>& task)
std::function<void()> taskWrapper = GetTaskWrapper(task);
std::unique_lock<ffrt::mutex> lock(mutex_);
taskMap_.EnsureInsert(task->GetTaskId(), task);
tasks_.push_back(std::move(taskWrapper));
tasks_.push(std::move(taskWrapper));
cond_.notify_one();
return true;
}

View File

@ -17,6 +17,7 @@
#define APP_DOMAIN_VERIFY_AGENT_SERVICE_H
#include "app_domain_verify_agent_service_stub.h"
#include <chrono>
#include "inner_verify_status.h"
#include "skill_uri.h"
#include "system_ability.h"
@ -27,14 +28,17 @@
#include "dfx/app_domain_verify_hisysevent.h"
#include "event_handler.h"
#include "event_runner.h"
#include "net_conn_client.h"
namespace OHOS {
namespace AppDomainVerify {
using namespace NetManagerStandard;
class AppDomainVerifyAgentService : public SystemAbility, public AppDomainVerifyAgentServiceStub {
DECLARE_SYSTEM_ABILITY(AppDomainVerifyAgentService);
public:
API_EXPORT AppDomainVerifyAgentService();
API_EXPORT virtual ~AppDomainVerifyAgentService();
API_EXPORT ~AppDomainVerifyAgentService() override;
API_EXPORT void SingleVerify(
const AppVerifyBaseInfo& appVerifyBaseInfo, const std::vector<SkillUri>& skillUris) override;
API_EXPORT void ConvertToExplicitWant(OHOS::AAFwk::Want& implicitWant, sptr<IConvertCallback>& callback) override;
@ -42,7 +46,6 @@ public:
protected:
void OnStart(const SystemAbilityOnDemandReason& startReason) override;
void OnStop() override;
void ExitIdleState() override;
void OnDump() override;
int Dump(int fd, const std::vector<std::u16string>& args) override;
@ -53,18 +56,22 @@ private:
void ExecuteVerifyTask(
const AppVerifyBaseInfo& appVerifyBaseInfo, const std::vector<SkillUri>& skillUris, TaskType type);
void UpdateWhiteList();
bool CanUnloadSa();
void OnDelayUnloadSA();
void PostDelayUnloadTask() override;
void PostDelayUnloadTask();
void DoSync(const TaskType& type);
void DoSync();
bool IsIdle();
bool IsNetAvaliable();
bool IsNetAvailable();
bool CanUnloadSa();
void UnloadSa();
std::string GetStatTime();
private:
std::shared_ptr<AppDomainVerifyExtensionMgr> appDomainVerifyExtMgr_;
std::shared_ptr<AppDomainVerifyTaskMgr> appDomainVerifyTaskMgr_;
std::shared_ptr<AppExecFwk::EventHandler> unloadHandler_;
std::shared_ptr<AppExecFwk::EventRunner> runner_;
std::chrono::system_clock::time_point now;
};
} // namespace AppDomainVerify

View File

@ -25,18 +25,13 @@ namespace OHOS {
namespace AppDomainVerify {
class AppDomainVerifyAgentServiceStub : public IRemoteStub<IAppDomainVerifyAgentService> {
public:
AppDomainVerifyAgentServiceStub();
virtual ~AppDomainVerifyAgentServiceStub();
AppDomainVerifyAgentServiceStub() = default;
~AppDomainVerifyAgentServiceStub() override = default;
int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
protected:
virtual void PostDelayUnloadTask() = 0;
virtual void ExitIdleState() = 0;
private:
int32_t OnSingleVerify(MessageParcel& data, MessageParcel& reply);
int32_t OnConvertToExplicitWant(MessageParcel& data, MessageParcel& reply);
};
} // namespace AppDomainVerify
} // namespace OHOS

View File

@ -24,26 +24,33 @@
#include "bms/bundle_info_query.h"
#include "app_domain_verify_mgr_client.h"
#include "verify_task.h"
#include "net_conn_client.h"
#include "iservice_registry.h"
namespace OHOS {
namespace AppDomainVerify {
namespace {
constexpr int32_t DELAY_TIME = 300000; // 5min = 5*60*1000
#ifndef _TEST
constexpr int32_t DELAY_TIME = 180000; // 3min = 3*60*1000
constexpr int MAX_DELAY_RETRY_CNT = 10;
#elif
constexpr int32_t DELAY_TIME = 60000; // 1min = 60*1000 for test
constexpr int MAX_DELAY_RETRY_CNT = 3; // 3 for test
#endif
std::atomic<int> retryCnt = 0;
std::atomic<bool> isDoSyncDone = false;
constexpr int MAX_NET_RETRY_CNT = 3;
constexpr int32_t DUMP_SYSTEM_START_YEAR = 1900;
constexpr int32_t FORMAT_BLANK_SIZE = 32;
}
static const std::string TASK_ID = "unload";
static const std::string BOOT_COMPLETED_EVENT = "usual.event.BOOT_COMPLETED";
static const std::string LOOP_EVENT = "loopevent";
using namespace NetManagerStandard;
const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(new AppDomainVerifyAgentService());
AppDomainVerifyAgentService::AppDomainVerifyAgentService() : SystemAbility(APP_DOMAIN_VERIFY_AGENT_SA_ID, true)
{
APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "new instance create.");
now = std::chrono::system_clock::now();
appDomainVerifyExtMgr_ = std::make_shared<AppDomainVerifyExtensionMgr>();
appDomainVerifyTaskMgr_ = AppDomainVerifyTaskMgr::GetInstance();
runner_ = AppExecFwk::EventRunner::Create("unload", AppExecFwk::ThreadMode::FFRT);
@ -169,52 +176,6 @@ void AppDomainVerifyAgentService::OnStop()
{
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "called");
}
void AppDomainVerifyAgentService::DoSync(const TaskType& type)
{
QueryAndCompleteRefresh(std::vector<InnerVerifyStatus>{ UNKNOWN, STATE_FAIL, FAILURE_REDIRECT, FAILURE_CLIENT_ERROR,
FAILURE_REJECTED_BY_SERVER, FAILURE_HTTP_UNKNOWN, FAILURE_TIMEOUT, FAILURE_CONFIG },
0, type);
UpdateWhiteList();
}
bool AppDomainVerifyAgentService::IsIdle()
{
if (appDomainVerifyTaskMgr_ == nullptr) {
return true;
} else {
return appDomainVerifyTaskMgr_->IsIdle();
}
}
bool AppDomainVerifyAgentService::IsNetAvaliable()
{
bool IsNetAvailable = false;
NetManagerStandard::NetConnClient::GetInstance().HasDefaultNet(IsNetAvailable);
return IsNetAvailable;
}
bool AppDomainVerifyAgentService::CanUnloadSa()
{
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "called");
// 尝试进行最大次数的有网络同步操作
if (!isDoSyncDone && retryCnt++ < MAX_NET_RETRY_CNT) {
if (IsNetAvaliable()) {
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "net avaliable, do sync");
DoSync(BOOT_REFRESH_TASK);
isDoSyncDone = true;
} else {
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "net un avaliable, retry and delay");
return false;
}
}
// 还需要判断是否有网络任务
if (IsIdle()) {
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "task idle");
return true;
}
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "task not idle");
return false;
}
void AppDomainVerifyAgentService::UnloadSa()
{
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "do unload sa");
@ -230,16 +191,65 @@ void AppDomainVerifyAgentService::UnloadSa()
}
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "do unload sa done");
}
bool AppDomainVerifyAgentService::IsIdle()
{
if (appDomainVerifyTaskMgr_ == nullptr) {
return true;
} else {
return appDomainVerifyTaskMgr_->IsIdle();
}
}
void AppDomainVerifyAgentService::DoSync(const TaskType& type)
{
QueryAndCompleteRefresh(std::vector<InnerVerifyStatus>{ UNKNOWN, STATE_FAIL, FAILURE_REDIRECT, FAILURE_CLIENT_ERROR,
FAILURE_REJECTED_BY_SERVER, FAILURE_HTTP_UNKNOWN, FAILURE_TIMEOUT, FAILURE_CONFIG },
0, type);
UpdateWhiteList();
}
bool AppDomainVerifyAgentService::IsNetAvailable()
{
bool isNetAvailable = false;
NetManagerStandard::NetConnClient::GetInstance().HasDefaultNet(isNetAvailable);
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "IsNetAvailable:%{public}d", isNetAvailable);
return isNetAvailable;
}
void AppDomainVerifyAgentService::DoSync()
{
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "do sync");
if (!isDoSyncDone && IsNetAvailable()) {
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "net connected, do sync once");
DoSync(BOOT_REFRESH_TASK);
isDoSyncDone = true;
}
}
bool AppDomainVerifyAgentService::CanUnloadSa()
{
auto reachedMaxCnt = (retryCnt >= MAX_DELAY_RETRY_CNT - 1);
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE,
"can unload? isDoSyncDone:%{public}d, retryCnt:%{public}d, IsIdle:%{public}d, reachedMaxCnt:%{public}d, "
"maxCnt:%{public}d",
isDoSyncDone.load(), retryCnt.load(), IsIdle(), reachedMaxCnt, MAX_DELAY_RETRY_CNT);
return (isDoSyncDone || reachedMaxCnt) && IsIdle();
}
void AppDomainVerifyAgentService::OnDelayUnloadSA()
{
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "on unload task");
if (!CanUnloadSa()) {
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "can not unload sa, delay unload");
PostDelayUnloadTask();
if (CanUnloadSa()) {
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "can unload sa");
UnloadSa();
return;
}
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "on unload task, do unload");
UnloadSa();
DoSync();
PostDelayUnloadTask();
retryCnt++;
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "on unload task, delay unload");
}
void AppDomainVerifyAgentService::PostDelayUnloadTask()
{
@ -248,28 +258,46 @@ void AppDomainVerifyAgentService::PostDelayUnloadTask()
unloadHandler_->PostTask([this] { OnDelayUnloadSA(); }, TASK_ID, DELAY_TIME);
}
void AppDomainVerifyAgentService::ExitIdleState()
{
CancelIdle();
}
void AppDomainVerifyAgentService::OnDump()
{
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "OnDump");
}
std::string AppDomainVerifyAgentService::GetStatTime()
{
std::string startTime;
time_t tt = std::chrono::system_clock::to_time_t(now);
auto ptm = localtime(&tt);
if (ptm != nullptr) {
char date[FORMAT_BLANK_SIZE] = { 0 };
auto flag = sprintf_s(date, sizeof(date), "%04d-%02d-%02d %02d:%02d:%02d",
(int)ptm->tm_year + DUMP_SYSTEM_START_YEAR, (int)ptm->tm_mon + 1, (int)ptm->tm_mday, (int)ptm->tm_hour,
(int)ptm->tm_min, (int)ptm->tm_sec);
if (flag < 0) {
return startTime;
}
startTime = date;
}
return startTime;
}
int AppDomainVerifyAgentService::Dump(int fd, const std::vector<std::u16string>& args)
{
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "Dump");
std::string dumpString{};
dumpString.append("Running state: ");
dumpString.append("Agent start time: ");
dumpString.append(GetStatTime());
dumpString.append("\n");
dumpString.append("TaskMgr state: ");
IsIdle() ? dumpString.append("idle.") : dumpString.append("running.");
dumpString.append("\n");
isDoSyncDone ? dumpString.append("isDoSyncDone:true.") : dumpString.append("isDoSyncDone:false.");
dumpString.append("isDoSyncDone:");
isDoSyncDone ? dumpString.append("true.") : dumpString.append("false.");
dumpString.append("\n");
dumpString.append("retryCnt:");
dumpString.append(std::to_string(retryCnt));
dumpString.append("\n");
dumpString.append("maxCnt:");
dumpString.append(std::to_string(MAX_DELAY_RETRY_CNT));
dumpString.append("\n");
(void)write(fd, dumpString.c_str(), dumpString.size());
return 0;
}

View File

@ -27,19 +27,11 @@ namespace {
constexpr int32_t DELAY_TIME = 300000; // 5min = 5*60*1000
const std::string TASK_ID = "unload";
}
AppDomainVerifyAgentServiceStub::AppDomainVerifyAgentServiceStub()
{
}
AppDomainVerifyAgentServiceStub::~AppDomainVerifyAgentServiceStub()
{
}
int32_t AppDomainVerifyAgentServiceStub::OnRemoteRequest(
uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
{
APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "onRemoteRequest##code = %{public}u", code);
ExitIdleState();
PostDelayUnloadTask();
std::u16string myDescripter = AppDomainVerifyAgentServiceStub::GetDescriptor();
std::u16string remoteDescripter = data.ReadInterfaceToken();
if (myDescripter != remoteDescripter) {

View File

@ -66,6 +66,7 @@ ohos_unittest("app_domain_verify_agent_client_test") {
"hisysevent:libhisysevent",
"image_framework:image_native",
"ipc:ipc_core",
"netmanager_base:net_conn_manager_if",
"netstack:http_client",
"safwk:system_ability_fwk",
"samgr:samgr_proxy",

View File

@ -57,9 +57,6 @@ public:
virtual void ConvertToExplicitWant(OHOS::AAFwk::Want& implicitWant, sptr<IConvertCallback>& callback) override
{
}
virtual void ExitIdleState() override
{
}
};
} // namespace AppDomainVerify

View File

@ -64,6 +64,7 @@ ohos_unittest("app_domain_verify_module_test") {
"hisysevent:libhisysevent",
"image_framework:image_native",
"ipc:ipc_core",
"netmanager_base:net_conn_manager_if",
"os_account:os_account_innerkits",
"relational_store:native_rdb",
"safwk:system_ability_fwk",

View File

@ -69,6 +69,7 @@ ohos_unittest("app_domain_verify_agent_service_test") {
"image_framework:image_native",
"init:libbegetutil",
"ipc:ipc_core",
"netmanager_base:net_conn_manager_if",
"netstack:http_client",
"os_account:os_account_innerkits",
"os_account:os_account_innerkits",

View File

@ -253,7 +253,6 @@ HWTEST_F(AgentServiceTest, AgentServiceTest020, TestSize.Level0)
appDomainVerifyAgentService->OnIdle(idleReason);
idleReason.SetName(BOOT_COMPLETED_EVENT);
appDomainVerifyAgentService->OnIdle(idleReason);
appDomainVerifyAgentService->ExitIdleState();
appDomainVerifyAgentService->OnStop();
}
/**
@ -270,7 +269,6 @@ HWTEST_F(AgentServiceTest, AgentServiceTest021, TestSize.Level0)
appDomainVerifyAgentService->OnIdle(idleReason);
idleReason.SetName(BOOT_COMPLETED_EVENT);
appDomainVerifyAgentService->OnIdle(idleReason);
appDomainVerifyAgentService->ExitIdleState();
appDomainVerifyAgentService->OnStop();
}
/**
@ -287,7 +285,6 @@ HWTEST_F(AgentServiceTest, AgentServiceTest022, TestSize.Level0)
appDomainVerifyAgentService->OnIdle(idleReason);
idleReason.SetName(BOOT_COMPLETED_EVENT);
appDomainVerifyAgentService->OnIdle(idleReason);
appDomainVerifyAgentService->ExitIdleState();
appDomainVerifyAgentService->OnStop();
}
/**
@ -305,7 +302,6 @@ HWTEST_F(AgentServiceTest, AgentServiceTest023, TestSize.Level0)
appDomainVerifyAgentService->OnIdle(idleReason);
idleReason.SetName(BOOT_COMPLETED_EVENT);
appDomainVerifyAgentService->OnIdle(idleReason);
appDomainVerifyAgentService->ExitIdleState();
appDomainVerifyAgentService->OnStop();
}
/**
@ -323,7 +319,6 @@ HWTEST_F(AgentServiceTest, AgentServiceTest024, TestSize.Level0)
appDomainVerifyAgentService->OnIdle(idleReason);
idleReason.SetName(BOOT_COMPLETED_EVENT);
appDomainVerifyAgentService->OnIdle(idleReason);
appDomainVerifyAgentService->ExitIdleState();
appDomainVerifyAgentService->OnStop();
}
}