From c356b9f04076f3f95dae80426ce455efe1e2c543 Mon Sep 17 00:00:00 2001 From: Derek Sollenberger Date: Thu, 30 Jul 2009 16:57:07 -0400 Subject: [PATCH] adding sample cod to access dom and javascript. --- .../jni/background/BackgroundPlugin.cpp | 67 +++++++++++++++++++ .../jni/background/BackgroundPlugin.h | 2 + 2 files changed, 69 insertions(+) diff --git a/samples/BrowserPlugin/jni/background/BackgroundPlugin.cpp b/samples/BrowserPlugin/jni/background/BackgroundPlugin.cpp index 09f58cf75..4ca79ed0e 100644 --- a/samples/BrowserPlugin/jni/background/BackgroundPlugin.cpp +++ b/samples/BrowserPlugin/jni/background/BackgroundPlugin.cpp @@ -68,6 +68,8 @@ BackgroundPlugin::BackgroundPlugin(NPP inst) : SubPlugin(inst) { test_logging(); // android logging test_timers(); // plugin timers test_bitmaps(); // android bitmaps + test_domAccess(); + test_javascript(); } BackgroundPlugin::~BackgroundPlugin() { @@ -308,3 +310,68 @@ void BackgroundPlugin::test_bitmap_transparency(const ANPEvent* evt) { mFinishedStageThree = true; } } + +/////////////////////////////////////////////////////////////////////////////// +// DOM TESTS +/////////////////////////////////////////////////////////////////////////////// + +void BackgroundPlugin::test_domAccess() { + NPP instance = this->inst(); + + gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing DOM Access", instance); + + // Get the plugin's DOM object + NPObject* windowObject = NULL; + browser->getvalue(instance, NPNVWindowNPObject, &windowObject); + + if (!windowObject) + gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to retrieve DOM Window", instance); + + // Retrieve a property from the plugin's DOM object + NPIdentifier topIdentifier = browser->getstringidentifier("top"); + NPVariant topObjectVariant; + browser->getproperty(instance, windowObject, topIdentifier, &topObjectVariant); + + if (topObjectVariant.type != NPVariantType_Object) + gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Variant type for DOM Property: %d,%d", instance, topObjectVariant.type, NPVariantType_Object); +} + + +/////////////////////////////////////////////////////////////////////////////// +// JAVASCRIPT TESTS +/////////////////////////////////////////////////////////////////////////////// + + +void BackgroundPlugin::test_javascript() { + NPP instance = this->inst(); + + gLogI.log(instance, kDebug_ANPLogType, " ------ %p Testing JavaScript Access", instance); + + // Get the plugin's DOM object + NPObject* windowObject = NULL; + browser->getvalue(instance, NPNVWindowNPObject, &windowObject); + + if (!windowObject) + gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to retrieve DOM Window", instance); + + // create a string (JS code) that is stored in memory allocated by the browser + const char* jsString = "1200 + 34"; + void* stringMem = browser->memalloc(strlen(jsString)); + memcpy(stringMem, jsString, strlen(jsString)); + + // execute the javascript in the plugin's DOM object + NPString script = { (char*)stringMem, strlen(jsString) }; + NPVariant scriptVariant; + if (!browser->evaluate(instance, windowObject, &script, &scriptVariant)) + gLogI.log(instance, kError_ANPLogType, " ------ %p Unable to eval the JS.", instance); + + if (scriptVariant.type == NPVariantType_Int32) { + if (scriptVariant.value.intValue != 1234) + gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Value for JS Return: %d,1234", instance, scriptVariant.value.intValue); + } else { + gLogI.log(instance, kError_ANPLogType, " ------ %p Invalid Variant type for JS Return: %d,%d", instance, scriptVariant.type, NPVariantType_Int32); + } + + // free the memory allocated within the browser + browser->memfree(stringMem); +} diff --git a/samples/BrowserPlugin/jni/background/BackgroundPlugin.h b/samples/BrowserPlugin/jni/background/BackgroundPlugin.h index 8893c9dc8..0d7389bc2 100644 --- a/samples/BrowserPlugin/jni/background/BackgroundPlugin.h +++ b/samples/BrowserPlugin/jni/background/BackgroundPlugin.h @@ -57,6 +57,8 @@ private: void test_timers(); void test_bitmaps(); void test_bitmap_transparency(const ANPEvent* evt); + void test_domAccess(); + void test_javascript(); };