Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(502)

Side by Side Diff: webkit/plugins/npapi/webplugin_delegate_impl_mac.mm

Issue 19761007: Move NPAPI implementation out of webkit/plugins/npapi and into content. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix mac Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "webkit/plugins/npapi/webplugin_delegate_impl.h"
6
7 #import <Cocoa/Cocoa.h>
8 #import <QuartzCore/QuartzCore.h>
9 #include <unistd.h>
10
11 #include <set>
12 #include <string>
13
14 #include "base/mac/mac_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/metrics/stats_counters.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/sys_string_conversions.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "skia/ext/skia_utils_mac.h"
21 #include "third_party/WebKit/public/web/WebInputEvent.h"
22 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
23 #include "webkit/common/cursors/webcursor.h"
24 #include "webkit/plugins/npapi/plugin_instance.h"
25 #include "webkit/plugins/npapi/plugin_lib.h"
26 #include "webkit/plugins/npapi/plugin_web_event_converter_mac.h"
27 #include "webkit/plugins/npapi/webplugin.h"
28 #include "webkit/plugins/npapi/webplugin_accelerated_surface_mac.h"
29
30 using WebKit::WebKeyboardEvent;
31 using WebKit::WebInputEvent;
32 using WebKit::WebMouseEvent;
33 using WebKit::WebMouseWheelEvent;
34
35 // Important implementation notes: The Mac definition of NPAPI, particularly
36 // the distinction between windowed and windowless modes, differs from the
37 // Windows and Linux definitions. Most of those differences are
38 // accomodated by the WebPluginDelegate class.
39
40 namespace webkit {
41 namespace npapi {
42
43 namespace {
44
45 const int kCoreAnimationRedrawPeriodMs = 10; // 100 Hz
46
47 WebPluginDelegateImpl* g_active_delegate;
48
49 // Helper to simplify correct usage of g_active_delegate. Instantiating will
50 // set the active delegate to |delegate| for the lifetime of the object, then
51 // NULL when it goes out of scope.
52 class ScopedActiveDelegate {
53 public:
54 explicit ScopedActiveDelegate(WebPluginDelegateImpl* delegate) {
55 g_active_delegate = delegate;
56 }
57 ~ScopedActiveDelegate() {
58 g_active_delegate = NULL;
59 }
60
61 private:
62 DISALLOW_COPY_AND_ASSIGN(ScopedActiveDelegate);
63 };
64
65 } // namespace
66
67 // Helper to build and maintain a model of a drag entering the plugin but not
68 // starting there. See explanation in PlatformHandleInputEvent.
69 class ExternalDragTracker {
70 public:
71 ExternalDragTracker() : pressed_buttons_(0) {}
72
73 // Returns true if an external drag is in progress.
74 bool IsDragInProgress() { return pressed_buttons_ != 0; };
75
76 // Returns true if the given event appears to be related to an external drag.
77 bool EventIsRelatedToDrag(const WebInputEvent& event);
78
79 // Updates the tracking of whether an external drag is in progress--and if
80 // so what buttons it involves--based on the given event.
81 void UpdateDragStateFromEvent(const WebInputEvent& event);
82
83 private:
84 // Returns the mask for just the button state in a WebInputEvent's modifiers.
85 static int WebEventButtonModifierMask();
86
87 // The WebInputEvent modifier flags for any buttons that were down when an
88 // external drag entered the plugin, and which and are still down now.
89 int pressed_buttons_;
90
91 DISALLOW_COPY_AND_ASSIGN(ExternalDragTracker);
92 };
93
94 void ExternalDragTracker::UpdateDragStateFromEvent(const WebInputEvent& event) {
95 switch (event.type) {
96 case WebInputEvent::MouseEnter:
97 pressed_buttons_ = event.modifiers & WebEventButtonModifierMask();
98 break;
99 case WebInputEvent::MouseUp: {
100 const WebMouseEvent* mouse_event =
101 static_cast<const WebMouseEvent*>(&event);
102 if (mouse_event->button == WebMouseEvent::ButtonLeft)
103 pressed_buttons_ &= ~WebInputEvent::LeftButtonDown;
104 if (mouse_event->button == WebMouseEvent::ButtonMiddle)
105 pressed_buttons_ &= ~WebInputEvent::MiddleButtonDown;
106 if (mouse_event->button == WebMouseEvent::ButtonRight)
107 pressed_buttons_ &= ~WebInputEvent::RightButtonDown;
108 break;
109 }
110 default:
111 break;
112 }
113 }
114
115 bool ExternalDragTracker::EventIsRelatedToDrag(const WebInputEvent& event) {
116 const WebMouseEvent* mouse_event = static_cast<const WebMouseEvent*>(&event);
117 switch (event.type) {
118 case WebInputEvent::MouseUp:
119 // We only care about release of buttons that were part of the drag.
120 return ((mouse_event->button == WebMouseEvent::ButtonLeft &&
121 (pressed_buttons_ & WebInputEvent::LeftButtonDown)) ||
122 (mouse_event->button == WebMouseEvent::ButtonMiddle &&
123 (pressed_buttons_ & WebInputEvent::MiddleButtonDown)) ||
124 (mouse_event->button == WebMouseEvent::ButtonRight &&
125 (pressed_buttons_ & WebInputEvent::RightButtonDown)));
126 case WebInputEvent::MouseEnter:
127 return (event.modifiers & WebEventButtonModifierMask()) != 0;
128 case WebInputEvent::MouseLeave:
129 case WebInputEvent::MouseMove: {
130 int event_buttons = (event.modifiers & WebEventButtonModifierMask());
131 return (pressed_buttons_ &&
132 pressed_buttons_ == event_buttons);
133 }
134 default:
135 return false;
136 }
137 return false;
138 }
139
140 int ExternalDragTracker::WebEventButtonModifierMask() {
141 return WebInputEvent::LeftButtonDown |
142 WebInputEvent::RightButtonDown |
143 WebInputEvent::MiddleButtonDown;
144 }
145
146 #pragma mark -
147 #pragma mark Core WebPluginDelegate implementation
148
149 WebPluginDelegateImpl::WebPluginDelegateImpl(
150 PluginInstance* instance)
151 : windowed_handle_(gfx::kNullPluginWindow),
152 // all Mac plugins are "windowless" in the Windows/X11 sense
153 windowless_(true),
154 plugin_(NULL),
155 instance_(instance),
156 quirks_(0),
157 use_buffer_context_(true),
158 buffer_context_(NULL),
159 layer_(nil),
160 surface_(NULL),
161 renderer_(nil),
162 containing_window_has_focus_(false),
163 initial_window_focus_(false),
164 container_is_visible_(false),
165 have_called_set_window_(false),
166 ime_enabled_(false),
167 keyup_ignore_count_(0),
168 external_drag_tracker_(new ExternalDragTracker()),
169 handle_event_depth_(0),
170 first_set_window_call_(true),
171 plugin_has_focus_(false),
172 has_webkit_focus_(false),
173 containing_view_has_focus_(true),
174 creation_succeeded_(false) {
175 memset(&window_, 0, sizeof(window_));
176 instance->set_windowless(true);
177 }
178
179 WebPluginDelegateImpl::~WebPluginDelegateImpl() {
180 DestroyInstance();
181 }
182
183 bool WebPluginDelegateImpl::PlatformInitialize() {
184 // Don't set a NULL window handle on destroy for Mac plugins. This matches
185 // Safari and other Mac browsers (see PluginView::stop() in PluginView.cpp,
186 // where code to do so is surrounded by an #ifdef that excludes Mac OS X, or
187 // destroyPlugin in WebNetscapePluginView.mm, for examples).
188 quirks_ |= PLUGIN_QUIRK_DONT_SET_NULL_WINDOW_HANDLE_ON_DESTROY;
189
190 // Mac plugins don't expect to be unloaded, and they don't always do so
191 // cleanly, so don't unload them at shutdown.
192 instance()->plugin_lib()->PreventLibraryUnload();
193
194 #ifndef NP_NO_CARBON
195 if (instance()->event_model() == NPEventModelCarbon)
196 return false;
197 #endif
198
199 window_.type = NPWindowTypeDrawable;
200 NPDrawingModel drawing_model = instance()->drawing_model();
201 switch (drawing_model) {
202 #ifndef NP_NO_QUICKDRAW
203 case NPDrawingModelQuickDraw:
204 return false;
205 #endif
206 case NPDrawingModelCoreGraphics:
207 break;
208 case NPDrawingModelCoreAnimation:
209 case NPDrawingModelInvalidatingCoreAnimation: {
210 // Ask the plug-in for the CALayer it created for rendering content.
211 // Create a surface to host it, and request a "window" handle to identify
212 // the surface.
213 CALayer* layer = nil;
214 NPError err = instance()->NPP_GetValue(NPPVpluginCoreAnimationLayer,
215 reinterpret_cast<void*>(&layer));
216 if (!err) {
217 if (drawing_model == NPDrawingModelCoreAnimation) {
218 // Create the timer; it will be started when we get a window handle.
219 redraw_timer_.reset(new base::RepeatingTimer<WebPluginDelegateImpl>);
220 }
221 layer_ = layer;
222
223 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
224 // On dual GPU systems, force the use of the discrete GPU for
225 // the CARenderer underlying our Core Animation backend for
226 // all plugins except Flash. For some reason Unity3D's output
227 // doesn't show up if the integrated GPU is used. Safari keeps
228 // even Flash 11 with Stage3D on the integrated GPU, so mirror
229 // that behavior here.
230 const WebPluginInfo& plugin_info =
231 instance_->plugin_lib()->plugin_info();
232 if (plugin_info.name.find(ASCIIToUTF16("Flash")) !=
233 base::string16::npos)
234 gpu_preference = gfx::PreferIntegratedGpu;
235 surface_ = plugin_->GetAcceleratedSurface(gpu_preference);
236
237 // If surface initialization fails for some reason, just continue
238 // without any drawing; returning false would be a more confusing user
239 // experience (since it triggers a missing plugin placeholder).
240 if (surface_ && surface_->context()) {
241 renderer_ = [[CARenderer rendererWithCGLContext:surface_->context()
242 options:NULL] retain];
243 [renderer_ setLayer:layer_];
244 plugin_->AcceleratedPluginEnabledRendering();
245 }
246 }
247 break;
248 }
249 default:
250 NOTREACHED();
251 break;
252 }
253
254 // Let the WebPlugin know that we are windowless, unless this is a Core
255 // Animation plugin, in which case AcceleratedPluginEnabledRendering
256 // calls SetWindow. Rendering breaks if SetWindow is called before
257 // accelerated rendering is enabled.
258 if (!layer_)
259 plugin_->SetWindow(gfx::kNullPluginWindow);
260
261 return true;
262 }
263
264 void WebPluginDelegateImpl::PlatformDestroyInstance() {
265 if (redraw_timer_)
266 redraw_timer_->Stop();
267 [renderer_ release];
268 renderer_ = nil;
269 layer_ = nil;
270 }
271
272 void WebPluginDelegateImpl::UpdateGeometryAndContext(
273 const gfx::Rect& window_rect, const gfx::Rect& clip_rect,
274 CGContextRef context) {
275 buffer_context_ = context;
276 UpdateGeometry(window_rect, clip_rect);
277 }
278
279 void WebPluginDelegateImpl::Paint(WebKit::WebCanvas* canvas,
280 const gfx::Rect& rect) {
281 gfx::SkiaBitLocker bit_locker(canvas);
282 CGContextRef context = bit_locker.cgContext();
283 CGPaint(context, rect);
284 }
285
286 void WebPluginDelegateImpl::CGPaint(CGContextRef context,
287 const gfx::Rect& rect) {
288 WindowlessPaint(context, rect);
289 }
290
291 bool WebPluginDelegateImpl::PlatformHandleInputEvent(
292 const WebInputEvent& event, WebCursor::CursorInfo* cursor_info) {
293 DCHECK(cursor_info != NULL);
294
295 // If an event comes in before the plugin has been set up, bail.
296 if (!have_called_set_window_)
297 return false;
298
299 // WebKit sometimes sends spurious mouse move events when the window doesn't
300 // have focus; Cocoa event model plugins don't expect to receive mouse move
301 // events when they are in a background window, so drop those events.
302 if (!containing_window_has_focus_ &&
303 (event.type == WebInputEvent::MouseMove ||
304 event.type == WebInputEvent::MouseEnter ||
305 event.type == WebInputEvent::MouseLeave)) {
306 return false;
307 }
308
309 if (WebInputEvent::isMouseEventType(event.type) ||
310 event.type == WebInputEvent::MouseWheel) {
311 // Check our plugin location before we send the event to the plugin, just
312 // in case we somehow missed a plugin frame change.
313 const WebMouseEvent* mouse_event =
314 static_cast<const WebMouseEvent*>(&event);
315 gfx::Point content_origin(
316 mouse_event->globalX - mouse_event->x - window_rect_.x(),
317 mouse_event->globalY - mouse_event->y - window_rect_.y());
318 if (content_origin.x() != content_area_origin_.x() ||
319 content_origin.y() != content_area_origin_.y()) {
320 DLOG(WARNING) << "Stale plugin content area location: "
321 << content_area_origin_.ToString() << " instead of "
322 << content_origin.ToString();
323 SetContentAreaOrigin(content_origin);
324 }
325
326 current_windowless_cursor_.GetCursorInfo(cursor_info);
327 }
328
329 // Per the Cocoa Plugin IME spec, plugins shoudn't receive keydown or keyup
330 // events while composition is in progress. Treat them as handled, however,
331 // since IME is consuming them on behalf of the plugin.
332 if ((event.type == WebInputEvent::KeyDown && ime_enabled_) ||
333 (event.type == WebInputEvent::KeyUp && keyup_ignore_count_)) {
334 // Composition ends on a keydown, so ime_enabled_ will be false at keyup;
335 // because the keydown wasn't sent to the plugin, the keyup shouldn't be
336 // either (per the spec).
337 if (event.type == WebInputEvent::KeyDown)
338 ++keyup_ignore_count_;
339 else
340 --keyup_ignore_count_;
341 return true;
342 }
343
344 ScopedActiveDelegate active_delegate(this);
345
346 // Create the plugin event structure.
347 scoped_ptr<PluginWebEventConverter> event_converter(
348 new PluginWebEventConverter);
349 if (!event_converter->InitWithEvent(event)) {
350 // Silently consume any keyboard event types that aren't handled, so that
351 // they don't fall through to the page.
352 if (WebInputEvent::isKeyboardEventType(event.type))
353 return true;
354 return false;
355 }
356 NPCocoaEvent* plugin_event = event_converter->plugin_event();
357
358 // The plugin host recieves events related to drags starting outside the
359 // plugin, but the NPAPI Cocoa event model spec says plugins shouldn't receive
360 // them, so filter them out.
361 // If WebKit adds a page capture mode (like the plugin capture mode that
362 // handles drags starting inside) this can be removed.
363 bool drag_related = external_drag_tracker_->EventIsRelatedToDrag(event);
364 external_drag_tracker_->UpdateDragStateFromEvent(event);
365 if (drag_related) {
366 if (event.type == WebInputEvent::MouseUp &&
367 !external_drag_tracker_->IsDragInProgress()) {
368 // When an external drag ends, we need to synthesize a MouseEntered.
369 NPCocoaEvent enter_event = *plugin_event;
370 enter_event.type = NPCocoaEventMouseEntered;
371 ScopedCurrentPluginEvent event_scope(instance(), &enter_event);
372 instance()->NPP_HandleEvent(&enter_event);
373 }
374 return false;
375 }
376
377 // Send the plugin the event.
378 scoped_ptr<ScopedCurrentPluginEvent> event_scope(
379 new ScopedCurrentPluginEvent(instance(), plugin_event));
380 int16_t handle_response = instance()->NPP_HandleEvent(plugin_event);
381 bool handled = handle_response != kNPEventNotHandled;
382
383 // Start IME if requested by the plugin.
384 if (handled && handle_response == kNPEventStartIME &&
385 event.type == WebInputEvent::KeyDown) {
386 StartIme();
387 ++keyup_ignore_count_;
388 }
389
390 // Plugins don't give accurate information about whether or not they handled
391 // events, so browsers on the Mac ignore the return value.
392 // Scroll events are the exception, since the Cocoa spec defines a meaning
393 // for the return value.
394 if (WebInputEvent::isMouseEventType(event.type)) {
395 handled = true;
396 } else if (WebInputEvent::isKeyboardEventType(event.type)) {
397 // For Command-key events, trust the return value since eating all menu
398 // shortcuts is not ideal.
399 // TODO(stuartmorgan): Implement the advanced key handling spec, and trust
400 // trust the key event return value from plugins that implement it.
401 if (!(event.modifiers & WebInputEvent::MetaKey))
402 handled = true;
403 }
404
405 return handled;
406 }
407
408 #pragma mark -
409
410 void WebPluginDelegateImpl::WindowlessUpdateGeometry(
411 const gfx::Rect& window_rect,
412 const gfx::Rect& clip_rect) {
413 gfx::Rect old_clip_rect = clip_rect_;
414 cached_clip_rect_ = clip_rect;
415 if (container_is_visible_) // Remove check when cached_clip_rect_ is removed.
416 clip_rect_ = clip_rect;
417 bool clip_rect_changed = (clip_rect_ != old_clip_rect);
418 bool window_size_changed = (window_rect.size() != window_rect_.size());
419
420 if (window_rect == window_rect_ && !clip_rect_changed)
421 return;
422
423 if (old_clip_rect.IsEmpty() != clip_rect_.IsEmpty()) {
424 PluginVisibilityChanged();
425 }
426
427 SetPluginRect(window_rect);
428
429 if (window_size_changed || clip_rect_changed)
430 WindowlessSetWindow();
431 }
432
433 void WebPluginDelegateImpl::WindowlessPaint(gfx::NativeDrawingContext context,
434 const gfx::Rect& damage_rect) {
435 // If we get a paint event before we are completely set up (e.g., a nested
436 // call while the plugin is still in NPP_SetWindow), bail.
437 if (!have_called_set_window_ || (use_buffer_context_ && !buffer_context_))
438 return;
439 DCHECK(!use_buffer_context_ || buffer_context_ == context);
440
441 base::StatsRate plugin_paint("Plugin.Paint");
442 base::StatsScope<base::StatsRate> scope(plugin_paint);
443
444 gfx::Rect paint_rect = damage_rect;
445 if (use_buffer_context_) {
446 // Plugin invalidates trigger asynchronous paints with the original
447 // invalidation rect; the plugin may be resized before the paint is handled,
448 // so we need to ensure that the damage rect is still sane.
449 paint_rect.Intersect(
450 gfx::Rect(0, 0, window_rect_.width(), window_rect_.height()));
451 } else {
452 // Use the actual window region when drawing directly to the window context.
453 paint_rect.Intersect(window_rect_);
454 }
455
456 ScopedActiveDelegate active_delegate(this);
457
458 CGContextSaveGState(context);
459
460 if (!use_buffer_context_) {
461 // Reposition the context origin so that plugins will draw at the correct
462 // location in the window.
463 CGContextClipToRect(context, paint_rect.ToCGRect());
464 CGContextTranslateCTM(context, window_rect_.x(), window_rect_.y());
465 }
466
467 NPCocoaEvent paint_event;
468 memset(&paint_event, 0, sizeof(NPCocoaEvent));
469 paint_event.type = NPCocoaEventDrawRect;
470 paint_event.data.draw.context = context;
471 paint_event.data.draw.x = paint_rect.x();
472 paint_event.data.draw.y = paint_rect.y();
473 paint_event.data.draw.width = paint_rect.width();
474 paint_event.data.draw.height = paint_rect.height();
475 instance()->NPP_HandleEvent(&paint_event);
476
477 if (use_buffer_context_) {
478 // The backing buffer can change during the call to NPP_HandleEvent, in
479 // which case the old context is (or is about to be) invalid.
480 if (context == buffer_context_)
481 CGContextRestoreGState(context);
482 } else {
483 // Always restore the context to the saved state.
484 CGContextRestoreGState(context);
485 }
486 }
487
488 void WebPluginDelegateImpl::WindowlessSetWindow() {
489 if (!instance())
490 return;
491
492 window_.x = 0;
493 window_.y = 0;
494 window_.height = window_rect_.height();
495 window_.width = window_rect_.width();
496 window_.clipRect.left = clip_rect_.x();
497 window_.clipRect.top = clip_rect_.y();
498 window_.clipRect.right = clip_rect_.x() + clip_rect_.width();
499 window_.clipRect.bottom = clip_rect_.y() + clip_rect_.height();
500
501 NPError err = instance()->NPP_SetWindow(&window_);
502
503 // Send an appropriate window focus event after the first SetWindow.
504 if (!have_called_set_window_) {
505 have_called_set_window_ = true;
506 SetWindowHasFocus(initial_window_focus_);
507 }
508
509 DCHECK(err == NPERR_NO_ERROR);
510 }
511
512 #pragma mark -
513
514 bool WebPluginDelegateImpl::WindowedCreatePlugin() {
515 NOTREACHED();
516 return false;
517 }
518
519 void WebPluginDelegateImpl::WindowedDestroyWindow() {
520 NOTREACHED();
521 }
522
523 bool WebPluginDelegateImpl::WindowedReposition(const gfx::Rect& window_rect,
524 const gfx::Rect& clip_rect) {
525 NOTREACHED();
526 return false;
527 }
528
529 void WebPluginDelegateImpl::WindowedSetWindow() {
530 NOTREACHED();
531 }
532
533 #pragma mark -
534 #pragma mark Mac Extensions
535
536 void WebPluginDelegateImpl::PluginDidInvalidate() {
537 if (instance()->drawing_model() == NPDrawingModelInvalidatingCoreAnimation)
538 DrawLayerInSurface();
539 }
540
541 WebPluginDelegateImpl* WebPluginDelegateImpl::GetActiveDelegate() {
542 return g_active_delegate;
543 }
544
545 void WebPluginDelegateImpl::SetWindowHasFocus(bool has_focus) {
546 // If we get a window focus event before calling SetWindow, just remember the
547 // states (WindowlessSetWindow will then send it on the first call).
548 if (!have_called_set_window_) {
549 initial_window_focus_ = has_focus;
550 return;
551 }
552
553 if (has_focus == containing_window_has_focus_)
554 return;
555 containing_window_has_focus_ = has_focus;
556
557 ScopedActiveDelegate active_delegate(this);
558 NPCocoaEvent focus_event;
559 memset(&focus_event, 0, sizeof(focus_event));
560 focus_event.type = NPCocoaEventWindowFocusChanged;
561 focus_event.data.focus.hasFocus = has_focus;
562 instance()->NPP_HandleEvent(&focus_event);
563 }
564
565 bool WebPluginDelegateImpl::PlatformSetPluginHasFocus(bool focused) {
566 if (!have_called_set_window_)
567 return false;
568
569 plugin_->FocusChanged(focused);
570
571 ScopedActiveDelegate active_delegate(this);
572
573 NPCocoaEvent focus_event;
574 memset(&focus_event, 0, sizeof(focus_event));
575 focus_event.type = NPCocoaEventFocusChanged;
576 focus_event.data.focus.hasFocus = focused;
577 instance()->NPP_HandleEvent(&focus_event);
578
579 return true;
580 }
581
582 void WebPluginDelegateImpl::SetContainerVisibility(bool is_visible) {
583 if (is_visible == container_is_visible_)
584 return;
585 container_is_visible_ = is_visible;
586
587 // TODO(stuartmorgan): This is a temporary workarond for
588 // <http://crbug.com/34266>. When that is fixed, the cached_clip_rect_ code
589 // should all be removed.
590 if (is_visible) {
591 clip_rect_ = cached_clip_rect_;
592 } else {
593 clip_rect_.set_width(0);
594 clip_rect_.set_height(0);
595 }
596
597 // If the plugin is changing visibility, let the plugin know. If it's scrolled
598 // off screen (i.e., cached_clip_rect_ is empty), then container visibility
599 // doesn't change anything.
600 if (!cached_clip_rect_.IsEmpty()) {
601 PluginVisibilityChanged();
602 WindowlessSetWindow();
603 }
604
605 // When the plugin become visible, send an empty invalidate. If there were any
606 // pending invalidations this will trigger a paint event for the damaged
607 // region, and if not it's a no-op. This is necessary since higher levels
608 // that would normally do this weren't responsible for the clip_rect_ change).
609 if (!clip_rect_.IsEmpty())
610 instance()->webplugin()->InvalidateRect(gfx::Rect());
611 }
612
613 void WebPluginDelegateImpl::WindowFrameChanged(const gfx::Rect& window_frame,
614 const gfx::Rect& view_frame) {
615 instance()->set_window_frame(window_frame);
616 SetContentAreaOrigin(gfx::Point(view_frame.x(), view_frame.y()));
617 }
618
619 void WebPluginDelegateImpl::ImeCompositionCompleted(
620 const base::string16& text) {
621 ime_enabled_ = false;
622
623 // If |text| is empty this was just called to tell us composition was
624 // cancelled externally (e.g., the user pressed esc).
625 if (!text.empty()) {
626 NPCocoaEvent text_event;
627 memset(&text_event, 0, sizeof(NPCocoaEvent));
628 text_event.type = NPCocoaEventTextInput;
629 text_event.data.text.text =
630 reinterpret_cast<NPNSString*>(base::SysUTF16ToNSString(text));
631 instance()->NPP_HandleEvent(&text_event);
632 }
633 }
634
635 void WebPluginDelegateImpl::SetNSCursor(NSCursor* cursor) {
636 current_windowless_cursor_.InitFromNSCursor(cursor);
637 }
638
639 void WebPluginDelegateImpl::SetNoBufferContext() {
640 use_buffer_context_ = false;
641 }
642
643 #pragma mark -
644 #pragma mark Internal Tracking
645
646 void WebPluginDelegateImpl::SetPluginRect(const gfx::Rect& rect) {
647 bool plugin_size_changed = rect.width() != window_rect_.width() ||
648 rect.height() != window_rect_.height();
649 window_rect_ = rect;
650 PluginScreenLocationChanged();
651 if (plugin_size_changed)
652 UpdateAcceleratedSurface();
653 }
654
655 void WebPluginDelegateImpl::SetContentAreaOrigin(const gfx::Point& origin) {
656 content_area_origin_ = origin;
657 PluginScreenLocationChanged();
658 }
659
660 void WebPluginDelegateImpl::PluginScreenLocationChanged() {
661 gfx::Point plugin_origin(content_area_origin_.x() + window_rect_.x(),
662 content_area_origin_.y() + window_rect_.y());
663 instance()->set_plugin_origin(plugin_origin);
664 }
665
666 void WebPluginDelegateImpl::PluginVisibilityChanged() {
667 if (instance()->drawing_model() == NPDrawingModelCoreAnimation) {
668 bool plugin_visible = container_is_visible_ && !clip_rect_.IsEmpty();
669 if (plugin_visible && !redraw_timer_->IsRunning()) {
670 redraw_timer_->Start(FROM_HERE,
671 base::TimeDelta::FromMilliseconds(kCoreAnimationRedrawPeriodMs),
672 this, &WebPluginDelegateImpl::DrawLayerInSurface);
673 } else if (!plugin_visible) {
674 redraw_timer_->Stop();
675 }
676 }
677 }
678
679 void WebPluginDelegateImpl::StartIme() {
680 if (ime_enabled_)
681 return;
682 ime_enabled_ = true;
683 plugin_->StartIme();
684 }
685
686 #pragma mark -
687 #pragma mark Core Animation Support
688
689 void WebPluginDelegateImpl::DrawLayerInSurface() {
690 // If we haven't plumbed up the surface yet, don't try to draw.
691 if (!renderer_)
692 return;
693
694 [renderer_ beginFrameAtTime:CACurrentMediaTime() timeStamp:NULL];
695 if (CGRectIsEmpty([renderer_ updateBounds])) {
696 // If nothing has changed, we are done.
697 [renderer_ endFrame];
698 return;
699 }
700
701 surface_->StartDrawing();
702
703 CGRect layerRect = [layer_ bounds];
704 [renderer_ addUpdateRect:layerRect];
705 [renderer_ render];
706 [renderer_ endFrame];
707
708 surface_->EndDrawing();
709 }
710
711 // Update the size of the surface to match the current size of the plug-in.
712 void WebPluginDelegateImpl::UpdateAcceleratedSurface() {
713 if (!surface_ || !layer_)
714 return;
715
716 [CATransaction begin];
717 [CATransaction setValue:[NSNumber numberWithInt:0]
718 forKey:kCATransactionAnimationDuration];
719 [layer_ setFrame:CGRectMake(0, 0,
720 window_rect_.width(), window_rect_.height())];
721 [CATransaction commit];
722
723 [renderer_ setBounds:[layer_ bounds]];
724 surface_->SetSize(window_rect_.size());
725 // Kick off the drawing timer, if necessary.
726 PluginVisibilityChanged();
727 }
728
729 } // namespace npapi
730 } // namespace webkit
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698