Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 35 | extern NPNetscapeFuncs* browser; |
| 36 | extern ANPBitmapInterfaceV0 gBitmapI; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 37 | extern ANPCanvasInterfaceV0 gCanvasI; |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 38 | extern ANPLogInterfaceV0 gLogI; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 39 | extern ANPPaintInterfaceV0 gPaintI; |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 40 | extern ANPSurfaceInterfaceV0 gSurfaceI; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 41 | extern ANPTypefaceInterfaceV0 gTypefaceI; |
| 42 | |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 43 | #define ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) |
| 44 | |
Derek Sollenberger | 0c763b6 | 2009-08-11 10:05:46 -0400 | [diff] [blame] | 45 | static 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 Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 50 | |
| 51 | /////////////////////////////////////////////////////////////////////////////// |
| 52 | |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 53 | BackgroundPlugin::BackgroundPlugin(NPP inst) : SurfaceSubPlugin(inst) { |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 54 | |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 55 | // initialize the drawing surface |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 56 | m_surface = NULL; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 57 | |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 58 | //initialize bitmap transparency variables |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 59 | 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 Sollenberger | c356b9f | 2009-07-30 16:57:07 -0400 | [diff] [blame] | 67 | test_domAccess(); |
| 68 | test_javascript(); |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame^] | 71 | BackgroundPlugin::~BackgroundPlugin() { |
| 72 | surfaceDestroyed(); |
| 73 | } |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 74 | |
Derek Sollenberger | 21f3991 | 2009-07-09 09:19:39 -0400 | [diff] [blame] | 75 | bool BackgroundPlugin::supportsDrawingModel(ANPDrawingModel model) { |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 76 | return (model == kSurface_ANPDrawingModel); |
Derek Sollenberger | 21f3991 | 2009-07-09 09:19:39 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 79 | bool BackgroundPlugin::isFixedSurface() { |
| 80 | return false; |
| 81 | } |
| 82 | |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame^] | 83 | void BackgroundPlugin::surfaceCreated(jobject surface) { |
| 84 | m_surface = surface; |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void BackgroundPlugin::surfaceChanged(int format, int width, int height) { |
| 88 | drawPlugin(width, height); |
| 89 | } |
| 90 | |
| 91 | void BackgroundPlugin::surfaceDestroyed() { |
| 92 | JNIEnv* env = NULL; |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame^] | 93 | if (m_surface && gVM->GetEnv((void**) &env, JNI_VERSION_1_4) == JNI_OK) { |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 94 | env->DeleteGlobalRef(m_surface); |
| 95 | m_surface = NULL; |
| 96 | } |
| 97 | } |
| 98 | |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 99 | void 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 Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 121 | JNIEnv* env = NULL; |
Derek Sollenberger | b8947ee | 2009-10-05 14:40:18 -0400 | [diff] [blame^] | 122 | if (!m_surface || gVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK || |
Derek Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 123 | !gSurfaceI.lock(env, m_surface, &bitmap, NULL)) { |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 124 | gLogI.log(inst(), kError_ANPLogType, " ------ %p unable to lock the plugin", inst()); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | // create a canvas |
Derek Sollenberger | c0f2657 | 2009-07-16 11:38:02 -0400 | [diff] [blame] | 129 | ANPCanvas* canvas = gCanvasI.newCanvas(&bitmap); |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 130 | gCanvasI.drawColor(canvas, 0xFFFFFFFF); |
| 131 | |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 132 | ANPPaint* paint = gPaintI.newPaint(); |
| 133 | gPaintI.setFlags(paint, gPaintI.getFlags(paint) | kAntiAlias_ANPPaintFlag); |
| 134 | gPaintI.setColor(paint, 0xFFFF0000); |
| 135 | gPaintI.setTextSize(paint, fontSize); |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 136 | |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 137 | 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 Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 145 | const char c[] = "This is a background plugin."; |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 146 | 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 Sollenberger | 08581f1 | 2009-09-08 18:36:29 -0400 | [diff] [blame] | 151 | gSurfaceI.unlock(env, m_surface); |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 154 | int16 BackgroundPlugin::handleEvent(const ANPEvent* evt) { |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 155 | switch (evt->eventType) { |
| 156 | case kDraw_ANPEventType: |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 157 | gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request draw events", inst()); |
| 158 | break; |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 159 | 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 Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 163 | } |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 164 | break; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 165 | case kTouch_ANPEventType: |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 166 | gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request touch events", inst()); |
| 167 | break; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 168 | case kKey_ANPEventType: |
Derek Sollenberger | d049ec1 | 2009-08-07 11:26:44 -0400 | [diff] [blame] | 169 | gLogI.log(inst(), kError_ANPLogType, " ------ %p the plugin did not request key events", inst()); |
| 170 | break; |
Derek Sollenberger | d7ebf27 | 2009-06-18 11:19:41 -0400 | [diff] [blame] | 171 | default: |
| 172 | break; |
| 173 | } |
| 174 | return 0; // unknown or unhandled event |
| 175 | } |
| 176 | |
| 177 | /////////////////////////////////////////////////////////////////////////////// |
| 178 | // LOGGING TESTS |
| 179 | /////////////////////////////////////////////////////////////////////////////// |
| 180 | |
| 181 | |
| 182 | void 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 |
| 196 | static void timer_oneshot(NPP instance, uint32 timerID); |
| 197 | static void timer_repeat(NPP instance, uint32 timerID); |
| 198 | static void timer_neverfires(NPP instance, uint32 timerID); |
| 199 | static void timer_latency(NPP instance, uint32 timerID); |
| 200 | |
| 201 | void 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 | |
| 225 | static void timer_oneshot(NPP instance, uint32 timerID) { |
| 226 | gLogI.log(instance, kDebug_ANPLogType, "-------- oneshot timer\n"); |
| 227 | } |
| 228 | |
| 229 | static 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 | |
| 239 | static void timer_neverfires(NPP instance, uint32 timerID) { |
| 240 | gLogI.log(instance, kError_ANPLogType, "-------- timer_neverfires!!!\n"); |
| 241 | } |
| 242 | |
| 243 | static 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 | |
| 271 | static void test_formats(NPP instance); |
| 272 | |
| 273 | void BackgroundPlugin::test_bitmaps() { |
| 274 | test_formats(this->inst()); |
| 275 | } |
| 276 | |
| 277 | static 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 | |
| 307 | void 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 Sollenberger | c356b9f | 2009-07-30 16:57:07 -0400 | [diff] [blame] | 359 | |
| 360 | /////////////////////////////////////////////////////////////////////////////// |
| 361 | // DOM TESTS |
| 362 | /////////////////////////////////////////////////////////////////////////////// |
| 363 | |
| 364 | void 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 | |
| 391 | void 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 | } |