blob: af518a9a80c9da48a385d69d4e6a1462a6fd4fa9 [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
Derek Sollenberger08581f12009-09-08 18:36:29 -040053BackgroundPlugin::BackgroundPlugin(NPP inst) : SurfaceSubPlugin(inst) {
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040054
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040055 // initialize the drawing surface
Derek Sollenberger08581f12009-09-08 18:36:29 -040056 m_surface = NULL;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040057
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040058 //initialize bitmap transparency variables
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040059 mFinishedStageOne = false;
60 mFinishedStageTwo = false;
61 mFinishedStageThree = false;
62
63 // test basic plugin functionality
64 test_logging(); // android logging
65 test_timers(); // plugin timers
66 test_bitmaps(); // android bitmaps
Derek Sollenbergerc356b9f2009-07-30 16:57:07 -040067 test_domAccess();
68 test_javascript();
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040069}
70
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -040071BackgroundPlugin::~BackgroundPlugin() {
72 surfaceDestroyed();
73}
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040074
Derek Sollenberger21f39912009-07-09 09:19:39 -040075bool BackgroundPlugin::supportsDrawingModel(ANPDrawingModel model) {
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040076 return (model == kSurface_ANPDrawingModel);
Derek Sollenberger21f39912009-07-09 09:19:39 -040077}
78
Derek Sollenberger08581f12009-09-08 18:36:29 -040079bool BackgroundPlugin::isFixedSurface() {
80 return false;
81}
82
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -040083void BackgroundPlugin::surfaceCreated(jobject surface) {
84 m_surface = surface;
Derek Sollenberger08581f12009-09-08 18:36:29 -040085}
86
87void BackgroundPlugin::surfaceChanged(int format, int width, int height) {
88 drawPlugin(width, height);
89}
90
91void BackgroundPlugin::surfaceDestroyed() {
92 JNIEnv* env = NULL;
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -040093 if (m_surface && gVM->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK) {
Derek Sollenberger08581f12009-09-08 18:36:29 -040094 env->DeleteGlobalRef(m_surface);
95 m_surface = NULL;
96 }
97}
98
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040099void BackgroundPlugin::drawPlugin(int surfaceWidth, int surfaceHeight) {
100
101 // get the plugin's dimensions according to the DOM
102 PluginObject *obj = (PluginObject*) inst()->pdata;
103 const int W = obj->window->width;
104 const int H = obj->window->height;
105
106 // compute the current zoom level
107 const float zoomFactorW = static_cast<float>(surfaceWidth) / W;
108 const float zoomFactorH = static_cast<float>(surfaceHeight) / H;
109
110 // check to make sure the zoom level is uniform
111 if (zoomFactorW + .01 < zoomFactorH && zoomFactorW - .01 > zoomFactorH)
112 gLogI.log(inst(), kError_ANPLogType, " ------ %p zoom is out of sync (%f,%f)",
113 inst(), zoomFactorW, zoomFactorH);
114
115 // scale the variables based on the zoom level
116 const int fontSize = (int)(zoomFactorW * 16);
117 const int leftMargin = (int)(zoomFactorW * 10);
118
119 // lock the surface
120 ANPBitmap bitmap;
Derek Sollenberger08581f12009-09-08 18:36:29 -0400121 JNIEnv* env = NULL;
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -0400122 if (!m_surface || gVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK ||
Derek Sollenberger08581f12009-09-08 18:36:29 -0400123 !gSurfaceI.lock(env, m_surface, &bitmap, NULL)) {
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400124 gLogI.log(inst(), kError_ANPLogType, " ------ %p unable to lock the plugin", inst());
125 return;
126 }
127
128 // create a canvas
Derek Sollenbergerc0f26572009-07-16 11:38:02 -0400129 ANPCanvas* canvas = gCanvasI.newCanvas(&bitmap);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400130 gCanvasI.drawColor(canvas, 0xFFFFFFFF);
131
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400132 ANPPaint* paint = gPaintI.newPaint();
133 gPaintI.setFlags(paint, gPaintI.getFlags(paint) | kAntiAlias_ANPPaintFlag);
134 gPaintI.setColor(paint, 0xFFFF0000);
135 gPaintI.setTextSize(paint, fontSize);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400136
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400137 ANPTypeface* tf = gTypefaceI.createFromName("serif", kItalic_ANPTypefaceStyle);
138 gPaintI.setTypeface(paint, tf);
139 gTypefaceI.unref(tf);
140
141 ANPFontMetrics fm;
142 gPaintI.getFontMetrics(paint, &fm);
143
144 gPaintI.setColor(paint, 0xFF0000FF);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400145 const char c[] = "This is a background plugin.";
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400146 gCanvasI.drawText(canvas, c, sizeof(c)-1, leftMargin, -fm.fTop, paint);
147
148 // clean up variables and unlock the surface
149 gPaintI.deletePaint(paint);
150 gCanvasI.deleteCanvas(canvas);
Derek Sollenberger08581f12009-09-08 18:36:29 -0400151 gSurfaceI.unlock(env, m_surface);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400152}
153
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400154int16 BackgroundPlugin::handleEvent(const ANPEvent* evt) {
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400155 switch (evt->eventType) {
156 case kDraw_ANPEventType:
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400157 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request draw events", inst());
158 break;
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400159 case kLifecycle_ANPEventType:
160 if (evt->data.lifecycle.action == kOnLoad_ANPLifecycleAction) {
161 gLogI.log(inst(), kDebug_ANPLogType, " ------ %p the plugin received an onLoad event", inst());
162 return 1;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400163 }
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400164 break;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400165 case kTouch_ANPEventType:
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400166 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request touch events", inst());
167 break;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400168 case kKey_ANPEventType:
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400169 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request key events", inst());
170 break;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400171 default:
172 break;
173 }
174 return 0; // unknown or unhandled event
175}
176
177///////////////////////////////////////////////////////////////////////////////
178// LOGGING TESTS
179///////////////////////////////////////////////////////////////////////////////
180
181
182void BackgroundPlugin::test_logging() {
183 NPP instance = this->inst();
184
185 //LOG_ERROR(instance, " ------ %p Testing Log Error", instance);
186 gLogI.log(instance, kError_ANPLogType, " ------ %p Testing Log Error", instance);
187 gLogI.log(instance, kWarning_ANPLogType, " ------ %p Testing Log Warning", instance);
188 gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing Log Debug", instance);
189}
190
191///////////////////////////////////////////////////////////////////////////////
192// TIMER TESTS
193///////////////////////////////////////////////////////////////////////////////
194
195#define TIMER_INTERVAL 50
196static void timer_oneshot(NPP instance, uint32 timerID);
197static void timer_repeat(NPP instance, uint32 timerID);
198static void timer_neverfires(NPP instance, uint32 timerID);
199static void timer_latency(NPP instance, uint32 timerID);
200
201void BackgroundPlugin::test_timers() {
202 NPP instance = this->inst();
203
204 //Setup the testing counters
205 mTimerRepeatCount = 5;
206 mTimerLatencyCount = 5;
207
208 // test for bogus timerID
209 browser->unscheduletimer(instance, 999999);
210 // test one-shot
211 browser->scheduletimer(instance, 100, false, timer_oneshot);
212 // test repeat
213 browser->scheduletimer(instance, 50, true, timer_repeat);
214 // test timer latency
215 browser->scheduletimer(instance, TIMER_INTERVAL, true, timer_latency);
216 mStartTime = mPrevTime = getMSecs();
217 // test unschedule immediately
218 uint32 id = browser->scheduletimer(instance, 100, false, timer_neverfires);
219 browser->unscheduletimer(instance, id);
220 // test double unschedule (should be no-op)
221 browser->unscheduletimer(instance, id);
222
223}
224
225static void timer_oneshot(NPP instance, uint32 timerID) {
226 gLogI.log(instance, kDebug_ANPLogType, "-------- oneshot timer\n");
227}
228
229static void timer_repeat(NPP instance, uint32 timerID) {
230 BackgroundPlugin *obj = ((BackgroundPlugin*) ((PluginObject*) instance->pdata)->activePlugin);
231
232 gLogI.log(instance, kDebug_ANPLogType, "-------- repeat timer %d\n",
233 obj->mTimerRepeatCount);
234 if (--obj->mTimerRepeatCount == 0) {
235 browser->unscheduletimer(instance, timerID);
236 }
237}
238
239static void timer_neverfires(NPP instance, uint32 timerID) {
240 gLogI.log(instance, kError_ANPLogType, "-------- timer_neverfires!!!\n");
241}
242
243static void timer_latency(NPP instance, uint32 timerID) {
244 BackgroundPlugin *obj = ((BackgroundPlugin*) ((PluginObject*) instance->pdata)->activePlugin);
245
246 obj->mTimerLatencyCurrentCount += 1;
247
248 uint32_t now = getMSecs();
249 uint32_t interval = now - obj->mPrevTime;
250 uint32_t dur = now - obj->mStartTime;
251 uint32_t expectedDur = obj->mTimerLatencyCurrentCount * TIMER_INTERVAL;
252 int32_t drift = dur - expectedDur;
253 int32_t avgDrift = drift / obj->mTimerLatencyCurrentCount;
254
255 obj->mPrevTime = now;
256
257 gLogI.log(instance, kDebug_ANPLogType,
258 "-------- latency test: [%3d] interval %d expected %d, total %d expected %d, drift %d avg %d\n",
259 obj->mTimerLatencyCurrentCount, interval, TIMER_INTERVAL, dur,
260 expectedDur, drift, avgDrift);
261
262 if (--obj->mTimerLatencyCount == 0) {
263 browser->unscheduletimer(instance, timerID);
264 }
265}
266
267///////////////////////////////////////////////////////////////////////////////
268// BITMAP TESTS
269///////////////////////////////////////////////////////////////////////////////
270
271static void test_formats(NPP instance);
272
273void BackgroundPlugin::test_bitmaps() {
274 test_formats(this->inst());
275}
276
277static void test_formats(NPP instance) {
278
279 // TODO pull names from enum in npapi instead of hardcoding them
280 static const struct {
281 ANPBitmapFormat fFormat;
282 const char* fName;
283 } gRecs[] = {
284 { kUnknown_ANPBitmapFormat, "unknown" },
285 { kRGBA_8888_ANPBitmapFormat, "8888" },
286 { kRGB_565_ANPBitmapFormat, "565" },
287 };
288
289 ANPPixelPacking packing;
290 for (size_t i = 0; i < ARRAY_COUNT(gRecs); i++) {
291 if (gBitmapI.getPixelPacking(gRecs[i].fFormat, &packing)) {
292 gLogI.log(instance, kDebug_ANPLogType,
293 "pixel format [%d] %s has packing ARGB [%d %d] [%d %d] [%d %d] [%d %d]\n",
294 gRecs[i].fFormat, gRecs[i].fName,
295 packing.AShift, packing.ABits,
296 packing.RShift, packing.RBits,
297 packing.GShift, packing.GBits,
298 packing.BShift, packing.BBits);
299 } else {
300 gLogI.log(instance, kDebug_ANPLogType,
301 "pixel format [%d] %s has no packing\n",
302 gRecs[i].fFormat, gRecs[i].fName);
303 }
304 }
305}
306
307void BackgroundPlugin::test_bitmap_transparency(const ANPEvent* evt) {
308 NPP instance = this->inst();
309
310 // check default & set transparent
311 if (!mFinishedStageOne) {
312
313 gLogI.log(instance, kDebug_ANPLogType, "BEGIN: testing bitmap transparency");
314
315 //check to make sure it is not transparent
316 if (evt->data.draw.data.bitmap.format == kRGBA_8888_ANPBitmapFormat) {
317 gLogI.log(instance, kError_ANPLogType, "bitmap default format is transparent");
318 }
319
320 //make it transparent (any non-null value will set it to true)
321 bool value = true;
322 NPError err = browser->setvalue(instance, NPPVpluginTransparentBool, &value);
323 if (err != NPERR_NO_ERROR) {
324 gLogI.log(instance, kError_ANPLogType, "Error setting transparency.");
325 }
326
327 mFinishedStageOne = true;
328 browser->invalidaterect(instance, NULL);
329 }
330 // check transparent & set opaque
331 else if (!mFinishedStageTwo) {
332
333 //check to make sure it is transparent
334 if (evt->data.draw.data.bitmap.format != kRGBA_8888_ANPBitmapFormat) {
335 gLogI.log(instance, kError_ANPLogType, "bitmap did not change to transparent format");
336 }
337
338 //make it opaque
339 NPError err = browser->setvalue(instance, NPPVpluginTransparentBool, NULL);
340 if (err != NPERR_NO_ERROR) {
341 gLogI.log(instance, kError_ANPLogType, "Error setting transparency.");
342 }
343
344 mFinishedStageTwo = true;
345 }
346 // check opaque
347 else if (!mFinishedStageThree) {
348
349 //check to make sure it is not transparent
350 if (evt->data.draw.data.bitmap.format == kRGBA_8888_ANPBitmapFormat) {
351 gLogI.log(instance, kError_ANPLogType, "bitmap default format is transparent");
352 }
353
354 gLogI.log(instance, kDebug_ANPLogType, "END: testing bitmap transparency");
355
356 mFinishedStageThree = true;
357 }
358}
Derek Sollenbergerc356b9f2009-07-30 16:57:07 -0400359
360///////////////////////////////////////////////////////////////////////////////
361// DOM TESTS
362///////////////////////////////////////////////////////////////////////////////
363
364void BackgroundPlugin::test_domAccess() {
365 NPP instance = this->inst();
366
367 gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing DOM Access", instance);
368
369 // Get the plugin's DOM object
370 NPObject* windowObject = NULL;
371 browser->getvalue(instance, NPNVWindowNPObject, &windowObject);
372
373 if (!windowObject)
374 gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to retrieve DOM Window", instance);
375
376 // Retrieve a property from the plugin's DOM object
377 NPIdentifier topIdentifier = browser->getstringidentifier("top");
378 NPVariant topObjectVariant;
379 browser->getproperty(instance, windowObject, topIdentifier, &topObjectVariant);
380
381 if (topObjectVariant.type != NPVariantType_Object)
382 gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Variant type for DOM Property: %d,%d", instance, topObjectVariant.type, NPVariantType_Object);
383}
384
385
386///////////////////////////////////////////////////////////////////////////////
387// JAVASCRIPT TESTS
388///////////////////////////////////////////////////////////////////////////////
389
390
391void BackgroundPlugin::test_javascript() {
392 NPP instance = this->inst();
393
394 gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing JavaScript Access", instance);
395
396 // Get the plugin's DOM object
397 NPObject* windowObject = NULL;
398 browser->getvalue(instance, NPNVWindowNPObject, &windowObject);
399
400 if (!windowObject)
401 gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to retrieve DOM Window", instance);
402
403 // create a string (JS code) that is stored in memory allocated by the browser
404 const char* jsString = "1200 + 34";
405 void* stringMem = browser->memalloc(strlen(jsString));
406 memcpy(stringMem, jsString, strlen(jsString));
407
408 // execute the javascript in the plugin's DOM object
409 NPString script = { (char*)stringMem, strlen(jsString) };
410 NPVariant scriptVariant;
411 if (!browser->evaluate(instance, windowObject, &script, &scriptVariant))
412 gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to eval the JS.", instance);
413
414 if (scriptVariant.type == NPVariantType_Int32) {
415 if (scriptVariant.value.intValue != 1234)
416 gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Value for JS Return: %d,1234", instance, scriptVariant.value.intValue);
417 } else {
418 gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Variant type for JS Return: %d,%d", instance, scriptVariant.type, NPVariantType_Int32);
419 }
420
421 // free the memory allocated within the browser
422 browser->memfree(stringMem);
423}