OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "content/browser/browser_plugin/browser_plugin_embedder_helper.h" | |
6 | |
7 #include "base/time.h" | |
8 #include "content/browser/browser_plugin/browser_plugin_embedder.h" | |
9 #include "content/browser/renderer_host/render_view_host_impl.h" | |
10 #include "content/browser/renderer_host/render_widget_host_impl.h" | |
11 #include "content/browser/web_contents/web_contents_impl.h" | |
12 #include "content/common/browser_plugin_messages.h" | |
13 #include "content/common/view_messages.h" | |
14 #include "content/public/browser/notification_details.h" | |
15 #include "content/public/browser/notification_service.h" | |
16 #include "content/public/browser/notification_source.h" | |
17 #include "content/public/browser/notification_types.h" | |
18 #include "content/public/browser/render_process_host.h" | |
19 #include "content/public/browser/render_view_host.h" | |
20 #include "content/public/browser/render_widget_host_view.h" | |
21 #include "ui/gfx/size.h" | |
22 | |
23 namespace content { | |
24 | |
25 BrowserPluginEmbedderHelper::BrowserPluginEmbedderHelper( | |
26 BrowserPluginEmbedder* embedder, | |
27 WebContentsImpl* web_contents, | |
Fady Samuel
2012/08/27 15:35:00
It seems the only reason BrowserPluginEmbedderHelp
lazyboy
2012/08/28 19:07:14
Moved.
Done.
| |
28 RenderViewHost* render_view_host) | |
29 : RenderViewHostObserver(render_view_host), | |
30 embedder_(embedder) { | |
31 // Listen to visibility changes so that an embedder hides its guests | |
32 // as well. | |
33 registrar_.Add(this, | |
34 NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, | |
35 Source<WebContents>(web_contents)); | |
36 } | |
37 | |
38 BrowserPluginEmbedderHelper::~BrowserPluginEmbedderHelper() { | |
39 } | |
40 | |
41 bool BrowserPluginEmbedderHelper::Send(IPC::Message* message) { | |
42 return RenderViewHostObserver::Send(message); | |
43 } | |
44 | |
45 bool BrowserPluginEmbedderHelper::OnMessageReceived( | |
46 const IPC::Message& message) { | |
47 bool handled = true; | |
48 IPC_BEGIN_MESSAGE_MAP(BrowserPluginEmbedderHelper, message) | |
49 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_NavigateGuest, | |
50 OnNavigateGuest); | |
51 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ResizeGuest, OnResizeGuest) | |
52 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_UpdateRect_ACK, OnUpdateRectACK); | |
53 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_SetFocus, OnSetFocus); | |
54 IPC_MESSAGE_HANDLER_GENERIC(BrowserPluginHostMsg_HandleInputEvent, | |
55 OnHandleInputEvent(*static_cast<const IPC::SyncMessage*>(&message), | |
56 &handled)) | |
57 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_PluginDestroyed, | |
58 OnPluginDestroyed); | |
59 IPC_MESSAGE_UNHANDLED(handled = false) | |
60 IPC_END_MESSAGE_MAP() | |
61 return handled; | |
62 } | |
63 | |
64 void BrowserPluginEmbedderHelper::OnResizeGuest( | |
65 int instance_id, | |
66 const BrowserPluginHostMsg_ResizeGuest_Params& params) { | |
67 TransportDIB* damage_buffer = NULL; | |
68 #if defined(OS_WIN) | |
69 // On Windows we need to duplicate the handle from the remote process. | |
70 HANDLE section; | |
71 DuplicateHandle(GetHandle(), | |
72 params.damage_buffer_id.handle, | |
73 GetCurrentProcess(), | |
74 §ion, | |
75 STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ | FILE_MAP_WRITE, | |
76 FALSE, 0); | |
77 damage_buffer = TransportDIB::Map(section); | |
78 #elif defined(OS_MACOSX) | |
79 // On OSX, the browser allocates all DIBs and keeps a file descriptor around | |
80 // for each. | |
81 damage_buffer = widget_helper_->MapTransportDIB(params.damage_buffer_id); | |
82 #elif defined(OS_ANDROID) | |
83 damage_buffer = TransportDIB::Map(params.damage_buffer_id); | |
84 #elif defined(OS_POSIX) | |
85 damage_buffer = TransportDIB::Map(params.damage_buffer_id.shmkey); | |
86 #endif // defined(OS_POSIX) | |
87 DCHECK(damage_buffer); | |
88 // TODO(fsamuel): Schedule this later so that we don't stall the embedder for | |
89 // too long. | |
90 embedder_->ResizeGuest(instance_id, | |
91 damage_buffer, | |
92 params.width, | |
93 params.height, | |
94 params.resize_pending, | |
95 params.scale_factor); | |
96 } | |
97 | |
98 void BrowserPluginEmbedderHelper::OnHandleInputEvent( | |
99 const IPC::SyncMessage& message, | |
100 bool* handled) { | |
101 *handled = true; | |
102 PickleIterator iter(message); | |
103 | |
104 // TODO(fsamuel): This appears to be a monotonically increasing value. | |
105 int instance_id = -1; | |
106 const char* guest_rect_data = NULL; | |
107 int guest_rect_data_length = -1; | |
108 const char* input_event_data = NULL; | |
109 int input_event_data_length = -1; | |
110 if (!iter.SkipBytes(4) || | |
111 !message.ReadInt(&iter, &instance_id) || | |
112 !message.ReadData(&iter, &guest_rect_data, &guest_rect_data_length) || | |
113 !message.ReadData(&iter, &input_event_data, &input_event_data_length)) { | |
114 *handled = false; | |
115 return; | |
116 } | |
117 const gfx::Rect* guest_rect = | |
118 reinterpret_cast<const gfx::Rect*>(guest_rect_data); | |
119 const WebKit::WebInputEvent* input_event = | |
120 reinterpret_cast<const WebKit::WebInputEvent*>(input_event_data); | |
121 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>( | |
122 render_view_host()); | |
123 | |
124 // Convert the window coordinates into screen coordinates. | |
125 gfx::Rect guest_screen_rect(*guest_rect); | |
126 if (rvh->GetView()) | |
127 guest_screen_rect.Offset( | |
128 rvh->GetView()->GetViewBounds().origin()); | |
129 | |
130 IPC::Message* reply_message = | |
131 IPC::SyncMessage::GenerateReply(&message); | |
132 embedder_->HandleInputEvent(instance_id, | |
133 rvh, | |
134 guest_screen_rect, | |
135 *input_event, | |
136 reply_message); | |
137 } | |
138 | |
139 void BrowserPluginEmbedderHelper::OnNavigateGuest(int instance_id, | |
140 int64 frame_id, | |
141 const std::string& src, | |
142 const gfx::Size& size) { | |
143 embedder_->NavigateGuest(render_view_host(), instance_id, frame_id, src, | |
144 size); | |
145 } | |
146 | |
147 void BrowserPluginEmbedderHelper::OnUpdateRectACK(int instance_id, | |
148 int message_id, | |
149 const gfx::Size& size) { | |
150 embedder_->UpdateRectACK(instance_id, message_id, size); | |
151 } | |
152 | |
153 void BrowserPluginEmbedderHelper::OnSetFocus(int instance_id, bool focused) { | |
154 embedder_->SetFocus(instance_id, focused); | |
155 } | |
156 | |
157 void BrowserPluginEmbedderHelper::OnPluginDestroyed(int instance_id) { | |
158 embedder_->PluginDestroyed(instance_id); | |
159 } | |
160 | |
161 void BrowserPluginEmbedderHelper::Observe(int type, | |
162 const NotificationSource& source, | |
163 const NotificationDetails& details) { | |
164 switch (type) { | |
165 case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: { | |
166 bool visible = *Details<bool>(details).ptr(); | |
167 embedder_->WebContentsVisiblitlyChanged(visible); | |
168 break; | |
169 } | |
170 default: | |
171 NOTREACHED() << "Unexpected notification type: " << type; | |
172 } | |
173 } | |
174 | |
175 } // namespace content | |
OLD | NEW |