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_embedder.h"
7 #include "base/values.h"
8 #include "content/browser/browser_plugin/browser_plugin_guest.h"
9 #include "content/browser/renderer_host/render_view_host_impl.h"
10 #include "content/browser/web_contents/web_contents_impl.h"
11 #include "content/common/browser_plugin/browser_plugin_constants.h"
12 #include "content/common/browser_plugin/browser_plugin_messages.h"
13 #include "content/common/drag_messages.h"
14 #include "content/common/gpu/gpu_messages.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_plugin_guest_manager.h"
17 #include "content/public/browser/content_browser_client.h"
18 #include "content/public/browser/native_web_keyboard_event.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/user_metrics.h"
21 #include "content/public/common/content_switches.h"
22 #include "content/public/common/result_codes.h"
23 #include "content/public/common/url_constants.h"
24 #include "net/base/escape.h"
25 #include "third_party/WebKit/public/web/WebFindOptions.h"
26 #include "ui/events/keycodes/keyboard_codes.h"
30 BrowserPluginEmbedder::BrowserPluginEmbedder(WebContentsImpl
* web_contents
)
31 : WebContentsObserver(web_contents
),
32 guest_drag_ending_(false),
33 weak_ptr_factory_(this) {
36 BrowserPluginEmbedder::~BrowserPluginEmbedder() {
40 BrowserPluginEmbedder
* BrowserPluginEmbedder::Create(
41 WebContentsImpl
* web_contents
) {
42 return new BrowserPluginEmbedder(web_contents
);
45 void BrowserPluginEmbedder::DragEnteredGuest(BrowserPluginGuest
* guest
) {
46 guest_dragging_over_
= guest
->AsWeakPtr();
49 void BrowserPluginEmbedder::DragLeftGuest(BrowserPluginGuest
* guest
) {
50 // Avoid race conditions in switching between guests being hovered over by
51 // only un-setting if the caller is marked as the guest being dragged over.
52 if (guest_dragging_over_
.get() == guest
) {
53 guest_dragging_over_
.reset();
57 void BrowserPluginEmbedder::StartDrag(BrowserPluginGuest
* guest
) {
58 guest_started_drag_
= guest
->AsWeakPtr();
59 guest_drag_ending_
= false;
62 WebContentsImpl
* BrowserPluginEmbedder::GetWebContents() const {
63 return static_cast<WebContentsImpl
*>(web_contents());
66 BrowserPluginGuestManager
*
67 BrowserPluginEmbedder::GetBrowserPluginGuestManager() const {
68 return GetWebContents()->GetBrowserContext()->GetGuestManager();
71 void BrowserPluginEmbedder::ClearGuestDragStateIfApplicable() {
72 // The order at which we observe SystemDragEnded() and DragSourceEndedAt() is
73 // platform dependent.
74 // In OSX, we see SystemDragEnded() first, where in aura, we see
75 // DragSourceEndedAt() first. For this reason, we check if both methods were
76 // called before resetting |guest_started_drag_|.
77 if (guest_drag_ending_
) {
78 if (guest_started_drag_
)
79 guest_started_drag_
.reset();
81 guest_drag_ending_
= true;
85 bool BrowserPluginEmbedder::DidSendScreenRectsCallback(
86 WebContents
* guest_web_contents
) {
87 static_cast<RenderViewHostImpl
*>(
88 guest_web_contents
->GetRenderViewHost())->SendScreenRects();
89 // Not handled => Iterate over all guests.
93 void BrowserPluginEmbedder::DidSendScreenRects() {
94 GetBrowserPluginGuestManager()->ForEachGuest(
95 GetWebContents(), base::Bind(
96 &BrowserPluginEmbedder::DidSendScreenRectsCallback
,
97 base::Unretained(this)));
100 bool BrowserPluginEmbedder::OnMessageReceived(const IPC::Message
& message
) {
102 IPC_BEGIN_MESSAGE_MAP(BrowserPluginEmbedder
, message
)
103 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_Attach
, OnAttach
)
104 IPC_MESSAGE_HANDLER_GENERIC(DragHostMsg_UpdateDragCursor
,
105 OnUpdateDragCursor(&handled
));
106 IPC_MESSAGE_UNHANDLED(handled
= false)
107 IPC_END_MESSAGE_MAP()
111 void BrowserPluginEmbedder::DragSourceEndedAt(int client_x
, int client_y
,
112 int screen_x
, int screen_y
, blink::WebDragOperation operation
) {
113 if (guest_started_drag_
) {
114 gfx::Point guest_offset
=
115 guest_started_drag_
->GetScreenCoordinates(gfx::Point());
116 guest_started_drag_
->DragSourceEndedAt(client_x
- guest_offset
.x(),
117 client_y
- guest_offset
.y(), screen_x
, screen_y
, operation
);
119 ClearGuestDragStateIfApplicable();
122 void BrowserPluginEmbedder::SystemDragEnded() {
123 // When the embedder's drag/drop operation ends, we need to pass the message
124 // to the guest that initiated the drag/drop operation. This will ensure that
125 // the guest's RVH state is reset properly.
126 if (guest_started_drag_
)
127 guest_started_drag_
->EndSystemDrag();
128 guest_dragging_over_
.reset();
129 ClearGuestDragStateIfApplicable();
132 void BrowserPluginEmbedder::OnUpdateDragCursor(bool* handled
) {
133 *handled
= (guest_dragging_over_
.get() != NULL
);
136 void BrowserPluginEmbedder::OnAttach(
137 int browser_plugin_instance_id
,
138 const BrowserPluginHostMsg_Attach_Params
& params
) {
139 WebContents
* guest_web_contents
=
140 GetBrowserPluginGuestManager()->GetGuestByInstanceID(
141 GetWebContents(), browser_plugin_instance_id
);
142 if (!guest_web_contents
)
144 BrowserPluginGuest
* guest
= static_cast<WebContentsImpl
*>(guest_web_contents
)
145 ->GetBrowserPluginGuest();
146 guest
->Attach(browser_plugin_instance_id
, GetWebContents(), params
);
149 bool BrowserPluginEmbedder::HandleKeyboardEvent(
150 const NativeWebKeyboardEvent
& event
) {
151 if ((event
.windowsKeyCode
!= ui::VKEY_ESCAPE
) ||
152 (event
.modifiers
& blink::WebInputEvent::InputModifiers
)) {
156 bool event_consumed
= false;
157 GetBrowserPluginGuestManager()->ForEachGuest(
159 base::Bind(&BrowserPluginEmbedder::UnlockMouseIfNecessaryCallback
,
160 base::Unretained(this),
163 return event_consumed
;
166 bool BrowserPluginEmbedder::Find(int request_id
,
167 const base::string16
& search_text
,
168 const blink::WebFindOptions
& options
) {
169 return GetBrowserPluginGuestManager()->ForEachGuest(
171 base::Bind(&BrowserPluginEmbedder::FindInGuest
,
172 base::Unretained(this),
178 bool BrowserPluginEmbedder::UnlockMouseIfNecessaryCallback(bool* mouse_unlocked
,
179 WebContents
* guest
) {
180 *mouse_unlocked
|= static_cast<WebContentsImpl
*>(guest
)
181 ->GetBrowserPluginGuest()
183 guest
->GotResponseToLockMouseRequest(false);
185 // Returns false to iterate over all guests.
189 bool BrowserPluginEmbedder::FindInGuest(int request_id
,
190 const base::string16
& search_text
,
191 const blink::WebFindOptions
& options
,
192 WebContents
* guest
) {
193 if (static_cast<WebContentsImpl
*>(guest
)->GetBrowserPluginGuest()->Find(
194 request_id
, search_text
, options
)) {
195 // There can only ever currently be one browser plugin that handles find so
196 // we can break the iteration at this point.
202 } // namespace content