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/plugin/webplugin_delegate_stub.h" | |
6 | |
7 #include "build/build_config.h" | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/command_line.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "content/child/npapi/plugin_instance.h" | |
13 #include "content/child/npapi/webplugin_delegate_impl.h" | |
14 #include "content/child/npapi/webplugin_resource_client.h" | |
15 #include "content/child/plugin_messages.h" | |
16 #include "content/common/cursors/webcursor.h" | |
17 #include "content/plugin/plugin_channel.h" | |
18 #include "content/plugin/plugin_thread.h" | |
19 #include "content/plugin/webplugin_proxy.h" | |
20 #include "content/public/common/content_client.h" | |
21 #include "content/public/common/content_constants.h" | |
22 #include "content/public/common/content_switches.h" | |
23 #include "third_party/WebKit/public/platform/WebCursorInfo.h" | |
24 #include "third_party/npapi/bindings/npapi.h" | |
25 #include "third_party/npapi/bindings/npruntime.h" | |
26 | |
27 using blink::WebCursorInfo; | |
28 | |
29 namespace content { | |
30 | |
31 static void DestroyWebPluginAndDelegate( | |
32 WebPluginDelegateImpl* delegate, | |
33 WebPlugin* webplugin) { | |
34 if (delegate) | |
35 delegate->PluginDestroyed(); | |
36 | |
37 delete webplugin; | |
38 } | |
39 | |
40 WebPluginDelegateStub::WebPluginDelegateStub( | |
41 const std::string& mime_type, int instance_id, PluginChannel* channel) : | |
42 mime_type_(mime_type), | |
43 instance_id_(instance_id), | |
44 channel_(channel), | |
45 delegate_(NULL), | |
46 webplugin_(NULL), | |
47 in_destructor_(false) { | |
48 DCHECK(channel); | |
49 } | |
50 | |
51 WebPluginDelegateStub::~WebPluginDelegateStub() { | |
52 in_destructor_ = true; | |
53 GetContentClient()->SetActiveURL(page_url_); | |
54 | |
55 if (channel_->in_send()) { | |
56 // The delegate or an npobject is in the callstack, so don't delete it | |
57 // right away. | |
58 base::MessageLoop::current()->PostNonNestableTask( | |
59 FROM_HERE, | |
60 base::Bind(&DestroyWebPluginAndDelegate, | |
61 delegate_, | |
62 webplugin_)); | |
63 } else { | |
64 // Safe to delete right away. | |
65 DestroyWebPluginAndDelegate(delegate_, webplugin_); | |
66 } | |
67 } | |
68 | |
69 bool WebPluginDelegateStub::OnMessageReceived(const IPC::Message& msg) { | |
70 GetContentClient()->SetActiveURL(page_url_); | |
71 | |
72 // A plugin can execute a script to delete itself in any of its NPP methods. | |
73 // Hold an extra reference to ourself so that if this does occur and we're | |
74 // handling a sync message, we don't crash when attempting to send a reply. | |
75 // The exception to this is when we're already in the destructor. | |
76 if (!in_destructor_) | |
77 AddRef(); | |
78 | |
79 bool handled = true; | |
80 IPC_BEGIN_MESSAGE_MAP(WebPluginDelegateStub, msg) | |
81 IPC_MESSAGE_HANDLER(PluginMsg_Init, OnInit) | |
82 IPC_MESSAGE_HANDLER(PluginMsg_SetFocus, OnSetFocus) | |
83 IPC_MESSAGE_HANDLER(PluginMsg_HandleInputEvent, OnHandleInputEvent) | |
84 IPC_MESSAGE_HANDLER(PluginMsg_Paint, OnPaint) | |
85 IPC_MESSAGE_HANDLER(PluginMsg_DidPaint, OnDidPaint) | |
86 IPC_MESSAGE_HANDLER(PluginMsg_UpdateGeometry, OnUpdateGeometry) | |
87 IPC_MESSAGE_HANDLER(PluginMsg_UpdateGeometrySync, OnUpdateGeometry) | |
88 IPC_MESSAGE_HANDLER(PluginMsg_SetContentAreaFocus, OnSetContentAreaFocus) | |
89 #if defined(OS_MACOSX) | |
90 IPC_MESSAGE_HANDLER(PluginMsg_SetWindowFocus, OnSetWindowFocus) | |
91 IPC_MESSAGE_HANDLER(PluginMsg_ContainerHidden, OnContainerHidden) | |
92 IPC_MESSAGE_HANDLER(PluginMsg_ContainerShown, OnContainerShown) | |
93 IPC_MESSAGE_HANDLER(PluginMsg_WindowFrameChanged, OnWindowFrameChanged) | |
94 IPC_MESSAGE_HANDLER(PluginMsg_ImeCompositionCompleted, | |
95 OnImeCompositionCompleted) | |
96 #endif | |
97 IPC_MESSAGE_UNHANDLED(handled = false) | |
98 IPC_END_MESSAGE_MAP() | |
99 | |
100 if (!in_destructor_) | |
101 Release(); | |
102 | |
103 DCHECK(handled); | |
104 return handled; | |
105 } | |
106 | |
107 bool WebPluginDelegateStub::Send(IPC::Message* msg) { | |
108 return channel_->Send(msg); | |
109 } | |
110 | |
111 void WebPluginDelegateStub::OnInit(const PluginMsg_Init_Params& params, | |
112 bool* transparent, | |
113 bool* result) { | |
114 page_url_ = params.page_url; | |
115 GetContentClient()->SetActiveURL(page_url_); | |
116 | |
117 *transparent = false; | |
118 *result = false; | |
119 if (params.arg_names.size() != params.arg_values.size()) { | |
120 NOTREACHED(); | |
121 return; | |
122 } | |
123 | |
124 const base::CommandLine& command_line = | |
125 *base::CommandLine::ForCurrentProcess(); | |
126 base::FilePath path = | |
127 command_line.GetSwitchValuePath(switches::kPluginPath); | |
128 | |
129 webplugin_ = new WebPluginProxy(channel_.get(), | |
130 instance_id_, | |
131 page_url_, | |
132 params.host_render_view_routing_id); | |
133 delegate_ = WebPluginDelegateImpl::Create(webplugin_, path, mime_type_); | |
134 if (delegate_) { | |
135 if (delegate_->GetQuirks() & | |
136 WebPluginDelegateImpl::PLUGIN_QUIRK_DIE_AFTER_UNLOAD) { | |
137 PluginThread::current()->SetForcefullyTerminatePluginProcess(); | |
138 } | |
139 | |
140 webplugin_->set_delegate(delegate_); | |
141 std::vector<std::string> arg_names = params.arg_names; | |
142 std::vector<std::string> arg_values = params.arg_values; | |
143 | |
144 *result = delegate_->Initialize(params.url, | |
145 arg_names, | |
146 arg_values, | |
147 params.load_manually); | |
148 *transparent = delegate_->instance()->transparent(); | |
149 } | |
150 } | |
151 | |
152 void WebPluginDelegateStub::OnSetFocus(bool focused) { | |
153 delegate_->SetFocus(focused); | |
154 } | |
155 | |
156 void WebPluginDelegateStub::OnHandleInputEvent( | |
157 const blink::WebInputEvent *event, | |
158 bool* handled, | |
159 WebCursor* cursor) { | |
160 WebCursor::CursorInfo cursor_info; | |
161 *handled = delegate_->HandleInputEvent(*event, &cursor_info); | |
162 cursor->InitFromCursorInfo(cursor_info); | |
163 } | |
164 | |
165 void WebPluginDelegateStub::OnPaint(const gfx::Rect& damaged_rect) { | |
166 webplugin_->Paint(damaged_rect); | |
167 } | |
168 | |
169 void WebPluginDelegateStub::OnDidPaint() { | |
170 webplugin_->DidPaint(); | |
171 } | |
172 | |
173 void WebPluginDelegateStub::OnUpdateGeometry( | |
174 const PluginMsg_UpdateGeometry_Param& param) { | |
175 webplugin_->UpdateGeometry( | |
176 param.window_rect, param.clip_rect, | |
177 param.windowless_buffer0, param.windowless_buffer1, | |
178 param.windowless_buffer_index); | |
179 } | |
180 | |
181 void WebPluginDelegateStub::OnSetContentAreaFocus(bool has_focus) { | |
182 if (delegate_) | |
183 delegate_->SetContentAreaHasFocus(has_focus); | |
184 } | |
185 | |
186 #if defined(OS_MACOSX) | |
187 void WebPluginDelegateStub::OnSetWindowFocus(bool has_focus) { | |
188 if (delegate_) | |
189 delegate_->SetWindowHasFocus(has_focus); | |
190 } | |
191 | |
192 void WebPluginDelegateStub::OnContainerHidden() { | |
193 if (delegate_) | |
194 delegate_->SetContainerVisibility(false); | |
195 } | |
196 | |
197 void WebPluginDelegateStub::OnContainerShown(gfx::Rect window_frame, | |
198 gfx::Rect view_frame, | |
199 bool has_focus) { | |
200 if (delegate_) { | |
201 delegate_->WindowFrameChanged(window_frame, view_frame); | |
202 delegate_->SetContainerVisibility(true); | |
203 delegate_->SetWindowHasFocus(has_focus); | |
204 } | |
205 } | |
206 | |
207 void WebPluginDelegateStub::OnWindowFrameChanged(const gfx::Rect& window_frame, | |
208 const gfx::Rect& view_frame) { | |
209 if (delegate_) | |
210 delegate_->WindowFrameChanged(window_frame, view_frame); | |
211 } | |
212 | |
213 void WebPluginDelegateStub::OnImeCompositionCompleted( | |
214 const base::string16& text) { | |
215 if (delegate_) | |
216 delegate_->ImeCompositionCompleted(text); | |
217 } | |
218 #endif // OS_MACOSX | |
219 | |
220 } // namespace content | |
OLD | NEW |