Move PostMessage() to PdfViewWebPlugin::Client
Relocates the PostMessage() method from ContainerWrapper to Client. This is the first step towards merging the rest of ContainerWrapper into Client, and also eliminates the need for a separate PostMessageSender. Bug: 1323307 Change-Id: Ifbd96650c9dbeecc214fd3e369b4bf2b66827bd9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3641425 Commit-Queue: K. Moon <kmoon@chromium.org> Reviewed-by: Nigi <nigi@chromium.org> Reviewed-by: Leszek Swirski <leszeks@chromium.org> Reviewed-by: Charlie Reis <creis@chromium.org> Cr-Commit-Position: refs/heads/main@{#1003174}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
4dae85884c
commit
96f28bad8f
@ -16,4 +16,5 @@ include_rules = [
|
||||
"+third_party/skia/include/core",
|
||||
"+ui/accessibility",
|
||||
"+ui/base",
|
||||
"+v8/include",
|
||||
]
|
||||
|
@ -4,12 +4,25 @@
|
||||
|
||||
#include "components/pdf/renderer/pdf_view_web_plugin_client.h"
|
||||
|
||||
#include "base/check.h"
|
||||
#include <utility>
|
||||
|
||||
#include "base/check_op.h"
|
||||
#include "base/values.h"
|
||||
#include "components/pdf/renderer/pdf_accessibility_tree.h"
|
||||
#include "content/public/renderer/render_thread.h"
|
||||
#include "content/public/renderer/v8_value_converter.h"
|
||||
#include "printing/buildflags/buildflags.h"
|
||||
#include "third_party/blink/public/web/blink.h"
|
||||
#include "third_party/blink/public/web/web_document.h"
|
||||
#include "third_party/blink/public/web/web_dom_message_event.h"
|
||||
#include "third_party/blink/public/web/web_element.h"
|
||||
#include "third_party/blink/public/web/web_local_frame.h"
|
||||
#include "third_party/blink/public/web/web_plugin_container.h"
|
||||
#include "third_party/blink/public/web/web_serialized_script_value.h"
|
||||
#include "v8/include/v8-context.h"
|
||||
#include "v8/include/v8-isolate.h"
|
||||
#include "v8/include/v8-local-handle.h"
|
||||
#include "v8/include/v8-value.h"
|
||||
|
||||
#if BUILDFLAG(ENABLE_PRINTING)
|
||||
#include "components/printing/renderer/print_render_frame_helper.h"
|
||||
@ -20,7 +33,8 @@ namespace pdf {
|
||||
PdfViewWebPluginClient::PdfViewWebPluginClient(
|
||||
content::RenderFrame* render_frame)
|
||||
: render_frame_(render_frame),
|
||||
v8_value_converter_(content::V8ValueConverter::Create()) {
|
||||
v8_value_converter_(content::V8ValueConverter::Create()),
|
||||
isolate_(blink::MainThreadIsolate()) {
|
||||
DCHECK(render_frame_);
|
||||
}
|
||||
|
||||
@ -32,17 +46,36 @@ std::unique_ptr<base::Value> PdfViewWebPluginClient::FromV8Value(
|
||||
return v8_value_converter_->FromV8Value(value, context);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> PdfViewWebPluginClient::ToV8Value(
|
||||
const base::Value& value,
|
||||
v8::Local<v8::Context> context) {
|
||||
return v8_value_converter_->ToV8Value(&value, context);
|
||||
}
|
||||
|
||||
base::WeakPtr<chrome_pdf::PdfViewWebPlugin::Client>
|
||||
PdfViewWebPluginClient::GetWeakPtr() {
|
||||
return weak_factory_.GetWeakPtr();
|
||||
}
|
||||
|
||||
void PdfViewWebPluginClient::SetPluginContainer(
|
||||
blink::WebPluginContainer* container) {
|
||||
plugin_container_ = container;
|
||||
}
|
||||
|
||||
blink::WebPluginContainer* PdfViewWebPluginClient::PluginContainer() {
|
||||
return plugin_container_;
|
||||
}
|
||||
|
||||
void PdfViewWebPluginClient::PostMessage(base::Value::Dict message) {
|
||||
v8::Isolate::Scope isolate_scope(isolate_);
|
||||
v8::HandleScope handle_scope(isolate_);
|
||||
v8::Local<v8::Context> context =
|
||||
plugin_container_->GetDocument().GetFrame()->MainWorldScriptContext();
|
||||
DCHECK_EQ(isolate_, context->GetIsolate());
|
||||
v8::Context::Scope context_scope(context);
|
||||
|
||||
base::Value message_as_value(std::move(message));
|
||||
v8::Local<v8::Value> converted_message =
|
||||
v8_value_converter_->ToV8Value(&message_as_value, context);
|
||||
|
||||
plugin_container_->EnqueueMessageEvent(
|
||||
blink::WebSerializedScriptValue::Serialize(isolate_, converted_message));
|
||||
}
|
||||
|
||||
void PdfViewWebPluginClient::Print(const blink::WebElement& element) {
|
||||
DCHECK(!element.IsNull());
|
||||
#if BUILDFLAG(ENABLE_PRINTING)
|
||||
|
@ -10,11 +10,19 @@
|
||||
#include "base/memory/weak_ptr.h"
|
||||
#include "pdf/pdf_view_web_plugin.h"
|
||||
|
||||
namespace blink {
|
||||
class WebPluginContainer;
|
||||
} // namespace blink
|
||||
|
||||
namespace content {
|
||||
class RenderFrame;
|
||||
class V8ValueConverter;
|
||||
} // namespace content
|
||||
|
||||
namespace v8 {
|
||||
class Isolate;
|
||||
} // namespace v8
|
||||
|
||||
namespace pdf {
|
||||
|
||||
class PdfViewWebPluginClient : public chrome_pdf::PdfViewWebPlugin::Client {
|
||||
@ -28,9 +36,10 @@ class PdfViewWebPluginClient : public chrome_pdf::PdfViewWebPlugin::Client {
|
||||
std::unique_ptr<base::Value> FromV8Value(
|
||||
v8::Local<v8::Value> value,
|
||||
v8::Local<v8::Context> context) override;
|
||||
v8::Local<v8::Value> ToV8Value(const base::Value& value,
|
||||
v8::Local<v8::Context> context) override;
|
||||
base::WeakPtr<chrome_pdf::PdfViewWebPlugin::Client> GetWeakPtr() override;
|
||||
void SetPluginContainer(blink::WebPluginContainer* container) override;
|
||||
blink::WebPluginContainer* PluginContainer() override;
|
||||
void PostMessage(base::Value::Dict message) override;
|
||||
void Print(const blink::WebElement& element) override;
|
||||
void RecordComputedAction(const std::string& action) override;
|
||||
std::unique_ptr<chrome_pdf::PdfAccessibilityDataHandler>
|
||||
@ -41,6 +50,9 @@ class PdfViewWebPluginClient : public chrome_pdf::PdfViewWebPlugin::Client {
|
||||
content::RenderFrame* const render_frame_;
|
||||
|
||||
const std::unique_ptr<content::V8ValueConverter> v8_value_converter_;
|
||||
v8::Isolate* const isolate_;
|
||||
|
||||
blink::WebPluginContainer* plugin_container_;
|
||||
|
||||
base::WeakPtrFactory<PdfViewWebPluginClient> weak_factory_{this};
|
||||
};
|
||||
|
@ -271,8 +271,6 @@ if (enable_pdf) {
|
||||
"pdf_view_web_plugin.cc",
|
||||
"post_message_receiver.cc",
|
||||
"post_message_receiver.h",
|
||||
"post_message_sender.cc",
|
||||
"post_message_sender.h",
|
||||
"v8_value_converter.h",
|
||||
]
|
||||
|
||||
|
@ -41,7 +41,6 @@
|
||||
#include "pdf/pdf_init.h"
|
||||
#include "pdf/pdfium/pdfium_engine.h"
|
||||
#include "pdf/post_message_receiver.h"
|
||||
#include "pdf/post_message_sender.h"
|
||||
#include "pdf/ppapi_migration/result_codes.h"
|
||||
#include "pdf/ppapi_migration/url_loader.h"
|
||||
#include "pdf/ui/document_properties.h"
|
||||
@ -150,12 +149,9 @@ class PerProcessInitializer final {
|
||||
|
||||
class BlinkContainerWrapper final : public PdfViewWebPlugin::ContainerWrapper {
|
||||
public:
|
||||
BlinkContainerWrapper(blink::WebPluginContainer* container,
|
||||
V8ValueConverter* v8_value_converter)
|
||||
: container_(container),
|
||||
post_message_sender_(container_, v8_value_converter) {
|
||||
explicit BlinkContainerWrapper(blink::WebPluginContainer* container)
|
||||
: container_(container) {
|
||||
DCHECK(container_);
|
||||
DCHECK(v8_value_converter);
|
||||
}
|
||||
BlinkContainerWrapper(const BlinkContainerWrapper&) = delete;
|
||||
BlinkContainerWrapper& operator=(const BlinkContainerWrapper&) = delete;
|
||||
@ -202,10 +198,6 @@ class BlinkContainerWrapper final : public PdfViewWebPlugin::ContainerWrapper {
|
||||
return GetFrame()->GetScrollOffset();
|
||||
}
|
||||
|
||||
void PostMessage(base::Value::Dict message) override {
|
||||
post_message_sender_.Post(std::move(message));
|
||||
}
|
||||
|
||||
void UsePluginAsFindHandler() override {
|
||||
container_->UsePluginAsFindHandler();
|
||||
}
|
||||
@ -284,11 +276,8 @@ class BlinkContainerWrapper final : public PdfViewWebPlugin::ContainerWrapper {
|
||||
return GetFrame()->Client();
|
||||
}
|
||||
|
||||
blink::WebPluginContainer* Container() override { return container_; }
|
||||
|
||||
private:
|
||||
const raw_ptr<blink::WebPluginContainer> container_;
|
||||
PostMessageSender post_message_sender_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@ -316,10 +305,12 @@ PdfViewWebPlugin::PdfViewWebPlugin(
|
||||
PdfViewWebPlugin::~PdfViewWebPlugin() = default;
|
||||
|
||||
bool PdfViewWebPlugin::Initialize(blink::WebPluginContainer* container) {
|
||||
DCHECK(container);
|
||||
client_->SetPluginContainer(container);
|
||||
|
||||
DCHECK_EQ(container->Plugin(), this);
|
||||
return InitializeCommon(
|
||||
std::make_unique<BlinkContainerWrapper>(container, client_.get()),
|
||||
/*engine_override=*/nullptr);
|
||||
return InitializeCommon(std::make_unique<BlinkContainerWrapper>(container),
|
||||
/*engine_override=*/nullptr);
|
||||
}
|
||||
|
||||
bool PdfViewWebPlugin::InitializeForTesting(
|
||||
@ -391,20 +382,20 @@ void PdfViewWebPlugin::SendSetSmoothScrolling() {
|
||||
}
|
||||
|
||||
void PdfViewWebPlugin::Destroy() {
|
||||
if (container_wrapper_) {
|
||||
if (client_->PluginContainer()) {
|
||||
// Explicitly destroy the PDFEngine during destruction as it may call back
|
||||
// into this object.
|
||||
DestroyPreviewEngine();
|
||||
DestroyEngine();
|
||||
PerProcessInitializer::GetInstance().Release();
|
||||
container_wrapper_.reset();
|
||||
client_->SetPluginContainer(nullptr);
|
||||
}
|
||||
|
||||
delete this;
|
||||
}
|
||||
|
||||
blink::WebPluginContainer* PdfViewWebPlugin::Container() const {
|
||||
return container_wrapper_ ? container_wrapper_->Container() : nullptr;
|
||||
return client_->PluginContainer();
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> PdfViewWebPlugin::V8ScriptableObject(
|
||||
@ -933,7 +924,7 @@ void PdfViewWebPlugin::OnDocumentLoadComplete() {
|
||||
}
|
||||
|
||||
void PdfViewWebPlugin::SendMessage(base::Value::Dict message) {
|
||||
container_wrapper_->PostMessage(std::move(message));
|
||||
client_->PostMessage(std::move(message));
|
||||
}
|
||||
|
||||
void PdfViewWebPlugin::SaveAs() {
|
||||
|
@ -94,9 +94,6 @@ class PdfViewWebPlugin final : public PdfViewPluginBase,
|
||||
// Gets the scroll position.
|
||||
virtual gfx::PointF GetScrollPosition() = 0;
|
||||
|
||||
// Enqueues a "message" event carrying `message` to the plugin embedder.
|
||||
virtual void PostMessage(base::Value::Dict message) = 0;
|
||||
|
||||
// Tells the embedder to allow the plugin to handle find requests.
|
||||
virtual void UsePluginAsFindHandler() = 0;
|
||||
|
||||
@ -139,10 +136,6 @@ class PdfViewWebPlugin final : public PdfViewPluginBase,
|
||||
// Returns the local frame's client (render frame). May be null in unit
|
||||
// tests.
|
||||
virtual blink::WebLocalFrameClient* GetWebLocalFrameClient() = 0;
|
||||
|
||||
// Returns the blink web plugin container pointer that's wrapped inside this
|
||||
// object. Returns nullptr if this object is for test only.
|
||||
virtual blink::WebPluginContainer* Container() = 0;
|
||||
};
|
||||
|
||||
// Allows for dependency injections into `PdfViewWebPlugin`.
|
||||
@ -152,6 +145,17 @@ class PdfViewWebPlugin final : public PdfViewPluginBase,
|
||||
|
||||
virtual base::WeakPtr<Client> GetWeakPtr() = 0;
|
||||
|
||||
// Passes the plugin container to the client. This is first called in
|
||||
// `Initialize()`, and cleared to null in `Destroy()`. The container may
|
||||
// also be null for testing.
|
||||
virtual void SetPluginContainer(blink::WebPluginContainer* container) = 0;
|
||||
|
||||
// Returns the plugin container set by `SetPluginContainer()`.
|
||||
virtual blink::WebPluginContainer* PluginContainer() = 0;
|
||||
|
||||
// Enqueues a "message" event carrying `message` to the plugin embedder.
|
||||
virtual void PostMessage(base::Value::Dict message) {}
|
||||
|
||||
// Prints the given `element`.
|
||||
virtual void Print(const blink::WebElement& element) {}
|
||||
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include "base/test/bind.h"
|
||||
#include "base/test/values_test_util.h"
|
||||
#include "base/time/time.h"
|
||||
#include "base/values.h"
|
||||
#include "cc/paint/paint_canvas.h"
|
||||
#include "cc/test/pixel_comparator.h"
|
||||
#include "cc/test/pixel_test_utils.h"
|
||||
@ -182,8 +183,6 @@ class FakeContainerWrapper : public PdfViewWebPlugin::ContainerWrapper {
|
||||
|
||||
MOCK_METHOD(gfx::PointF, GetScrollPosition, (), (override));
|
||||
|
||||
MOCK_METHOD(void, PostMessage, (base::Value::Dict), (override));
|
||||
|
||||
MOCK_METHOD(void, UsePluginAsFindHandler, (), (override));
|
||||
|
||||
MOCK_METHOD(void,
|
||||
@ -224,11 +223,6 @@ class FakeContainerWrapper : public PdfViewWebPlugin::ContainerWrapper {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
blink::WebPluginContainer* Container() override {
|
||||
ADD_FAILURE();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
blink::WebTextInputType widget_text_input_type() const {
|
||||
return widget_text_input_type_;
|
||||
}
|
||||
@ -261,11 +255,13 @@ class FakePdfViewWebPluginClient : public PdfViewWebPlugin::Client {
|
||||
FromV8Value,
|
||||
(v8::Local<v8::Value>, v8::Local<v8::Context>),
|
||||
(override));
|
||||
MOCK_METHOD(v8::Local<v8::Value>,
|
||||
ToV8Value,
|
||||
(const base::Value&, v8::Local<v8::Context>),
|
||||
(override));
|
||||
MOCK_METHOD(base::WeakPtr<Client>, GetWeakPtr, (), (override));
|
||||
MOCK_METHOD(void,
|
||||
SetPluginContainer,
|
||||
(blink::WebPluginContainer*),
|
||||
(override));
|
||||
MOCK_METHOD(blink::WebPluginContainer*, PluginContainer, (), (override));
|
||||
MOCK_METHOD(void, PostMessage, (base::Value::Dict), (override));
|
||||
|
||||
private:
|
||||
base::WeakPtrFactory<FakePdfViewWebPluginClient> weak_factory_{this};
|
||||
@ -945,8 +941,8 @@ class PdfViewWebPluginWithoutDocInfoTest : public PdfViewWebPluginTest {
|
||||
|
||||
TEST_F(PdfViewWebPluginWithoutDocInfoTest, DocumentLoadCompletePostMessages) {
|
||||
const base::Value::Dict expect_metadata = CreateExpectedNoMetadataResponse();
|
||||
EXPECT_CALL(*wrapper_ptr_, PostMessage);
|
||||
EXPECT_CALL(*wrapper_ptr_, PostMessage(Eq(std::ref(expect_metadata))));
|
||||
EXPECT_CALL(*client_ptr_, PostMessage);
|
||||
EXPECT_CALL(*client_ptr_, PostMessage(Eq(std::ref(expect_metadata))));
|
||||
plugin_->DocumentLoadComplete();
|
||||
}
|
||||
|
||||
@ -1108,10 +1104,10 @@ TEST_F(PdfViewWebPluginWithDocInfoTest, DocumentLoadCompletePostMessages) {
|
||||
const base::Value::Dict expect_bookmarks =
|
||||
CreateExpectedBookmarksResponse(engine_ptr_->GetBookmarks());
|
||||
const base::Value::Dict expect_metadata = CreateExpectedMetadataResponse();
|
||||
EXPECT_CALL(*wrapper_ptr_, PostMessage);
|
||||
EXPECT_CALL(*wrapper_ptr_, PostMessage(Eq(std::ref(expect_attachments))));
|
||||
EXPECT_CALL(*wrapper_ptr_, PostMessage(Eq(std::ref(expect_bookmarks))));
|
||||
EXPECT_CALL(*wrapper_ptr_, PostMessage(Eq(std::ref(expect_metadata))));
|
||||
EXPECT_CALL(*client_ptr_, PostMessage);
|
||||
EXPECT_CALL(*client_ptr_, PostMessage(Eq(std::ref(expect_attachments))));
|
||||
EXPECT_CALL(*client_ptr_, PostMessage(Eq(std::ref(expect_bookmarks))));
|
||||
EXPECT_CALL(*client_ptr_, PostMessage(Eq(std::ref(expect_metadata))));
|
||||
plugin_->DocumentLoadComplete();
|
||||
}
|
||||
|
||||
|
@ -1,52 +0,0 @@
|
||||
// Copyright 2021 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "pdf/post_message_sender.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "base/check_op.h"
|
||||
#include "base/values.h"
|
||||
#include "pdf/v8_value_converter.h"
|
||||
#include "third_party/blink/public/web/blink.h"
|
||||
#include "third_party/blink/public/web/web_document.h"
|
||||
#include "third_party/blink/public/web/web_dom_message_event.h"
|
||||
#include "third_party/blink/public/web/web_local_frame.h"
|
||||
#include "third_party/blink/public/web/web_plugin_container.h"
|
||||
#include "third_party/blink/public/web/web_serialized_script_value.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace chrome_pdf {
|
||||
|
||||
PostMessageSender::PostMessageSender(blink::WebPluginContainer* container,
|
||||
V8ValueConverter* v8_value_converter)
|
||||
: v8_value_converter_(v8_value_converter),
|
||||
isolate_(blink::MainThreadIsolate()),
|
||||
container_(container) {
|
||||
DCHECK(container_);
|
||||
}
|
||||
|
||||
PostMessageSender::~PostMessageSender() = default;
|
||||
|
||||
// TODO(crbug.com/1109796): This method is currently called from the renderer
|
||||
// main thread, but will be called from a dedicated plugin thread in the future.
|
||||
// When that happens, the body of this method needs to be posted to the main
|
||||
// thread as a task because that's where the Blink and V8 interactions need to
|
||||
// occur.
|
||||
void PostMessageSender::Post(base::Value::Dict message) {
|
||||
v8::Isolate::Scope isolate_scope(isolate_);
|
||||
v8::HandleScope handle_scope(isolate_);
|
||||
v8::Local<v8::Context> context =
|
||||
container_->GetDocument().GetFrame()->MainWorldScriptContext();
|
||||
DCHECK_EQ(isolate_, context->GetIsolate());
|
||||
v8::Context::Scope context_scope(context);
|
||||
|
||||
v8::Local<v8::Value> converted_message =
|
||||
v8_value_converter_->ToV8Value(base::Value(std::move(message)), context);
|
||||
|
||||
container_->EnqueueMessageEvent(
|
||||
blink::WebSerializedScriptValue::Serialize(isolate_, converted_message));
|
||||
}
|
||||
|
||||
} // namespace chrome_pdf
|
@ -1,42 +0,0 @@
|
||||
// Copyright 2021 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef PDF_POST_MESSAGE_SENDER_H_
|
||||
#define PDF_POST_MESSAGE_SENDER_H_
|
||||
|
||||
#include "base/memory/raw_ptr.h"
|
||||
#include "base/values.h"
|
||||
#include "v8/include/v8-forward.h"
|
||||
|
||||
namespace blink {
|
||||
class WebPluginContainer;
|
||||
} // namespace blink
|
||||
|
||||
namespace chrome_pdf {
|
||||
|
||||
class V8ValueConverter;
|
||||
|
||||
// Manages messages sent from the plugin to its embedder.
|
||||
class PostMessageSender final {
|
||||
public:
|
||||
PostMessageSender(blink::WebPluginContainer* container,
|
||||
V8ValueConverter* v8_value_converter);
|
||||
PostMessageSender(const PostMessageSender&) = delete;
|
||||
PostMessageSender& operator=(const PostMessageSender&) = delete;
|
||||
~PostMessageSender();
|
||||
|
||||
// Enqueues a "message" event carrying `message` to the plugin embedder.
|
||||
void Post(base::Value::Dict message);
|
||||
|
||||
private:
|
||||
const raw_ptr<V8ValueConverter> v8_value_converter_;
|
||||
|
||||
const raw_ptr<v8::Isolate> isolate_;
|
||||
|
||||
raw_ptr<blink::WebPluginContainer> const container_;
|
||||
};
|
||||
|
||||
} // namespace chrome_pdf
|
||||
|
||||
#endif // PDF_POST_MESSAGE_SENDER_H_
|
@ -25,8 +25,6 @@ class V8ValueConverter {
|
||||
virtual std::unique_ptr<base::Value> FromV8Value(
|
||||
v8::Local<v8::Value> value,
|
||||
v8::Local<v8::Context> context) = 0;
|
||||
virtual v8::Local<v8::Value> ToV8Value(const base::Value& value,
|
||||
v8::Local<v8::Context> context) = 0;
|
||||
|
||||
protected:
|
||||
~V8ValueConverter() = default;
|
||||
|
Reference in New Issue
Block a user