0

[unseasoned-pdf] Migrate WebLocalFrame references into ContainerWrapper.

In PdfViewWebPlugin, some methods use WebLocalFrame-referencing calls.
Replace these calls with ContainerWrapper methods, so that these
PdfViewWebPlugin methods can be unit tested.

Bug: 1199558
Change-Id: I324d47c4b2af13dabbceb74785d506ec11ffa000
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2877856
Commit-Queue: Hui Yingst <nigi@chromium.org>
Reviewed-by: K. Moon <kmoon@chromium.org>
Cr-Commit-Position: refs/heads/master@{#881251}
This commit is contained in:
Hui Yingst
2021-05-10 21:46:14 +00:00
committed by Chromium LUCI CQ
parent 50e117683e
commit a176c9a000
3 changed files with 83 additions and 15 deletions

@ -130,6 +130,30 @@ class BlinkContainerWrapper final : public PdfViewWebPlugin::ContainerWrapper {
return container_->DeviceScaleFactor();
}
void SetReferrerForRequest(blink::WebURLRequest& request,
const blink::WebURL& referrer_url) override {
GetFrame()->SetReferrerForRequest(request, referrer_url);
}
void TextSelectionChanged(const blink::WebString& selection_text,
uint32_t offset,
const gfx::Range& range) override {
GetFrame()->TextSelectionChanged(selection_text, offset, range);
}
std::unique_ptr<blink::WebAssociatedURLLoader> CreateAssociatedURLLoader(
const blink::WebAssociatedURLLoaderOptions& options) override {
return GetFrame()->CreateAssociatedURLLoader(options);
}
void UpdateTextInputState() override {
return GetFrame()->FrameWidget()->UpdateTextInputState();
}
blink::WebLocalFrame* GetFrame() override {
return container_->GetDocument().GetFrame();
}
blink::WebPluginContainer* Container() override { return container_; }
private:
@ -377,7 +401,7 @@ void PdfViewWebPlugin::EnteredEditMode() {}
void PdfViewWebPlugin::SetSelectedText(const std::string& selected_text) {
selected_text_ = blink::WebString::FromUTF8(selected_text);
GetValidContainerFrame()->TextSelectionChanged(
container_wrapper_->TextSelectionChanged(
selected_text_, /*offset=*/0, gfx::Range(0, selected_text_.length()));
}
@ -414,7 +438,7 @@ void PdfViewWebPlugin::ScheduleTaskOnMainThread(const base::Location& from_here,
}
bool PdfViewWebPlugin::IsValid() const {
return Container() && Container()->GetDocument().GetFrame();
return container_wrapper_ && container_wrapper_->GetFrame();
}
blink::WebURL PdfViewWebPlugin::CompleteURL(
@ -431,13 +455,13 @@ net::SiteForCookies PdfViewWebPlugin::SiteForCookies() const {
void PdfViewWebPlugin::SetReferrerForRequest(
blink::WebURLRequest& request,
const blink::WebURL& referrer_url) {
GetValidContainerFrame()->SetReferrerForRequest(request, referrer_url);
container_wrapper_->SetReferrerForRequest(request, referrer_url);
}
std::unique_ptr<blink::WebAssociatedURLLoader>
PdfViewWebPlugin::CreateAssociatedURLLoader(
const blink::WebAssociatedURLLoaderOptions& options) {
return GetValidContainerFrame()->CreateAssociatedURLLoader(options);
return container_wrapper_->CreateAssociatedURLLoader(options);
}
void PdfViewWebPlugin::OnMessage(const base::Value& message) {
@ -491,8 +515,7 @@ void PdfViewWebPlugin::InitImageData(const gfx::Size& size) {
void PdfViewWebPlugin::SetFormFieldInFocus(bool in_focus) {
text_input_type_ = in_focus ? blink::WebTextInputType::kWebTextInputTypeText
: blink::WebTextInputType::kWebTextInputTypeNone;
// Notify the frame widget.
GetValidContainerFrame()->FrameWidget()->UpdateTextInputState();
container_wrapper_->UpdateTextInputState();
}
// TODO(https://crbug.com/1144444): Add a Pepper-free implementation to set
@ -539,11 +562,6 @@ void PdfViewWebPlugin::UserMetricsRecordAction(const std::string& action) {
base::RecordAction(base::UserMetricsAction(action.c_str()));
}
blink::WebLocalFrame* PdfViewWebPlugin::GetValidContainerFrame() const {
DCHECK(IsValid());
return Container()->GetDocument().GetFrame();
}
void PdfViewWebPlugin::OnViewportChanged(const gfx::Rect& view_rect,
float new_device_scale) {
UpdateGeometryOnViewChanged(view_rect, new_device_scale);

@ -21,10 +21,18 @@
#include "v8/include/v8.h"
namespace blink {
class WebAssociatedURLLoader;
class WebLocalFrame;
class WebPluginContainer;
class WebURL;
class WebURLRequest;
struct WebAssociatedURLLoaderOptions;
} // namespace blink
namespace gfx {
class Range;
} // namespace gfx
namespace chrome_pdf {
// Skeleton for a `blink::WebPlugin` to replace `OutOfProcessInstance`.
@ -45,6 +53,26 @@ class PdfViewWebPlugin final : public PdfViewPluginBase,
// Returns the device scale factor.
virtual float DeviceScaleFactor() const = 0;
// Calls underlying WebLocalFrame::SetReferrerForRequest().
virtual void SetReferrerForRequest(blink::WebURLRequest& request,
const blink::WebURL& referrer_url) = 0;
// Calls underlying WebLocalFrame::TextSelectionChanged().
virtual void TextSelectionChanged(const blink::WebString& selection_text,
uint32_t offset,
const gfx::Range& range) = 0;
// Calls underlying WebLocalFrame::CreateAssociatedURLLoader().
virtual std::unique_ptr<blink::WebAssociatedURLLoader>
CreateAssociatedURLLoader(
const blink::WebAssociatedURLLoaderOptions& options) = 0;
// Notifies the frame widget about the text input type change.
virtual void UpdateTextInputState() = 0;
// Returns the local frame to which the web plugin container belongs.
virtual blink::WebLocalFrame* GetFrame() = 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;
@ -164,10 +192,6 @@ class PdfViewWebPlugin final : public PdfViewPluginBase,
bool InitializeCommon(std::unique_ptr<ContainerWrapper> container_wrapper);
// Returns the local frame to which the web plugin container belongs to. May
// only be called when the plugin has the container inside a valid frame.
blink::WebLocalFrame* GetValidContainerFrame() const;
void OnViewportChanged(const gfx::Rect& view_rect, float new_device_scale);
// Invalidates the entire web plugin container and schedules a paint of the

@ -12,7 +12,10 @@
#include "cc/test/pixel_test_utils.h"
#include "pdf/ppapi_migration/bitmap.h"
#include "pdf/test/test_helpers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/web_text_input_type.h"
#include "third_party/blink/public/web/web_associated_url_loader.h"
#include "third_party/blink/public/web/web_plugin_params.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
@ -76,6 +79,29 @@ class FakeContainerWrapper final : public PdfViewWebPlugin::ContainerWrapper {
float DeviceScaleFactor() const override { return device_scale_; }
MOCK_METHOD(void,
SetReferrerForRequest,
(blink::WebURLRequest&, const blink::WebURL&),
(override));
MOCK_METHOD(void,
TextSelectionChanged,
(const blink::WebString&, uint32_t, const gfx::Range&),
(override));
MOCK_METHOD(std::unique_ptr<blink::WebAssociatedURLLoader>,
CreateAssociatedURLLoader,
(const blink::WebAssociatedURLLoaderOptions&),
(override));
MOCK_METHOD(void, UpdateTextInputState, (), (override));
blink::WebLocalFrame* GetFrame() override { return nullptr; }
// TODO(https://crbug.com/1207575): Container() should not be used for testing
// since it doesn't have a valid blink::WebPluginContainer. Make this method
// fail once ContainerWrapper instead of blink::WebPluginContainer is used for
// initializing `PostMessageSender`.
blink::WebPluginContainer* Container() override { return nullptr; }
void set_device_scale(float device_scale) { device_scale_ = device_scale; }