0

Switch PdfViewPluginBase::HandleMessage() to use a base::Value::Dict.

Make it clear that it is always dealing with a dictionary, and not some
other type of base::Value. Then recursively convert its callers and
callees to use base::Value::Dict as well. Switching to using
base::Value::Dict APIs removes a bunch of deprecated base::Value API
usage.

Bug: 1303949
Change-Id: I63cfdfd09a2ac2c5f24c2257c93ba41622abc69f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3580051
Reviewed-by: K. Moon <kmoon@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#991252}
This commit is contained in:
Lei Zhang
2022-04-11 22:58:51 +00:00
committed by Chromium LUCI CQ
parent 336d30d371
commit 277b156cff
11 changed files with 165 additions and 148 deletions

@ -50,26 +50,23 @@ DocumentLayout::Options& DocumentLayout::Options::operator=(
DocumentLayout::Options::~Options() = default;
base::Value DocumentLayout::Options::ToValue() const {
base::Value dictionary(base::Value::Type::DICTIONARY);
dictionary.SetIntKey(kDirection, direction_);
dictionary.SetIntKey(kDefaultPageOrientation,
static_cast<int32_t>(default_page_orientation_));
dictionary.SetBoolKey(kTwoUpViewEnabled,
page_spread_ == PageSpread::kTwoUpOdd);
base::Value::Dict DocumentLayout::Options::ToValue() const {
base::Value::Dict dictionary;
dictionary.Set(kDirection, direction_);
dictionary.Set(kDefaultPageOrientation,
static_cast<int>(default_page_orientation_));
dictionary.Set(kTwoUpViewEnabled, page_spread_ == PageSpread::kTwoUpOdd);
return dictionary;
}
void DocumentLayout::Options::FromValue(const base::Value& value) {
DCHECK(value.is_dict());
int32_t direction = value.FindIntKey(kDirection).value();
void DocumentLayout::Options::FromValue(const base::Value::Dict& value) {
int32_t direction = value.FindInt(kDirection).value();
DCHECK_GE(direction, base::i18n::UNKNOWN_DIRECTION);
DCHECK_LE(direction, base::i18n::TEXT_DIRECTION_MAX);
direction_ = static_cast<base::i18n::TextDirection>(direction);
int32_t default_page_orientation =
value.FindIntKey(kDefaultPageOrientation).value();
value.FindInt(kDefaultPageOrientation).value();
DCHECK_GE(default_page_orientation,
static_cast<int32_t>(PageOrientation::kOriginal));
DCHECK_LE(default_page_orientation,
@ -77,7 +74,7 @@ void DocumentLayout::Options::FromValue(const base::Value& value) {
default_page_orientation_ =
static_cast<PageOrientation>(default_page_orientation);
page_spread_ = value.FindBoolKey(kTwoUpViewEnabled).value()
page_spread_ = value.FindBool(kTwoUpViewEnabled).value()
? PageSpread::kTwoUpOdd
: PageSpread::kOneUp;
}

@ -10,15 +10,12 @@
#include "base/check_op.h"
#include "base/i18n/rtl.h"
#include "base/values.h"
#include "pdf/draw_utils/coordinates.h"
#include "pdf/page_orientation.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
namespace base {
class Value;
}
namespace chrome_pdf {
// Layout of pages within a PDF document. Pages are placed as rectangles
@ -56,11 +53,11 @@ class DocumentLayout final {
return !(lhs == rhs);
}
// Serializes layout options to a base::Value.
base::Value ToValue() const;
// Serializes layout options to a base::Value::Dict.
base::Value::Dict ToValue() const;
// Deserializes layout options from a base::Value.
void FromValue(const base::Value& value);
// Deserializes layout options from a base::Value::Dict.
void FromValue(const base::Value::Dict& value);
// Page layout direction. This is tied to the direction of the user's UI,
// rather than the direction of individual pages.

@ -111,7 +111,7 @@ TEST_F(DocumentLayoutOptionsTest, NotEquals) {
}
TEST_F(DocumentLayoutOptionsTest, ToValueDefault) {
base::Value value = options_.ToValue();
base::Value value(options_.ToValue());
EXPECT_THAT(value, base::test::IsJson(R"({
"direction": 0,
@ -124,7 +124,7 @@ TEST_F(DocumentLayoutOptionsTest, ToValueModified) {
options_.set_direction(base::i18n::LEFT_TO_RIGHT);
options_.RotatePagesClockwise();
options_.set_page_spread(DocumentLayout::PageSpread::kTwoUpOdd);
base::Value value = options_.ToValue();
base::Value value(options_.ToValue());
EXPECT_THAT(value, base::test::IsJson(R"({
"direction": 2,
@ -134,21 +134,23 @@ TEST_F(DocumentLayoutOptionsTest, ToValueModified) {
}
TEST_F(DocumentLayoutOptionsTest, FromValueDefault) {
options_.FromValue(base::test::ParseJson(R"({
base::Value value = base::test::ParseJson(R"({
"direction": 0,
"defaultPageOrientation": 0,
"twoUpViewEnabled": false,
})"));
})");
options_.FromValue(value.GetDict());
EXPECT_EQ(options_, DocumentLayout::Options());
}
TEST_F(DocumentLayoutOptionsTest, FromValueModified) {
options_.FromValue(base::test::ParseJson(R"({
base::Value value = base::test::ParseJson(R"({
"direction": 2,
"defaultPageOrientation": 1,
"twoUpViewEnabled": true,
})"));
})");
options_.FromValue(value.GetDict());
EXPECT_EQ(options_.direction(), base::i18n::LEFT_TO_RIGHT);
EXPECT_EQ(options_.default_page_orientation(), PageOrientation::kClockwise90);

@ -104,13 +104,13 @@ enum class PinchPhase {
// "fooReply". The `message` from the embedder must have a "messageId" value
// that will be copied to the reply message.
base::Value PrepareReplyMessage(base::StringPiece reply_type,
const base::Value& message) {
DCHECK_EQ(reply_type, *message.FindStringKey("type") + "Reply");
const base::Value::Dict& message) {
DCHECK_EQ(reply_type, *message.FindString("type") + "Reply");
base::Value reply(base::Value::Type::DICTIONARY);
reply.SetStringKey("type", reply_type);
reply.SetStringKey("messageId", *message.FindStringKey("messageId"));
return reply;
base::Value::Dict reply;
reply.Set("type", reply_type);
reply.Set("messageId", *message.FindString("messageId"));
return base::Value(std::move(reply));
}
bool IsPrintPreviewUrl(base::StringPiece url) {
@ -198,7 +198,7 @@ void PdfViewPluginBase::ProposeDocumentLayout(const DocumentLayout& layout) {
message.SetStringKey("type", "documentDimensions");
message.SetIntKey("width", layout.size().width());
message.SetIntKey("height", layout.size().height());
message.SetKey("layoutOptions", layout.options().ToValue());
message.SetKey("layoutOptions", base::Value(layout.options().ToValue()));
base::Value page_dimensions_list(base::Value::Type::LIST);
for (size_t i = 0; i < layout.page_count(); ++i)
page_dimensions_list.Append(base::Value(DictFromRect(layout.page_rect(i))));
@ -548,8 +548,8 @@ bool PdfViewPluginBase::HandleInputEvent(const blink::WebInputEvent& event) {
return event_to_handle.GetType() == blink::WebInputEvent::Type::kMouseDown;
}
void PdfViewPluginBase::HandleMessage(const base::Value& message) {
using MessageHandler = void (PdfViewPluginBase::*)(const base::Value&);
void PdfViewPluginBase::HandleMessage(const base::Value::Dict& message) {
using MessageHandler = void (PdfViewPluginBase::*)(const base::Value::Dict&);
static constexpr auto kMessageHandlers =
base::MakeFixedFlatMap<base::StringPiece, MessageHandler>({
{"displayAnnotations",
@ -578,7 +578,7 @@ void PdfViewPluginBase::HandleMessage(const base::Value& message) {
{"viewport", &PdfViewPluginBase::HandleViewportMessage},
});
MessageHandler handler = kMessageHandlers.at(*message.FindStringKey("type"));
MessageHandler handler = kMessageHandlers.at(*message.FindString("type"));
(this->*handler)(message);
}
@ -1040,14 +1040,14 @@ base::Value::DictStorage PdfViewPluginBase::DictFromRect(
}
void PdfViewPluginBase::HandleDisplayAnnotationsMessage(
const base::Value& message) {
engine()->DisplayAnnotations(message.FindBoolKey("display").value());
const base::Value::Dict& message) {
engine()->DisplayAnnotations(message.FindBool("display").value());
}
void PdfViewPluginBase::HandleGetNamedDestinationMessage(
const base::Value& message) {
const base::Value::Dict& message) {
absl::optional<PDFEngine::NamedDestination> named_destination =
engine()->GetNamedDestination(*message.FindStringKey("namedDestination"));
engine()->GetNamedDestination(*message.FindString("namedDestination"));
const int page_number = named_destination.has_value()
? base::checked_cast<int>(named_destination->page)
@ -1073,13 +1073,13 @@ void PdfViewPluginBase::HandleGetNamedDestinationMessage(
}
void PdfViewPluginBase::HandleGetPasswordCompleteMessage(
const base::Value& message) {
const base::Value::Dict& message) {
DCHECK(password_callback_);
std::move(password_callback_).Run(*message.FindStringKey("password"));
std::move(password_callback_).Run(*message.FindString("password"));
}
void PdfViewPluginBase::HandleGetSelectedTextMessage(
const base::Value& message) {
const base::Value::Dict& message) {
// Always return unix newlines to JavaScript.
std::string selected_text;
base::RemoveChars(engine()->GetSelectedText(), "\r", &selected_text);
@ -1089,8 +1089,9 @@ void PdfViewPluginBase::HandleGetSelectedTextMessage(
SendMessage(std::move(reply));
}
void PdfViewPluginBase::HandleGetThumbnailMessage(const base::Value& message) {
const int page_index = message.FindIntKey("page").value();
void PdfViewPluginBase::HandleGetThumbnailMessage(
const base::Value::Dict& message) {
const int page_index = message.FindInt("page").value();
base::Value reply = PrepareReplyMessage("getThumbnailReply", message);
engine()->RequestThumbnail(page_index, device_scale_,
@ -1099,9 +1100,9 @@ void PdfViewPluginBase::HandleGetThumbnailMessage(const base::Value& message) {
}
void PdfViewPluginBase::HandleLoadPreviewPageMessage(
const base::Value& message) {
const std::string& url = *message.FindStringKey("url");
int index = message.FindIntKey("index").value();
const base::Value::Dict& message) {
const std::string& url = *message.FindString("url");
int index = message.FindInt("index").value();
// For security reasons, crash if `url` is not for Print Preview.
CHECK(IsPrintPreview());
@ -1109,15 +1110,16 @@ void PdfViewPluginBase::HandleLoadPreviewPageMessage(
ProcessPreviewPageInfo(url, index);
}
void PdfViewPluginBase::HandlePrintMessage(const base::Value& /*message*/) {
void PdfViewPluginBase::HandlePrintMessage(
const base::Value::Dict& /*message*/) {
Print();
}
void PdfViewPluginBase::HandleResetPrintPreviewModeMessage(
const base::Value& message) {
const std::string& url = *message.FindStringKey("url");
bool is_grayscale = message.FindBoolKey("grayscale").value();
int print_preview_page_count = message.FindIntKey("pageCount").value();
const base::Value::Dict& message) {
const std::string& url = *message.FindString("url");
bool is_grayscale = message.FindBool("grayscale").value();
int print_preview_page_count = message.FindInt("pageCount").value();
// For security reasons, crash if `url` is not for Print Preview.
CHECK(IsPrintPreview());
@ -1156,18 +1158,18 @@ void PdfViewPluginBase::HandleResetPrintPreviewModeMessage(
}
void PdfViewPluginBase::HandleRotateClockwiseMessage(
const base::Value& /*message*/) {
const base::Value::Dict& /*message*/) {
engine()->RotateClockwise();
}
void PdfViewPluginBase::HandleRotateCounterclockwiseMessage(
const base::Value& /*message*/) {
const base::Value::Dict& /*message*/) {
engine()->RotateCounterclockwise();
}
void PdfViewPluginBase::HandleSaveMessage(const base::Value& message) {
const std::string& token = *message.FindStringKey("token");
int request_type = message.FindIntKey("saveRequestType").value();
void PdfViewPluginBase::HandleSaveMessage(const base::Value::Dict& message) {
const std::string& token = *message.FindString("token");
int request_type = message.FindInt("saveRequestType").value();
DCHECK_GE(request_type, static_cast<int>(SaveRequestType::kAnnotation));
DCHECK_LE(request_type, static_cast<int>(SaveRequestType::kEdited));
@ -1194,8 +1196,8 @@ void PdfViewPluginBase::HandleSaveMessage(const base::Value& message) {
}
void PdfViewPluginBase::HandleSaveAttachmentMessage(
const base::Value& message) {
const int index = message.FindIntKey("attachmentIndex").value();
const base::Value::Dict& message) {
const int index = message.FindInt("attachmentIndex").value();
const std::vector<DocumentAttachmentInfo>& list =
engine()->GetDocumentAttachmentInfoList();
@ -1213,32 +1215,36 @@ void PdfViewPluginBase::HandleSaveAttachmentMessage(
SendMessage(std::move(reply));
}
void PdfViewPluginBase::HandleSelectAllMessage(const base::Value& /*message*/) {
void PdfViewPluginBase::HandleSelectAllMessage(
const base::Value::Dict& /*message*/) {
engine()->SelectAll();
}
void PdfViewPluginBase::HandleSetBackgroundColorMessage(
const base::Value& message) {
const base::Value::Dict& message) {
background_color_ =
base::checked_cast<SkColor>(message.FindDoubleKey("color").value());
base::checked_cast<SkColor>(message.FindDouble("color").value());
}
void PdfViewPluginBase::HandleSetReadOnlyMessage(const base::Value& message) {
engine()->SetReadOnly(message.FindBoolKey("enableReadOnly").value());
void PdfViewPluginBase::HandleSetReadOnlyMessage(
const base::Value::Dict& message) {
engine()->SetReadOnly(message.FindBool("enableReadOnly").value());
}
void PdfViewPluginBase::HandleSetTwoUpViewMessage(const base::Value& message) {
engine()->SetTwoUpView(message.FindBoolKey("enableTwoUpView").value());
void PdfViewPluginBase::HandleSetTwoUpViewMessage(
const base::Value::Dict& message) {
engine()->SetTwoUpView(message.FindBool("enableTwoUpView").value());
}
void PdfViewPluginBase::HandleStopScrollingMessage(
const base::Value& /*message*/) {
const base::Value::Dict& /*message*/) {
stop_scrolling_ = true;
}
void PdfViewPluginBase::HandleViewportMessage(const base::Value& message) {
const base::Value* layout_options_value =
message.FindDictKey("layoutOptions");
void PdfViewPluginBase::HandleViewportMessage(
const base::Value::Dict& message) {
const base::Value::Dict* layout_options_value =
message.FindDict("layoutOptions");
if (layout_options_value) {
DocumentLayout::Options layout_options;
layout_options.FromValue(*layout_options_value);
@ -1259,11 +1265,11 @@ void PdfViewPluginBase::HandleViewportMessage(const base::Value& message) {
}
}
gfx::Vector2dF scroll_offset(*message.FindDoubleKey("xOffset"),
*message.FindDoubleKey("yOffset"));
double new_zoom = *message.FindDoubleKey("zoom");
gfx::Vector2dF scroll_offset(*message.FindDouble("xOffset"),
*message.FindDouble("yOffset"));
double new_zoom = *message.FindDouble("zoom");
const PinchPhase pinch_phase =
static_cast<PinchPhase>(*message.FindIntKey("pinchPhase"));
static_cast<PinchPhase>(*message.FindInt("pinchPhase"));
received_viewport_message_ = true;
stop_scrolling_ = false;
@ -1284,14 +1290,14 @@ void PdfViewPluginBase::HandleViewportMessage(const base::Value& message) {
if (pinch_phase == PinchPhase::kUpdateZoomIn ||
(pinch_phase == PinchPhase::kUpdateZoomOut && zoom_ratio > 1.0)) {
// Get the coordinates of the center of the pinch gesture.
const double pinch_x = *message.FindDoubleKey("pinchX");
const double pinch_y = *message.FindDoubleKey("pinchY");
const double pinch_x = *message.FindDouble("pinchX");
const double pinch_y = *message.FindDouble("pinchY");
gfx::Point pinch_center(pinch_x, pinch_y);
// Get the pinch vector which represents the panning caused by the change in
// pinch center between the start and the end of the gesture.
const double pinch_vector_x = *message.FindDoubleKey("pinchVectorX");
const double pinch_vector_y = *message.FindDoubleKey("pinchVectorY");
const double pinch_vector_x = *message.FindDouble("pinchVectorX");
const double pinch_vector_y = *message.FindDouble("pinchVectorY");
gfx::Vector2d pinch_vector =
gfx::Vector2d(pinch_vector_x * zoom_ratio, pinch_vector_y * zoom_ratio);
@ -1353,7 +1359,7 @@ void PdfViewPluginBase::HandleViewportMessage(const base::Value& message) {
// Bound the input parameters.
new_zoom = std::max(kMinZoom, new_zoom);
DCHECK(message.FindBoolKey("userInitiated").has_value());
DCHECK(message.FindBool("userInitiated").has_value());
SetZoom(new_zoom);
UpdateScroll(GetScrollPositionFromOffset(scroll_offset));

@ -230,7 +230,7 @@ class PdfViewPluginBase : public PDFEngine::Client,
bool HandleInputEvent(const blink::WebInputEvent& event);
// Handles `postMessage()` calls from the embedder.
void HandleMessage(const base::Value& message);
void HandleMessage(const base::Value::Dict& message);
// Enqueues a "message" event carrying `message` to the embedder. Messages are
// guaranteed to be received in the order that they are sent. This method is
@ -416,24 +416,25 @@ class PdfViewPluginBase : public PDFEngine::Client,
const gfx::Vector2dF& scroll_offset) const;
// Message handlers.
void HandleDisplayAnnotationsMessage(const base::Value& message);
void HandleGetNamedDestinationMessage(const base::Value& message);
void HandleGetPasswordCompleteMessage(const base::Value& message);
void HandleGetSelectedTextMessage(const base::Value& message);
void HandleGetThumbnailMessage(const base::Value& message);
void HandleLoadPreviewPageMessage(const base::Value& message);
void HandlePrintMessage(const base::Value& /*message*/);
void HandleResetPrintPreviewModeMessage(const base::Value& message);
void HandleRotateClockwiseMessage(const base::Value& /*message*/);
void HandleRotateCounterclockwiseMessage(const base::Value& /*message*/);
void HandleSaveMessage(const base::Value& message);
void HandleSaveAttachmentMessage(const base::Value& message);
void HandleSelectAllMessage(const base::Value& /*message*/);
void HandleSetBackgroundColorMessage(const base::Value& message);
void HandleSetReadOnlyMessage(const base::Value& message);
void HandleSetTwoUpViewMessage(const base::Value& message);
void HandleStopScrollingMessage(const base::Value& /*message*/);
void HandleViewportMessage(const base::Value& message);
void HandleDisplayAnnotationsMessage(const base::Value::Dict& message);
void HandleGetNamedDestinationMessage(const base::Value::Dict& message);
void HandleGetPasswordCompleteMessage(const base::Value::Dict& message);
void HandleGetSelectedTextMessage(const base::Value::Dict& message);
void HandleGetThumbnailMessage(const base::Value::Dict& message);
void HandleLoadPreviewPageMessage(const base::Value::Dict& message);
void HandlePrintMessage(const base::Value::Dict& /*message*/);
void HandleResetPrintPreviewModeMessage(const base::Value::Dict& message);
void HandleRotateClockwiseMessage(const base::Value::Dict& /*message*/);
void HandleRotateCounterclockwiseMessage(
const base::Value::Dict& /*message*/);
void HandleSaveMessage(const base::Value::Dict& message);
void HandleSaveAttachmentMessage(const base::Value::Dict& message);
void HandleSelectAllMessage(const base::Value::Dict& /*message*/);
void HandleSetBackgroundColorMessage(const base::Value::Dict& message);
void HandleSetReadOnlyMessage(const base::Value::Dict& message);
void HandleSetTwoUpViewMessage(const base::Value::Dict& message);
void HandleStopScrollingMessage(const base::Value::Dict& /*message*/);
void HandleViewportMessage(const base::Value::Dict& message);
// Sends start/stop loading notifications to the plugin's render frame
// depending on `did_call_start_loading_`.

@ -208,12 +208,13 @@ base::Value CreateExpectedFormTextFieldFocusChangeResponse() {
return message;
}
base::Value CreateSaveRequestMessage(PdfViewPluginBase::SaveRequestType type,
const std::string& token) {
base::Value message(base::Value::Type::DICTIONARY);
message.SetStringKey("type", "save");
message.SetIntKey("saveRequestType", static_cast<int>(type));
message.SetStringKey("token", token);
base::Value::Dict CreateSaveRequestMessage(
PdfViewPluginBase::SaveRequestType type,
const std::string& token) {
base::Value::Dict message;
message.Set("type", "save");
message.Set("saveRequestType", static_cast<int>(type));
message.Set("token", token);
return message;
}
@ -252,7 +253,7 @@ class PdfViewPluginBaseWithEngineTest : public PdfViewPluginBaseTest {
protected:
void SendDefaultViewportMessage() {
fake_plugin_.HandleMessage(base::test::ParseJson(R"({
base::Value message = base::test::ParseJson(R"({
"type": "viewport",
"userInitiated": false,
"zoom": 1,
@ -264,7 +265,8 @@ class PdfViewPluginBaseWithEngineTest : public PdfViewPluginBaseTest {
"xOffset": 0,
"yOffset": 0,
"pinchPhase": 0,
})"));
})");
fake_plugin_.HandleMessage(message.GetDict());
}
};
@ -613,7 +615,7 @@ TEST_F(PdfViewPluginBaseSaveTest, SaveAnnotationInNonEditMode) {
static constexpr char kSaveAnnotInNonEditModeToken[] =
"save-annot-in-non-edit-mode-token";
base::Value message =
base::Value::Dict message =
CreateSaveRequestMessage(PdfViewPluginBase::SaveRequestType::kAnnotation,
kSaveAnnotInNonEditModeToken);
base::Value expected_response =
@ -633,7 +635,7 @@ TEST_F(PdfViewPluginBaseSaveTest, SaveAnnotationInEditMode) {
static constexpr char kSaveAnnotInEditModeToken[] =
"save-annot-in-edit-mode-token";
base::Value message =
base::Value::Dict message =
CreateSaveRequestMessage(PdfViewPluginBase::SaveRequestType::kAnnotation,
kSaveAnnotInEditModeToken);
base::Value expected_response =
@ -653,7 +655,7 @@ TEST_F(PdfViewPluginBaseSaveTest, SaveOriginalInNonEditMode) {
static constexpr char kSaveOriginalInNonEditModeToken[] =
"save-original-in-non-edit-mode-token";
base::Value message =
base::Value::Dict message =
CreateSaveRequestMessage(PdfViewPluginBase::SaveRequestType::kOriginal,
kSaveOriginalInNonEditModeToken);
base::Value expected_response =
@ -674,7 +676,7 @@ TEST_F(PdfViewPluginBaseSaveTest, SaveOriginalInEditMode) {
static constexpr char kSaveOriginalInEditModeToken[] =
"save-original-in-edit-mode-token";
base::Value message =
base::Value::Dict message =
CreateSaveRequestMessage(PdfViewPluginBase::SaveRequestType::kOriginal,
kSaveOriginalInEditModeToken);
base::Value expected_response =
@ -696,7 +698,7 @@ TEST_F(PdfViewPluginBaseSaveTest, SaveEditedInNonEditMode) {
static constexpr char kSaveEditedInNonEditModeToken[] =
"save-edited-in-non-edit-mode";
base::Value message =
base::Value::Dict message =
CreateSaveRequestMessage(PdfViewPluginBase::SaveRequestType::kEdited,
kSaveEditedInNonEditModeToken);
base::Value expected_response =
@ -716,7 +718,7 @@ TEST_F(PdfViewPluginBaseSaveTest, SaveEditedInEditMode) {
static constexpr char kSaveEditedInEditModeToken[] =
"save-edited-in-edit-mode-token";
base::Value message = CreateSaveRequestMessage(
base::Value::Dict message = CreateSaveRequestMessage(
PdfViewPluginBase::SaveRequestType::kEdited, kSaveEditedInEditModeToken);
base::Value expected_response =
CreateExpectedSaveToBufferResponse(kSaveEditedInEditModeToken,
@ -732,9 +734,9 @@ TEST_F(PdfViewPluginBaseTest, HandleSetBackgroundColorMessage) {
const SkColor kNewBackgroundColor = SK_ColorGREEN;
ASSERT_NE(kNewBackgroundColor, fake_plugin_.GetBackgroundColor());
base::Value message(base::Value::Type::DICTIONARY);
message.SetStringKey("type", "setBackgroundColor");
message.SetDoubleKey("color", kNewBackgroundColor);
base::Value::Dict message;
message.Set("type", "setBackgroundColor");
message.Set("color", static_cast<double>(kNewBackgroundColor));
fake_plugin_.HandleMessage(message);
EXPECT_EQ(kNewBackgroundColor, fake_plugin_.GetBackgroundColor());
@ -745,7 +747,7 @@ TEST_F(PdfViewPluginBaseWithEngineTest,
auto* engine = static_cast<TestPDFiumEngine*>(fake_plugin_.engine());
EXPECT_CALL(*engine, ApplyDocumentLayout(DocumentLayout::Options()));
fake_plugin_.HandleMessage(base::test::ParseJson(R"({
base::Value message = base::test::ParseJson(R"({
"type": "viewport",
"userInitiated": false,
"zoom": 1,
@ -757,7 +759,8 @@ TEST_F(PdfViewPluginBaseWithEngineTest,
"xOffset": 0,
"yOffset": 0,
"pinchPhase": 0,
})"));
})");
fake_plugin_.HandleMessage(message.GetDict());
EXPECT_THAT(fake_plugin_.sent_messages(), IsEmpty());
}
@ -770,7 +773,7 @@ TEST_F(PdfViewPluginBaseWithEngineTest,
fake_plugin_.DocumentLoadComplete();
fake_plugin_.clear_sent_messages();
fake_plugin_.HandleMessage(base::test::ParseJson(R"({
base::Value message = base::test::ParseJson(R"({
"type": "viewport",
"userInitiated": false,
"zoom": 1,
@ -782,7 +785,8 @@ TEST_F(PdfViewPluginBaseWithEngineTest,
"xOffset": 0,
"yOffset": 0,
"pinchPhase": 0,
})"));
})");
fake_plugin_.HandleMessage(message.GetDict());
EXPECT_THAT(fake_plugin_.sent_messages(), ElementsAre(base::test::IsJson(R"({
"type": "loadProgress",
@ -793,7 +797,7 @@ TEST_F(PdfViewPluginBaseWithEngineTest,
TEST_F(PdfViewPluginBaseWithEngineTest, HandleViewportMessageSubsequently) {
auto* engine = static_cast<TestPDFiumEngine*>(fake_plugin_.engine());
fake_plugin_.HandleMessage(base::test::ParseJson(R"({
base::Value message1 = base::test::ParseJson(R"({
"type": "viewport",
"userInitiated": false,
"zoom": 1,
@ -805,14 +809,15 @@ TEST_F(PdfViewPluginBaseWithEngineTest, HandleViewportMessageSubsequently) {
"xOffset": 0,
"yOffset": 0,
"pinchPhase": 0,
})"));
})");
fake_plugin_.HandleMessage(message1.GetDict());
fake_plugin_.clear_sent_messages();
DocumentLayout::Options two_up_options;
two_up_options.set_page_spread(DocumentLayout::PageSpread::kTwoUpOdd);
EXPECT_CALL(*engine, ApplyDocumentLayout(two_up_options));
fake_plugin_.HandleMessage(base::test::ParseJson(R"({
base::Value message2 = base::test::ParseJson(R"({
"type": "viewport",
"userInitiated": false,
"zoom": 1,
@ -824,7 +829,8 @@ TEST_F(PdfViewPluginBaseWithEngineTest, HandleViewportMessageSubsequently) {
"xOffset": 0,
"yOffset": 0,
"pinchPhase": 0,
})"));
})");
fake_plugin_.HandleMessage(message2.GetDict());
EXPECT_THAT(fake_plugin_.sent_messages(), IsEmpty());
}
@ -836,7 +842,7 @@ TEST_F(PdfViewPluginBaseWithEngineTest, HandleViewportMessageScroll) {
EXPECT_CALL(*engine, ScrolledToXPosition(2));
EXPECT_CALL(*engine, ScrolledToYPosition(3));
fake_plugin_.HandleMessage(base::test::ParseJson(R"({
base::Value message = base::test::ParseJson(R"({
"type": "viewport",
"userInitiated": false,
"zoom": 1,
@ -848,7 +854,8 @@ TEST_F(PdfViewPluginBaseWithEngineTest, HandleViewportMessageScroll) {
"xOffset": 2,
"yOffset": 3,
"pinchPhase": 0,
})"));
})");
fake_plugin_.HandleMessage(message.GetDict());
}
TEST_F(PdfViewPluginBaseWithEngineTest,
@ -859,7 +866,7 @@ TEST_F(PdfViewPluginBaseWithEngineTest,
EXPECT_CALL(*engine, ScrolledToXPosition(2));
EXPECT_CALL(*engine, ScrolledToYPosition(3));
fake_plugin_.HandleMessage(base::test::ParseJson(R"({
base::Value message = base::test::ParseJson(R"({
"type": "viewport",
"userInitiated": false,
"zoom": 1,
@ -871,7 +878,8 @@ TEST_F(PdfViewPluginBaseWithEngineTest,
"xOffset": 2,
"yOffset": 3,
"pinchPhase": 0,
})"));
})");
fake_plugin_.HandleMessage(message.GetDict());
}
TEST_F(PdfViewPluginBaseWithEngineTest,
@ -883,7 +891,7 @@ TEST_F(PdfViewPluginBaseWithEngineTest,
EXPECT_CALL(*engine, ScrolledToYPosition(3));
EXPECT_CALL(fake_plugin_, IsPrintPreview).WillRepeatedly(Return(true));
fake_plugin_.HandleMessage(base::test::ParseJson(R"({
base::Value message = base::test::ParseJson(R"({
"type": "viewport",
"userInitiated": false,
"zoom": 1,
@ -895,7 +903,8 @@ TEST_F(PdfViewPluginBaseWithEngineTest,
"xOffset": -2,
"yOffset": 3,
"pinchPhase": 0,
})"));
})");
fake_plugin_.HandleMessage(message.GetDict());
}
TEST_F(PdfViewPluginBaseWithEngineTest, UpdateScroll) {
@ -911,9 +920,10 @@ TEST_F(PdfViewPluginBaseWithEngineTest, UpdateScrollStopped) {
EXPECT_CALL(*engine, ScrolledToXPosition).Times(0);
EXPECT_CALL(*engine, ScrolledToYPosition).Times(0);
fake_plugin_.HandleMessage(base::test::ParseJson(R"({
base::Value message = base::test::ParseJson(R"({
"type": "stopScrolling",
})"));
})");
fake_plugin_.HandleMessage(message.GetDict());
fake_plugin_.UpdateScroll({0, 0});
}
@ -1085,12 +1095,13 @@ TEST_F(PdfViewPluginBaseTest, HandleResetPrintPreviewModeMessage) {
PDFiumFormFiller::ScriptOption::kNoJavaScript))
.WillOnce(Return(ByMove(std::move(engine))));
fake_plugin_.HandleMessage(base::test::ParseJson(R"({
base::Value message = base::test::ParseJson(R"({
"type": "resetPrintPreviewMode",
"url": "chrome-untrusted://print/0/0/print.pdf",
"grayscale": false,
"pageCount": 1,
})"));
})");
fake_plugin_.HandleMessage(message.GetDict());
}
TEST_F(PdfViewPluginBaseTest, HandleResetPrintPreviewModeMessageSetGrayscale) {
@ -1103,12 +1114,13 @@ TEST_F(PdfViewPluginBaseTest, HandleResetPrintPreviewModeMessageSetGrayscale) {
PDFiumFormFiller::ScriptOption::kNoJavaScript))
.WillOnce(Return(ByMove(std::move(engine))));
fake_plugin_.HandleMessage(base::test::ParseJson(R"({
base::Value message = base::test::ParseJson(R"({
"type": "resetPrintPreviewMode",
"url": "chrome-untrusted://print/0/0/print.pdf",
"grayscale": true,
"pageCount": 1,
})"));
})");
fake_plugin_.HandleMessage(message.GetDict());
}
TEST_F(PdfViewPluginBaseWithEngineTest, NormalPrinting) {

@ -876,7 +876,7 @@ PdfViewWebPlugin::CreateAssociatedURLLoader(
return container_wrapper_->CreateAssociatedURLLoader(options);
}
void PdfViewWebPlugin::OnMessage(const base::Value& message) {
void PdfViewWebPlugin::OnMessage(const base::Value::Dict& message) {
PdfViewPluginBase::HandleMessage(message);
}

@ -273,7 +273,7 @@ class PdfViewWebPlugin final : public PdfViewPluginBase,
const blink::WebAssociatedURLLoaderOptions& options) override;
// PostMessageReceiver::Client:
void OnMessage(const base::Value& message) override;
void OnMessage(const base::Value::Dict& message) override;
// SkiaGraphics::Client:
void UpdateSnapshot(sk_sp<SkImage> snapshot) override;

@ -348,7 +348,7 @@ class PdfViewWebPluginTest : public PdfViewWebPluginWithoutInitializeTest {
void SetDocumentDimensions(const gfx::Size& dimensions) {
EXPECT_CALL(*engine_ptr_, ApplyDocumentLayout)
.WillRepeatedly(Return(dimensions));
plugin_->OnMessage(base::test::ParseJson(R"({
base::Value message = base::test::ParseJson(R"({
"type": "viewport",
"userInitiated": false,
"zoom": 1,
@ -360,7 +360,8 @@ class PdfViewWebPluginTest : public PdfViewWebPluginWithoutInitializeTest {
"xOffset": 0,
"yOffset": 0,
"pinchPhase": 0,
})"));
})");
plugin_->OnMessage(message.GetDict());
}
void UpdatePluginGeometry(float device_scale, const gfx::Rect& window_rect) {

@ -123,12 +123,13 @@ void PostMessageReceiver::PostMessage(v8::Local<v8::Value> message) {
std::unique_ptr<base::Value> converted_message =
v8_value_converter_->FromV8Value(message, isolate_->GetCurrentContext());
DCHECK(converted_message) << "The PDF Viewer UI should not be sending "
"messages that cannot be converted.";
// The PDF Viewer UI should not be sending messages that cannot be converted.
DCHECK(converted_message);
DCHECK(converted_message->is_dict());
client_task_runner_->PostTask(FROM_HERE,
base::BindOnce(&Client::OnMessage, client_,
std::move(*converted_message)));
client_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&Client::OnMessage, client_,
std::move(converted_message->GetDict())));
}
} // namespace chrome_pdf

@ -10,6 +10,7 @@
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "gin/interceptor.h"
#include "gin/public/wrapper_info.h"
#include "gin/wrappable.h"
@ -17,7 +18,6 @@
namespace base {
class SequencedTaskRunner;
class Value;
} // namespace base
namespace gin {
@ -40,7 +40,7 @@ class PostMessageReceiver final : public gin::Wrappable<PostMessageReceiver>,
class Client {
public:
// Handles converted messages from the embedder.
virtual void OnMessage(const base::Value& message) = 0;
virtual void OnMessage(const base::Value::Dict& message) = 0;
protected:
Client() = default;