FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
PluginWindowMacCA.mm
1 /**********************************************************\
2 Original Author: Anson MacKeracher
3 
4 Created: May 5, 2010
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2010 Georg Fritzsche, Firebreath development team
13 \**********************************************************/
14 
15 #include "ConstructDefaultPluginWindows.h"
16 
17 #include "PluginWindowMacCA.h"
18 
19 using namespace FB;
20 
21 @interface MyCALayer : CALayer
22 {
23 }
24 
25 @end
26 
27 @implementation MyCALayer
28 
29 - (void)dealloc {
30  FBLOG_DEBUG("PluginCore", "");
31  [super dealloc];
32 }
33 
34 @end
35 
36 
37 FB::PluginWindowMacCA* FB::createPluginWindowMacCA()
38 {
39  return new PluginWindowMacCA();
40 }
41 
42 PluginWindowMacCA::PluginWindowMacCA()
43  : PluginWindowMac(), m_layer(NULL), m_txtlayer(NULL)
44 {
45  MyCALayer *mlayer = [[MyCALayer layer] retain];
46  mlayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
47  mlayer.needsDisplayOnBoundsChange = YES;
48  m_layer = mlayer;
49 
50  CATextLayer* txtlayer = [[CATextLayer layer] retain];
51  txtlayer.string = (DrawingModelCoreAnimation == getDrawingModel()) ? @"CoreAnimation" : @"InvalidatingCoreAnimation";
52  txtlayer.fontSize = 24;
53  txtlayer.foregroundColor = CGColorCreateGenericRGB(0.0, 1.0, 0.0, 1.0);
54  txtlayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
55  txtlayer.needsDisplayOnBoundsChange = YES;
56  txtlayer.hidden = true;
57  [mlayer addSublayer:txtlayer];
58  m_txtlayer = txtlayer;
59 }
60 
61 PluginWindowMacCA::~PluginWindowMacCA()
62 {
63  CATextLayer *txtlayer = (CATextLayer*) m_txtlayer;
64  [txtlayer release];
65  m_txtlayer = NULL;
66 
67  MyCALayer *mlayer = (MyCALayer*) m_layer;
68  [mlayer release];
69  m_layer = NULL;
70 }
71 
72 // Whether the browser expects a non-retained Core Animation layer.
73 static const unsigned WKNVExpectsNonretainedLayer = 74657;
74 
75 
76 int16_t PluginWindowMacCA::GetValue(NPPVariable variable, void *value) {
77  MyCALayer *mlayer = (MyCALayer*) m_layer;
78  if (NPPVpluginCoreAnimationLayer == variable) {
79  NSBundle* mainb = [NSBundle mainBundle];
80  // Check the undocumented WKNVExpectsNonretainedLayer added to WebKit2 June 4.
81  // Use the supported response as definitive. Not supported as of Firefox 7.0.0.
82  NPBool expectsNonretainedLayer = false;
83  if (NPERR_NO_ERROR == m_npHost->GetValue((NPNVariable) WKNVExpectsNonretainedLayer, &expectsNonretainedLayer))
84  *(CALayer**) value = (expectsNonretainedLayer) ? mlayer : [mlayer retain];
85  // Need to only retain when Safari is run in 32bit mode and is the host process. Backwards compatiblity.
86  // When Safari is run in 64bit mode, the plugin is sandboxed in another process. According to curr spec.
87  // Also, allow the application to specify this behavior in their main bundle plist file, in case it's
88  // an application linking to WebKit in 32bit mode, but it's not called Safari
89  else if (NSOrderedSame == [[mainb bundleIdentifier] compare:@"com.apple.Safari"] ||
90  [mainb objectForInfoDictionaryKey:@"FirebreathCALayerRetain"])
91  *(CALayer**) value = [mlayer retain];
92  else
93  *(CALayer**) value = mlayer;
94  FBLOG_INFO("PluginCore", "GetValue(NPPVpluginCoreAnimationLayer)=" << (void*) mlayer);
95  }
96  return NPERR_NO_ERROR;
97 }
98 
99 NPError PluginWindowMacCA::SetWindow(NPWindow* window)
100 {
101  MyCALayer *mlayer = (MyCALayer*) m_layer;
102  CATextLayer* txtlayer = (CATextLayer*) m_txtlayer;
103  FBLOG_INFO("PluginCore", "CALayer=" << (void*) mlayer);
104  FBLOG_INFO("PluginCore", " bounds(" << window->x << "," << window->y << ")(" << window->x + window->width << "," << window->y + window->height << ")");
105  FBLOG_INFO("PluginCore", " clip(" << window->clipRect.left << "," << window->clipRect.top << ")(" << window->clipRect.right << "," << window->clipRect.bottom << ")");
106  FBLOG_INFO("PluginCore", " layer(" << mlayer.bounds.origin.x << "," << mlayer.bounds.origin.y << ")(" << mlayer.bounds.origin.x + mlayer.bounds.size.width << "," << mlayer.bounds.origin.y + mlayer.bounds.size.height << ")");
107 
108  NPError rval = PluginWindowMac::SetWindow(window);
109  txtlayer.hidden = !m_drawLabel;
110  [txtlayer removeFromSuperlayer];
111  [mlayer addSublayer:txtlayer];
112  return rval;
113 }
114