Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / browser_plugin / browser_plugin_guest.cc
blob5fde66c951d3927a7f2612a621ded15c162d794a
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/browser_plugin/browser_plugin_guest.h"
7 #include <algorithm>
9 #include "base/message_loop/message_loop.h"
10 #include "base/pickle.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "content/browser/browser_plugin/browser_plugin_embedder.h"
13 #include "content/browser/browser_thread_impl.h"
14 #include "content/browser/child_process_security_policy_impl.h"
15 #include "content/browser/frame_host/render_frame_host_impl.h"
16 #include "content/browser/frame_host/render_widget_host_view_guest.h"
17 #include "content/browser/loader/resource_dispatcher_host_impl.h"
18 #include "content/browser/renderer_host/render_view_host_impl.h"
19 #include "content/browser/renderer_host/render_widget_host_impl.h"
20 #include "content/browser/renderer_host/render_widget_host_view_base.h"
21 #include "content/browser/web_contents/web_contents_impl.h"
22 #include "content/browser/web_contents/web_contents_view_guest.h"
23 #include "content/common/browser_plugin/browser_plugin_constants.h"
24 #include "content/common/browser_plugin/browser_plugin_messages.h"
25 #include "content/common/content_constants_internal.h"
26 #include "content/common/drag_messages.h"
27 #include "content/common/frame_messages.h"
28 #include "content/common/host_shared_bitmap_manager.h"
29 #include "content/common/input_messages.h"
30 #include "content/common/view_messages.h"
31 #include "content/public/browser/browser_context.h"
32 #include "content/public/browser/browser_plugin_guest_manager.h"
33 #include "content/public/browser/content_browser_client.h"
34 #include "content/public/browser/guest_host.h"
35 #include "content/public/browser/render_widget_host_view.h"
36 #include "content/public/browser/user_metrics.h"
37 #include "content/public/browser/web_contents_observer.h"
38 #include "content/public/common/drop_data.h"
39 #include "ui/gfx/geometry/size_conversions.h"
41 #if defined(OS_MACOSX)
42 #include "content/browser/browser_plugin/browser_plugin_popup_menu_helper_mac.h"
43 #endif
45 namespace content {
47 class BrowserPluginGuest::EmbedderVisibilityObserver
48 : public WebContentsObserver {
49 public:
50 explicit EmbedderVisibilityObserver(BrowserPluginGuest* guest)
51 : WebContentsObserver(guest->embedder_web_contents()),
52 browser_plugin_guest_(guest) {
55 ~EmbedderVisibilityObserver() override {}
57 // WebContentsObserver implementation.
58 void WasShown() override {
59 browser_plugin_guest_->EmbedderVisibilityChanged(true);
62 void WasHidden() override {
63 browser_plugin_guest_->EmbedderVisibilityChanged(false);
66 private:
67 BrowserPluginGuest* browser_plugin_guest_;
69 DISALLOW_COPY_AND_ASSIGN(EmbedderVisibilityObserver);
72 BrowserPluginGuest::BrowserPluginGuest(bool has_render_view,
73 WebContentsImpl* web_contents,
74 BrowserPluginGuestDelegate* delegate)
75 : WebContentsObserver(web_contents),
76 owner_web_contents_(nullptr),
77 attached_(false),
78 browser_plugin_instance_id_(browser_plugin::kInstanceIDNone),
79 focused_(false),
80 mouse_locked_(false),
81 pending_lock_request_(false),
82 guest_visible_(false),
83 embedder_visible_(true),
84 is_full_page_plugin_(false),
85 has_render_view_(has_render_view),
86 is_in_destruction_(false),
87 initialized_(false),
88 last_text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
89 last_input_mode_(ui::TEXT_INPUT_MODE_DEFAULT),
90 last_input_flags_(0),
91 last_can_compose_inline_(true),
92 guest_proxy_routing_id_(MSG_ROUTING_NONE),
93 last_drag_status_(blink::WebDragStatusUnknown),
94 seen_embedder_system_drag_ended_(false),
95 seen_embedder_drag_source_ended_at_(false),
96 delegate_(delegate),
97 weak_ptr_factory_(this) {
98 DCHECK(web_contents);
99 DCHECK(delegate);
100 RecordAction(base::UserMetricsAction("BrowserPlugin.Guest.Create"));
101 web_contents->SetBrowserPluginGuest(this);
102 delegate->SetGuestHost(this);
105 int BrowserPluginGuest::GetGuestProxyRoutingID() {
106 if (guest_proxy_routing_id_ != MSG_ROUTING_NONE)
107 return guest_proxy_routing_id_;
109 // Create a swapped out RenderView for the guest in the embedder renderer
110 // process, so that the embedder can access the guest's window object.
111 // On reattachment, we can reuse the same swapped out RenderView because
112 // the embedder process will always be the same even if the embedder
113 // WebContents changes.
115 // TODO(fsamuel): Make sure this works for transferring guests across
116 // owners in different processes. We probably need to clear the
117 // |guest_proxy_routing_id_| and perform any necessary cleanup on Detach
118 // to enable this.
119 SiteInstance* owner_site_instance = owner_web_contents_->GetSiteInstance();
120 guest_proxy_routing_id_ =
121 GetWebContents()->CreateSwappedOutRenderView(owner_site_instance);
123 return guest_proxy_routing_id_;
126 int BrowserPluginGuest::LoadURLWithParams(
127 const NavigationController::LoadURLParams& load_params) {
128 GetWebContents()->GetController().LoadURLWithParams(load_params);
129 return GetGuestProxyRoutingID();
132 void BrowserPluginGuest::SizeContents(const gfx::Size& new_size) {
133 GetWebContents()->GetView()->SizeContents(new_size);
136 void BrowserPluginGuest::WillDestroy() {
137 is_in_destruction_ = true;
138 owner_web_contents_ = nullptr;
139 attached_ = false;
142 void BrowserPluginGuest::Init() {
143 if (initialized_)
144 return;
145 initialized_ = true;
147 // TODO(fsamuel): Initiailization prior to attachment should be behind a
148 // command line flag once we introduce experimental guest types that rely on
149 // this functionality.
150 if (!delegate_->CanRunInDetachedState())
151 return;
153 WebContentsImpl* owner_web_contents = static_cast<WebContentsImpl*>(
154 delegate_->GetOwnerWebContents());
155 owner_web_contents->CreateBrowserPluginEmbedderIfNecessary();
156 InitInternal(BrowserPluginHostMsg_Attach_Params(), owner_web_contents);
159 base::WeakPtr<BrowserPluginGuest> BrowserPluginGuest::AsWeakPtr() {
160 return weak_ptr_factory_.GetWeakPtr();
163 void BrowserPluginGuest::SetFocus(RenderWidgetHost* rwh,
164 bool focused,
165 blink::WebFocusType focus_type) {
166 focused_ = focused;
167 if (!rwh)
168 return;
170 if ((focus_type == blink::WebFocusTypeForward) ||
171 (focus_type == blink::WebFocusTypeBackward)) {
172 static_cast<RenderViewHostImpl*>(RenderViewHost::From(rwh))->
173 SetInitialFocus(focus_type == blink::WebFocusTypeBackward);
175 rwh->Send(new InputMsg_SetFocus(rwh->GetRoutingID(), focused));
176 if (!focused && mouse_locked_)
177 OnUnlockMouse();
179 // Restore the last seen state of text input to the view.
180 RenderWidgetHostViewBase* rwhv = static_cast<RenderWidgetHostViewBase*>(
181 rwh->GetView());
182 SendTextInputTypeChangedToView(rwhv);
185 void BrowserPluginGuest::SetTooltipText(const base::string16& tooltip_text) {
186 if (tooltip_text == current_tooltip_text_)
187 return;
188 current_tooltip_text_ = tooltip_text;
190 SendMessageToEmbedder(new BrowserPluginMsg_SetTooltipText(
191 browser_plugin_instance_id_, tooltip_text));
194 bool BrowserPluginGuest::LockMouse(bool allowed) {
195 if (!attached() || (mouse_locked_ == allowed))
196 return false;
198 return embedder_web_contents()->GotResponseToLockMouseRequest(allowed);
201 WebContentsImpl* BrowserPluginGuest::CreateNewGuestWindow(
202 const WebContents::CreateParams& params) {
203 WebContentsImpl* new_contents =
204 static_cast<WebContentsImpl*>(delegate_->CreateNewGuestWindow(params));
205 DCHECK(new_contents);
206 return new_contents;
209 bool BrowserPluginGuest::OnMessageReceivedFromEmbedder(
210 const IPC::Message& message) {
211 RenderWidgetHostViewGuest* rwhv = static_cast<RenderWidgetHostViewGuest*>(
212 web_contents()->GetRenderWidgetHostView());
213 if (rwhv &&
214 rwhv->OnMessageReceivedFromEmbedder(
215 message,
216 static_cast<RenderViewHostImpl*>(
217 embedder_web_contents()->GetRenderViewHost()))) {
218 return true;
221 bool handled = true;
222 IPC_BEGIN_MESSAGE_MAP(BrowserPluginGuest, message)
223 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_CompositorFrameSwappedACK,
224 OnCompositorFrameSwappedACK)
225 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_Detach, OnDetach)
226 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_DragStatusUpdate,
227 OnDragStatusUpdate)
228 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ExecuteEditCommand,
229 OnExecuteEditCommand)
230 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ExtendSelectionAndDelete,
231 OnExtendSelectionAndDelete)
232 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ImeConfirmComposition,
233 OnImeConfirmComposition)
234 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ImeSetComposition,
235 OnImeSetComposition)
236 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_LockMouse_ACK, OnLockMouseAck)
237 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ReclaimCompositorResources,
238 OnReclaimCompositorResources)
239 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_SetEditCommandsForNextKeyEvent,
240 OnSetEditCommandsForNextKeyEvent)
241 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_SetFocus, OnSetFocus)
242 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_SetVisibility, OnSetVisibility)
243 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_UnlockMouse_ACK, OnUnlockMouseAck)
244 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_UpdateGeometry, OnUpdateGeometry)
245 IPC_MESSAGE_UNHANDLED(handled = false)
246 IPC_END_MESSAGE_MAP()
247 return handled;
250 void BrowserPluginGuest::InitInternal(
251 const BrowserPluginHostMsg_Attach_Params& params,
252 WebContentsImpl* owner_web_contents) {
253 focused_ = params.focused;
254 OnSetFocus(browser_plugin::kInstanceIDNone,
255 focused_,
256 blink::WebFocusTypeNone);
258 guest_visible_ = params.visible;
259 UpdateVisibility();
261 is_full_page_plugin_ = params.is_full_page_plugin;
262 guest_window_rect_ = params.view_rect;
264 if (owner_web_contents_ != owner_web_contents) {
265 WebContentsViewGuest* new_view =
266 static_cast<WebContentsViewGuest*>(GetWebContents()->GetView());
267 if (owner_web_contents_)
268 new_view->OnGuestDetached(owner_web_contents_->GetView());
270 // Once a BrowserPluginGuest has an embedder WebContents, it's considered to
271 // be attached.
272 owner_web_contents_ = owner_web_contents;
273 new_view->OnGuestAttached(owner_web_contents_->GetView());
276 RendererPreferences* renderer_prefs =
277 GetWebContents()->GetMutableRendererPrefs();
278 std::string guest_user_agent_override = renderer_prefs->user_agent_override;
279 // Copy renderer preferences (and nothing else) from the embedder's
280 // WebContents to the guest.
282 // For GTK and Aura this is necessary to get proper renderer configuration
283 // values for caret blinking interval, colors related to selection and
284 // focus.
285 *renderer_prefs = *owner_web_contents_->GetMutableRendererPrefs();
286 renderer_prefs->user_agent_override = guest_user_agent_override;
288 // We would like the guest to report changes to frame names so that we can
289 // update the BrowserPlugin's corresponding 'name' attribute.
290 // TODO(fsamuel): Remove this once http://crbug.com/169110 is addressed.
291 renderer_prefs->report_frame_name_changes = true;
292 // Navigation is disabled in Chrome Apps. We want to make sure guest-initiated
293 // navigations still continue to function inside the app.
294 renderer_prefs->browser_handles_all_top_level_requests = false;
295 // Disable "client blocked" error page for browser plugin.
296 renderer_prefs->disable_client_blocked_error_page = true;
298 embedder_visibility_observer_.reset(new EmbedderVisibilityObserver(this));
300 DCHECK(GetWebContents()->GetRenderViewHost());
302 // Initialize the device scale factor by calling |NotifyScreenInfoChanged|.
303 auto render_widget_host =
304 RenderWidgetHostImpl::From(GetWebContents()->GetRenderViewHost());
305 render_widget_host->NotifyScreenInfoChanged();
307 // TODO(chrishtr): this code is wrong. The navigate_on_drag_drop field will
308 // be reset again the next time preferences are updated.
309 WebPreferences prefs =
310 GetWebContents()->GetRenderViewHost()->GetWebkitPreferences();
311 prefs.navigate_on_drag_drop = false;
312 GetWebContents()->GetRenderViewHost()->UpdateWebkitPreferences(prefs);
315 BrowserPluginGuest::~BrowserPluginGuest() {
318 // static
319 BrowserPluginGuest* BrowserPluginGuest::Create(
320 WebContentsImpl* web_contents,
321 BrowserPluginGuestDelegate* delegate) {
322 return new BrowserPluginGuest(
323 web_contents->opener() != nullptr, web_contents, delegate);
326 // static
327 bool BrowserPluginGuest::IsGuest(WebContentsImpl* web_contents) {
328 return web_contents && web_contents->GetBrowserPluginGuest();
331 // static
332 bool BrowserPluginGuest::IsGuest(RenderViewHostImpl* render_view_host) {
333 return render_view_host && IsGuest(
334 static_cast<WebContentsImpl*>(WebContents::FromRenderViewHost(
335 render_view_host)));
338 RenderWidgetHostView* BrowserPluginGuest::GetOwnerRenderWidgetHostView() {
339 if (!owner_web_contents_)
340 return nullptr;
341 return owner_web_contents_->GetRenderWidgetHostView();
344 void BrowserPluginGuest::UpdateVisibility() {
345 OnSetVisibility(browser_plugin_instance_id(), visible());
348 BrowserPluginGuestManager*
349 BrowserPluginGuest::GetBrowserPluginGuestManager() const {
350 return GetWebContents()->GetBrowserContext()->GetGuestManager();
353 void BrowserPluginGuest::EmbedderVisibilityChanged(bool visible) {
354 embedder_visible_ = visible;
355 UpdateVisibility();
358 void BrowserPluginGuest::PointerLockPermissionResponse(bool allow) {
359 SendMessageToEmbedder(
360 new BrowserPluginMsg_SetMouseLock(browser_plugin_instance_id(), allow));
363 void BrowserPluginGuest::SwapCompositorFrame(
364 uint32 output_surface_id,
365 int host_process_id,
366 int host_routing_id,
367 scoped_ptr<cc::CompositorFrame> frame) {
368 cc::RenderPass* root_pass =
369 frame->delegated_frame_data->render_pass_list.back();
370 gfx::Size view_size(gfx::ToFlooredSize(gfx::ScaleSize(
371 root_pass->output_rect.size(),
372 1.0f / frame->metadata.device_scale_factor)));
374 if (last_seen_view_size_ != view_size) {
375 delegate_->GuestSizeChanged(view_size);
376 last_seen_view_size_ = view_size;
379 last_pending_frame_.reset(new FrameMsg_CompositorFrameSwapped_Params());
380 frame->AssignTo(&last_pending_frame_->frame);
381 last_pending_frame_->output_surface_id = output_surface_id;
382 last_pending_frame_->producing_route_id = host_routing_id;
383 last_pending_frame_->producing_host_id = host_process_id;
385 SendMessageToEmbedder(
386 new BrowserPluginMsg_CompositorFrameSwapped(
387 browser_plugin_instance_id(), *last_pending_frame_));
390 void BrowserPluginGuest::SetContentsOpaque(bool opaque) {
391 SendMessageToEmbedder(
392 new BrowserPluginMsg_SetContentsOpaque(
393 browser_plugin_instance_id(), opaque));
396 bool BrowserPluginGuest::Find(int request_id,
397 const base::string16& search_text,
398 const blink::WebFindOptions& options) {
399 return delegate_->Find(request_id, search_text, options);
402 bool BrowserPluginGuest::StopFinding(StopFindAction action) {
403 return delegate_->StopFinding(action);
406 WebContentsImpl* BrowserPluginGuest::GetWebContents() const {
407 return static_cast<WebContentsImpl*>(web_contents());
410 gfx::Point BrowserPluginGuest::GetScreenCoordinates(
411 const gfx::Point& relative_position) const {
412 if (!attached())
413 return relative_position;
415 gfx::Point screen_pos(relative_position);
416 screen_pos += guest_window_rect_.OffsetFromOrigin();
417 if (embedder_web_contents()->GetBrowserPluginGuest()) {
418 BrowserPluginGuest* embedder_guest =
419 embedder_web_contents()->GetBrowserPluginGuest();
420 screen_pos += embedder_guest->guest_window_rect_.OffsetFromOrigin();
422 return screen_pos;
425 void BrowserPluginGuest::SendMessageToEmbedder(IPC::Message* msg) {
426 if (!attached()) {
427 // Some pages such as data URLs, javascript URLs, and about:blank
428 // do not load external resources and so they load prior to attachment.
429 // As a result, we must save all these IPCs until attachment and then
430 // forward them so that the embedder gets a chance to see and process
431 // the load events.
432 pending_messages_.push_back(linked_ptr<IPC::Message>(msg));
433 return;
435 owner_web_contents_->Send(msg);
438 void BrowserPluginGuest::DragSourceEndedAt(int client_x, int client_y,
439 int screen_x, int screen_y, blink::WebDragOperation operation) {
440 web_contents()->GetRenderViewHost()->DragSourceEndedAt(client_x, client_y,
441 screen_x, screen_y, operation);
442 seen_embedder_drag_source_ended_at_ = true;
443 EndSystemDragIfApplicable();
446 void BrowserPluginGuest::EndSystemDragIfApplicable() {
447 // Ideally we'd want either WebDragStatusDrop or WebDragStatusLeave...
448 // Call guest RVH->DragSourceSystemDragEnded() correctly on the guest where
449 // the drag was initiated. Calling DragSourceSystemDragEnded() correctly
450 // means we call it in all cases and also make sure we only call it once.
451 // This ensures that the input state of the guest stays correct, otherwise
452 // it will go stale and won't accept any further input events.
454 // The strategy used here to call DragSourceSystemDragEnded() on the RVH
455 // is when the following conditions are met:
456 // a. Embedder has seen SystemDragEnded()
457 // b. Embedder has seen DragSourceEndedAt()
458 // c. The guest has seen some drag status update other than
459 // WebDragStatusUnknown. Note that this step should ideally be done
460 // differently: The guest has seen at least one of
461 // {WebDragStatusOver, WebDragStatusDrop}. However, if a user drags
462 // a source quickly outside of <webview> bounds, then the
463 // BrowserPluginGuest never sees any of these drag status updates,
464 // there we just check whether we've seen any drag status update or
465 // not.
466 if (last_drag_status_ != blink::WebDragStatusOver &&
467 seen_embedder_drag_source_ended_at_ && seen_embedder_system_drag_ended_) {
468 RenderViewHostImpl* guest_rvh = static_cast<RenderViewHostImpl*>(
469 GetWebContents()->GetRenderViewHost());
470 guest_rvh->DragSourceSystemDragEnded();
471 last_drag_status_ = blink::WebDragStatusUnknown;
472 seen_embedder_system_drag_ended_ = false;
473 seen_embedder_drag_source_ended_at_ = false;
474 dragged_url_ = GURL();
478 void BrowserPluginGuest::EmbedderSystemDragEnded() {
479 seen_embedder_system_drag_ended_ = true;
480 EndSystemDragIfApplicable();
483 void BrowserPluginGuest::SendQueuedMessages() {
484 if (!attached())
485 return;
487 while (!pending_messages_.empty()) {
488 linked_ptr<IPC::Message> message_ptr = pending_messages_.front();
489 pending_messages_.pop_front();
490 SendMessageToEmbedder(message_ptr.release());
494 void BrowserPluginGuest::SendTextInputTypeChangedToView(
495 RenderWidgetHostViewBase* guest_rwhv) {
496 if (!guest_rwhv)
497 return;
499 guest_rwhv->TextInputTypeChanged(last_text_input_type_, last_input_mode_,
500 last_can_compose_inline_, last_input_flags_);
501 // Enable input method for guest if it's enabled for the embedder.
502 if (!static_cast<RenderViewHostImpl*>(
503 owner_web_contents_->GetRenderViewHost())->input_method_active()) {
504 return;
506 RenderViewHostImpl* guest_rvh =
507 static_cast<RenderViewHostImpl*>(GetWebContents()->GetRenderViewHost());
508 guest_rvh->SetInputMethodActive(true);
511 void BrowserPluginGuest::DidCommitProvisionalLoadForFrame(
512 RenderFrameHost* render_frame_host,
513 const GURL& url,
514 ui::PageTransition transition_type) {
515 RecordAction(base::UserMetricsAction("BrowserPlugin.Guest.DidNavigate"));
518 void BrowserPluginGuest::RenderViewReady() {
519 RenderViewHost* rvh = GetWebContents()->GetRenderViewHost();
520 // TODO(fsamuel): Investigate whether it's possible to update state earlier
521 // here (see http://crbug.com/158151).
522 Send(new InputMsg_SetFocus(routing_id(), focused_));
523 UpdateVisibility();
525 RenderWidgetHostImpl::From(rvh)->set_hung_renderer_delay(
526 base::TimeDelta::FromMilliseconds(kHungRendererDelayMs));
529 void BrowserPluginGuest::RenderProcessGone(base::TerminationStatus status) {
530 SendMessageToEmbedder(
531 new BrowserPluginMsg_GuestGone(browser_plugin_instance_id()));
532 switch (status) {
533 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED:
534 RecordAction(base::UserMetricsAction("BrowserPlugin.Guest.Killed"));
535 break;
536 case base::TERMINATION_STATUS_PROCESS_CRASHED:
537 RecordAction(base::UserMetricsAction("BrowserPlugin.Guest.Crashed"));
538 break;
539 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
540 RecordAction(
541 base::UserMetricsAction("BrowserPlugin.Guest.AbnormalDeath"));
542 break;
543 default:
544 break;
548 // static
549 bool BrowserPluginGuest::ShouldForwardToBrowserPluginGuest(
550 const IPC::Message& message) {
551 switch (message.type()) {
552 case BrowserPluginHostMsg_CompositorFrameSwappedACK::ID:
553 case BrowserPluginHostMsg_Detach::ID:
554 case BrowserPluginHostMsg_DragStatusUpdate::ID:
555 case BrowserPluginHostMsg_ExecuteEditCommand::ID:
556 case BrowserPluginHostMsg_ExtendSelectionAndDelete::ID:
557 case BrowserPluginHostMsg_HandleInputEvent::ID:
558 case BrowserPluginHostMsg_ImeConfirmComposition::ID:
559 case BrowserPluginHostMsg_ImeSetComposition::ID:
560 case BrowserPluginHostMsg_LockMouse_ACK::ID:
561 case BrowserPluginHostMsg_ReclaimCompositorResources::ID:
562 case BrowserPluginHostMsg_SetEditCommandsForNextKeyEvent::ID:
563 case BrowserPluginHostMsg_SetFocus::ID:
564 case BrowserPluginHostMsg_SetVisibility::ID:
565 case BrowserPluginHostMsg_UnlockMouse_ACK::ID:
566 case BrowserPluginHostMsg_UpdateGeometry::ID:
567 return true;
568 default:
569 return false;
573 bool BrowserPluginGuest::OnMessageReceived(const IPC::Message& message) {
574 bool handled = true;
575 IPC_BEGIN_MESSAGE_MAP(BrowserPluginGuest, message)
576 IPC_MESSAGE_HANDLER(InputHostMsg_ImeCancelComposition,
577 OnImeCancelComposition)
578 #if defined(OS_MACOSX) || defined(USE_AURA)
579 IPC_MESSAGE_HANDLER(InputHostMsg_ImeCompositionRangeChanged,
580 OnImeCompositionRangeChanged)
581 #endif
582 IPC_MESSAGE_HANDLER(ViewHostMsg_HasTouchEventHandlers,
583 OnHasTouchEventHandlers)
584 IPC_MESSAGE_HANDLER(ViewHostMsg_LockMouse, OnLockMouse)
585 IPC_MESSAGE_HANDLER(ViewHostMsg_ShowWidget, OnShowWidget)
586 IPC_MESSAGE_HANDLER(ViewHostMsg_TakeFocus, OnTakeFocus)
587 IPC_MESSAGE_HANDLER(ViewHostMsg_TextInputTypeChanged,
588 OnTextInputTypeChanged)
589 IPC_MESSAGE_HANDLER(ViewHostMsg_UnlockMouse, OnUnlockMouse)
590 IPC_MESSAGE_UNHANDLED(handled = false)
591 IPC_END_MESSAGE_MAP()
592 return handled;
595 bool BrowserPluginGuest::OnMessageReceived(const IPC::Message& message,
596 RenderFrameHost* render_frame_host) {
597 // This will eventually be the home for more IPC handlers that depend on
598 // RenderFrameHost. Until more are moved here, though, the IPC_* macros won't
599 // compile if there are no handlers for a platform. So we have both #if guards
600 // around the whole thing (unfortunate but temporary), and #if guards where
601 // they belong, only around the one IPC handler. TODO(avi): Move more of the
602 // frame-based handlers to this function and remove the outer #if layer.
603 #if defined(OS_MACOSX)
604 bool handled = true;
605 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(BrowserPluginGuest, message,
606 render_frame_host)
607 #if defined(OS_MACOSX)
608 // MacOS X creates and populates platform-specific select drop-down menus
609 // whereas other platforms merely create a popup window that the guest
610 // renderer process paints inside.
611 IPC_MESSAGE_HANDLER(FrameHostMsg_ShowPopup, OnShowPopup)
612 #endif
613 IPC_MESSAGE_UNHANDLED(handled = false)
614 IPC_END_MESSAGE_MAP()
615 return handled;
616 #else
617 return false;
618 #endif
621 void BrowserPluginGuest::Attach(
622 int browser_plugin_instance_id,
623 WebContentsImpl* embedder_web_contents,
624 const BrowserPluginHostMsg_Attach_Params& params) {
625 browser_plugin_instance_id_ = browser_plugin_instance_id;
626 // If a guest is detaching from one container and attaching to another
627 // container, then late arriving ACKs may be lost if the mapping from
628 // |browser_plugin_instance_id| to |guest_instance_id| changes. Thus we
629 // ensure that we always get new frames on attachment by ACKing the pending
630 // frame if it's still waiting on the ACK.
631 if (last_pending_frame_) {
632 cc::CompositorFrameAck ack;
633 RenderWidgetHostImpl::SendSwapCompositorFrameAck(
634 last_pending_frame_->producing_route_id,
635 last_pending_frame_->output_surface_id,
636 last_pending_frame_->producing_host_id,
637 ack);
638 last_pending_frame_.reset();
640 delegate_->WillAttach(embedder_web_contents, browser_plugin_instance_id,
641 params.is_full_page_plugin);
643 // If a RenderView has already been created for this new window, then we need
644 // to initialize the browser-side state now so that the RenderFrameHostManager
645 // does not create a new RenderView on navigation.
646 if (has_render_view_) {
647 // This will trigger a callback to RenderViewReady after a round-trip IPC.
648 static_cast<RenderViewHostImpl*>(
649 GetWebContents()->GetRenderViewHost())->Init();
650 WebContentsViewGuest* web_contents_view =
651 static_cast<WebContentsViewGuest*>(GetWebContents()->GetView());
652 if (!web_contents()->GetRenderViewHost()->GetView()) {
653 web_contents_view->CreateViewForWidget(
654 web_contents()->GetRenderViewHost(), true);
658 InitInternal(params, embedder_web_contents);
660 attached_ = true;
661 SendQueuedMessages();
663 delegate_->DidAttach(GetGuestProxyRoutingID());
665 has_render_view_ = true;
667 // Enable input method for guest if it's enabled for the embedder.
668 if (static_cast<RenderViewHostImpl*>(
669 owner_web_contents_->GetRenderViewHost())->input_method_active()) {
670 RenderViewHostImpl* guest_rvh = static_cast<RenderViewHostImpl*>(
671 GetWebContents()->GetRenderViewHost());
672 guest_rvh->SetInputMethodActive(true);
675 RecordAction(base::UserMetricsAction("BrowserPlugin.Guest.Attached"));
678 void BrowserPluginGuest::OnCompositorFrameSwappedACK(
679 int browser_plugin_instance_id,
680 const FrameHostMsg_CompositorFrameSwappedACK_Params& params) {
681 RenderWidgetHostImpl::SendSwapCompositorFrameAck(params.producing_route_id,
682 params.output_surface_id,
683 params.producing_host_id,
684 params.ack);
685 last_pending_frame_.reset();
688 void BrowserPluginGuest::OnDetach(int browser_plugin_instance_id) {
689 if (!attached())
690 return;
692 // This tells BrowserPluginGuest to queue up all IPCs to BrowserPlugin until
693 // it's attached again.
694 attached_ = false;
696 delegate_->DidDetach();
699 void BrowserPluginGuest::OnDragStatusUpdate(int browser_plugin_instance_id,
700 blink::WebDragStatus drag_status,
701 const DropData& drop_data,
702 blink::WebDragOperationsMask mask,
703 const gfx::Point& location) {
704 RenderViewHost* host = GetWebContents()->GetRenderViewHost();
705 auto embedder = owner_web_contents_->GetBrowserPluginEmbedder();
706 switch (drag_status) {
707 case blink::WebDragStatusEnter:
708 // Only track the URL being dragged over the guest if the link isn't
709 // coming from the guest.
710 if (!embedder->DragEnteredGuest(this))
711 dragged_url_ = drop_data.url;
712 host->DragTargetDragEnter(drop_data, location, location, mask, 0);
713 break;
714 case blink::WebDragStatusOver:
715 host->DragTargetDragOver(location, location, mask, 0);
716 break;
717 case blink::WebDragStatusLeave:
718 embedder->DragLeftGuest(this);
719 host->DragTargetDragLeave();
720 break;
721 case blink::WebDragStatusDrop:
722 host->DragTargetDrop(location, location, 0);
723 if (dragged_url_.is_valid()) {
724 delegate_->DidDropLink(dragged_url_);
725 dragged_url_ = GURL();
727 break;
728 case blink::WebDragStatusUnknown:
729 NOTREACHED();
731 last_drag_status_ = drag_status;
732 EndSystemDragIfApplicable();
735 void BrowserPluginGuest::OnExecuteEditCommand(int browser_plugin_instance_id,
736 const std::string& name) {
737 RenderFrameHost* focused_frame = web_contents()->GetFocusedFrame();
738 if (!focused_frame)
739 return;
741 focused_frame->Send(new InputMsg_ExecuteNoValueEditCommand(
742 focused_frame->GetRoutingID(), name));
745 void BrowserPluginGuest::OnImeSetComposition(
746 int browser_plugin_instance_id,
747 const std::string& text,
748 const std::vector<blink::WebCompositionUnderline>& underlines,
749 int selection_start,
750 int selection_end) {
751 Send(new InputMsg_ImeSetComposition(routing_id(),
752 base::UTF8ToUTF16(text), underlines,
753 selection_start, selection_end));
756 void BrowserPluginGuest::OnImeConfirmComposition(
757 int browser_plugin_instance_id,
758 const std::string& text,
759 bool keep_selection) {
760 Send(new InputMsg_ImeConfirmComposition(routing_id(),
761 base::UTF8ToUTF16(text),
762 gfx::Range::InvalidRange(),
763 keep_selection));
766 void BrowserPluginGuest::OnExtendSelectionAndDelete(
767 int browser_plugin_instance_id,
768 int before,
769 int after) {
770 RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>(
771 web_contents()->GetFocusedFrame());
772 if (rfh)
773 rfh->ExtendSelectionAndDelete(before, after);
776 void BrowserPluginGuest::OnReclaimCompositorResources(
777 int browser_plugin_instance_id,
778 const FrameHostMsg_ReclaimCompositorResources_Params& params) {
779 RenderWidgetHostImpl::SendReclaimCompositorResources(params.route_id,
780 params.output_surface_id,
781 params.renderer_host_id,
782 params.ack);
785 void BrowserPluginGuest::OnLockMouse(bool user_gesture,
786 bool last_unlocked_by_target,
787 bool privileged) {
788 if (pending_lock_request_) {
789 // Immediately reject the lock because only one pointerLock may be active
790 // at a time.
791 Send(new ViewMsg_LockMouse_ACK(routing_id(), false));
792 return;
795 pending_lock_request_ = true;
797 delegate_->RequestPointerLockPermission(
798 user_gesture,
799 last_unlocked_by_target,
800 base::Bind(&BrowserPluginGuest::PointerLockPermissionResponse,
801 weak_ptr_factory_.GetWeakPtr()));
804 void BrowserPluginGuest::OnLockMouseAck(int browser_plugin_instance_id,
805 bool succeeded) {
806 Send(new ViewMsg_LockMouse_ACK(routing_id(), succeeded));
807 pending_lock_request_ = false;
808 if (succeeded)
809 mouse_locked_ = true;
812 void BrowserPluginGuest::OnSetFocus(int browser_plugin_instance_id,
813 bool focused,
814 blink::WebFocusType focus_type) {
815 RenderWidgetHostView* rwhv = web_contents()->GetRenderWidgetHostView();
816 RenderWidgetHost* rwh = rwhv ? rwhv->GetRenderWidgetHost() : nullptr;
817 SetFocus(rwh, focused, focus_type);
820 void BrowserPluginGuest::OnSetEditCommandsForNextKeyEvent(
821 int browser_plugin_instance_id,
822 const std::vector<EditCommand>& edit_commands) {
823 Send(new InputMsg_SetEditCommandsForNextKeyEvent(routing_id(),
824 edit_commands));
827 void BrowserPluginGuest::OnSetVisibility(int browser_plugin_instance_id,
828 bool visible) {
829 guest_visible_ = visible;
830 if (embedder_visible_ && guest_visible_)
831 GetWebContents()->WasShown();
832 else
833 GetWebContents()->WasHidden();
836 void BrowserPluginGuest::OnUnlockMouse() {
837 SendMessageToEmbedder(
838 new BrowserPluginMsg_SetMouseLock(browser_plugin_instance_id(), false));
841 void BrowserPluginGuest::OnUnlockMouseAck(int browser_plugin_instance_id) {
842 // mouse_locked_ could be false here if the lock attempt was cancelled due
843 // to window focus, or for various other reasons before the guest was informed
844 // of the lock's success.
845 if (mouse_locked_)
846 Send(new ViewMsg_MouseLockLost(routing_id()));
847 mouse_locked_ = false;
850 void BrowserPluginGuest::OnUpdateGeometry(int browser_plugin_instance_id,
851 const gfx::Rect& view_rect) {
852 // The plugin has moved within the embedder without resizing or the
853 // embedder/container's view rect changing.
854 guest_window_rect_ = view_rect;
855 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>(
856 GetWebContents()->GetRenderViewHost());
857 if (rvh)
858 rvh->SendScreenRects();
861 void BrowserPluginGuest::OnHasTouchEventHandlers(bool accept) {
862 SendMessageToEmbedder(
863 new BrowserPluginMsg_ShouldAcceptTouchEvents(
864 browser_plugin_instance_id(), accept));
867 #if defined(OS_MACOSX)
868 void BrowserPluginGuest::OnShowPopup(
869 RenderFrameHost* render_frame_host,
870 const FrameHostMsg_ShowPopup_Params& params) {
871 gfx::Rect translated_bounds(params.bounds);
872 translated_bounds.Offset(guest_window_rect_.OffsetFromOrigin());
873 BrowserPluginPopupMenuHelper popup_menu_helper(
874 owner_web_contents_->GetRenderViewHost(), render_frame_host);
875 popup_menu_helper.ShowPopupMenu(translated_bounds,
876 params.item_height,
877 params.item_font_size,
878 params.selected_item,
879 params.popup_items,
880 params.right_aligned,
881 params.allow_multiple_selection);
883 #endif
885 void BrowserPluginGuest::OnShowWidget(int route_id,
886 const gfx::Rect& initial_rect) {
887 GetWebContents()->ShowCreatedWidget(route_id, initial_rect);
890 void BrowserPluginGuest::OnTakeFocus(bool reverse) {
891 SendMessageToEmbedder(
892 new BrowserPluginMsg_AdvanceFocus(browser_plugin_instance_id(), reverse));
895 void BrowserPluginGuest::OnTextInputTypeChanged(ui::TextInputType type,
896 ui::TextInputMode input_mode,
897 bool can_compose_inline,
898 int flags) {
899 // Save the state of text input so we can restore it on focus.
900 last_text_input_type_ = type;
901 last_input_mode_ = input_mode;
902 last_input_flags_ = flags;
903 last_can_compose_inline_ = can_compose_inline;
905 SendTextInputTypeChangedToView(
906 static_cast<RenderWidgetHostViewBase*>(
907 web_contents()->GetRenderWidgetHostView()));
910 void BrowserPluginGuest::OnImeCancelComposition() {
911 static_cast<RenderWidgetHostViewBase*>(
912 web_contents()->GetRenderWidgetHostView())->ImeCancelComposition();
915 #if defined(OS_MACOSX) || defined(USE_AURA)
916 void BrowserPluginGuest::OnImeCompositionRangeChanged(
917 const gfx::Range& range,
918 const std::vector<gfx::Rect>& character_bounds) {
919 static_cast<RenderWidgetHostViewBase*>(
920 web_contents()->GetRenderWidgetHostView())->ImeCompositionRangeChanged(
921 range, character_bounds);
923 #endif
925 } // namespace content