!813 【需求】新增 updateTimer功能

Merge pull request !813 from 李勃/master
This commit is contained in:
openharmony_ci 2024-04-19 03:55:04 +00:00 committed by Gitee
commit 0f157906e9
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
22 changed files with 332 additions and 0 deletions

View File

@ -485,4 +485,32 @@ ErrCode ServiceProxy::GetBackupInfo(BundleName &bundleName, std::string &result)
HILOGI("ServiceProxy GetBackupInfo end. result = %s", result.c_str());
return BError(BError::Codes::OK, "success");
}
ErrCode ServiceProxy::UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result)
{
HILOGI("ServiceProxy UpdateTimer Begin.");
BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
MessageParcel data;
if (!data.WriteInterfaceToken(GetDescriptor())) {
return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
}
if (!data.WriteString(bundleName)) {
return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleName").GetCode();
}
if (!data.WriteUint32(timeOut)) {
return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send timeOut").GetCode();
}
MessageParcel reply;
MessageOption option;
option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_TIMER),
data, reply, option);
if (ret != NO_ERROR) {
string str = "Failed to send out the request because of " + to_string(ret);
return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
}
reply.ReadBool(result);
HILOGI("ServiceProxy UpdateTimer end. result = %d", result);
return BError(BError::Codes::OK, "success");
}
} // namespace OHOS::FileManagement::Backup

View File

@ -75,6 +75,7 @@ public:
virtual ErrCode AppIncrementalDone(ErrCode errCode) = 0;
virtual ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) = 0;
virtual ErrCode GetBackupInfo(BundleName &bundleName, std::string &result) = 0;
virtual ErrCode UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result) = 0;
DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Filemanagement.Backup.IService")
};

View File

@ -41,6 +41,7 @@ enum class IServiceInterfaceCode {
SERVICE_CMD_APP_INCREMENTAL_DONE,
SERVICE_CMD_GET_INCREMENTAL_FILE_NAME,
SERVICE_CMD_GET_BACKUP_INFO,
SERVICE_CMD_UPDATE_TIMER,
};
} // namespace OHOS::FileManagement::Backup

View File

@ -58,6 +58,7 @@ public:
ErrCode AppIncrementalDone(ErrCode errCode) override;
ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) override;
ErrCode GetBackupInfo(BundleName &bundleName, std::string &result) override;
ErrCode UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result) override;
public:
explicit ServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IService>(impl) {}

View File

@ -155,6 +155,8 @@ ohos_shared_library("backup") {
]
external_deps = [
"access_token:libaccesstoken_sdk",
"access_token:libtokenid_sdk",
"c_utils:utils",
"file_api:filemgmt_libhilog",
"file_api:filemgmt_libn",

View File

@ -25,6 +25,7 @@ bool PropNExporter::Export()
return exports_.AddProp({
NVal::DeclareNapiFunction("getLocalCapabilities", PropNOperation::Async),
NVal::DeclareNapiFunction("getBackupInfo", PropNOperation::DoGetBackupInfo),
NVal::DeclareNapiFunction("updateTimer", PropNOperation::DoUpdateTimer),
});
}

View File

@ -21,11 +21,28 @@
#include "filemgmt_libn.h"
#include "incremental_backup_data.h"
#include "service_proxy.h"
#include "access_token.h"
#include "accesstoken_kit.h"
#include "ipc_skeleton.h"
#include "tokenid_kit.h"
namespace OHOS::FileManagement::Backup {
using namespace std;
using namespace LibN;
static bool CheckPermission(const string &permission)
{
Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
return Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission) ==
Security::AccessToken::PermissionState::PERMISSION_GRANTED;
}
static bool IsSystemApp()
{
uint64_t fullTokenId = OHOS::IPCSkeleton::GetCallingFullTokenID();
return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
}
static napi_value AsyncCallback(napi_env env, const NFuncArg& funcArg)
{
HILOGD("called LocalCapabilities::AsyncCallback begin");
@ -219,4 +236,72 @@ napi_value PropNOperation::DoGetBackupInfo(napi_env env, napi_callback_info info
HILOGI("DoGetBackupInfo success with result: %{public}s", result.c_str());
return nResult;
}
bool PropNOperation::UpdateTimer(std::string &bundleName, uint32_t timeOut)
{
bool result = false;
ServiceProxy::InvaildInstance();
auto proxy = ServiceProxy::GetInstance();
if (!proxy) {
HILOGE("called DoUpdateTimer,failed to get proxy");
return result;
}
ErrCode errcode = proxy->UpdateTimer(bundleName, timeOut, result);
if (errcode != 0) {
HILOGE("proxy->UpdateTimer faild.");
return result;
}
return result;
}
napi_value PropNOperation::DoUpdateTimer(napi_env env, napi_callback_info info)
{
HILOGD("called DoUpdateTimer begin");
if (!CheckPermission("ohos.permission.BACKUP")) {
HILOGE("has not permission!");
NError(E_PERMISSION).ThrowErr(env);
return nullptr;
}
if (!IsSystemApp()) {
HILOGE("System App check fail!");
NError(E_PERMISSION_SYS).ThrowErr(env);
return nullptr;
}
NFuncArg funcArg(env, info);
if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
HILOGE("Number of arguments unmatched.");
NError(E_PARAMS).ThrowErr(env);
return nullptr;
}
NVal jsBundleStr(env, funcArg[NARG_POS::FIRST]);
auto [succStr, bundle, sizeStr] = jsBundleStr.ToUTF8String();
if (!succStr) {
HILOGE("First argument is not string.");
NError(E_PARAMS).ThrowErr(env);
return nullptr;
}
NVal jsBundleInt(env, funcArg[NARG_POS::SECOND]);
auto [succInt, time] = jsBundleInt.ToInt32();
if (!succInt || time <= 0) {
HILOGE("Second argument is not number.");
NError(E_PARAMS).ThrowErr(env);
return nullptr;
}
std::string bundleName = bundle.get();
uint32_t timeOut = time;
bool result = UpdateTimer(bundleName, timeOut);
if (result != true) {
return nullptr;
}
napi_value nResult;
napi_status status = napi_get_boolean(env, result, &nResult);
if (status != napi_ok) {
HILOGE("napi_get_boolean faild.");
return nullptr;
}
HILOGI("DoUpdateTimer success with result: %{public}d", result);
return nResult;
}
} // namespace OHOS::FileManagement::Backup

View File

@ -23,6 +23,9 @@ class PropNOperation final {
public:
static napi_value Async(napi_env env, napi_callback_info info);
static napi_value DoGetBackupInfo(napi_env env, napi_callback_info info);
static napi_value DoUpdateTimer(napi_env env, napi_callback_info info);
private:
static bool UpdateTimer(std::string &bundleName, uint32_t timeOut);
};
const std::string PROCEDURE_LOCALCAPABILITIES_NAME = "getLocalCapalities";

View File

@ -65,6 +65,7 @@ public:
ErrCode AppIncrementalDone(ErrCode errCode) override;
ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) override;
ErrCode GetBackupInfo(BundleName &bundleName, std::string &result) override;
ErrCode UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result) override;
// 以下都是非IPC接口
public:

View File

@ -56,6 +56,7 @@ private:
int32_t CmdAppIncrementalDone(MessageParcel &data, MessageParcel &reply);
int32_t CmdGetIncrementalFileHandle(MessageParcel &data, MessageParcel &reply);
int32_t CmdGetBackupInfo(MessageParcel &data, MessageParcel &reply);
int32_t CmdUpdateTimer(MessageParcel &data, MessageParcel &reply);
public:
template <typename T>

View File

@ -362,6 +362,17 @@ public:
*/
void BundleExtTimerStart(const std::string &bundleName, const Utils::Timer::TimerCallback &callback);
/**
* @brief
*
* @param bundleName
* @param timeOut
* @return true
* @return false
*/
bool UpdateTimer(const std::string &bundleName, uint32_t timeOut,
const Utils::Timer::TimerCallback &callback);
/**
* @brief /
*

View File

@ -1096,6 +1096,36 @@ ErrCode Service::GetBackupInfo(BundleName &bundleName, std::string &result)
}
}
ErrCode Service::UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result)
{
auto timeoutCallback = [ptr {wptr(this)}, bundleName]() {
auto thisPtr = ptr.promote();
if (!thisPtr) {
HILOGW("this pointer is null.");
return;
}
auto sessionPtr = ptr->session_;
auto sessionConnection = sessionPtr->GetExtConnection(bundleName);
HILOGE("Backup <%{public}s> Extension Process Timeout", bundleName.data());
sessionPtr->BundleExtTimerStop(bundleName);
sessionConnection->DisconnectBackupExtAbility();
thisPtr->ClearSessionAndSchedInfo(bundleName);
thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT));
};
try {
HILOGI("Service::UpdateTimer begin.");
session_->IncreaseSessionCnt();
result = session_->UpdateTimer(bundleName, timeOut, timeoutCallback);
session_->DecreaseSessionCnt();
return BError(BError::Codes::OK);
} catch (...) {
result = false;
session_->DecreaseSessionCnt();
HILOGI("Unexpected exception");
return EPERM;
}
}
AAFwk::Want Service::CreateConnectWant (BundleName &bundleName)
{
BConstants::ExtensionAction action = BConstants::ExtensionAction::BACKUP;

View File

@ -76,6 +76,8 @@ ServiceStub::ServiceStub()
&ServiceStub::CmdGetIncrementalFileHandle;
opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_INFO)] =
&ServiceStub::CmdGetBackupInfo;
opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_TIMER)] =
&ServiceStub::CmdUpdateTimer;
}
int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
@ -344,6 +346,30 @@ int32_t ServiceStub::CmdGetBackupInfo(MessageParcel &data, MessageParcel &reply)
return BError(BError::Codes::OK);
}
int32_t ServiceStub::CmdUpdateTimer(MessageParcel &data, MessageParcel &reply)
{
HILOGI("ServiceStub::CmdUpdateTimer Begin.");
int ret = ERR_OK;
string bundleName;
if (!data.ReadString(bundleName)) {
return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
}
uint32_t timeOut;
if (!data.ReadUint32(timeOut)) {
return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive timeOut"));
}
bool result;
ret = UpdateTimer(bundleName, timeOut, result);
if (ret != ERR_OK) {
return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call UpdateTimer"));
}
if (!reply.WriteBool(result)) {
return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result"));
}
HILOGI("ServiceStub::CmdUpdateTimer end.");
return BError(BError::Codes::OK);
}
int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply)
{
int res = Release();

View File

@ -580,6 +580,28 @@ void SvcSessionManager::BundleExtTimerStart(const std::string &bundleName, const
}
}
bool SvcSessionManager::UpdateTimer(const std::string &bundleName, uint32_t timeOut,
const Utils::Timer::TimerCallback &callback)
{
unique_lock<shared_mutex> lock(lock_);
if (!impl_.clientToken) {
throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified");
}
auto it = GetBackupExtNameMap(bundleName);
if (it->second.timerStatus == true) {
// 定时器已存在,则先销毁,再重新注册
it->second.timerStatus = false;
extBundleTimer.Unregister(it->second.extTimerId);
HILOGI("UpdateTimer timeout=%{public}u(ms), bundleName=%{public}s ",
timeOut, bundleName.c_str());
it->second.extTimerId = extBundleTimer.Register(callback, timeOut, true);
it->second.timerStatus = true;
return true;
}
return false;
}
void SvcSessionManager::BundleExtTimerStop(const std::string &bundleName)
{
unique_lock<shared_mutex> lock(lock_);

View File

@ -154,6 +154,11 @@ ErrCode ServiceProxy::GetBackupInfo(std::string &bundleName, std::string &result
return BError(BError::Codes::OK);
}
ErrCode ServiceProxy::UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result)
{
return BError(BError::Codes::OK);
}
sptr<IService> ServiceProxy::GetInstance()
{
if (!GetMockGetInstance()) {

View File

@ -187,4 +187,9 @@ ErrCode Service::GetBackupInfo(BundleName &bundleName, std::string &result)
{
return BError(BError::Codes::OK);
}
ErrCode Service::UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result)
{
return BError(BError::Codes::OK);
}
} // namespace OHOS::FileManagement::Backup

View File

@ -66,6 +66,8 @@ ServiceStub::ServiceStub()
&ServiceStub::CmdGetIncrementalFileHandle;
opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_INFO)] =
&ServiceStub::CmdGetBackupInfo;
opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_TIMER)] =
&ServiceStub::CmdUpdateTimer;
}
int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
@ -217,6 +219,22 @@ int32_t ServiceStub::CmdGetBackupInfo(MessageParcel &data, MessageParcel &reply)
return BError(BError::Codes::OK);
}
int32_t ServiceStub::CmdUpdateTimer(MessageParcel &data, MessageParcel &reply)
{
int ret = ERR_OK;
string bundleName;
if (!data.ReadString(bundleName)) {
return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
}
uint32_t timeOut;
if (!data.ReadUint32(timeOut)) {
return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive timeOut"));
}
bool result;
ret = UpdateTimer(bundleName, timeOut, result);
return BError(BError::Codes::OK);
}
int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply)
{
int res = Release();

View File

@ -311,6 +311,12 @@ void SvcSessionManager::BundleExtTimerStart(const std::string &bundleName, const
{
}
bool SvcSessionManager::UpdateTimer(const std::string &bundleName, uint32_t timeOut,
const Utils::Timer::TimerCallback &callback)
{
return true;
}
void SvcSessionManager::BundleExtTimerStop(const std::string &bundleName) {}
void SvcSessionManager::SetBackupExtName(const string &bundleName, const string &backupExtName) {}

View File

@ -131,6 +131,11 @@ public:
return BError(BError::Codes::OK);
}
ErrCode UpdateTimer(BundleName &bundleName, uint32_t timeOut, bool &result) override
{
return BError(BError::Codes::OK);
}
ErrCode Release() override
{
return BError(BError::Codes::OK);

View File

@ -736,4 +736,27 @@ HWTEST_F(ServiceProxyTest, SUB_Service_proxy_GetBackupInfo_0100, testing::ext::T
EXPECT_EQ(ret, BError(BError::Codes::OK));
GTEST_LOG_(INFO) << "ServiceProxyTest-end SUB_Service_proxy_GetBackupInfo_0100";
}
/**
* @tc.number: SUB_Service_proxy_UpdateTimer_0100
* @tc.name: SUB_Service_proxy_UpdateTimer_0100
* @tc.desc: UpdateTimer
* @tc.size: MEDIUM
* @tc.type: FUNC
* @tc.level Level 1
* @tc.require: I6F3GV
*/
HWTEST_F(ServiceProxyTest, SUB_Service_proxy_UpdateTimer_0100, testing::ext::TestSize.Level1)
{
GTEST_LOG_(INFO) << "ServiceProxyTest-begin SUB_Service_proxy_UpdateTimer_0100";
EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
.Times(1)
.WillOnce(Invoke(mock_.GetRefPtr(), &IServiceMock::InvokeSendRequest));
bool result;
std::string bundleName = "com.example.app2backup";
uint32_t timeOut = 30000;
int32_t ret = proxy_->UpdateTimer(bundleName, timeOut, result);
EXPECT_EQ(ret, BError(BError::Codes::OK));
GTEST_LOG_(INFO) << "ServiceProxyTest-end SUB_Service_proxy_UpdateTimer_0100";
}
} // namespace OHOS::FileManagement::Backup

View File

@ -69,6 +69,7 @@ public:
MOCK_METHOD1(AppIncrementalDone, ErrCode(ErrCode errCode));
MOCK_METHOD2(GetIncrementalFileHandle, ErrCode(const std::string &bundleName, const std::string &fileName));
MOCK_METHOD2(GetBackupInfo, ErrCode(string &bundleName, string &result));
MOCK_METHOD3(UpdateTimer, ErrCode(BundleName &bundleName, uint32_t timeOut, bool &result));
UniqueFd InvokeGetLocalCapabilities()
{
if (bCapabilities_) {
@ -586,4 +587,36 @@ HWTEST_F(ServiceStubTest, SUB_backup_sa_ServiceStub_GetBackupInfo_0100, testing:
}
GTEST_LOG_(INFO) << "ServiceStubTest-end SUB_backup_sa_ServiceStub_GetBackupInfo_0100";
}
/**
* @tc.number: SUB_backup_sa_ServiceStub_UpdateTimer_0100
* @tc.name: SUB_backup_sa_ServiceStub_UpdateTimer_0100
* @tc.desc: Test function of UpdateTimer interface for SUCCESS.
* @tc.size: MEDIUM
* @tc.type: FUNC
* @tc.level Level 0
* @tc.require: I6URNZ
*/
HWTEST_F(ServiceStubTest, SUB_backup_sa_ServiceStub_UpdateTimer_0100, testing::ext::TestSize.Level0)
{
GTEST_LOG_(INFO) << "ServiceStubTest-begin SUB_backup_sa_ServiceStub_UpdateTimer_0100";
try {
MockService service;
EXPECT_CALL(service, UpdateTimer(_, _, _)).WillOnce(Return(BError(BError::Codes::OK)));
MessageParcel data;
MessageParcel reply;
MessageOption option;
std::string bundleName = "com.example.app2backup";
uint32_t timeOut = 30000;
EXPECT_TRUE(data.WriteInterfaceToken(IService::GetDescriptor()));
EXPECT_TRUE(data.WriteString(bundleName));
EXPECT_TRUE(data.WriteUint32(timeOut));
EXPECT_EQ(BError(BError::Codes::OK), service.OnRemoteRequest(
static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_TIMER), data, reply, option));
} catch (...) {
EXPECT_TRUE(false);
GTEST_LOG_(INFO) << "ServiceStubTest-an exception occurred by UpdateTimer.";
}
GTEST_LOG_(INFO) << "ServiceStubTest-end SUB_backup_sa_ServiceStub_UpdateTimer_0100";
}
} // namespace OHOS::FileManagement::Backup

View File

@ -875,4 +875,28 @@ HWTEST_F(ServiceTest, SUB_Service_GetBackupInfo_0100, testing::ext::TestSize.Lev
}
GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_GetBackupInfo_0100";
}
/**
* @tc.number: SUB_Service_UpdateTimer_0100
* @tc.name: SUB_Service_UpdateTimer_0100
* @tc.desc: UpdateTimer
* @tc.size: MEDIUM
* @tc.type: FUNC
* @tc.level Level 1
* @tc.require: I8ZIMJ
*/
HWTEST_F(ServiceTest, SUB_Service_UpdateTimer_0100, testing::ext::TestSize.Level1)
{
GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_UpdateTimer_0100";
try {
std::string bundleName = "com.example.app2backup";
bool result = true;
uint32_t timeOut = 30000;
servicePtr_->UpdateTimer(bundleName, timeOut, result);
} catch (...) {
EXPECT_TRUE(false);
GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by UpdateTimer.";
}
GTEST_LOG_(INFO) << "ServiceTest-end SUB_Service_UpdateTimer_0100";
}
} // namespace OHOS::FileManagement::Backup