新增更新打印机信息接口 Signed-off-by:baozewei@huawei.com

Signed-off-by: b30052170 <baozewei@huawei.com>
This commit is contained in:
b30052170 2024-10-31 20:43:20 +08:00
parent efdec8711a
commit 3e9e7578f0
20 changed files with 222 additions and 3 deletions

View File

@ -64,6 +64,7 @@ namespace OHOS::Print {
CMD_ADDPRINTERTODISCOVERY,
CMD_UPDATEPRINTERINDISCOVERY,
CMD_REMOVEPRINTERFROMDISCOVERY,
CMD_UPDATEPRINTERINSYSTEM,
};
} // namespace OHOS:Print
#endif // PRINT_SERVICE_INTERFACE_H

View File

@ -84,6 +84,7 @@ public:
virtual int32_t AddPrinterToDiscovery(const PrinterInfo &printerInfo) = 0;
virtual int32_t UpdatePrinterInDiscovery(const PrinterInfo &printerInfo) = 0;
virtual int32_t RemovePrinterFromDiscovery(const std::string &printerId) = 0;
virtual int32_t UpdatePrinterInSystem(const PrinterInfo &printerInfo) = 0;
};
} // namespace OHOS::Print
#endif // PRINT_SERVICE_INTERFACE_H

View File

@ -83,6 +83,7 @@ public:
int32_t AddPrinterToDiscovery(const PrinterInfo &printerInfo);
int32_t UpdatePrinterInDiscovery(const PrinterInfo &printerInfo);
int32_t RemovePrinterFromDiscovery(const std::string &printerId);
int32_t UpdatePrinterInSystem(const PrinterInfo &printerInfo);
int32_t On(const std::string &taskId, const std::string &type, const sptr<IPrintCallback> &listener);
int32_t Off(const std::string &taskId, const std::string &type);

View File

@ -66,6 +66,7 @@ public:
int32_t AddPrinterToDiscovery(const PrinterInfo &printerInfo) override;
int32_t UpdatePrinterInDiscovery(const PrinterInfo &printerInfo) override;
int32_t RemovePrinterFromDiscovery(const std::string &printerId) override;
int32_t UpdatePrinterInSystem(const PrinterInfo& printerInfo) override;
int32_t QueryPrinterInfoByPrinterId(const std::string &printerId, PrinterInfo &info) override;
int32_t QueryAddedPrinter(std::vector<std::string> &printerNameList) override;

View File

@ -413,6 +413,19 @@ int32_t PrintManagerClient::RemovePrinterFromDiscovery(const std::string &printe
return ret;
}
int32_t PrintManagerClient::UpdatePrinterInSystem(const PrinterInfo &printerInfo)
{
std::lock_guard<std::recursive_mutex> lock(proxyLock_);
printerInfo.Dump();
int32_t ret = E_PRINT_RPC_FAILURE;
if (LoadServer() && GetPrintServiceProxy()) {
ret = printServiceProxy_->UpdatePrinterInSystem(printerInfo);
PRINT_HILOGD("PrintManagerClient UpdatePrinterInSystem out ret = [%{public}d].", ret);
}
return ret;
}
int32_t PrintManagerClient::QueryAllPrintJob(std::vector<PrintJob> &printJobs)
{
std::lock_guard<std::recursive_mutex> lock(proxyLock_);

View File

@ -1004,6 +1004,25 @@ int32_t PrintServiceProxy::RemovePrinterFromDiscovery(const std::string &printer
return ret;
}
int32_t PrintServiceProxy::UpdatePrinterInSystem(const PrinterInfo& printerInfo)
{
MessageParcel data, reply;
MessageOption option;
data.WriteInterfaceToken(GetDescriptor());
printerInfo.Marshalling(data);
PRINT_HILOGD("PrintServiceProxy UpdatePrinterInSystem started.");
sptr<IRemoteObject> remote = Remote();
if (remote == nullptr) {
PRINT_HILOGE("PrintServiceProxy UpdatePrinterInSystem remote is null");
return E_PRINT_RPC_FAILURE;
}
int32_t ret = remote->SendRequest(OHOS::Print::IPrintInterfaceCode::CMD_UPDATEPRINTERINSYSTEM,
data, reply, option);
ret = GetResult(ret, reply);
PRINT_HILOGD("PrintServiceProxy UpdatePrinterInSystem out. ret = [%{public}d]", ret);
return ret;
}
int32_t PrintServiceProxy::UnregisterAllExtCallback(const std::string &extensionId)
{
PRINT_HILOGD("PrintServiceProxy::UnregisterAllExtCallback in");

View File

@ -42,6 +42,7 @@ public:
static napi_value AddPrinterToDiscovery(napi_env env, napi_callback_info info);
static napi_value UpdatePrinterInDiscovery(napi_env env, napi_callback_info info);
static napi_value RemovePrinterFromDiscovery(napi_env env, napi_callback_info info);
static napi_value UpdatePrinterInSystem(napi_env env, napi_callback_info info);
private:
static bool IsValidPrinterState(uint32_t state);

View File

@ -45,6 +45,7 @@ private:
const std::shared_ptr<PrintTaskContext> context);
static napi_value PrintByAdapter(napi_env env, napi_callback_info info);
static napi_value ParsePrintAdapterParameter(napi_env env, size_t argc, napi_value *argv, napi_value self);
static napi_value CreatePrintTask(napi_env env, napi_callback_info info);
private:
static __thread napi_ref globalCtor;

View File

@ -639,6 +639,56 @@ napi_value NapiPrintExt::RemovePrinterFromDiscovery(napi_env env, napi_callback_
return asyncCall.Call(env, exec);
}
napi_value NapiPrintExt::UpdatePrinterInSystem(napi_env env, napi_callback_info info)
{
PRINT_HILOGD("Enter UpdatePrinterInSystem---->");
auto context = std::make_shared<NapiPrintExtContext>();
auto input =
[context](
napi_env env, size_t argc, napi_value* argv, napi_value self, napi_callback_info info) -> napi_status {
PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should have 1 parameter!", napi_invalid_arg);
napi_valuetype valuetype;
napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype);
PRINT_ASSERT_BASE(env, valuetype == napi_object, " parameter is not an object!", napi_invalid_arg);
auto printerInfoPtr = PrinterInfoHelper::BuildFromJs(env, argv[NapiPrintUtils::INDEX_ZERO]);
if (printerInfoPtr == nullptr) {
context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
PRINT_HILOGE("UpdatePrinterInSystem PrinterInfo format error!");
return napi_invalid_arg;
}
context->printerInfos.emplace_back(*printerInfoPtr);
PRINT_HILOGD("UpdatePrinterInSystem printerId = %{public}s", printerInfoPtr->GetPrinterId().c_str());
return napi_ok;
};
auto output = [context](napi_env env, napi_value *result) -> napi_status {
napi_status status = napi_get_boolean(env, context->result, result);
PRINT_HILOGD("output ---- [%{public}s], status[%{public}d]", context->result ? "true" : "false", status);
return status;
};
auto exec = [context](PrintAsyncCall::Context *ctx) {
if (context->printerInfos.empty()) {
context->result = false;
PRINT_HILOGE("UpdatePrinterInSystem printerInfos is empty!");
return;
}
int32_t ret = PrintManagerClient::GetInstance()->UpdatePrinterInSystem(context->printerInfos.front());
context->result = ret == E_PRINT_NONE;
if (ret != E_PRINT_NONE) {
PRINT_HILOGE("UpdatePrinterInSystem Failed to update printer");
}
};
context->SetAction(std::move(input), std::move(output));
PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
return asyncCall.Call(env, exec);
}
bool NapiPrintExt::IsValidPrinterState(uint32_t state)
{
if (state >= PRINTER_ADDED && state < PRINTER_UNKNOWN) {

View File

@ -41,7 +41,12 @@ napi_value NapiPrintTask::Print(napi_env env, napi_callback_info info)
if ((paramCount > NapiPrintUtils::ARGC_THREE) && type == napi_string) {
return PrintByAdapter(env, info);
}
return CreatePrintTask(env, info);
}
napi_value NapiPrintTask::CreatePrintTask(napi_env env, napi_callback_info info)
{
PRINT_HILOGI("CreatePrintTask start ---->");
auto context = std::make_shared<PrintTaskContext>();
auto input = [context](
napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status {

View File

@ -62,6 +62,7 @@ static constexpr const char *FUNCTION_UPDATE_PRINTER_IN_DISCOVERY = "updatePrint
static constexpr const char *FUNCTION_REMOVE_PRINTER_FROM_DISCOVERY = "removePrinterFromDiscovery";
static constexpr const char *FUNCTION_GET_ADDED_PRINTER_INFO_BY_ID = "getAddedPrinterInfoById";
static constexpr const char *FUNCTION_GET_PRINTER_INFORMATION_BY_ID = "getPrinterInformationById";
static constexpr const char *FUNCTION_UPDATE_PRINTER_IN_SYSTEM = "updatePrinterInSystem";
#define PRINT_NAPI_METHOD(name, func) \
{ \
@ -357,6 +358,7 @@ static napi_value Init(napi_env env, napi_value exports)
PRINT_NAPI_METHOD(FUNCTION_REMOVE_PRINTER_FROM_DISCOVERY, NapiPrintExt::RemovePrinterFromDiscovery),
PRINT_NAPI_METHOD(FUNCTION_GET_ADDED_PRINTER_INFO_BY_ID, NapiInnerPrint::GetAddedPrinterInfoById),
PRINT_NAPI_METHOD(FUNCTION_GET_PRINTER_INFORMATION_BY_ID, NapiInnerPrint::GetAddedPrinterInfoById),
PRINT_NAPI_METHOD(FUNCTION_UPDATE_PRINTER_IN_SYSTEM, NapiPrintExt::UpdatePrinterInSystem),
};
napi_status status = napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc);

View File

@ -109,6 +109,7 @@ public:
int32_t AddPrinterToDiscovery(const PrinterInfo &printerInfo) override;
int32_t UpdatePrinterInDiscovery(const PrinterInfo &printerInfo) override;
int32_t RemovePrinterFromDiscovery(const std::string &printerId) override;
int32_t UpdatePrinterInSystem(const PrinterInfo &printerInfo) override;
protected:
void OnStart() override;

View File

@ -76,6 +76,7 @@ private:
bool OnAddPrinterToDiscovery(MessageParcel &data, MessageParcel &reply);
bool OnUpdatePrinterInDiscovery(MessageParcel &data, MessageParcel &reply);
bool OnRemovePrinterFromDiscovery(MessageParcel &data, MessageParcel &reply);
bool OnUpdatePrinterInSystem(MessageParcel &data, MessageParcel &reply);
private:
using PrintCmdHandler = bool (PrintServiceStub::*)(MessageParcel &, MessageParcel &);

View File

@ -2885,6 +2885,24 @@ int32_t PrintServiceAbility::RemovePrinterFromDiscovery(const std::string &print
return result ? E_PRINT_NONE : E_PRINT_INVALID_PRINTER;
}
int32_t PrintServiceAbility::UpdatePrinterInSystem(const PrinterInfo &printerInfo)
{
ManualStart();
if (!CheckPermission(PERMISSION_NAME_PRINT)) {
PRINT_HILOGE("no permission to access print service");
return E_PRINT_NO_PERMISSION;
}
std::lock_guard<std::recursive_mutex> lock(apiMutex_);
if (!UpdatePrinterSystemData(printerInfo)) {
PRINT_HILOGE("UpdatePrinterSystemData failed");
return E_PRINT_INVALID_PARAMETER;
}
printSystemData_.SaveCupsPrinterMap();
return E_PRINT_NONE;
}
void PrintServiceAbility::DeletePrinterFromUserData(const std::string &printerId)
{
std::vector<int32_t> allPrintUserList;

View File

@ -83,6 +83,8 @@ PrintServiceStub::PrintServiceStub()
&PrintServiceStub::OnUpdatePrinterInDiscovery;
cmdMap_[OHOS::Print::IPrintInterfaceCode::CMD_REMOVEPRINTERFROMDISCOVERY] =
&PrintServiceStub::OnRemovePrinterFromDiscovery;
cmdMap_[OHOS::Print::IPrintInterfaceCode::CMD_UPDATEPRINTERINSYSTEM] =
&PrintServiceStub::OnUpdatePrinterInSystem;
}
int32_t PrintServiceStub::OnRemoteRequest(
@ -794,4 +796,23 @@ bool PrintServiceStub::OnRemovePrinterFromDiscovery(MessageParcel &data, Message
return ret == E_PRINT_NONE;
}
bool PrintServiceStub::OnUpdatePrinterInSystem(MessageParcel &data, MessageParcel &reply)
{
PRINT_HILOGI("PrintServiceStub::OnUpdatePrinterInSystem in");
auto infoPtr = PrinterInfo::Unmarshalling(data);
if (infoPtr == nullptr) {
PRINT_HILOGE("Failed to unmarshall printer info");
reply.WriteInt32(E_PRINT_RPC_FAILURE);
return false;
}
infoPtr->Dump();
int32_t ret = UpdatePrinterInSystem(*infoPtr);
reply.WriteInt32(ret);
PRINT_HILOGD("PrintServiceStub::OnUpdatePrinterInSystem out");
return ret == E_PRINT_NONE;
}
} // namespace OHOS::Print

View File

@ -2664,7 +2664,7 @@ HWTEST_F(PrintManagerClientTest, PrintManagerClientTest_0157, TestSize.Level1)
}
/**
* @tc.name: PrintManagerClientTest_0023
* @tc.name: PrintManagerClientTest_0158
* @tc.desc: AddPrinterToDiscovery failed case.
* @tc.type: FUNC
* @tc.require:
@ -2679,7 +2679,7 @@ HWTEST_F(PrintManagerClientTest, PrintManagerClientTest_0158, TestSize.Level1)
}
/**
* @tc.name: PrintManagerClientTest_0023
* @tc.name: PrintManagerClientTest_0159
* @tc.desc: UpdatePrinterInDiscovery failed case.
* @tc.type: FUNC
* @tc.require:
@ -2695,7 +2695,7 @@ HWTEST_F(PrintManagerClientTest, PrintManagerClientTest_0159, TestSize.Level1)
/**
* @tc.name: PrintManagerClientTest_0160
* @tc.desc: UpdatePrinterInDiscovery failed case.
* @tc.desc: RemovePrinterFromDiscovery failed case.
* @tc.type: FUNC
* @tc.require:
*/
@ -2708,5 +2708,20 @@ HWTEST_F(PrintManagerClientTest, PrintManagerClientTest_0160, TestSize.Level1)
EXPECT_EQ(ret, E_PRINT_NO_PERMISSION);
}
/**
* @tc.name: PrintManagerClientTest_0161
* @tc.desc: UpdatePrinterInSystem failed case.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(PrintManagerClientTest, PrintManagerClientTest_0161, TestSize.Level1)
{
PrinterInfo info;
PrintManagerClient::GetInstance()->LoadServerFail();
PrintManagerClient::GetInstance()->ResetProxy();
int32_t ret = PrintManagerClient::GetInstance()->UpdatePrinterInSystem(info);
EXPECT_EQ(ret, E_PRINT_NO_PERMISSION);
}
} // namespace Print
} // namespace OHOS

View File

@ -940,5 +940,29 @@ HWTEST_F(PrintServiceProxyTest, PrintServiceProxyTest_0032, TestSize.Level1)
proxy->RemovePrinterFromDiscovery(testPrinterId);
}
HWTEST_F(PrintServiceProxyTest, PrintServiceProxyTest_0031, TestSize.Level1)
{
OHOS::Print::PrinterInfo testInfo1;
testInfo1.SetOption("option-1");
sptr<MockRemoteObject> obj = new MockRemoteObject();
EXPECT_NE(obj, nullptr);
auto proxy = std::make_shared<PrintServiceProxy>(obj);
EXPECT_NE(proxy, nullptr);
auto service = std::make_shared<MockPrintService>();
EXPECT_NE(service, nullptr);
EXPECT_CALL(*service, UpdatePrinterInSystem(_)).Times(Exactly(1)).WillOnce(
[&testInfo1](const PrinterInfo &printerInfo) {
EXPECT_EQ(testInfo1.GetOption(), printerInfo.GetOption());
return E_PRINT_NONE;
});
EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
ON_CALL(*obj, SendRequest)
.WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
service->OnRemoteRequest(code, data, reply, option);
return E_PRINT_NONE;
});
proxy->UpdatePrinterInSystem(testInfo1);
}
} // namespace Print
} // namespace OHOS

View File

@ -2145,4 +2145,17 @@ HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0136, TestSize.Level1)
EXPECT_TRUE(service->RenamePrinterWhenAdded(info1) == "fwk.driver 1");
service->RemoveVendorPrinterFromCups(vendorName, printerId);
}
HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0137, TestSize.Level1)
{
auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
PrinterInfo info;
info.SetPrinterId(DEFAULT_EXT_PRINTER_ID);
EXPECT_EQ(service->UpdatePrinterInSystem(info), E_PRINT_INVALID_PARAMETER);
nlohmann::json infoJson;
infoJson["alias"] = "testPrinterName";
info.SetOption(infoJson.dump());
EXPECT_EQ(service->UpdatePrinterInSystem(info), E_PRINT_NONE);
}
} // namespace OHOS::Print

View File

@ -210,6 +210,10 @@ public:
{
return E_PRINT_NONE;
}
int32_t UpdatePrinterInSystem(const PrinterInfo& printerInfo) override
{
return E_PRINT_NONE;
}
};
class MockPrintService final : public DummyPrintServiceStub {
@ -247,6 +251,7 @@ public:
MOCK_METHOD1(AddPrinterToDiscovery, int32_t(const PrinterInfo&));
MOCK_METHOD1(UpdatePrinterInDiscovery, int32_t(const PrinterInfo&));
MOCK_METHOD1(RemovePrinterFromDiscovery, int32_t(const std::string&));
MOCK_METHOD1(UpdatePrinterInSystem, int32_t(const PrinterInfo&));
};
} // namespace Print
} // namespace OHOS

View File

@ -1445,5 +1445,31 @@ HWTEST_F(PrintServiceStubTest, PrintServiceStubTest_0062, TestSize.Level1)
EXPECT_TRUE(static_cast<bool>(stub->OnRemoteRequest(code, data, reply, option)));
}
/**
* @tc.name: PrintServiceStubTest_0063
* @tc.desc: Verify the capability function.
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(PrintServiceStubTest, PrintServiceStubTest_0063, TestSize.Level1)
{
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_SYNC);
uint32_t code = static_cast<uint32_t>(CMD_UPDATEPRINTERINSYSTEM);
PrinterInfo testInfo;
std::string testPrinterId = "com.sample.ext:1";
testInfo.SetPrinterId(testPrinterId);
EXPECT_TRUE(data.WriteInterfaceToken(IPrintCallback::GetDescriptor()));
testInfo.Marshalling(data);
auto stub = std::make_shared<MockPrintService>();
EXPECT_NE(stub, nullptr);
ON_CALL(*stub, UpdatePrinterInSystem).WillByDefault(Return(E_PRINT_NONE));
EXPECT_TRUE(static_cast<bool>(stub->OnRemoteRequest(code, data, reply, option)));
}
} // namespace Print
} // namespace OHOS