0

[PDF Ink Signatures] Move and refactor PrepareReplyMessage()

Move PrepareReplyMessage() to a util file so that it can be used in
other files in future CLs (see https://crrev.com/c/5941005).

Remove a redundant param for setting the reply type. This can already be
retrieved from the `message` param.

Add CHECKs before accessing pointers.

Bug: 373672165
Change-Id: I88aaef1bdf2cde42309b6a4e4a06046fb3e790df
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5941004
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Andy Phan <andyphan@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1370814}
This commit is contained in:
Andy Phan
2024-10-18 20:41:52 +00:00
committed by Chromium LUCI CQ
parent 5c081403d4
commit a6d0515d1a
5 changed files with 84 additions and 22 deletions

@ -92,6 +92,8 @@ if (enable_pdf) {
"draw_utils/shadow.h",
"input_utils.cc",
"input_utils.h",
"message_util.cc",
"message_util.h",
"metrics_handler.cc",
"metrics_handler.h",
"page_orientation.cc",
@ -411,6 +413,7 @@ if (enable_pdf) {
"document_layout_unittest.cc",
"draw_utils/coordinates_unittest.cc",
"input_utils_unittest.cc",
"message_util_unittest.cc",
"page_orientation_unittest.cc",
"paint_manager_unittest.cc",
"parsed_params_unittest.cc",

26
pdf/message_util.cc Normal file

@ -0,0 +1,26 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "pdf/message_util.h"
#include <string>
#include "base/values.h"
namespace chrome_pdf {
base::Value::Dict PrepareReplyMessage(const base::Value::Dict& message) {
const std::string* original_type = message.FindString("type");
CHECK(original_type);
const std::string* message_id = message.FindString("messageId");
CHECK(message_id);
base::Value::Dict reply;
reply.Set("type", *original_type + "Reply");
reply.Set("messageId", *message_id);
return reply;
}
} // namespace chrome_pdf

20
pdf/message_util.h Normal file

@ -0,0 +1,20 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PDF_MESSAGE_UTIL_H_
#define PDF_MESSAGE_UTIL_H_
#include "base/values.h"
namespace chrome_pdf {
// Prepares messages from the plugin that reply to messages from the embedder.
// If the "type" value of `message` is "foo", then the reply "type" will be
// "fooReply". The `message` from the embedder must have a "messageId" value
// that will be copied to the reply message.
base::Value::Dict PrepareReplyMessage(const base::Value::Dict& message);
} // namespace chrome_pdf
#endif // PDF_MESSAGE_UTIL_H_

@ -0,0 +1,29 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "pdf/message_util.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chrome_pdf {
TEST(PrepareReplyMessageTest, BasicReply) {
base::Value::Dict message;
message.Set("type", "typeBasic");
message.Set("messageId", "messageIdBasic");
base::Value::Dict reply = PrepareReplyMessage(message);
const std::string* reply_type = reply.FindString("type");
ASSERT_TRUE(reply_type);
EXPECT_EQ("typeBasicReply", *reply_type);
const std::string* reply_message_id = reply.FindString("messageId");
ASSERT_TRUE(reply_message_id);
EXPECT_EQ("messageIdBasic", *reply_message_id);
}
} // namespace chrome_pdf

@ -58,6 +58,7 @@
#include "pdf/document_layout.h"
#include "pdf/loader/result_codes.h"
#include "pdf/loader/url_loader.h"
#include "pdf/message_util.h"
#include "pdf/metrics_handler.h"
#include "pdf/mojom/pdf.mojom.h"
#include "pdf/paint_manager.h"
@ -255,20 +256,6 @@ bool IsPreviewingPDF(int print_preview_page_count) {
return print_preview_page_count == 0;
}
// Prepares messages from the plugin that reply to messages from the embedder.
// If the "type" value of `message` is "foo", then the `reply_type` must be
// "fooReply". The `message` from the embedder must have a "messageId" value
// that will be copied to the reply message.
base::Value::Dict PrepareReplyMessage(std::string_view reply_type,
const base::Value::Dict& message) {
DCHECK_EQ(reply_type, *message.FindString("type") + "Reply");
base::Value::Dict reply;
reply.Set("type", reply_type);
reply.Set("messageId", *message.FindString("messageId"));
return reply;
}
bool IsSaveDataSizeValid(size_t size) {
return size > 0 && size <= PdfViewWebPlugin::kMaximumSavedFileSize;
}
@ -1460,8 +1447,7 @@ void PdfViewWebPlugin::HandleGetNamedDestinationMessage(
? base::checked_cast<int>(named_destination->page)
: -1;
base::Value::Dict reply =
PrepareReplyMessage("getNamedDestinationReply", message);
base::Value::Dict reply = PrepareReplyMessage(message);
reply.Set("pageNumber", page_number);
if (named_destination.has_value() && !named_destination->view.empty()) {
@ -1483,8 +1469,7 @@ void PdfViewWebPlugin::HandleGetNamedDestinationMessage(
void PdfViewWebPlugin::HandleGetPageBoundingBoxMessage(
const base::Value::Dict& message) {
const int page_index = message.FindInt("page").value();
base::Value::Dict reply =
PrepareReplyMessage("getPageBoundingBoxReply", message);
base::Value::Dict reply = PrepareReplyMessage(message);
PDFiumPage* page = engine_->GetPage(page_index);
CHECK(page);
@ -1514,8 +1499,7 @@ void PdfViewWebPlugin::HandleGetSelectedTextMessage(
std::string selected_text;
base::RemoveChars(engine_->GetSelectedText(), "\r", &selected_text);
base::Value::Dict reply =
PrepareReplyMessage("getSelectedTextReply", message);
base::Value::Dict reply = PrepareReplyMessage(message);
reply.Set("selectedText", selected_text);
client_->PostMessage(std::move(reply));
}
@ -1523,7 +1507,7 @@ void PdfViewWebPlugin::HandleGetSelectedTextMessage(
void PdfViewWebPlugin::HandleGetThumbnailMessage(
const base::Value::Dict& message) {
const int page_index = message.FindInt("pageIndex").value();
base::Value::Dict reply = PrepareReplyMessage("getThumbnailReply", message);
base::Value::Dict reply = PrepareReplyMessage(message);
engine_->RequestThumbnail(
page_index, device_scale_,
@ -1561,7 +1545,7 @@ void PdfViewWebPlugin::HandleSaveAttachmentMessage(
base::Value data_to_save(
IsSaveDataSizeValid(data.size()) ? data : std::vector<uint8_t>());
base::Value::Dict reply = PrepareReplyMessage("saveAttachmentReply", message);
base::Value::Dict reply = PrepareReplyMessage(message);
reply.Set("dataToSave", std::move(data_to_save));
client_->PostMessage(std::move(reply));
}