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