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 "ui/events/keycodes/keyboard_codes.h"
29 BrowserPluginEmbedder::BrowserPluginEmbedder(WebContentsImpl
* web_contents
)
30 : WebContentsObserver(web_contents
),
31 weak_ptr_factory_(this) {
34 BrowserPluginEmbedder::~BrowserPluginEmbedder() {
38 BrowserPluginEmbedder
* BrowserPluginEmbedder::Create(
39 WebContentsImpl
* web_contents
) {
40 return new BrowserPluginEmbedder(web_contents
);
43 void BrowserPluginEmbedder::DragEnteredGuest(BrowserPluginGuest
* guest
) {
44 guest_dragging_over_
= guest
->AsWeakPtr();
47 void BrowserPluginEmbedder::DragLeftGuest(BrowserPluginGuest
* guest
) {
48 // Avoid race conditions in switching between guests being hovered over by
49 // only un-setting if the caller is marked as the guest being dragged over.
50 if (guest_dragging_over_
.get() == guest
) {
51 guest_dragging_over_
.reset();
55 void BrowserPluginEmbedder::StartDrag(BrowserPluginGuest
* guest
) {
56 guest_started_drag_
= guest
->AsWeakPtr();
59 WebContentsImpl
* BrowserPluginEmbedder::GetWebContents() const {
60 return static_cast<WebContentsImpl
*>(web_contents());
63 BrowserPluginGuestManager
*
64 BrowserPluginEmbedder::GetBrowserPluginGuestManager() const {
65 return GetWebContents()->GetBrowserContext()->GetGuestManager();
68 bool BrowserPluginEmbedder::DidSendScreenRectsCallback(
69 WebContents
* guest_web_contents
) {
70 static_cast<RenderViewHostImpl
*>(
71 guest_web_contents
->GetRenderViewHost())->SendScreenRects();
72 // Not handled => Iterate over all guests.
76 void BrowserPluginEmbedder::DidSendScreenRects() {
77 GetBrowserPluginGuestManager()->ForEachGuest(
78 GetWebContents(), base::Bind(
79 &BrowserPluginEmbedder::DidSendScreenRectsCallback
,
80 base::Unretained(this)));
83 bool BrowserPluginEmbedder::OnMessageReceived(const IPC::Message
& message
) {
85 IPC_BEGIN_MESSAGE_MAP(BrowserPluginEmbedder
, message
)
86 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_Attach
, OnAttach
)
87 IPC_MESSAGE_HANDLER_GENERIC(DragHostMsg_UpdateDragCursor
,
88 OnUpdateDragCursor(&handled
));
89 IPC_MESSAGE_UNHANDLED(handled
= false)
94 void BrowserPluginEmbedder::DragSourceEndedAt(int client_x
, int client_y
,
95 int screen_x
, int screen_y
, blink::WebDragOperation operation
) {
96 if (guest_started_drag_
.get()) {
97 gfx::Point guest_offset
=
98 guest_started_drag_
->GetScreenCoordinates(gfx::Point());
99 guest_started_drag_
->DragSourceEndedAt(client_x
- guest_offset
.x(),
100 client_y
- guest_offset
.y(), screen_x
, screen_y
, operation
);
104 void BrowserPluginEmbedder::SystemDragEnded() {
105 // When the embedder's drag/drop operation ends, we need to pass the message
106 // to the guest that initiated the drag/drop operation. This will ensure that
107 // the guest's RVH state is reset properly.
108 if (guest_started_drag_
.get())
109 guest_started_drag_
->EndSystemDrag();
110 guest_started_drag_
.reset();
111 guest_dragging_over_
.reset();
114 void BrowserPluginEmbedder::OnUpdateDragCursor(bool* handled
) {
115 *handled
= (guest_dragging_over_
.get() != NULL
);
118 void BrowserPluginEmbedder::OnGuestCallback(
120 const BrowserPluginHostMsg_Attach_Params
& params
,
121 const base::DictionaryValue
* extra_params
,
122 WebContents
* guest_web_contents
) {
123 BrowserPluginGuest
* guest
= guest_web_contents
?
124 static_cast<WebContentsImpl
*>(guest_web_contents
)->
125 GetBrowserPluginGuest() : NULL
;
127 guest
->Attach(GetWebContents(), params
, *extra_params
);
130 void BrowserPluginEmbedder::OnAttach(
132 const BrowserPluginHostMsg_Attach_Params
& params
,
133 const base::DictionaryValue
& extra_params
) {
134 GetBrowserPluginGuestManager()->MaybeGetGuestByInstanceIDOrKill(
135 instance_id
, GetWebContents()->GetRenderProcessHost()->GetID(),
136 base::Bind(&BrowserPluginEmbedder::OnGuestCallback
,
137 base::Unretained(this),
143 } // namespace content