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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
// Copyright 2014 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 "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "content/browser/browser_plugin/browser_plugin_guest.h"
#include "content/browser/frame_host/render_widget_host_view_guest.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/common/browser_plugin/browser_plugin_messages.h"
#include "content/common/frame_messages.h"
#include "content/common/gpu/gpu_messages.h"
#include "content/common/input/web_touch_event_traits.h"
#include "content/common/view_messages.h"
#include "content/common/webplugin_geometry.h"
#include "content/public/common/content_switches.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/WebKit/public/platform/WebScreenInfo.h"
#if defined(OS_MACOSX)
#import "content/browser/renderer_host/render_widget_host_view_mac_dictionary_helper.h"
#endif
#if defined(USE_AURA)
#include "content/browser/renderer_host/ui_events_helper.h"
#endif
namespace content {
namespace {
#if defined(USE_AURA)
blink::WebGestureEvent CreateFlingCancelEvent(double time_stamp) {
blink::WebGestureEvent gesture_event;
gesture_event.timeStampSeconds = time_stamp;
gesture_event.type = blink::WebGestureEvent::GestureFlingCancel;
gesture_event.sourceDevice = blink::WebGestureDeviceTouchscreen;
return gesture_event;
}
#endif // defined(USE_AURA)
} // namespace
RenderWidgetHostViewGuest::RenderWidgetHostViewGuest(
RenderWidgetHost* widget_host,
BrowserPluginGuest* guest,
base::WeakPtr<RenderWidgetHostViewBase> platform_view)
: RenderWidgetHostViewChildFrame(widget_host),
// |guest| is NULL during test.
guest_(guest ? guest->AsWeakPtr() : base::WeakPtr<BrowserPluginGuest>()),
platform_view_(platform_view) {
#if defined(USE_AURA)
gesture_recognizer_.reset(ui::GestureRecognizer::Create());
gesture_recognizer_->AddGestureEventHelper(this);
#endif // defined(USE_AURA)
}
RenderWidgetHostViewGuest::~RenderWidgetHostViewGuest() {
#if defined(USE_AURA)
gesture_recognizer_->RemoveGestureEventHelper(this);
#endif // defined(USE_AURA)
}
bool RenderWidgetHostViewGuest::OnMessageReceivedFromEmbedder(
const IPC::Message& message,
RenderWidgetHostImpl* embedder) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(RenderWidgetHostViewGuest, message,
embedder)
IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_HandleInputEvent,
OnHandleInputEvent)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void RenderWidgetHostViewGuest::WasShown() {
// If the WebContents associated with us showed an interstitial page in the
// beginning, the teardown path might call WasShown() while |host_| is in
// the process of destruction. Avoid calling WasShown below in this case.
// TODO(lazyboy): We shouldn't be showing interstitial pages in guests in the
// first place: http://crbug.com/273089.
//
// |guest_| is NULL during test.
if ((guest_ && guest_->is_in_destruction()) || !host_->is_hidden())
return;
// Make sure the size of this view matches the size of the WebContentsView.
// The two sizes may fall out of sync if we switch RenderWidgetHostViews,
// resize, and then switch page, as is the case with interstitial pages.
// NOTE: |guest_| is NULL in unit tests.
if (guest_)
SetSize(guest_->web_contents()->GetViewBounds().size());
host_->WasShown(ui::LatencyInfo());
}
void RenderWidgetHostViewGuest::WasHidden() {
// |guest_| is NULL during test.
if ((guest_ && guest_->is_in_destruction()) || host_->is_hidden())
return;
host_->WasHidden();
}
void RenderWidgetHostViewGuest::SetSize(const gfx::Size& size) {
size_ = size;
host_->WasResized();
}
void RenderWidgetHostViewGuest::SetBounds(const gfx::Rect& rect) {
SetSize(rect.size());
}
void RenderWidgetHostViewGuest::Focus() {
// InterstitialPageImpl focuses views directly, so we place focus logic here.
// InterstitialPages are not WebContents, and so BrowserPluginGuest does not
// have direct access to the interstitial page's RenderWidgetHost.
if (guest_)
guest_->SetFocus(host_, true);
}
bool RenderWidgetHostViewGuest::HasFocus() const {
if (!guest_)
return false;
return guest_->focused();
}
#if defined(USE_AURA)
void RenderWidgetHostViewGuest::ProcessAckedTouchEvent(
const TouchEventWithLatencyInfo& touch, InputEventAckState ack_result) {
// TODO(fsamuel): Currently we will only take this codepath if the guest has
// requested touch events. A better solution is to always forward touchpresses
// to the embedder process to target a BrowserPlugin, and then route all
// subsequent touch points of that touchdown to the appropriate guest until
// that touch point is released.
ScopedVector<ui::TouchEvent> events;
if (!MakeUITouchEventsFromWebTouchEvents(touch, &events, LOCAL_COORDINATES))
return;
ui::EventResult result = (ack_result ==
INPUT_EVENT_ACK_STATE_CONSUMED) ? ui::ER_HANDLED : ui::ER_UNHANDLED;
for (ScopedVector<ui::TouchEvent>::iterator iter = events.begin(),
end = events.end(); iter != end; ++iter) {
if (!gesture_recognizer_->ProcessTouchEventPreDispatch(*iter, this))
continue;
scoped_ptr<ui::GestureRecognizer::Gestures> gestures;
gestures.reset(gesture_recognizer_->AckSyncTouchEvent(
(*iter)->unique_event_id(), result, this));
ProcessGestures(gestures.get());
}
}
#endif
gfx::Rect RenderWidgetHostViewGuest::GetViewBounds() const {
if (!guest_)
return gfx::Rect();
RenderWidgetHostViewBase* rwhv = GetOwnerRenderWidgetHostView();
gfx::Rect embedder_bounds;
if (rwhv)
embedder_bounds = rwhv->GetViewBounds();
return gfx::Rect(
guest_->GetScreenCoordinates(embedder_bounds.origin()), size_);
}
void RenderWidgetHostViewGuest::RenderProcessGone(
base::TerminationStatus status,
int error_code) {
platform_view_->RenderProcessGone(status, error_code);
// Destroy the guest view instance only, so we don't end up calling
// platform_view_->Destroy().
DestroyGuestView();
}
void RenderWidgetHostViewGuest::Destroy() {
// The RenderWidgetHost's destruction led here, so don't call it.
DestroyGuestView();
if (platform_view_) // The platform view might have been destroyed already.
platform_view_->Destroy();
}
gfx::Size RenderWidgetHostViewGuest::GetPhysicalBackingSize() const {
return RenderWidgetHostViewBase::GetPhysicalBackingSize();
}
base::string16 RenderWidgetHostViewGuest::GetSelectedText() const {
return platform_view_->GetSelectedText();
}
void RenderWidgetHostViewGuest::SetTooltipText(
const base::string16& tooltip_text) {
if (guest_)
guest_->SetTooltipText(tooltip_text);
}
void RenderWidgetHostViewGuest::OnSwapCompositorFrame(
uint32 output_surface_id,
scoped_ptr<cc::CompositorFrame> frame) {
if (!guest_)
return;
last_scroll_offset_ = frame->metadata.root_scroll_offset;
guest_->SwapCompositorFrame(output_surface_id,
host_->GetProcess()->GetID(),
host_->GetRoutingID(),
frame.Pass());
}
bool RenderWidgetHostViewGuest::OnMessageReceived(const IPC::Message& msg) {
return platform_view_->OnMessageReceived(msg);
}
void RenderWidgetHostViewGuest::InitAsChild(
gfx::NativeView parent_view) {
platform_view_->InitAsChild(parent_view);
}
void RenderWidgetHostViewGuest::InitAsPopup(
RenderWidgetHostView* parent_host_view, const gfx::Rect& pos) {
// This should never get called.
NOTREACHED();
}
void RenderWidgetHostViewGuest::InitAsFullscreen(
RenderWidgetHostView* reference_host_view) {
// This should never get called.
NOTREACHED();
}
gfx::NativeView RenderWidgetHostViewGuest::GetNativeView() const {
if (!guest_)
return gfx::NativeView();
RenderWidgetHostView* rwhv = guest_->GetOwnerRenderWidgetHostView();
if (!rwhv)
return gfx::NativeView();
return rwhv->GetNativeView();
}
gfx::NativeViewId RenderWidgetHostViewGuest::GetNativeViewId() const {
if (!guest_)
return static_cast<gfx::NativeViewId>(NULL);
RenderWidgetHostView* rwhv = guest_->GetOwnerRenderWidgetHostView();
if (!rwhv)
return static_cast<gfx::NativeViewId>(NULL);
return rwhv->GetNativeViewId();
}
gfx::NativeViewAccessible RenderWidgetHostViewGuest::GetNativeViewAccessible() {
if (!guest_)
return gfx::NativeViewAccessible();
RenderWidgetHostView* rwhv = guest_->GetOwnerRenderWidgetHostView();
if (!rwhv)
return gfx::NativeViewAccessible();
return rwhv->GetNativeViewAccessible();
}
void RenderWidgetHostViewGuest::MovePluginWindows(
const std::vector<WebPluginGeometry>& moves) {
platform_view_->MovePluginWindows(moves);
}
void RenderWidgetHostViewGuest::UpdateCursor(const WebCursor& cursor) {
// InterstitialPages are not WebContents so we cannot intercept
// ViewHostMsg_SetCursor for interstitial pages in BrowserPluginGuest.
// All guest RenderViewHosts have RenderWidgetHostViewGuests however,
// and so we will always hit this code path.
if (!guest_)
return;
guest_->SendMessageToEmbedder(
new BrowserPluginMsg_SetCursor(guest_->browser_plugin_instance_id(),
cursor));
}
void RenderWidgetHostViewGuest::SetIsLoading(bool is_loading) {
platform_view_->SetIsLoading(is_loading);
}
void RenderWidgetHostViewGuest::TextInputTypeChanged(
ui::TextInputType type,
ui::TextInputMode input_mode,
bool can_compose_inline,
int flags) {
if (!guest_)
return;
RenderWidgetHostViewBase* rwhv = GetOwnerRenderWidgetHostView();
if (!rwhv)
return;
// Forward the information to embedding RWHV.
rwhv->TextInputTypeChanged(type, input_mode, can_compose_inline, flags);
}
void RenderWidgetHostViewGuest::ImeCancelComposition() {
if (!guest_)
return;
RenderWidgetHostViewBase* rwhv = GetOwnerRenderWidgetHostView();
if (!rwhv)
return;
// Forward the information to embedding RWHV.
rwhv->ImeCancelComposition();
}
#if defined(OS_MACOSX) || defined(USE_AURA)
void RenderWidgetHostViewGuest::ImeCompositionRangeChanged(
const gfx::Range& range,
const std::vector<gfx::Rect>& character_bounds) {
if (!guest_)
return;
RenderWidgetHostViewBase* rwhv = GetOwnerRenderWidgetHostView();
if (!rwhv)
return;
std::vector<gfx::Rect> guest_character_bounds;
for (size_t i = 0; i < character_bounds.size(); ++i) {
guest_character_bounds.push_back(gfx::Rect(
guest_->GetScreenCoordinates(character_bounds[i].origin()),
character_bounds[i].size()));
}
// Forward the information to embedding RWHV.
rwhv->ImeCompositionRangeChanged(range, guest_character_bounds);
}
#endif
void RenderWidgetHostViewGuest::SelectionChanged(const base::string16& text,
size_t offset,
const gfx::Range& range) {
platform_view_->SelectionChanged(text, offset, range);
}
void RenderWidgetHostViewGuest::SelectionBoundsChanged(
const ViewHostMsg_SelectionBounds_Params& params) {
if (!guest_)
return;
RenderWidgetHostViewBase* rwhv = GetOwnerRenderWidgetHostView();
if (!rwhv)
return;
ViewHostMsg_SelectionBounds_Params guest_params(params);
guest_params.anchor_rect.set_origin(
guest_->GetScreenCoordinates(params.anchor_rect.origin()));
guest_params.focus_rect.set_origin(
guest_->GetScreenCoordinates(params.focus_rect.origin()));
rwhv->SelectionBoundsChanged(guest_params);
}
void RenderWidgetHostViewGuest::SetBackgroundColor(SkColor color) {
// Content embedders can toggle opaque backgrounds through this API.
// We plumb the value here so that BrowserPlugin updates its compositing
// state in response to this change. We also want to preserve this flag
// after recovering from a crash so we let BrowserPluginGuest store it.
if (!guest_)
return;
RenderWidgetHostViewBase::SetBackgroundColor(color);
bool opaque = GetBackgroundOpaque();
host_->SetBackgroundOpaque(opaque);
guest_->SetContentsOpaque(opaque);
}
bool RenderWidgetHostViewGuest::LockMouse() {
return platform_view_->LockMouse();
}
void RenderWidgetHostViewGuest::UnlockMouse() {
return platform_view_->UnlockMouse();
}
void RenderWidgetHostViewGuest::GetScreenInfo(blink::WebScreenInfo* results) {
if (!guest_)
return;
RenderWidgetHostViewBase* embedder_view = GetOwnerRenderWidgetHostView();
if (embedder_view)
embedder_view->GetScreenInfo(results);
}
#if defined(OS_MACOSX)
void RenderWidgetHostViewGuest::SetActive(bool active) {
platform_view_->SetActive(active);
}
void RenderWidgetHostViewGuest::SetWindowVisibility(bool visible) {
platform_view_->SetWindowVisibility(visible);
}
void RenderWidgetHostViewGuest::WindowFrameChanged() {
platform_view_->WindowFrameChanged();
}
void RenderWidgetHostViewGuest::ShowDefinitionForSelection() {
if (!guest_)
return;
gfx::Point origin;
gfx::Rect guest_bounds = GetViewBounds();
RenderWidgetHostView* rwhv = guest_->GetOwnerRenderWidgetHostView();
gfx::Rect embedder_bounds;
if (rwhv)
embedder_bounds = rwhv->GetViewBounds();
gfx::Vector2d guest_offset = gfx::Vector2d(
// Horizontal offset of guest from embedder.
guest_bounds.x() - embedder_bounds.x(),
// Vertical offset from guest's top to embedder's bottom edge.
embedder_bounds.bottom() - guest_bounds.y());
RenderWidgetHostViewMacDictionaryHelper helper(platform_view_.get());
helper.SetTargetView(rwhv);
helper.set_offset(guest_offset);
helper.ShowDefinitionForSelection();
}
bool RenderWidgetHostViewGuest::SupportsSpeech() const {
return platform_view_->SupportsSpeech();
}
void RenderWidgetHostViewGuest::SpeakSelection() {
platform_view_->SpeakSelection();
}
bool RenderWidgetHostViewGuest::IsSpeaking() const {
return platform_view_->IsSpeaking();
}
void RenderWidgetHostViewGuest::StopSpeaking() {
platform_view_->StopSpeaking();
}
bool RenderWidgetHostViewGuest::PostProcessEventForPluginIme(
const NativeWebKeyboardEvent& event) {
return false;
}
#endif // defined(OS_MACOSX)
#if defined(OS_ANDROID) || defined(TOOLKIT_VIEWS)
void RenderWidgetHostViewGuest::ShowDisambiguationPopup(
const gfx::Rect& rect_pixels,
const SkBitmap& zoomed_bitmap) {
}
#endif // defined(OS_ANDROID) || defined(TOOLKIT_VIEWS)
#if defined(OS_ANDROID)
void RenderWidgetHostViewGuest::LockCompositingSurface() {
}
void RenderWidgetHostViewGuest::UnlockCompositingSurface() {
}
#endif // defined(OS_ANDROID)
#if defined(OS_WIN)
void RenderWidgetHostViewGuest::SetParentNativeViewAccessible(
gfx::NativeViewAccessible accessible_parent) {
}
gfx::NativeViewId RenderWidgetHostViewGuest::GetParentForWindowlessPlugin()
const {
return NULL;
}
#endif
void RenderWidgetHostViewGuest::DestroyGuestView() {
host_->SetView(NULL);
host_ = NULL;
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}
bool RenderWidgetHostViewGuest::CanDispatchToConsumer(
ui::GestureConsumer* consumer) {
CHECK_EQ(static_cast<RenderWidgetHostViewGuest*>(consumer), this);
return true;
}
void RenderWidgetHostViewGuest::DispatchGestureEvent(
ui::GestureEvent* event) {
ForwardGestureEventToRenderer(event);
}
void RenderWidgetHostViewGuest::DispatchCancelTouchEvent(
ui::TouchEvent* event) {
if (!host_)
return;
blink::WebTouchEvent cancel_event;
// TODO(rbyers): This event has no touches in it. Don't we need to know what
// touches are currently active in order to cancel them all properly?
WebTouchEventTraits::ResetType(blink::WebInputEvent::TouchCancel,
event->time_stamp().InSecondsF(),
&cancel_event);
host_->ForwardTouchEventWithLatencyInfo(cancel_event, *event->latency());
}
bool RenderWidgetHostViewGuest::ForwardGestureEventToRenderer(
ui::GestureEvent* gesture) {
#if defined(USE_AURA)
if (!host_)
return false;
if ((gesture->type() == ui::ET_GESTURE_PINCH_BEGIN ||
gesture->type() == ui::ET_GESTURE_PINCH_UPDATE ||
gesture->type() == ui::ET_GESTURE_PINCH_END) && !pinch_zoom_enabled_) {
return true;
}
blink::WebGestureEvent web_gesture =
MakeWebGestureEventFromUIEvent(*gesture);
const gfx::Point& client_point = gesture->location();
const gfx::Point& screen_point = gesture->location();
web_gesture.x = client_point.x();
web_gesture.y = client_point.y();
web_gesture.globalX = screen_point.x();
web_gesture.globalY = screen_point.y();
if (web_gesture.type == blink::WebGestureEvent::Undefined)
return false;
if (web_gesture.type == blink::WebGestureEvent::GestureTapDown) {
host_->ForwardGestureEvent(
CreateFlingCancelEvent(gesture->time_stamp().InSecondsF()));
}
host_->ForwardGestureEvent(web_gesture);
return true;
#else
return false;
#endif
}
void RenderWidgetHostViewGuest::ProcessGestures(
ui::GestureRecognizer::Gestures* gestures) {
if ((gestures == NULL) || gestures->empty())
return;
for (ui::GestureRecognizer::Gestures::iterator g_it = gestures->begin();
g_it != gestures->end();
++g_it) {
ForwardGestureEventToRenderer(*g_it);
}
}
SkColorType RenderWidgetHostViewGuest::PreferredReadbackFormat() {
return kN32_SkColorType;
}
RenderWidgetHostViewBase*
RenderWidgetHostViewGuest::GetOwnerRenderWidgetHostView() const {
return static_cast<RenderWidgetHostViewBase*>(
guest_->GetOwnerRenderWidgetHostView());
}
void RenderWidgetHostViewGuest::OnHandleInputEvent(
RenderWidgetHostImpl* embedder,
int browser_plugin_instance_id,
const gfx::Rect& guest_window_rect,
const blink::WebInputEvent* event) {
if (blink::WebInputEvent::isMouseEventType(event->type)) {
host_->ForwardMouseEvent(
*static_cast<const blink::WebMouseEvent*>(event));
return;
}
if (event->type == blink::WebInputEvent::MouseWheel) {
host_->ForwardWheelEvent(
*static_cast<const blink::WebMouseWheelEvent*>(event));
return;
}
if (blink::WebInputEvent::isKeyboardEventType(event->type)) {
if (!embedder->GetLastKeyboardEvent())
return;
NativeWebKeyboardEvent keyboard_event(*embedder->GetLastKeyboardEvent());
host_->ForwardKeyboardEvent(keyboard_event);
return;
}
if (blink::WebInputEvent::isTouchEventType(event->type)) {
host_->ForwardTouchEventWithLatencyInfo(
*static_cast<const blink::WebTouchEvent*>(event),
ui::LatencyInfo());
return;
}
if (blink::WebInputEvent::isGestureEventType(event->type)) {
host_->ForwardGestureEvent(
*static_cast<const blink::WebGestureEvent*>(event));
return;
}
}
} // namespace content
|