dom/plugins/test/testplugin/nptest_macosx.mm

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* ***** BEGIN LICENSE BLOCK *****
michael@0 2 *
michael@0 3 * Copyright (c) 2008, Mozilla Corporation
michael@0 4 * All rights reserved.
michael@0 5 *
michael@0 6 * Redistribution and use in source and binary forms, with or without
michael@0 7 * modification, are permitted provided that the following conditions are met:
michael@0 8 *
michael@0 9 * * Redistributions of source code must retain the above copyright notice, this
michael@0 10 * list of conditions and the following disclaimer.
michael@0 11 * * Redistributions in binary form must reproduce the above copyright notice,
michael@0 12 * this list of conditions and the following disclaimer in the documentation
michael@0 13 * and/or other materials provided with the distribution.
michael@0 14 * * Neither the name of the Mozilla Corporation nor the names of its
michael@0 15 * contributors may be used to endorse or promote products derived from this
michael@0 16 * software without specific prior written permission.
michael@0 17 *
michael@0 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
michael@0 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
michael@0 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
michael@0 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
michael@0 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
michael@0 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
michael@0 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
michael@0 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
michael@0 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 28 *
michael@0 29 * Contributor(s):
michael@0 30 * Josh Aas <josh@mozilla.com>
michael@0 31 *
michael@0 32 * ***** END LICENSE BLOCK ***** */
michael@0 33
michael@0 34 #include "nptest_platform.h"
michael@0 35 #include "nsAlgorithm.h"
michael@0 36 #include <CoreServices/CoreServices.h>
michael@0 37 #include <algorithm>
michael@0 38
michael@0 39 using namespace std;
michael@0 40
michael@0 41 bool
michael@0 42 pluginSupportsWindowMode()
michael@0 43 {
michael@0 44 return false;
michael@0 45 }
michael@0 46
michael@0 47 bool
michael@0 48 pluginSupportsWindowlessMode()
michael@0 49 {
michael@0 50 return true;
michael@0 51 }
michael@0 52
michael@0 53 bool
michael@0 54 pluginSupportsAsyncBitmapDrawing()
michael@0 55 {
michael@0 56 return false;
michael@0 57 }
michael@0 58
michael@0 59 NPError
michael@0 60 pluginInstanceInit(InstanceData* instanceData)
michael@0 61 {
michael@0 62 NPP npp = instanceData->npp;
michael@0 63
michael@0 64 NPBool supportsCoreGraphics = false;
michael@0 65 if ((NPN_GetValue(npp, NPNVsupportsCoreGraphicsBool, &supportsCoreGraphics) == NPERR_NO_ERROR) &&
michael@0 66 supportsCoreGraphics) {
michael@0 67 NPN_SetValue(npp, NPPVpluginDrawingModel, (void*)NPDrawingModelCoreGraphics);
michael@0 68 } else {
michael@0 69 printf("CoreGraphics drawing model not supported, can't create a plugin instance.\n");
michael@0 70 return NPERR_INCOMPATIBLE_VERSION_ERROR;
michael@0 71 }
michael@0 72
michael@0 73 NPBool supportsCocoaEvents = false;
michael@0 74 if ((NPN_GetValue(npp, NPNVsupportsCocoaBool, &supportsCocoaEvents) == NPERR_NO_ERROR) &&
michael@0 75 supportsCocoaEvents) {
michael@0 76 NPN_SetValue(npp, NPPVpluginEventModel, (void*)NPEventModelCocoa);
michael@0 77 instanceData->eventModel = NPEventModelCocoa;
michael@0 78 } else {
michael@0 79 printf("Cocoa event model not supported, can't create a plugin instance.\n");
michael@0 80 return NPERR_INCOMPATIBLE_VERSION_ERROR;
michael@0 81 }
michael@0 82
michael@0 83 return NPERR_NO_ERROR;
michael@0 84 }
michael@0 85
michael@0 86 void
michael@0 87 pluginInstanceShutdown(InstanceData* instanceData)
michael@0 88 {
michael@0 89 }
michael@0 90
michael@0 91 static bool
michael@0 92 RectEquals(const NPRect& r1, const NPRect& r2)
michael@0 93 {
michael@0 94 return r1.left == r2.left && r1.top == r2.top &&
michael@0 95 r1.right == r2.right && r1.bottom == r2.bottom;
michael@0 96 }
michael@0 97
michael@0 98 void
michael@0 99 pluginDoSetWindow(InstanceData* instanceData, NPWindow* newWindow)
michael@0 100 {
michael@0 101 // Ugh. Due to a terrible Gecko bug, we have to ignore position changes
michael@0 102 // when the clip rect doesn't change; the position can be wrong
michael@0 103 // when set by a path other than nsObjectFrame::FixUpPluginWindow.
michael@0 104 int32_t oldX = instanceData->window.x;
michael@0 105 int32_t oldY = instanceData->window.y;
michael@0 106 bool clipChanged =
michael@0 107 !RectEquals(instanceData->window.clipRect, newWindow->clipRect);
michael@0 108 instanceData->window = *newWindow;
michael@0 109 if (!clipChanged) {
michael@0 110 instanceData->window.x = oldX;
michael@0 111 instanceData->window.y = oldY;
michael@0 112 }
michael@0 113 }
michael@0 114
michael@0 115 void
michael@0 116 pluginWidgetInit(InstanceData* instanceData, void* oldWindow)
michael@0 117 {
michael@0 118 // Should never be called since we don't support window mode
michael@0 119 }
michael@0 120
michael@0 121 static void
michael@0 122 GetColorsFromRGBA(uint32_t rgba, float* r, float* g, float* b, float* a)
michael@0 123 {
michael@0 124 *b = (rgba & 0xFF) / 255.0;
michael@0 125 *g = ((rgba & 0xFF00) >> 8) / 255.0;
michael@0 126 *r = ((rgba & 0xFF0000) >> 16) / 255.0;
michael@0 127 *a = ((rgba & 0xFF000000) >> 24) / 255.0;
michael@0 128 }
michael@0 129
michael@0 130 static void
michael@0 131 pluginDraw(InstanceData* instanceData, NPCocoaEvent* event)
michael@0 132 {
michael@0 133 if (!instanceData)
michael@0 134 return;
michael@0 135
michael@0 136 notifyDidPaint(instanceData);
michael@0 137
michael@0 138 NPP npp = instanceData->npp;
michael@0 139 if (!npp)
michael@0 140 return;
michael@0 141
michael@0 142 const char* uaString = NPN_UserAgent(npp);
michael@0 143 if (!uaString)
michael@0 144 return;
michael@0 145
michael@0 146 NPWindow window = instanceData->window;
michael@0 147
michael@0 148 CGContextRef cgContext = event->data.draw.context;
michael@0 149
michael@0 150 float windowWidth = window.width;
michael@0 151 float windowHeight = window.height;
michael@0 152
michael@0 153 switch(instanceData->scriptableObject->drawMode) {
michael@0 154 case DM_DEFAULT: {
michael@0 155 CFStringRef uaCFString = CFStringCreateWithCString(kCFAllocatorDefault, uaString, kCFStringEncodingASCII);
michael@0 156 // save the cgcontext gstate
michael@0 157 CGContextSaveGState(cgContext);
michael@0 158
michael@0 159 // we get a flipped context
michael@0 160 CGContextTranslateCTM(cgContext, 0.0, windowHeight);
michael@0 161 CGContextScaleCTM(cgContext, 1.0, -1.0);
michael@0 162
michael@0 163 // draw a gray background for the plugin
michael@0 164 CGContextAddRect(cgContext, CGRectMake(0, 0, windowWidth, windowHeight));
michael@0 165 CGContextSetGrayFillColor(cgContext, 0.5, 1.0);
michael@0 166 CGContextDrawPath(cgContext, kCGPathFill);
michael@0 167
michael@0 168 // draw a black frame around the plugin
michael@0 169 CGContextAddRect(cgContext, CGRectMake(0, 0, windowWidth, windowHeight));
michael@0 170 CGContextSetGrayStrokeColor(cgContext, 0.0, 1.0);
michael@0 171 CGContextSetLineWidth(cgContext, 6.0);
michael@0 172 CGContextStrokePath(cgContext);
michael@0 173
michael@0 174 // draw the UA string using Core Text
michael@0 175 CGContextSetTextMatrix(cgContext, CGAffineTransformIdentity);
michael@0 176
michael@0 177 // Initialize a rectangular path.
michael@0 178 CGMutablePathRef path = CGPathCreateMutable();
michael@0 179 CGRect bounds = CGRectMake(10.0, 10.0, std::max(0.0, windowWidth - 20.0),
michael@0 180 std::max(0.0, windowHeight - 20.0));
michael@0 181 CGPathAddRect(path, NULL, bounds);
michael@0 182
michael@0 183 // Initialize an attributed string.
michael@0 184 CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
michael@0 185 CFAttributedStringReplaceString(attrString, CFRangeMake(0, 0), uaCFString);
michael@0 186
michael@0 187 // Create a color and add it as an attribute to the string.
michael@0 188 CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
michael@0 189 CGFloat components[] = { 0.0, 0.0, 0.0, 1.0 };
michael@0 190 CGColorRef red = CGColorCreate(rgbColorSpace, components);
michael@0 191 CGColorSpaceRelease(rgbColorSpace);
michael@0 192 CFAttributedStringSetAttribute(attrString, CFRangeMake(0, 50), kCTForegroundColorAttributeName, red);
michael@0 193
michael@0 194 // Create the framesetter with the attributed string.
michael@0 195 CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
michael@0 196 CFRelease(attrString);
michael@0 197
michael@0 198 // Create the frame and draw it into the graphics context
michael@0 199 CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
michael@0 200 CFRelease(framesetter);
michael@0 201 if (frame) {
michael@0 202 CTFrameDraw(frame, cgContext);
michael@0 203 CFRelease(frame);
michael@0 204 }
michael@0 205
michael@0 206 // restore the cgcontext gstate
michael@0 207 CGContextRestoreGState(cgContext);
michael@0 208 break;
michael@0 209 }
michael@0 210 case DM_SOLID_COLOR: {
michael@0 211 // save the cgcontext gstate
michael@0 212 CGContextSaveGState(cgContext);
michael@0 213
michael@0 214 // we get a flipped context
michael@0 215 CGContextTranslateCTM(cgContext, 0.0, windowHeight);
michael@0 216 CGContextScaleCTM(cgContext, 1.0, -1.0);
michael@0 217
michael@0 218 // draw a solid background for the plugin
michael@0 219 CGContextAddRect(cgContext, CGRectMake(0, 0, windowWidth, windowHeight));
michael@0 220
michael@0 221 float r,g,b,a;
michael@0 222 GetColorsFromRGBA(instanceData->scriptableObject->drawColor, &r, &g, &b, &a);
michael@0 223 CGContextSetRGBFillColor(cgContext, r, g, b, a);
michael@0 224 CGContextDrawPath(cgContext, kCGPathFill);
michael@0 225
michael@0 226 // restore the cgcontext gstate
michael@0 227 CGContextRestoreGState(cgContext);
michael@0 228 break;
michael@0 229 }
michael@0 230 }
michael@0 231 }
michael@0 232
michael@0 233 int16_t
michael@0 234 pluginHandleEvent(InstanceData* instanceData, void* event)
michael@0 235 {
michael@0 236 NPCocoaEvent* cocoaEvent = (NPCocoaEvent*)event;
michael@0 237 if (!cocoaEvent)
michael@0 238 return kNPEventNotHandled;
michael@0 239
michael@0 240 switch (cocoaEvent->type) {
michael@0 241 case NPCocoaEventDrawRect:
michael@0 242 pluginDraw(instanceData, cocoaEvent);
michael@0 243 break;
michael@0 244 case NPCocoaEventMouseDown:
michael@0 245 case NPCocoaEventMouseUp:
michael@0 246 case NPCocoaEventMouseMoved:
michael@0 247 instanceData->lastMouseX = (int32_t)cocoaEvent->data.mouse.pluginX;
michael@0 248 instanceData->lastMouseY = (int32_t)cocoaEvent->data.mouse.pluginY;
michael@0 249 if (cocoaEvent->type == NPCocoaEventMouseUp) {
michael@0 250 instanceData->mouseUpEventCount++;
michael@0 251 }
michael@0 252 break;
michael@0 253 case NPCocoaEventWindowFocusChanged:
michael@0 254 instanceData->topLevelWindowActivationState = cocoaEvent->data.focus.hasFocus ?
michael@0 255 ACTIVATION_STATE_ACTIVATED : ACTIVATION_STATE_DEACTIVATED;
michael@0 256 instanceData->topLevelWindowActivationEventCount = instanceData->topLevelWindowActivationEventCount + 1;
michael@0 257 break;
michael@0 258 case NPCocoaEventFocusChanged:
michael@0 259 instanceData->focusState = cocoaEvent->data.focus.hasFocus ?
michael@0 260 ACTIVATION_STATE_ACTIVATED : ACTIVATION_STATE_DEACTIVATED;
michael@0 261 instanceData->focusEventCount = instanceData->focusEventCount + 1;
michael@0 262 break;
michael@0 263 default:
michael@0 264 return kNPEventNotHandled;
michael@0 265 }
michael@0 266
michael@0 267 return kNPEventHandled;
michael@0 268 }
michael@0 269
michael@0 270 int32_t pluginGetEdge(InstanceData* instanceData, RectEdge edge)
michael@0 271 {
michael@0 272 NPWindow* w = &instanceData->window;
michael@0 273 switch (edge) {
michael@0 274 case EDGE_LEFT:
michael@0 275 return w->x;
michael@0 276 case EDGE_TOP:
michael@0 277 return w->y;
michael@0 278 case EDGE_RIGHT:
michael@0 279 return w->x + w->width;
michael@0 280 case EDGE_BOTTOM:
michael@0 281 return w->y + w->height;
michael@0 282 }
michael@0 283 return NPTEST_INT32_ERROR;
michael@0 284 }
michael@0 285
michael@0 286 int32_t pluginGetClipRegionRectCount(InstanceData* instanceData)
michael@0 287 {
michael@0 288 return 1;
michael@0 289 }
michael@0 290
michael@0 291 int32_t pluginGetClipRegionRectEdge(InstanceData* instanceData,
michael@0 292 int32_t rectIndex, RectEdge edge)
michael@0 293 {
michael@0 294 if (rectIndex != 0)
michael@0 295 return NPTEST_INT32_ERROR;
michael@0 296
michael@0 297 // We have to add the Cocoa titlebar height here since the clip rect
michael@0 298 // is being returned relative to that
michael@0 299 static const int COCOA_TITLEBAR_HEIGHT = 22;
michael@0 300
michael@0 301 NPWindow* w = &instanceData->window;
michael@0 302 switch (edge) {
michael@0 303 case EDGE_LEFT:
michael@0 304 return w->clipRect.left;
michael@0 305 case EDGE_TOP:
michael@0 306 return w->clipRect.top + COCOA_TITLEBAR_HEIGHT;
michael@0 307 case EDGE_RIGHT:
michael@0 308 return w->clipRect.right;
michael@0 309 case EDGE_BOTTOM:
michael@0 310 return w->clipRect.bottom + COCOA_TITLEBAR_HEIGHT;
michael@0 311 }
michael@0 312 return NPTEST_INT32_ERROR;
michael@0 313 }
michael@0 314
michael@0 315 void pluginDoInternalConsistencyCheck(InstanceData* instanceData, string& error)
michael@0 316 {
michael@0 317 }

mercurial