blob: 0f90291ef31bc8ad430c8add9313c995b4869f44 [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 Sollenbergerb4a23912009-11-09 15:38:58 -050041extern ANPSystemInterfaceV0 gSystemI;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040042extern ANPTypefaceInterfaceV0 gTypefaceI;
43
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040044#define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
45
Derek Sollenberger0c763b62009-08-11 10:05:46 -040046static uint32_t getMSecs() {
47 struct timeval tv;
48 gettimeofday(&tv, NULL);
49 return (uint32_t) (tv.tv_sec * 1000 + tv.tv_usec / 1000 ); // microseconds to milliseconds
50}
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040051
52///////////////////////////////////////////////////////////////////////////////
53
Derek Sollenberger08581f12009-09-08 18:36:29 -040054BackgroundPlugin::BackgroundPlugin(NPP inst) : SurfaceSubPlugin(inst) {
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040055
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040056 // initialize the drawing surface
Derek Sollenberger08581f12009-09-08 18:36:29 -040057 m_surface = NULL;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040058
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040059 //initialize bitmap transparency variables
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040060 mFinishedStageOne = false;
61 mFinishedStageTwo = false;
62 mFinishedStageThree = false;
63
64 // test basic plugin functionality
65 test_logging(); // android logging
66 test_timers(); // plugin timers
67 test_bitmaps(); // android bitmaps
Derek Sollenbergerc356b9f2009-07-30 16:57:07 -040068 test_domAccess();
69 test_javascript();
Derek Sollenbergerb4a23912009-11-09 15:38:58 -050070 test_loadJavaClass();
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040071}
72
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -040073BackgroundPlugin::~BackgroundPlugin() {
74 surfaceDestroyed();
75}
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -040076
Derek Sollenberger21f39912009-07-09 09:19:39 -040077bool BackgroundPlugin::supportsDrawingModel(ANPDrawingModel model) {
Derek Sollenbergerd049ec12009-08-07 11:26:44 -040078 return (model == kSurface_ANPDrawingModel);
Derek Sollenberger21f39912009-07-09 09:19:39 -040079}
80
Derek Sollenberger08581f12009-09-08 18:36:29 -040081bool BackgroundPlugin::isFixedSurface() {
82 return false;
83}
84
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -040085void BackgroundPlugin::surfaceCreated(jobject surface) {
86 m_surface = surface;
Derek Sollenberger08581f12009-09-08 18:36:29 -040087}
88
89void BackgroundPlugin::surfaceChanged(int format, int width, int height) {
90 drawPlugin(width, height);
91}
92
93void BackgroundPlugin::surfaceDestroyed() {
94 JNIEnv* env = NULL;
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -040095 if (m_surface && gVM->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK) {
Derek Sollenberger08581f12009-09-08 18:36:29 -040096 env->DeleteGlobalRef(m_surface);
97 m_surface = NULL;
98 }
99}
100
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400101void BackgroundPlugin::drawPlugin(int surfaceWidth, int surfaceHeight) {
102
103 // get the plugin's dimensions according to the DOM
104 PluginObject *obj = (PluginObject*) inst()->pdata;
105 const int W = obj->window->width;
106 const int H = obj->window->height;
107
108 // compute the current zoom level
109 const float zoomFactorW = static_cast<float>(surfaceWidth) / W;
110 const float zoomFactorH = static_cast<float>(surfaceHeight) / H;
111
112 // check to make sure the zoom level is uniform
113 if (zoomFactorW + .01 < zoomFactorH && zoomFactorW - .01 > zoomFactorH)
114 gLogI.log(inst(), kError_ANPLogType, " ------ %p zoom is out of sync (%f,%f)",
115 inst(), zoomFactorW, zoomFactorH);
116
117 // scale the variables based on the zoom level
118 const int fontSize = (int)(zoomFactorW * 16);
119 const int leftMargin = (int)(zoomFactorW * 10);
120
121 // lock the surface
122 ANPBitmap bitmap;
Derek Sollenberger08581f12009-09-08 18:36:29 -0400123 JNIEnv* env = NULL;
Derek Sollenbergerb8947ee2009-10-05 14:40:18 -0400124 if (!m_surface || gVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK ||
Derek Sollenberger08581f12009-09-08 18:36:29 -0400125 !gSurfaceI.lock(env, m_surface, &bitmap, NULL)) {
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400126 gLogI.log(inst(), kError_ANPLogType, " ------ %p unable to lock the plugin", inst());
127 return;
128 }
129
130 // create a canvas
Derek Sollenbergerc0f26572009-07-16 11:38:02 -0400131 ANPCanvas* canvas = gCanvasI.newCanvas(&bitmap);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400132 gCanvasI.drawColor(canvas, 0xFFFFFFFF);
133
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400134 ANPPaint* paint = gPaintI.newPaint();
135 gPaintI.setFlags(paint, gPaintI.getFlags(paint) | kAntiAlias_ANPPaintFlag);
136 gPaintI.setColor(paint, 0xFFFF0000);
137 gPaintI.setTextSize(paint, fontSize);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400138
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400139 ANPTypeface* tf = gTypefaceI.createFromName("serif", kItalic_ANPTypefaceStyle);
140 gPaintI.setTypeface(paint, tf);
141 gTypefaceI.unref(tf);
142
143 ANPFontMetrics fm;
144 gPaintI.getFontMetrics(paint, &fm);
145
146 gPaintI.setColor(paint, 0xFF0000FF);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400147 const char c[] = "This is a background plugin.";
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400148 gCanvasI.drawText(canvas, c, sizeof(c)-1, leftMargin, -fm.fTop, paint);
149
150 // clean up variables and unlock the surface
151 gPaintI.deletePaint(paint);
152 gCanvasI.deleteCanvas(canvas);
Derek Sollenberger08581f12009-09-08 18:36:29 -0400153 gSurfaceI.unlock(env, m_surface);
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400154}
155
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400156int16 BackgroundPlugin::handleEvent(const ANPEvent* evt) {
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400157 switch (evt->eventType) {
158 case kDraw_ANPEventType:
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400159 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request draw events", inst());
160 break;
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400161 case kLifecycle_ANPEventType:
162 if (evt->data.lifecycle.action == kOnLoad_ANPLifecycleAction) {
163 gLogI.log(inst(), kDebug_ANPLogType, " ------ %p the plugin received an onLoad event", inst());
164 return 1;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400165 }
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400166 break;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400167 case kTouch_ANPEventType:
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400168 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request touch events", inst());
169 break;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400170 case kKey_ANPEventType:
Derek Sollenbergerd049ec12009-08-07 11:26:44 -0400171 gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request key events", inst());
172 break;
Derek Sollenbergerd7ebf272009-06-18 11:19:41 -0400173 default:
174 break;
175 }
176 return 0; // unknown or unhandled event
177}
178
179///////////////////////////////////////////////////////////////////////////////
180// LOGGING TESTS
181///////////////////////////////////////////////////////////////////////////////
182
183
184void BackgroundPlugin::test_logging() {
185 NPP instance = this->inst();
186
187 //LOG_ERROR(instance, " ------ %p Testing Log Error", instance);
188 gLogI.log(instance, kError_ANPLogType, " ------ %p Testing Log Error", instance);
189 gLogI.log(instance, kWarning_ANPLogType, " ------ %p Testing Log Warning", instance);
190 gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing Log Debug", instance);
191}
192
193///////////////////////////////////////////////////////////////////////////////
194// TIMER TESTS
195///////////////////////////////////////////////////////////////////////////////
196
197#define TIMER_INTERVAL 50
198static void timer_oneshot(NPP instance, uint32 timerID);
199static void timer_repeat(NPP instance, uint32 timerID);
200static void timer_neverfires(NPP instance, uint32 timerID);
201static void timer_latency(NPP instance, uint32 timerID);
202
203void BackgroundPlugin::test_timers() {
204 NPP instance = this->inst();
205
206 //Setup the testing counters
207 mTimerRepeatCount = 5;
208 mTimerLatencyCount = 5;
209
210 // test for bogus timerID
211 browser->unscheduletimer(instance, 999999);
212 // test one-shot
213 browser->scheduletimer(instance, 100, false, timer_oneshot);
214 // test repeat
215 browser->scheduletimer(instance, 50, true, timer_repeat);
216 // test timer latency
217 browser->scheduletimer(instance, TIMER_INTERVAL, true, timer_latency);
218 mStartTime = mPrevTime = getMSecs();
219 // test unschedule immediately
220 uint32 id = browser->scheduletimer(instance, 100, false, timer_neverfires);
221 browser->unscheduletimer(instance, id);
222 // test double unschedule (should be no-op)
223 browser->unscheduletimer(instance, id);
224
225}
226
227static void timer_oneshot(NPP instance, uint32 timerID) {
228 gLogI.log(instance, kDebug_ANPLogType, "-------- oneshot timer\n");
229}
230
231static void timer_repeat(NPP instance, uint32 timerID) {
232 BackgroundPlugin *obj = ((BackgroundPlugin*) ((PluginObject*) instance->pdata)->activePlugin);
233
234 gLogI.log(instance, kDebug_ANPLogType, "-------- repeat timer %d\n",
235 obj->mTimerRepeatCount);
236 if (--obj->mTimerRepeatCount == 0) {
237 browser->unscheduletimer(instance, timerID);
238 }
239}
240
241static void timer_neverfires(NPP instance, uint32 timerID) {
242 gLogI.log(instance, kError_ANPLogType, "-------- timer_neverfires!!!\n");
243}
244
245static void timer_latency(NPP instance, uint32 timerID) {
246 BackgroundPlugin *obj = ((BackgroundPlugin*) ((PluginObject*) instance->pdata)->activePlugin);
247
248 obj->mTimerLatencyCurrentCount += 1;
249
250 uint32_t now = getMSecs();
251 uint32_t interval = now - obj->mPrevTime;
252 uint32_t dur = now - obj->mStartTime;
253 uint32_t expectedDur = obj->mTimerLatencyCurrentCount * TIMER_INTERVAL;
254 int32_t drift = dur - expectedDur;
255 int32_t avgDrift = drift / obj->mTimerLatencyCurrentCount;
256
257 obj->mPrevTime = now;
258
259 gLogI.log(instance, kDebug_ANPLogType,
260 "-------- latency test: [%3d] interval %d expected %d, total %d expected %d, drift %d avg %d\n",
261 obj->mTimerLatencyCurrentCount, interval, TIMER_INTERVAL, dur,
262 expectedDur, drift, avgDrift);
263
264 if (--obj->mTimerLatencyCount == 0) {
265 browser->unscheduletimer(instance, timerID);
266 }
267}
268
269///////////////////////////////////////////////////////////////////////////////
270// BITMAP TESTS
271///////////////////////////////////////////////////////////////////////////////
272
273static void test_formats(NPP instance);
274
275void BackgroundPlugin::test_bitmaps() {
276 test_formats(this->inst());
277}
278
279static void test_formats(NPP instance) {
280
281 // TODO pull names from enum in npapi instead of hardcoding them
282 static const struct {
283 ANPBitmapFormat fFormat;
284 const char* fName;
285 } gRecs[] = {
286 { kUnknown_ANPBitmapFormat, "unknown" },
287 { kRGBA_8888_ANPBitmapFormat, "8888" },
288 { kRGB_565_ANPBitmapFormat, "565" },
289 };
290
291 ANPPixelPacking packing;
292 for (size_t i = 0; i < ARRAY_COUNT(gRecs); i++) {
293 if (gBitmapI.getPixelPacking(gRecs[i].fFormat, &packing)) {
294 gLogI.log(instance, kDebug_ANPLogType,
295 "pixel format [%d] %s has packing ARGB [%d %d] [%d %d] [%d %d] [%d %d]\n",
296 gRecs[i].fFormat, gRecs[i].fName,
297 packing.AShift, packing.ABits,
298 packing.RShift, packing.RBits,
299 packing.GShift, packing.GBits,
300 packing.BShift, packing.BBits);
301 } else {
302 gLogI.log(instance, kDebug_ANPLogType,
303 "pixel format [%d] %s has no packing\n",
304 gRecs[i].fFormat, gRecs[i].fName);
305 }
306 }
307}
308
309void BackgroundPlugin::test_bitmap_transparency(const ANPEvent* evt) {
310 NPP instance = this->inst();
311
312 // check default & set transparent
313 if (!mFinishedStageOne) {
314
315 gLogI.log(instance, kDebug_ANPLogType, "BEGIN: testing bitmap transparency");
316
317 //check to make sure it is not transparent
318 if (evt->data.draw.data.bitmap.format == kRGBA_8888_ANPBitmapFormat) {
319 gLogI.log(instance, kError_ANPLogType, "bitmap default format is transparent");
320 }
321
322 //make it transparent (any non-null value will set it to true)
323 bool value = true;
324 NPError err = browser->setvalue(instance, NPPVpluginTransparentBool, &value);
325 if (err != NPERR_NO_ERROR) {
326 gLogI.log(instance, kError_ANPLogType, "Error setting transparency.");
327 }
328
329 mFinishedStageOne = true;
330 browser->invalidaterect(instance, NULL);
331 }
332 // check transparent & set opaque
333 else if (!mFinishedStageTwo) {
334
335 //check to make sure it is transparent
336 if (evt->data.draw.data.bitmap.format != kRGBA_8888_ANPBitmapFormat) {
337 gLogI.log(instance, kError_ANPLogType, "bitmap did not change to transparent format");
338 }
339
340 //make it opaque
341 NPError err = browser->setvalue(instance, NPPVpluginTransparentBool, NULL);
342 if (err != NPERR_NO_ERROR) {
343 gLogI.log(instance, kError_ANPLogType, "Error setting transparency.");
344 }
345
346 mFinishedStageTwo = true;
347 }
348 // check opaque
349 else if (!mFinishedStageThree) {
350
351 //check to make sure it is not transparent
352 if (evt->data.draw.data.bitmap.format == kRGBA_8888_ANPBitmapFormat) {
353 gLogI.log(instance, kError_ANPLogType, "bitmap default format is transparent");
354 }
355
356 gLogI.log(instance, kDebug_ANPLogType, "END: testing bitmap transparency");
357
358 mFinishedStageThree = true;
359 }
360}
Derek Sollenbergerc356b9f2009-07-30 16:57:07 -0400361
362///////////////////////////////////////////////////////////////////////////////
363// DOM TESTS
364///////////////////////////////////////////////////////////////////////////////
365
366void BackgroundPlugin::test_domAccess() {
367 NPP instance = this->inst();
368
369 gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing DOM Access", instance);
370
371 // Get the plugin's DOM object
372 NPObject* windowObject = NULL;
373 browser->getvalue(instance, NPNVWindowNPObject, &windowObject);
374
375 if (!windowObject)
376 gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to retrieve DOM Window", instance);
377
378 // Retrieve a property from the plugin's DOM object
379 NPIdentifier topIdentifier = browser->getstringidentifier("top");
380 NPVariant topObjectVariant;
381 browser->getproperty(instance, windowObject, topIdentifier, &topObjectVariant);
382
383 if (topObjectVariant.type != NPVariantType_Object)
384 gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Variant type for DOM Property: %d,%d", instance, topObjectVariant.type, NPVariantType_Object);
385}
386
387
388///////////////////////////////////////////////////////////////////////////////
389// JAVASCRIPT TESTS
390///////////////////////////////////////////////////////////////////////////////
391
392
393void BackgroundPlugin::test_javascript() {
394 NPP instance = this->inst();
395
396 gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing JavaScript Access", instance);
397
398 // Get the plugin's DOM object
399 NPObject* windowObject = NULL;
400 browser->getvalue(instance, NPNVWindowNPObject, &windowObject);
401
402 if (!windowObject)
403 gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to retrieve DOM Window", instance);
404
405 // create a string (JS code) that is stored in memory allocated by the browser
406 const char* jsString = "1200 + 34";
407 void* stringMem = browser->memalloc(strlen(jsString));
408 memcpy(stringMem, jsString, strlen(jsString));
409
410 // execute the javascript in the plugin's DOM object
411 NPString script = { (char*)stringMem, strlen(jsString) };
412 NPVariant scriptVariant;
413 if (!browser->evaluate(instance, windowObject, &script, &scriptVariant))
414 gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to eval the JS.", instance);
415
416 if (scriptVariant.type == NPVariantType_Int32) {
417 if (scriptVariant.value.intValue != 1234)
418 gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Value for JS Return: %d,1234", instance, scriptVariant.value.intValue);
419 } else {
420 gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Variant type for JS Return: %d,%d", instance, scriptVariant.type, NPVariantType_Int32);
421 }
422
423 // free the memory allocated within the browser
424 browser->memfree(stringMem);
425}
Derek Sollenbergerb4a23912009-11-09 15:38:58 -0500426
427///////////////////////////////////////////////////////////////////////////////
428// Load Java Classes Tests
429///////////////////////////////////////////////////////////////////////////////
430
431void BackgroundPlugin::test_loadJavaClass() {
432
433 JNIEnv* env = NULL;
434 if (gVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
435 gLogI.log(inst(), kError_ANPLogType, " ---- LoadJavaTest: failed to get env");
436 return;
437 }
438
439 const char* className = "com.android.sampleplugin.BackgroundTest";
440 jclass backgroundClass = gSystemI.loadJavaClass(inst(), className);
441
442 if(!backgroundClass) {
443 gLogI.log(inst(), kError_ANPLogType, " ---- LoadJavaTest: failed to load class");
444 return;
445 }
446
447 jmethodID constructor = env->GetMethodID(backgroundClass, "<init>", "()V");
448 jmethodID addMethod = env->GetMethodID(backgroundClass, "addInt", "(II)I");
449 jobject backgroundObject = env->NewObject(backgroundClass, constructor);
450
451 if(!backgroundObject) {
452 gLogI.log(inst(), kError_ANPLogType, " ---- LoadJavaTest: failed to construct object");
453 return;
454 }
455
456 jint result = env->CallIntMethod(backgroundObject, addMethod, 2, 2);
457
458 if (result != 4) {
459 gLogI.log(inst(), kError_ANPLogType, " ---- LoadJavaTest: invalid result (%d != 4)", result);
460 }
461}