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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
// Copyright 2013 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 "components/plugins/renderer/webview_plugin.h"
#include <stddef.h>
#include "base/auto_reset.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/metrics/histogram_macros.h"
#include "base/numerics/safe_conversions.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "content/public/common/web_preferences.h"
#include "content/public/renderer/render_view.h"
#include "gin/converter.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/WebKit/public/platform/WebInputEvent.h"
#include "third_party/WebKit/public/platform/WebURL.h"
#include "third_party/WebKit/public/platform/WebURLResponse.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebElement.h"
#include "third_party/WebKit/public/web/WebFrameWidget.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebPluginContainer.h"
#include "third_party/WebKit/public/web/WebView.h"
using blink::WebCanvas;
using blink::WebCursorInfo;
using blink::WebDragData;
using blink::WebDragOperationsMask;
using blink::WebFrameWidget;
using blink::WebImage;
using blink::WebInputEvent;
using blink::WebLocalFrame;
using blink::WebMouseEvent;
using blink::WebPlugin;
using blink::WebPluginContainer;
using blink::WebPoint;
using blink::WebRect;
using blink::WebString;
using blink::WebURLError;
using blink::WebURLResponse;
using blink::WebVector;
using blink::WebView;
using content::WebPreferences;
WebViewPlugin::WebViewPlugin(content::RenderView* render_view,
WebViewPlugin::Delegate* delegate,
const WebPreferences& preferences)
: content::RenderViewObserver(render_view),
delegate_(delegate),
container_(nullptr),
finished_loading_(false),
focused_(false),
is_painting_(false),
is_resizing_(false),
web_view_helper_(this, preferences),
weak_factory_(this) {
}
// static
WebViewPlugin* WebViewPlugin::Create(content::RenderView* render_view,
WebViewPlugin::Delegate* delegate,
const WebPreferences& preferences,
const std::string& html_data,
const GURL& url) {
DCHECK(url.is_valid()) << "Blink requires the WebView to have a valid URL.";
WebViewPlugin* plugin = new WebViewPlugin(render_view, delegate, preferences);
plugin->web_view()->mainFrame()->loadHTMLString(html_data, url);
return plugin;
}
WebViewPlugin::~WebViewPlugin() {
DCHECK(!weak_factory_.HasWeakPtrs());
}
void WebViewPlugin::ReplayReceivedData(WebPlugin* plugin) {
if (!response_.isNull()) {
plugin->didReceiveResponse(response_);
size_t total_bytes = 0;
for (std::list<std::string>::iterator it = data_.begin(); it != data_.end();
++it) {
plugin->didReceiveData(
it->c_str(), base::checked_cast<int>(it->length()));
total_bytes += it->length();
}
}
// We need to transfer the |focused_| to new plugin after it loaded.
if (focused_)
plugin->updateFocus(true, blink::WebFocusTypeNone);
if (finished_loading_)
plugin->didFinishLoading();
if (error_)
plugin->didFailLoading(*error_);
}
WebPluginContainer* WebViewPlugin::container() const { return container_; }
bool WebViewPlugin::initialize(WebPluginContainer* container) {
DCHECK(container);
DCHECK_EQ(this, container->plugin());
container_ = container;
// We must call layout again here to ensure that the container is laid
// out before we next try to paint it, which is a requirement of the
// document life cycle in Blink. In most cases, needsLayout is set by
// scheduleAnimation, but due to timers controlling widget update,
// scheduleAnimation may be invoked before this initialize call (which
// comes through the widget update process). It doesn't hurt to mark
// for animation again, and it does help us in the race-condition situation.
container_->scheduleAnimation();
old_title_ = container_->element().getAttribute("title");
// Propagate device scale and zoom level to inner webview.
web_view()->setDeviceScaleFactor(container_->deviceScaleFactor());
web_view()->setZoomLevel(
blink::WebView::zoomFactorToZoomLevel(container_->pageZoomFactor()));
return true;
}
void WebViewPlugin::destroy() {
weak_factory_.InvalidateWeakPtrs();
if (delegate_) {
delegate_->PluginDestroyed();
delegate_ = nullptr;
}
container_ = nullptr;
content::RenderViewObserver::Observe(nullptr);
base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
}
v8::Local<v8::Object> WebViewPlugin::v8ScriptableObject(v8::Isolate* isolate) {
if (!delegate_)
return v8::Local<v8::Object>();
return delegate_->GetV8ScriptableObject(isolate);
}
void WebViewPlugin::updateAllLifecyclePhases() {
web_view()->updateAllLifecyclePhases();
}
void WebViewPlugin::paint(WebCanvas* canvas, const WebRect& rect) {
gfx::Rect paint_rect = gfx::IntersectRects(rect_, rect);
if (paint_rect.IsEmpty())
return;
base::AutoReset<bool> is_painting(
&is_painting_, true);
paint_rect.Offset(-rect_.x(), -rect_.y());
canvas->save();
canvas->translate(SkIntToScalar(rect_.x()), SkIntToScalar(rect_.y()));
// Apply inverse device scale factor, as the outer webview has already
// applied it, and the inner webview will apply it again.
SkScalar inverse_scale =
SkFloatToScalar(1.0 / container_->deviceScaleFactor());
canvas->scale(inverse_scale, inverse_scale);
web_view()->paint(canvas, paint_rect);
canvas->restore();
}
// Coordinates are relative to the containing window.
void WebViewPlugin::updateGeometry(const WebRect& window_rect,
const WebRect& clip_rect,
const WebRect& unobscured_rect,
const WebVector<WebRect>& cut_outs_rects,
bool is_visible) {
DCHECK(container_);
base::AutoReset<bool> is_resizing(&is_resizing_, true);
if (static_cast<gfx::Rect>(window_rect) != rect_) {
rect_ = window_rect;
web_view()->resize(rect_.size());
}
// Plugin updates are forbidden during Blink layout. Therefore,
// UpdatePluginForNewGeometry must be posted to a task to run asynchronously.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(&WebViewPlugin::UpdatePluginForNewGeometry,
weak_factory_.GetWeakPtr(), window_rect, unobscured_rect));
}
void WebViewPlugin::updateFocus(bool focused, blink::WebFocusType focus_type) {
focused_ = focused;
}
blink::WebInputEventResult WebViewPlugin::handleInputEvent(
const WebInputEvent& event,
WebCursorInfo& cursor) {
// For tap events, don't handle them. They will be converted to
// mouse events later and passed to here.
if (event.type() == WebInputEvent::GestureTap)
return blink::WebInputEventResult::NotHandled;
// For LongPress events we return false, since otherwise the context menu will
// be suppressed. https://crbug.com/482842
if (event.type() == WebInputEvent::GestureLongPress)
return blink::WebInputEventResult::NotHandled;
if (event.type() == WebInputEvent::ContextMenu) {
if (delegate_) {
const WebMouseEvent& mouse_event =
reinterpret_cast<const WebMouseEvent&>(event);
delegate_->ShowContextMenu(mouse_event);
}
return blink::WebInputEventResult::HandledSuppressed;
}
current_cursor_ = cursor;
blink::WebInputEventResult handled = web_view()->handleInputEvent(event);
cursor = current_cursor_;
return handled;
}
void WebViewPlugin::didReceiveResponse(const WebURLResponse& response) {
DCHECK(response_.isNull());
response_ = response;
}
void WebViewPlugin::didReceiveData(const char* data, int data_length) {
data_.push_back(std::string(data, data_length));
}
void WebViewPlugin::didFinishLoading() {
DCHECK(!finished_loading_);
finished_loading_ = true;
}
void WebViewPlugin::didFailLoading(const WebURLError& error) {
DCHECK(!error_.get());
error_.reset(new WebURLError(error));
}
WebViewPlugin::WebViewHelper::WebViewHelper(
WebViewPlugin* plugin,
const WebPreferences& preferences) : plugin_(plugin) {
web_view_ =
WebView::create(this, blink::WebPageVisibilityStateVisible);
// ApplyWebPreferences before making a WebLocalFrame so that the frame sees a
// consistent view of our preferences.
content::RenderView::ApplyWebPreferences(preferences, web_view_);
WebLocalFrame* web_frame = WebLocalFrame::create(
blink::WebTreeScopeType::Document, this);
web_view_->setMainFrame(web_frame);
// TODO(dcheng): The main frame widget currently has a special case.
// Eliminate this once WebView is no longer a WebWidget.
WebFrameWidget::create(this, web_view_, web_frame);
}
WebViewPlugin::WebViewHelper::~WebViewHelper() {
web_view_->close();
}
bool WebViewPlugin::WebViewHelper::acceptsLoadDrops() { return false; }
void WebViewPlugin::WebViewHelper::setToolTipText(
const WebString& text,
blink::WebTextDirection hint) {
if (plugin_->container_)
plugin_->container_->element().setAttribute("title", text);
}
void WebViewPlugin::WebViewHelper::startDragging(blink::WebReferrerPolicy,
const WebDragData&,
WebDragOperationsMask,
const WebImage&,
const WebPoint&) {
// Immediately stop dragging.
DCHECK(web_view_->mainFrame()->isWebLocalFrame());
web_view_->mainFrame()->toWebLocalFrame()->frameWidget()->
dragSourceSystemDragEnded();
}
bool WebViewPlugin::WebViewHelper::allowsBrokenNullLayerTreeView() const {
return true;
}
void WebViewPlugin::WebViewHelper::didInvalidateRect(const WebRect& rect) {
if (plugin_->container_)
plugin_->container_->invalidateRect(rect);
}
void WebViewPlugin::WebViewHelper::didChangeCursor(
const WebCursorInfo& cursor) {
plugin_->current_cursor_ = cursor;
}
void WebViewPlugin::WebViewHelper::scheduleAnimation() {
// Resizes must be self-contained: any lifecycle updating must
// be triggerd from within the WebView or this WebViewPlugin.
// This is because this WebViewPlugin is contained in another
// Web View which may be in the middle of updating its lifecycle,
// but after layout is done, and it is illegal to dirty earlier
// lifecycle stages during later ones.
if (plugin_->is_resizing_)
return;
if (plugin_->container_) {
// This should never happen; see also crbug.com/545039 for context.
DCHECK(!plugin_->is_painting_);
plugin_->container_->scheduleAnimation();
}
}
void WebViewPlugin::WebViewHelper::didClearWindowObject(
WebLocalFrame* frame) {
if (!plugin_->delegate_)
return;
v8::Isolate* isolate = blink::mainThreadIsolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = frame->mainWorldScriptContext();
DCHECK(!context.IsEmpty());
v8::Context::Scope context_scope(context);
v8::Local<v8::Object> global = context->Global();
global->Set(gin::StringToV8(isolate, "plugin"),
plugin_->delegate_->GetV8Handle(isolate));
}
void WebViewPlugin::OnZoomLevelChanged() {
if (container_) {
web_view()->setZoomLevel(
blink::WebView::zoomFactorToZoomLevel(container_->pageZoomFactor()));
}
}
void WebViewPlugin::UpdatePluginForNewGeometry(
const blink::WebRect& window_rect,
const blink::WebRect& unobscured_rect) {
DCHECK(container_);
if (!delegate_)
return;
// The delegate may instantiate a new plugin.
delegate_->OnUnobscuredRectUpdate(gfx::Rect(unobscured_rect));
// The delegate may have dirtied style and layout of the WebView.
// See for example the resizePoster function in plugin_poster.html.
// Run the lifecycle now so that it is clean.
web_view()->updateAllLifecyclePhases();
}
|