1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/pdf/renderer/pdf_view_web_plugin_client.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/check_op.h"
#include "base/values.h"
#include "components/pdf/renderer/pdf_accessibility_tree.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/v8_value_converter.h"
#include "net/cookies/site_for_cookies.h"
#include "printing/buildflags/buildflags.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/public/platform/scheduler/web_agent_group_scheduler.h"
#include "third_party/blink/public/platform/web_security_origin.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/platform/web_url.h"
#include "third_party/blink/public/web/web_associated_url_loader.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_frame_widget.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_local_frame_client.h"
#include "third_party/blink/public/web/web_plugin_container.h"
#include "third_party/blink/public/web/web_serialized_script_value.h"
#include "third_party/blink/public/web/web_view.h"
#include "third_party/blink/public/web/web_widget.h"
#include "ui/accessibility/ax_features.mojom-features.h"
#include "ui/display/screen_info.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.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"
#endif // BUILDFLAG(ENABLE_PRINTING)
namespace pdf {
PdfViewWebPluginClient::PdfViewWebPluginClient(
content::RenderFrame* render_frame)
: render_frame_(render_frame),
v8_value_converter_(content::V8ValueConverter::Create()),
isolate_(
render_frame->GetWebFrame()->GetAgentGroupScheduler()->Isolate()) {
DCHECK(render_frame_);
}
PdfViewWebPluginClient::~PdfViewWebPluginClient() = default;
std::unique_ptr<base::Value> PdfViewWebPluginClient::FromV8Value(
v8::Local<v8::Value> value,
v8::Local<v8::Context> context) {
return v8_value_converter_->FromV8Value(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_;
}
v8::Isolate* PdfViewWebPluginClient::GetIsolate() {
return GetFrame()->GetAgentGroupScheduler()->Isolate();
}
net::SiteForCookies PdfViewWebPluginClient::SiteForCookies() const {
return plugin_container_->GetDocument().SiteForCookies();
}
blink::WebURL PdfViewWebPluginClient::CompleteURL(
const blink::WebString& partial_url) const {
return plugin_container_->GetDocument().CompleteURL(partial_url);
}
void PdfViewWebPluginClient::PostMessage(base::Value::Dict message) {
blink::WebLocalFrame* frame = GetFrame();
if (!frame) {
return;
}
v8::Isolate::Scope isolate_scope(isolate_);
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context = frame->MainWorldScriptContext();
DCHECK_EQ(isolate_, context->GetIsolate());
v8::Context::Scope context_scope(context);
v8::Local<v8::Value> converted_message =
v8_value_converter_->ToV8Value(message, context);
plugin_container_->EnqueueMessageEvent(
blink::WebSerializedScriptValue::Serialize(isolate_, converted_message));
}
void PdfViewWebPluginClient::Invalidate() {
plugin_container_->Invalidate();
}
void PdfViewWebPluginClient::RequestTouchEventType(
blink::WebPluginContainer::TouchEventRequestType request_type) {
plugin_container_->RequestTouchEventType(request_type);
}
void PdfViewWebPluginClient::ReportFindInPageMatchCount(int identifier,
int total,
bool final_update) {
plugin_container_->ReportFindInPageMatchCount(identifier, total,
final_update);
}
void PdfViewWebPluginClient::ReportFindInPageSelection(int identifier,
int index,
bool final_update) {
plugin_container_->ReportFindInPageSelection(identifier, index, final_update);
}
void PdfViewWebPluginClient::ReportFindInPageTickmarks(
const std::vector<gfx::Rect>& tickmarks) {
blink::WebLocalFrame* frame = GetFrame();
if (frame) {
frame->SetTickmarks(blink::WebElement(), std::vector<gfx::Rect>(tickmarks));
}
}
float PdfViewWebPluginClient::DeviceScaleFactor() {
// Do not rely on `blink::WebPluginContainer::DeviceScaleFactor()`, since it
// doesn't always reflect the real screen's device scale. Instead, get the
// device scale from the top-level frame's `display::ScreenInfo`.
blink::WebWidget* widget = GetFrame()->LocalRoot()->FrameWidget();
return widget->GetOriginalScreenInfo().device_scale_factor;
}
gfx::PointF PdfViewWebPluginClient::GetScrollPosition() {
// Note that `blink::WebLocalFrame::GetScrollOffset()` actually returns a
// scroll position (a point relative to the top-left corner).
return GetFrame()->GetScrollOffset();
}
void PdfViewWebPluginClient::UsePluginAsFindHandler() {
plugin_container_->UsePluginAsFindHandler();
}
void PdfViewWebPluginClient::SetReferrerForRequest(
blink::WebURLRequest& request,
const blink::WebURL& referrer_url) {
GetFrame()->SetReferrerForRequest(request, referrer_url);
}
void PdfViewWebPluginClient::Alert(const blink::WebString& message) {
blink::WebLocalFrame* frame = GetFrame();
if (frame)
frame->Alert(message);
}
bool PdfViewWebPluginClient::Confirm(const blink::WebString& message) {
blink::WebLocalFrame* frame = GetFrame();
return frame && frame->Confirm(message);
}
blink::WebString PdfViewWebPluginClient::Prompt(
const blink::WebString& message,
const blink::WebString& default_value) {
blink::WebLocalFrame* frame = GetFrame();
return frame ? frame->Prompt(message, default_value) : blink::WebString();
}
void PdfViewWebPluginClient::TextSelectionChanged(
const blink::WebString& selection_text,
uint32_t offset,
const gfx::Range& range) {
// Focus the plugin's containing frame before changing the text selection.
// TODO(crbug.com/40192026): Would it make more sense not to change the text
// selection at all in this case? Maybe we only have this problem because we
// support a "selectAll" message.
blink::WebLocalFrame* frame = GetFrame();
frame->View()->SetFocusedFrame(frame);
frame->TextSelectionChanged(selection_text, offset, range);
}
std::unique_ptr<blink::WebAssociatedURLLoader>
PdfViewWebPluginClient::CreateAssociatedURLLoader(
const blink::WebAssociatedURLLoaderOptions& options) {
return GetFrame()->CreateAssociatedURLLoader(options);
}
#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
void PdfViewWebPluginClient::GetOcrMaxImageDimension(
base::OnceCallback<void(uint32_t)> callback) {
ConnectOcrIfNeeded();
return screen_ai_annotator_->GetMaxImageDimension(std::move(callback));
}
void PdfViewWebPluginClient::PerformOcr(
const SkBitmap& image,
base::OnceCallback<void(screen_ai::mojom::VisualAnnotationPtr)> callback) {
ConnectOcrIfNeeded();
screen_ai_annotator_->PerformOcrAndReturnAnnotation(image,
std::move(callback));
}
void PdfViewWebPluginClient::SetOcrDisconnectedCallback(
base::RepeatingClosure callback) {
ocr_disconnect_callback_ = std::move(callback);
}
void PdfViewWebPluginClient::OnOcrDisconnected() {
screen_ai_annotator_.reset();
CHECK(ocr_disconnect_callback_);
ocr_disconnect_callback_.Run();
}
void PdfViewWebPluginClient::ConnectOcrIfNeeded() {
CHECK(base::FeatureList::IsEnabled(ax::mojom::features::kScreenAIOCREnabled));
if (!screen_ai_annotator_.is_bound()) {
render_frame_->GetBrowserInterfaceBroker().GetInterface(
screen_ai_annotator_.BindNewPipeAndPassReceiver());
screen_ai_annotator_->SetClientType(
screen_ai::mojom::OcrClientType::kPdfViewer);
screen_ai_annotator_.set_disconnect_handler(
base::BindOnce(&PdfViewWebPluginClient::OnOcrDisconnected,
weak_factory_.GetWeakPtr()));
}
}
#endif // BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
void PdfViewWebPluginClient::UpdateTextInputState() {
// `widget` is null in Print Preview.
auto* widget = GetFrame()->FrameWidget();
if (widget)
widget->UpdateTextInputState();
}
void PdfViewWebPluginClient::UpdateSelectionBounds() {
// `widget` is null in Print Preview.
auto* widget = GetFrame()->FrameWidget();
if (widget)
widget->UpdateSelectionBounds();
}
std::string PdfViewWebPluginClient::GetEmbedderOriginString() {
auto* frame = GetFrame();
if (!frame)
return {};
auto* parent_frame = frame->Parent();
if (!parent_frame)
return {};
return GURL(parent_frame->GetSecurityOrigin().ToString().Utf8()).spec();
}
bool PdfViewWebPluginClient::HasFrame() const {
return plugin_container_ && GetFrame();
}
blink::WebLocalFrame* PdfViewWebPluginClient::GetFrame() const {
return plugin_container_->GetDocument().GetFrame();
}
void PdfViewWebPluginClient::DidStartLoading() {
blink::WebLocalFrameClient* frame_client = GetFrame()->Client();
if (!frame_client)
return;
frame_client->DidStartLoading();
}
void PdfViewWebPluginClient::DidStopLoading() {
blink::WebLocalFrameClient* frame_client = GetFrame()->Client();
if (!frame_client)
return;
frame_client->DidStopLoading();
}
void PdfViewWebPluginClient::Print() {
blink::WebElement element = plugin_container_->GetElement();
DCHECK(!element.IsNull());
#if BUILDFLAG(ENABLE_PRINTING)
printing::PrintRenderFrameHelper::Get(render_frame_)->PrintNode(element);
#endif // BUILDFLAG(ENABLE_PRINTING)
}
void PdfViewWebPluginClient::RecordComputedAction(const std::string& action) {
content::RenderThread::Get()->RecordComputedAction(action);
}
std::unique_ptr<chrome_pdf::PdfAccessibilityDataHandler>
PdfViewWebPluginClient::CreateAccessibilityDataHandler(
chrome_pdf::PdfAccessibilityActionHandler* action_handler,
blink::WebPluginContainer* plugin_container) {
return std::make_unique<PdfAccessibilityTree>(render_frame_, action_handler,
plugin_container);
}
} // namespace pdf
|