diff --git a/browser/components/browserhelper/nsBrowserHelper.js b/browser/components/browserhelper/nsBrowserHelper.js index ef359be6..f726ddff 100644 --- a/browser/components/browserhelper/nsBrowserHelper.js +++ b/browser/components/browserhelper/nsBrowserHelper.js @@ -6,7 +6,7 @@ const CID = Components.ID("{475e1194-92bc-4e03-92f3-5ad6ccddaca3}"); const CONTRACT_ID = "@laptop.org/browser/browserhelper;1"; const CLASS_NAME = "Browser Helper"; -var browser; +var browsers = []; function BrowserHelperService() { } @@ -16,14 +16,15 @@ BrowserHelperService.prototype = { /* ........ nsIBrowserHelper API .............. */ getBrowser: function bh_getBrowser(aId) { - return browser; + return browsers[aId] }, - registerBrowser: function bh_registerBrowser(aBrowser, aId) { - browser = aBrowser; + registerBrowser: function bh_registerBrowser(aId, aBrowser) { + browsers[aId] = aBrowser; }, unregisterBrowser: function bh_unregisterBrowser(aId) { + browsers.pop(aId) }, QueryInterface: function(aIID) { diff --git a/browser/components/browserhelper/nsIBrowserHelper.idl b/browser/components/browserhelper/nsIBrowserHelper.idl index 08821fdc..abe52b3c 100644 --- a/browser/components/browserhelper/nsIBrowserHelper.idl +++ b/browser/components/browserhelper/nsIBrowserHelper.idl @@ -5,9 +5,9 @@ interface nsIWebBrowser; [scriptable, uuid(475e1194-92bc-4e03-92f3-5ad6ccddaca3)] interface nsIBrowserHelper : nsISupports { - nsIWebBrowser getBrowser(in ACString id); + nsIWebBrowser getBrowser(in long id); - void registerBrowser(in ACString id, in nsIWebBrowser browser); + void registerBrowser(in long id, in nsIWebBrowser browser); - void unregisterBrowser(in ACString id); + void unregisterBrowser(in long id); }; diff --git a/browser/sugar-browser.cpp b/browser/sugar-browser.cpp index 42a04d1d..d64224f1 100644 --- a/browser/sugar-browser.cpp +++ b/browser/sugar-browser.cpp @@ -64,6 +64,7 @@ #include #include "nsISessionStore.h" +#include "nsIBrowserHelper.h" #define SUGAR_PATH "SUGAR_PATH" @@ -83,6 +84,8 @@ enum { N_SIGNALS }; +static int last_instance_id = 0; + static guint signals[N_SIGNALS]; static GObjectClass *parent_class = NULL; @@ -366,12 +369,22 @@ sugar_browser_get_property(GObject *object, static void sugar_browser_realize(GtkWidget *widget) { + SugarBrowser *browser = SUGAR_BROWSER(widget); + GTK_WIDGET_CLASS(parent_class)->realize(widget); GtkMozEmbed *embed = GTK_MOZ_EMBED(widget); nsCOMPtr webBrowser; gtk_moz_embed_get_nsIWebBrowser(embed, getter_AddRefs(webBrowser)); NS_ENSURE_TRUE(webBrowser, ); + + nsCOMPtr browserHelper; + browserHelper = do_GetService("@laptop.org/browser/browserhelper;1"); + if (browserHelper) { + browserHelper->RegisterBrowser(browser->instance_id, webBrowser); + } else { + g_warning ("Failed to get nsIBrowserHelper"); + } nsCOMPtr commandManager = do_GetInterface(webBrowser); if (commandManager) { @@ -392,6 +405,25 @@ sugar_browser_realize(GtkWidget *widget) } } +static void +sugar_browser_dispose(GObject *object) +{ + SugarBrowser *browser = SUGAR_BROWSER(object); + + GtkMozEmbed *embed = GTK_MOZ_EMBED(object); + nsCOMPtr webBrowser; + gtk_moz_embed_get_nsIWebBrowser(embed, getter_AddRefs(webBrowser)); + NS_ENSURE_TRUE(webBrowser, ); + + nsCOMPtr browserHelper; + browserHelper = do_GetService("@laptop.org/browser/browserhelper;1"); + if (browserHelper) { + browserHelper->UnregisterBrowser(browser->instance_id); + } else { + g_warning ("Failed to get nsIBrowserHelper"); + } +} + static void sugar_browser_class_init(SugarBrowserClass *browser_class) { @@ -401,6 +433,7 @@ sugar_browser_class_init(SugarBrowserClass *browser_class) parent_class = (GObjectClass *) g_type_class_peek_parent(browser_class); gobject_class->get_property = sugar_browser_get_property; + gobject_class->dispose = sugar_browser_dispose; widget_class->realize = sugar_browser_realize; signals[MOUSE_CLICK] = g_signal_new ("mouse_click", @@ -620,6 +653,9 @@ dom_mouse_click_cb(GtkMozEmbed *embed, nsIDOMMouseEvent *mouseEvent) static void sugar_browser_init(SugarBrowser *browser) { + browser->instance_id = last_instance_id; + last_instance_id++; + browser->title = NULL; browser->address = NULL; browser->progress = 0.0; @@ -636,6 +672,12 @@ sugar_browser_init(SugarBrowser *browser) G_CALLBACK(dom_mouse_click_cb), NULL); } +int +sugar_browser_get_instance_id(SugarBrowser *browser) +{ + return browser->instance_id; +} + void sugar_browser_scroll_pixels(SugarBrowser *browser, int dx, diff --git a/browser/sugar-browser.h b/browser/sugar-browser.h index ab74a126..779bba5a 100644 --- a/browser/sugar-browser.h +++ b/browser/sugar-browser.h @@ -39,6 +39,7 @@ typedef struct _SugarBrowserMetadata SugarBrowserMetadata; struct _SugarBrowser { GtkMozEmbed base_instance; + int instance_id; int total_requests; int current_requests; float progress; @@ -58,25 +59,25 @@ struct _SugarBrowserClass { SugarBrowser * (* create_window) (SugarBrowser *browser); }; -GType sugar_browser_get_type (void); -SugarBrowser *sugar_browser_create_window (SugarBrowser *browser); -void sugar_browser_scroll_pixels (SugarBrowser *browser, - int dx, - int dy); -void sugar_browser_grab_focus (SugarBrowser *browser); -gboolean sugar_browser_save_uri (SugarBrowser *browser, - const char *uri, - const char *filename); -gboolean sugar_browser_save_document (SugarBrowser *browser, - const char *filename); +GType sugar_browser_get_type (void); +int sugar_browser_get_instance_id (SugarBrowser *browser); +SugarBrowser *sugar_browser_create_window (SugarBrowser *browser); +void sugar_browser_scroll_pixels (SugarBrowser *browser, + int dx, + int dy); +void sugar_browser_grab_focus (SugarBrowser *browser); +gboolean sugar_browser_save_uri (SugarBrowser *browser, + const char *uri, + const char *filename); +gboolean sugar_browser_save_document (SugarBrowser *browser, + const char *filename); +char *sugar_browser_get_session (SugarBrowser *browser); +gboolean sugar_browser_set_session (SugarBrowser *browser, + const char *session); -gboolean sugar_browser_startup (const char *profile_path, - const char *profile_name); -void sugar_browser_shutdown (void); - -char *sugar_browser_get_session (SugarBrowser *browser); -gboolean sugar_browser_set_session (SugarBrowser *browser, - const char *session); +gboolean sugar_browser_startup (const char *profile_path, + const char *profile_name); +void sugar_browser_shutdown (void); #define SUGAR_TYPE_BROWSER_EVENT (sugar_browser_event_get_type()) diff --git a/sugar/browser/__init__.py b/sugar/browser/__init__.py index 7de5339d..d02c90c7 100644 --- a/sugar/browser/__init__.py +++ b/sugar/browser/__init__.py @@ -5,7 +5,26 @@ XUL Runner and gtkmozembed and is produced by the PyGTK """ try: - from sugar.browser._sugarbrowser import * + from sugar.browser._sugarbrowser import startup, shutdown + from sugar.browser import _sugarbrowser except ImportError: from sugar import ltihooks - from sugar.browser._sugarbrowser import * + from sugar.browser._sugarbrowser import startup, shutdown + from sugar.browser import _sugarbrowser + +class Browser(_sugarbrowser.Browser): + def __init__(self): + _sugarbrowser.Browser.__init__(self) + + def get_browser(self): + from xpcom import components + cls = components.classes["@laptop.org/browser/browserhelper;1"] + browser_helper = cls.getService(components.interfaces.nsIBrowserHelper) + print self.get_instance_id() + return browser_helper.getBrowser(self.get_instance_id()) + + def get_document(self): + return self.browser.contentDOMWindow.document + + document = property(get_document) + browser = property(get_browser) diff --git a/sugar/browser/_sugarbrowser.defs b/sugar/browser/_sugarbrowser.defs index f323d5ec..036a34c8 100644 --- a/sugar/browser/_sugarbrowser.defs +++ b/sugar/browser/_sugarbrowser.defs @@ -128,6 +128,12 @@ ) ) +(define-method get_instance_id + (of-object "SugarBrowser") + (c-name "sugar_browser_get_instance_id") + (return-type "int") +) + ;; From sugar-key-grabber.h (define-function sugar_key_grabber_get_type diff --git a/sugar/browser/browser.py b/sugar/browser/browser.py new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/sugar/browser/browser.py @@ -0,0 +1 @@ + diff --git a/sugar/browser/xulrunner.cpp b/sugar/browser/xulrunner.cpp index f7dc0fd1..a03604dc 100644 --- a/sugar/browser/xulrunner.cpp +++ b/sugar/browser/xulrunner.cpp @@ -39,7 +39,7 @@ xulrunner_startup(void) fprintf(stderr, "Couldn't find a compatible GRE.\n"); return 1; } - + rv = XPCOMGlueStartup(xpcomPath); if (NS_FAILED(rv)) { fprintf(stderr, "Couldn't start XPCOM."); diff --git a/tests/test-browser.py b/tests/test-browser.py index 681aefab..ce9a3cf3 100755 --- a/tests/test-browser.py +++ b/tests/test-browser.py @@ -21,9 +21,14 @@ import os import pygtk pygtk.require('2.0') import gtk +import gobject import sugar.browser +def _print_document(): + #print browser.document + pass + def _quit(window): sugar.browser.shutdown() gtk.main_quit() @@ -38,7 +43,9 @@ browser = sugar.browser.Browser() window.add(browser) browser.show() -browser.load_url('http://www.google.com') +browser.load_url('about:blank') + +gobject.idle_add(_print_document) window.show()