Skip to content

Commit

Permalink
[unseasoned-pdf] Introduce common message handling
Browse files Browse the repository at this point in the history
Add PdfViewPluginBase::HandleMessage() to handle messages from the
embedder. Message handlers will be moved from
OutOfProcessInstance::HandleMessage() to the common handler.

Meanwhile, migrate HandleSetTwoUpViewMessage() as a simple migration
example of message handlers.

The migrated handlers will also be strict about the shapes of the sent
messages, and will crash if an unexpected message is sent. This is fine
as the PDF Viewer UI is trusted to send valid messages.

Bug: 1109796
Change-Id: I07e103aebf0e941f94d8d7a04c31950631ddd1da
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2653792
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: K. Moon <kmoon@chromium.org>
Cr-Commit-Position: refs/heads/master@{#848779}
  • Loading branch information
Daniel Hosseinian authored and Chromium LUCI CQ committed Jan 29, 2021
1 parent fa5b4f1 commit 39b42c4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
17 changes: 1 addition & 16 deletions pdf/out_of_process_instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,6 @@ constexpr char kJSEmailBody[] = "body";
// Rotation (Page -> Plugin)
constexpr char kJSRotateClockwiseType[] = "rotateClockwise";
constexpr char kJSRotateCounterclockwiseType[] = "rotateCounterclockwise";
// Toggle two-up view (Page -> Plugin)
constexpr char kJSSetTwoUpViewType[] = "setTwoUpView";
constexpr char kJSEnableTwoUpView[] = "enableTwoUpView";
// Display annotations (Page -> Plugin)
constexpr char kJSDisplayAnnotationsType[] = "displayAnnotations";
constexpr char kJSDisplayAnnotations[] = "display";
Expand Down Expand Up @@ -731,8 +728,6 @@ void OutOfProcessInstance::HandleMessage(const pp::Var& message) {
RotateCounterclockwise();
} else if (type == kJSSetReadOnlyType) {
HandleSetReadOnlyMessage(dict);
} else if (type == kJSSetTwoUpViewType) {
HandleSetTwoUpViewMessage(dict);
} else if (type == kJSDisplayAnnotationsType) {
HandleDisplayAnnotations(dict);
} else if (type == kJSSelectAllType) {
Expand All @@ -752,7 +747,7 @@ void OutOfProcessInstance::HandleMessage(const pp::Var& message) {
} else if (type == kJSGetThumbnailType) {
HandleGetThumbnailMessage(dict);
} else {
NOTREACHED();
PdfViewPluginBase::HandleMessage(ValueFromVar(message));
}
}

Expand Down Expand Up @@ -1927,16 +1922,6 @@ void OutOfProcessInstance::HandleSetReadOnlyMessage(
engine()->SetReadOnly(dict.Get(pp::Var(kJSEnableReadOnly)).AsBool());
}

void OutOfProcessInstance::HandleSetTwoUpViewMessage(
const pp::VarDictionary& dict) {
if (!dict.Get(pp::Var(kJSEnableTwoUpView)).is_bool()) {
NOTREACHED();
return;
}

engine()->SetTwoUpView(dict.Get(pp::Var(kJSEnableTwoUpView)).AsBool());
}

void OutOfProcessInstance::HandleUpdateScrollMessage(
const pp::VarDictionary& dict) {
if (!dict.Get(pp::Var(kJSUpdateScrollX)).is_number() ||
Expand Down
1 change: 0 additions & 1 deletion pdf/out_of_process_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ class OutOfProcessInstance : public PdfViewPluginBase,
void HandleSaveAttachmentMessage(const pp::VarDictionary& dict);
void HandleSaveMessage(const pp::VarDictionary& dict);
void HandleSetReadOnlyMessage(const pp::VarDictionary& dict);
void HandleSetTwoUpViewMessage(const pp::VarDictionary& dict);
void HandleUpdateScrollMessage(const pp::VarDictionary& dict);
void HandleViewportMessage(const pp::VarDictionary& dict);

Expand Down
31 changes: 31 additions & 0 deletions pdf/pdf_view_plugin_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
#include "base/auto_reset.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/check.h"
#include "base/containers/fixed_flat_map.h"
#include "base/memory/weak_ptr.h"
#include "base/notreached.h"
#include "base/optional.h"
#include "base/values.h"
#include "pdf/pdfium/pdfium_engine.h"
#include "pdf/ppapi_migration/url_loader.h"
#include "ui/gfx/geometry/rect.h"
Expand Down Expand Up @@ -41,6 +46,28 @@ uint32_t PdfViewPluginBase::GetBackgroundColor() {
return background_color_;
}

void PdfViewPluginBase::HandleMessage(const base::Value& message) {
using MessageHandler = void (PdfViewPluginBase::*)(const base::Value&);
static constexpr auto kMessageHandlers =
base::MakeFixedFlatMap<base::StringPiece, MessageHandler>({
{"setTwoUpView", &PdfViewPluginBase::HandleSetTwoUpViewMessage},
});

const std::string* type = message.FindStringKey("type");
CHECK(type);

// TODO(crbug.com/1109796): Use `fixed_flat_map<>::at()` when migration is
// complete to CHECK out-of-bounds lookups.
const auto* it = kMessageHandlers.find(*type);
if (it == kMessageHandlers.end()) {
NOTIMPLEMENTED() << message;
return;
}

MessageHandler handler = it->second;
(this->*handler)(message);
}

void PdfViewPluginBase::OnPaint(const std::vector<gfx::Rect>& paint_rects,
std::vector<PaintReadyRect>* ready,
std::vector<gfx::Rect>* pending) {
Expand Down Expand Up @@ -153,4 +180,8 @@ void PdfViewPluginBase::SetZoom(double scale) {
OnGeometryChanged(old_zoom, device_scale_);
}

void PdfViewPluginBase::HandleSetTwoUpViewMessage(const base::Value& message) {
engine()->SetTwoUpView(message.FindBoolKey("enableTwoUpView").value());
}

} // namespace chrome_pdf
10 changes: 10 additions & 0 deletions pdf/pdf_view_plugin_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/rect.h"

namespace base {
class Value;
} // namespace base

namespace chrome_pdf {

class PDFiumEngine;
Expand Down Expand Up @@ -81,6 +85,9 @@ class PdfViewPluginBase : public PDFEngine::Client,
virtual void DidOpenPreview(std::unique_ptr<UrlLoader> loader,
int32_t result) = 0;

// Handles `postMessage()` calls from the embedder.
void HandleMessage(const base::Value& message);

// Paints the given invalid area of the plugin to the given graphics device.
// PaintManager::Client::OnPaint() should be its only caller.
virtual void DoPaint(const std::vector<gfx::Rect>& paint_rects,
Expand Down Expand Up @@ -166,6 +173,9 @@ class PdfViewPluginBase : public PDFEngine::Client,
}

private:
// Message handlers.
void HandleSetTwoUpViewMessage(const base::Value& message);

std::unique_ptr<PDFiumEngine> engine_;
PaintManager paint_manager_{this};

Expand Down
2 changes: 1 addition & 1 deletion pdf/pdf_view_web_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ PdfViewWebPlugin::CreateAssociatedURLLoader(
}

void PdfViewWebPlugin::OnMessage(const base::Value& message) {
NOTIMPLEMENTED() << message;
PdfViewPluginBase::HandleMessage(message);
}

base::WeakPtr<PdfViewPluginBase> PdfViewWebPlugin::GetWeakPtr() {
Expand Down

0 comments on commit 39b42c4

Please sign in to comment.