blob: eac5db211f5f1509f2999a0ac2b3b87c97ac0a65 [file] [log] [blame]
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -04001/*
2 * Copyright 2008, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "BackgroundPlugin.h"
27#include "android_npapi.h"
28
29#include <stdio.h>
30#include <sys/time.h>
31#include <time.h>
32#include <math.h>
33#include <string.h>
34
35extern NPNetscapeFuncs* browser;
36extern ANPBitmapInterfaceV0 gBitmapI;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040037extern ANPCanvasInterfaceV0 gCanvasI;
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040038extern ANPLogInterfaceV0 gLogI;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040039extern ANPPaintInterfaceV0 gPaintI;
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040040extern ANPSurfaceInterfaceV0 gSurfaceI;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040041extern ANPTypefaceInterfaceV0 gTypefaceI;
42
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040043#define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
44
Derek Sollenberger0c763b62009-08-11 10:05:46 -040045static uint32_t getMSecs() {
46 struct timeval tv;
47 gettimeofday(&tv, NULL);
48 return (uint32_t) (tv.tv_sec * 1000 + tv.tv_usec / 1000 ); // microseconds to milliseconds
49}
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040050
51///////////////////////////////////////////////////////////////////////////////
52
53BackgroundPlugin::BackgroundPlugin(NPP inst) : SubPlugin(inst) {
54
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040055 // initialize the drawing surface
56 m_surfaceReady = false;
Derek Sollenberger5ad138f2009-08-10 10:50:59 -040057 m_surface = gSurfaceI.newRasterSurface(inst, kRGB_565_ANPBitmapFormat, false);
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040058 if(!m_surface)
59 gLogI.log(inst, kError_ANPLogType, "----%p Unable to create RGBA surface", inst);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040060
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040061 //initialize bitmap transparency variables
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040062 mFinishedStageOne = false;
63 mFinishedStageTwo = false;
64 mFinishedStageThree = false;
65
66 // test basic plugin functionality
67 test_logging(); // android logging
68 test_timers(); // plugin timers
69 test_bitmaps(); // android bitmaps
Derek Sollenbergerc356b9f2009-07-30 16:57:07 -040070 test_domAccess();
71 test_javascript();
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040072}
73
74BackgroundPlugin::~BackgroundPlugin() {
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040075 gSurfaceI.deleteSurface(m_surface);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040076}
77
Derek Sollenberger21f39912009-07-09 09:19:39 -040078bool BackgroundPlugin::supportsDrawingModel(ANPDrawingModel model) {
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040079 return (model == kSurface_ANPDrawingModel);
Derek Sollenberger21f39912009-07-09 09:19:39 -040080}
81
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040082void BackgroundPlugin::drawPlugin(int surfaceWidth, int surfaceHeight) {
83
84 // get the plugin's dimensions according to the DOM
85 PluginObject *obj = (PluginObject*) inst()->pdata;
86 const int W = obj->window->width;
87 const int H = obj->window->height;
88
89 // compute the current zoom level
90 const float zoomFactorW = static_cast<float>(surfaceWidth) / W;
91 const float zoomFactorH = static_cast<float>(surfaceHeight) / H;
92
93 // check to make sure the zoom level is uniform
94 if (zoomFactorW + .01 < zoomFactorH && zoomFactorW - .01 > zoomFactorH)
95 gLogI.log(inst(), kError_ANPLogType, " ------ %p zoom is out of sync (%f,%f)",
96 inst(), zoomFactorW, zoomFactorH);
97
98 // scale the variables based on the zoom level
99 const int fontSize = (int)(zoomFactorW * 16);
100 const int leftMargin = (int)(zoomFactorW * 10);
101
102 // lock the surface
103 ANPBitmap bitmap;
104 if (!m_surfaceReady || !gSurfaceI.lock(m_surface, &bitmap, NULL)) {
105 gLogI.log(inst(), kError_ANPLogType, " ------ %p unable to lock the plugin", inst());
106 return;
107 }
108
109 // create a canvas
Derek Sollenbergerc0f26572009-07-16 11:38:02 -0400110 ANPCanvas* canvas = gCanvasI.newCanvas(&bitmap);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400111 gCanvasI.drawColor(canvas, 0xFFFFFFFF);
112
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400113 ANPPaint* paint = gPaintI.newPaint();
114 gPaintI.setFlags(paint, gPaintI.getFlags(paint) | kAntiAlias_ANPPaintFlag);
115 gPaintI.setColor(paint, 0xFFFF0000);
116 gPaintI.setTextSize(paint, fontSize);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400117
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400118 ANPTypeface* tf = gTypefaceI.createFromName("serif", kItalic_ANPTypefaceStyle);
119 gPaintI.setTypeface(paint, tf);
120 gTypefaceI.unref(tf);
121
122 ANPFontMetrics fm;
123 gPaintI.getFontMetrics(paint, &fm);
124
125 gPaintI.setColor(paint, 0xFF0000FF);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400126 const char c[] = "This is a background plugin.";
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400127 gCanvasI.drawText(canvas, c, sizeof(c)-1, leftMargin, -fm.fTop, paint);
128
129 // clean up variables and unlock the surface
130 gPaintI.deletePaint(paint);
131 gCanvasI.deleteCanvas(canvas);
132 gSurfaceI.unlock(m_surface);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400133}
134
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400135int16 BackgroundPlugin::handleEvent(const ANPEvent* evt) {
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400136 switch (evt->eventType) {
137 case kDraw_ANPEventType:
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400138 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request draw events", inst());
139 break;
140 case kSurface_ANPEventType:
141 switch (evt->data.surface.action) {
142 case kCreated_ANPSurfaceAction:
143 m_surfaceReady = true;
144 return 1;
145 case kDestroyed_ANPSurfaceAction:
146 m_surfaceReady = false;
147 return 1;
148 case kChanged_ANPSurfaceAction:
149 drawPlugin(evt->data.surface.data.changed.width,
150 evt->data.surface.data.changed.height);
151 return 1;
152 }
153 break;
154 case kLifecycle_ANPEventType:
155 if (evt->data.lifecycle.action == kOnLoad_ANPLifecycleAction) {
156 gLogI.log(inst(), kDebug_ANPLogType, " ------ %p the plugin received an onLoad event", inst());
157 return 1;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400158 }
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400159 break;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400160 case kTouch_ANPEventType:
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400161 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request touch events", inst());
162 break;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400163 case kKey_ANPEventType:
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400164 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request key events", inst());
165 break;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400166 default:
167 break;
168 }
169 return 0; // unknown or unhandled event
170}
171
172///////////////////////////////////////////////////////////////////////////////
173// LOGGING TESTS
174///////////////////////////////////////////////////////////////////////////////
175
176
177void BackgroundPlugin::test_logging() {
178 NPP instance = this->inst();
179
180 //LOG_ERROR(instance, " ------ %p Testing Log Error", instance);
181 gLogI.log(instance, kError_ANPLogType, " ------ %p Testing Log Error", instance);
182 gLogI.log(instance, kWarning_ANPLogType, " ------ %p Testing Log Warning", instance);
183 gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing Log Debug", instance);
184}
185
186///////////////////////////////////////////////////////////////////////////////
187// TIMER TESTS
188///////////////////////////////////////////////////////////////////////////////
189
190#define TIMER_INTERVAL 50
191static void timer_oneshot(NPP instance, uint32 timerID);
192static void timer_repeat(NPP instance, uint32 timerID);
193static void timer_neverfires(NPP instance, uint32 timerID);
194static void timer_latency(NPP instance, uint32 timerID);
195
196void BackgroundPlugin::test_timers() {
197 NPP instance = this->inst();
198
199 //Setup the testing counters
200 mTimerRepeatCount = 5;
201 mTimerLatencyCount = 5;
202
203 // test for bogus timerID
204 browser->unscheduletimer(instance, 999999);
205 // test one-shot
206 browser->scheduletimer(instance, 100, false, timer_oneshot);
207 // test repeat
208 browser->scheduletimer(instance, 50, true, timer_repeat);
209 // test timer latency
210 browser->scheduletimer(instance, TIMER_INTERVAL, true, timer_latency);
211 mStartTime = mPrevTime = getMSecs();
212 // test unschedule immediately
213 uint32 id = browser->scheduletimer(instance, 100, false, timer_neverfires);
214 browser->unscheduletimer(instance, id);
215 // test double unschedule (should be no-op)
216 browser->unscheduletimer(instance, id);
217
218}
219
220static void timer_oneshot(NPP instance, uint32 timerID) {
221 gLogI.log(instance, kDebug_ANPLogType, "-------- oneshot timer\n");
222}
223
224static void timer_repeat(NPP instance, uint32 timerID) {
225 BackgroundPlugin *obj = ((BackgroundPlugin*) ((PluginObject*) instance->pdata)->activePlugin);
226
227 gLogI.log(instance, kDebug_ANPLogType, "-------- repeat timer %d\n",
228 obj->mTimerRepeatCount);
229 if (--obj->mTimerRepeatCount == 0) {
230 browser->unscheduletimer(instance, timerID);
231 }
232}
233
234static void timer_neverfires(NPP instance, uint32 timerID) {
235 gLogI.log(instance, kError_ANPLogType, "-------- timer_neverfires!!!\n");
236}
237
238static void timer_latency(NPP instance, uint32 timerID) {
239 BackgroundPlugin *obj = ((BackgroundPlugin*) ((PluginObject*) instance->pdata)->activePlugin);
240
241 obj->mTimerLatencyCurrentCount += 1;
242
243 uint32_t now = getMSecs();
244 uint32_t interval = now - obj->mPrevTime;
245 uint32_t dur = now - obj->mStartTime;
246 uint32_t expectedDur = obj->mTimerLatencyCurrentCount * TIMER_INTERVAL;
247 int32_t drift = dur - expectedDur;
248 int32_t avgDrift = drift / obj->mTimerLatencyCurrentCount;
249
250 obj->mPrevTime = now;
251
252 gLogI.log(instance, kDebug_ANPLogType,
253 "-------- latency test: [%3d] interval %d expected %d, total %d expected %d, drift %d avg %d\n",
254 obj->mTimerLatencyCurrentCount, interval, TIMER_INTERVAL, dur,
255 expectedDur, drift, avgDrift);
256
257 if (--obj->mTimerLatencyCount == 0) {
258 browser->unscheduletimer(instance, timerID);
259 }
260}
261
262///////////////////////////////////////////////////////////////////////////////
263// BITMAP TESTS
264///////////////////////////////////////////////////////////////////////////////
265
266static void test_formats(NPP instance);
267
268void BackgroundPlugin::test_bitmaps() {
269 test_formats(this->inst());
270}
271
272static void test_formats(NPP instance) {
273
274 // TODO pull names from enum in npapi instead of hardcoding them
275 static const struct {
276 ANPBitmapFormat fFormat;
277 const char* fName;
278 } gRecs[] = {
279 { kUnknown_ANPBitmapFormat, "unknown" },
280 { kRGBA_8888_ANPBitmapFormat, "8888" },
281 { kRGB_565_ANPBitmapFormat, "565" },
282 };
283
284 ANPPixelPacking packing;
285 for (size_t i = 0; i < ARRAY_COUNT(gRecs); i++) {
286 if (gBitmapI.getPixelPacking(gRecs[i].fFormat, &packing)) {
287 gLogI.log(instance, kDebug_ANPLogType,
288 "pixel format [%d] %s has packing ARGB [%d %d] [%d %d] [%d %d] [%d %d]\n",
289 gRecs[i].fFormat, gRecs[i].fName,
290 packing.AShift, packing.ABits,
291 packing.RShift, packing.RBits,
292 packing.GShift, packing.GBits,
293 packing.BShift, packing.BBits);
294 } else {
295 gLogI.log(instance, kDebug_ANPLogType,
296 "pixel format [%d] %s has no packing\n",
297 gRecs[i].fFormat, gRecs[i].fName);
298 }
299 }
300}
301
302void BackgroundPlugin::test_bitmap_transparency(const ANPEvent* evt) {
303 NPP instance = this->inst();
304
305 // check default & set transparent
306 if (!mFinishedStageOne) {
307
308 gLogI.log(instance, kDebug_ANPLogType, "BEGIN: testing bitmap transparency");
309
310 //check to make sure it is not transparent
311 if (evt->data.draw.data.bitmap.format == kRGBA_8888_ANPBitmapFormat) {
312 gLogI.log(instance, kError_ANPLogType, "bitmap default format is transparent");
313 }
314
315 //make it transparent (any non-null value will set it to true)
316 bool value = true;
317 NPError err = browser->setvalue(instance, NPPVpluginTransparentBool, &value);
318 if (err != NPERR_NO_ERROR) {
319 gLogI.log(instance, kError_ANPLogType, "Error setting transparency.");
320 }
321
322 mFinishedStageOne = true;
323 browser->invalidaterect(instance, NULL);
324 }
325 // check transparent & set opaque
326 else if (!mFinishedStageTwo) {
327
328 //check to make sure it is transparent
329 if (evt->data.draw.data.bitmap.format != kRGBA_8888_ANPBitmapFormat) {
330 gLogI.log(instance, kError_ANPLogType, "bitmap did not change to transparent format");
331 }
332
333 //make it opaque
334 NPError err = browser->setvalue(instance, NPPVpluginTransparentBool, NULL);
335 if (err != NPERR_NO_ERROR) {
336 gLogI.log(instance, kError_ANPLogType, "Error setting transparency.");
337 }
338
339 mFinishedStageTwo = true;
340 }
341 // check opaque
342 else if (!mFinishedStageThree) {
343
344 //check to make sure it is not transparent
345 if (evt->data.draw.data.bitmap.format == kRGBA_8888_ANPBitmapFormat) {
346 gLogI.log(instance, kError_ANPLogType, "bitmap default format is transparent");
347 }
348
349 gLogI.log(instance, kDebug_ANPLogType, "END: testing bitmap transparency");
350
351 mFinishedStageThree = true;
352 }
353}
Derek Sollenbergerc356b9f2009-07-30 16:57:07 -0400354
355///////////////////////////////////////////////////////////////////////////////
356// DOM TESTS
357///////////////////////////////////////////////////////////////////////////////
358
359void BackgroundPlugin::test_domAccess() {
360 NPP instance = this->inst();
361
362 gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing DOM Access", instance);
363
364 // Get the plugin's DOM object
365 NPObject* windowObject = NULL;
366 browser->getvalue(instance, NPNVWindowNPObject, &windowObject);
367
368 if (!windowObject)
369 gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to retrieve DOM Window", instance);
370
371 // Retrieve a property from the plugin's DOM object
372 NPIdentifier topIdentifier = browser->getstringidentifier("top");
373 NPVariant topObjectVariant;
374 browser->getproperty(instance, windowObject, topIdentifier, &topObjectVariant);
375
376 if (topObjectVariant.type != NPVariantType_Object)
377 gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Variant type for DOM Property: %d,%d", instance, topObjectVariant.type, NPVariantType_Object);
378}
379
380
381///////////////////////////////////////////////////////////////////////////////
382// JAVASCRIPT TESTS
383///////////////////////////////////////////////////////////////////////////////
384
385
386void BackgroundPlugin::test_javascript() {
387 NPP instance = this->inst();
388
389 gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing JavaScript Access", instance);
390
391 // Get the plugin's DOM object
392 NPObject* windowObject = NULL;
393 browser->getvalue(instance, NPNVWindowNPObject, &windowObject);
394
395 if (!windowObject)
396 gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to retrieve DOM Window", instance);
397
398 // create a string (JS code) that is stored in memory allocated by the browser
399 const char* jsString = "1200 + 34";
400 void* stringMem = browser->memalloc(strlen(jsString));
401 memcpy(stringMem, jsString, strlen(jsString));
402
403 // execute the javascript in the plugin's DOM object
404 NPString script = { (char*)stringMem, strlen(jsString) };
405 NPVariant scriptVariant;
406 if (!browser->evaluate(instance, windowObject, &script, &scriptVariant))
407 gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to eval the JS.", instance);
408
409 if (scriptVariant.type == NPVariantType_Int32) {
410 if (scriptVariant.value.intValue != 1234)
411 gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Value for JS Return: %d,1234", instance, scriptVariant.value.intValue);
412 } else {
413 gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Variant type for JS Return: %d,%d", instance, scriptVariant.type, NPVariantType_Int32);
414 }
415
416 // free the memory allocated within the browser
417 browser->memfree(stringMem);
418}