Author: zoltan
Date: 2007-10-04 16:14:35 -0400 (Thu, 04 Oct 2007)
New Revision: 86902

Modified:
   trunk/moon/plugin/ChangeLog
   trunk/moon/plugin/plugin.cpp
   trunk/moon/plugin/plugin.h
Log:
2007-10-04  Zoltan Varga  <[EMAIL PROTECTED]>

        * plugin.cpp (LoadUrl): New method to download stuff synchronously 
using the browser.
        (plugin_instance_load_url): C wrapper for the above.
        (ReportException): Fix a leak.


Modified: trunk/moon/plugin/ChangeLog
===================================================================
--- trunk/moon/plugin/ChangeLog 2007-10-04 20:06:54 UTC (rev 86901)
+++ trunk/moon/plugin/ChangeLog 2007-10-04 20:14:35 UTC (rev 86902)
@@ -1,3 +1,9 @@
+2007-10-04  Zoltan Varga  <[EMAIL PROTECTED]>
+
+       * plugin.cpp (LoadUrl): New method to download stuff synchronously 
using the browser.
+       (plugin_instance_load_url): C wrapper for the above.
+       (ReportException): Fix a leak.
+
 2007-10-03  Chris Toshok  <[EMAIL PROTECTED]>
 
        * moonlight.h: need to wrap the renamed symbols in an extern "C"

Modified: trunk/moon/plugin/plugin.cpp
===================================================================
--- trunk/moon/plugin/plugin.cpp        2007-10-04 20:06:54 UTC (rev 86901)
+++ trunk/moon/plugin/plugin.cpp        2007-10-04 20:14:35 UTC (rev 86902)
@@ -574,8 +574,88 @@
        if (res)
                NPN_ReleaseVariantValue (&result);
        NPN_ReleaseObject (object);
+       g_free (script);
 }
 
+//
+// Download URL synchronously using the browser and return its contents as a 
byte array.
+//
+void*
+PluginInstance::LoadUrl (char *url, gint32 *length)
+{
+       NPObject *object = NULL;
+       NPVariant result;
+       char *script, *url_escaped;
+       void *load_res = NULL;
+       NPString str;
+       int i;
+       bool res;
+
+       *length = 0;
+
+       // Get a reference to our element
+       if (NPERR_NO_ERROR != NPN_GetValue(instance, NPNVPluginElementNPObject, 
&object)) {
+               DEBUGMSG ("*** Failed to get plugin element object");
+               return NULL;
+       }
+
+       //
+       // Since NPAPI doesn't contain the neccessary functionality, we use the 
JS XMLHttpRequest
+       // object of AJAX fame. Some info on downloading binary data:
+       // 
http://mgran.blogspot.com/2006/08/downloading-binary-streams-with.html
+       // This can only load stuff below our base URL.
+       // During the call, the UI will freeze, which is a problem, but this is 
the price we
+       // pay for synchronous access.
+
+       // FIXME:
+       // - make sure the variables do not become global
+       url_escaped = escape_quotes (url);
+       script = g_strdup_printf ("var req = new XMLHttpRequest(); 
req.open('GET', '%s', false); req.overrideMimeType('text/plain; 
charset=x-user-defined'); req.send (null); req.responseText;", url_escaped);
+
+       str.utf8characters = script;
+       str.utf8length = strlen (script);
+
+       res = NPN_Evaluate (instance, object, &str, &result);
+       if (res) {
+               if (NPVARIANT_IS_STRING (result)) {
+                       char *s, *in, *arr;
+                       int arr_len, len;
+
+                       len = NPVARIANT_TO_STRING (result).utf8length;
+                       s = (char*)NPVARIANT_TO_STRING (result).utf8characters;
+
+                       // Convert the utf8 string into an ASCII string
+                       // See the blog entry above for why this is needed
+                       in = s;
+                       arr_len = 0;
+                       while (in - s < len) {
+                               in = g_utf8_next_char (in);
+                               arr_len ++;
+                       }
+
+                       arr = (char*)g_malloc (arr_len);
+
+                       in = s;
+                       i = 0;
+                       while (in - s < len) {
+                               arr [i] = g_utf8_get_char (in);
+      
+                               in = g_utf8_next_char (in);
+                               i ++;
+                       }
+
+                       load_res = arr;
+                       *length = arr_len;
+               }
+               NPN_ReleaseVariantValue (&result);
+       }
+       NPN_ReleaseObject (object);
+       g_free (script);
+       g_free (url_escaped);
+
+       return load_res;
+}
+
 void
 PluginInstance::StreamAsFile (NPStream* stream, const char* fname)
 {
@@ -854,6 +934,12 @@
        instance->ReportException (msg, details, stack_trace, num_frames);
 }
 
+void*
+plugin_instance_load_url (PluginInstance *instance, char *url, gint32 *length)
+{
+       return instance->LoadUrl (url, length);
+}
+
 /*
        XamlLoader
 */
@@ -909,6 +995,8 @@
                return NULL;
        }
        
+       printf ("PluginXamlLoader::TryLoad () succeeded.\n");
+
        surface_attach (GetSurface (), (Canvas*) element);
        
        element->unref ();

Modified: trunk/moon/plugin/plugin.h
===================================================================
--- trunk/moon/plugin/plugin.h  2007-10-04 20:06:54 UTC (rev 86901)
+++ trunk/moon/plugin/plugin.h  2007-10-04 20:14:35 UTC (rev 86902)
@@ -80,6 +80,7 @@
        int16 EventHandle (void* event);
        bool JsRunOnload ();
        void ReportException (char *msg, char *details, char **stack_trace, int 
num_frames);
+       void *LoadUrl (char *url, gint32 *length);
 
        NPP getNPP () { return instance; }
 
@@ -199,6 +200,7 @@
                                              bool *cookieEnabled);
 
 void plugin_instance_report_exception (PluginInstance *instance, char *msg, 
char *details, char **stack_trace, int num_frames);
+void *plugin_instance_load_url (PluginInstance *instance, char *url, gint32 
*length);
 
 void     plugin_html_timer_timeout_stop (PluginInstance *instance, uint32_t 
source_id);
 uint32_t plugin_html_timer_timeout_add (PluginInstance *instance, int32_t 
interval, GSourceFunc callback, gpointer data);

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to