More work on pyxpcom integration

This commit is contained in:
Marco Pesenti Gritti 2007-05-13 22:49:27 +02:00
parent 7e0fd77b13
commit 9e10d15c21
9 changed files with 106 additions and 29 deletions

View File

@ -6,7 +6,7 @@ const CID = Components.ID("{475e1194-92bc-4e03-92f3-5ad6ccddaca3}");
const CONTRACT_ID = "@laptop.org/browser/browserhelper;1"; const CONTRACT_ID = "@laptop.org/browser/browserhelper;1";
const CLASS_NAME = "Browser Helper"; const CLASS_NAME = "Browser Helper";
var browser; var browsers = [];
function BrowserHelperService() { function BrowserHelperService() {
} }
@ -16,14 +16,15 @@ BrowserHelperService.prototype = {
/* ........ nsIBrowserHelper API .............. */ /* ........ nsIBrowserHelper API .............. */
getBrowser: function bh_getBrowser(aId) { getBrowser: function bh_getBrowser(aId) {
return browser; return browsers[aId]
}, },
registerBrowser: function bh_registerBrowser(aBrowser, aId) { registerBrowser: function bh_registerBrowser(aId, aBrowser) {
browser = aBrowser; browsers[aId] = aBrowser;
}, },
unregisterBrowser: function bh_unregisterBrowser(aId) { unregisterBrowser: function bh_unregisterBrowser(aId) {
browsers.pop(aId)
}, },
QueryInterface: function(aIID) { QueryInterface: function(aIID) {

View File

@ -5,9 +5,9 @@ interface nsIWebBrowser;
[scriptable, uuid(475e1194-92bc-4e03-92f3-5ad6ccddaca3)] [scriptable, uuid(475e1194-92bc-4e03-92f3-5ad6ccddaca3)]
interface nsIBrowserHelper : nsISupports 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);
}; };

View File

@ -64,6 +64,7 @@
#include <nsIClipboardDragDropHooks.h> #include <nsIClipboardDragDropHooks.h>
#include "nsISessionStore.h" #include "nsISessionStore.h"
#include "nsIBrowserHelper.h"
#define SUGAR_PATH "SUGAR_PATH" #define SUGAR_PATH "SUGAR_PATH"
@ -83,6 +84,8 @@ enum {
N_SIGNALS N_SIGNALS
}; };
static int last_instance_id = 0;
static guint signals[N_SIGNALS]; static guint signals[N_SIGNALS];
static GObjectClass *parent_class = NULL; static GObjectClass *parent_class = NULL;
@ -366,12 +369,22 @@ sugar_browser_get_property(GObject *object,
static void static void
sugar_browser_realize(GtkWidget *widget) sugar_browser_realize(GtkWidget *widget)
{ {
SugarBrowser *browser = SUGAR_BROWSER(widget);
GTK_WIDGET_CLASS(parent_class)->realize(widget); GTK_WIDGET_CLASS(parent_class)->realize(widget);
GtkMozEmbed *embed = GTK_MOZ_EMBED(widget); GtkMozEmbed *embed = GTK_MOZ_EMBED(widget);
nsCOMPtr<nsIWebBrowser> webBrowser; nsCOMPtr<nsIWebBrowser> webBrowser;
gtk_moz_embed_get_nsIWebBrowser(embed, getter_AddRefs(webBrowser)); gtk_moz_embed_get_nsIWebBrowser(embed, getter_AddRefs(webBrowser));
NS_ENSURE_TRUE(webBrowser, ); NS_ENSURE_TRUE(webBrowser, );
nsCOMPtr<nsIBrowserHelper> 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<nsICommandManager> commandManager = do_GetInterface(webBrowser); nsCOMPtr<nsICommandManager> commandManager = do_GetInterface(webBrowser);
if (commandManager) { 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<nsIWebBrowser> webBrowser;
gtk_moz_embed_get_nsIWebBrowser(embed, getter_AddRefs(webBrowser));
NS_ENSURE_TRUE(webBrowser, );
nsCOMPtr<nsIBrowserHelper> 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 static void
sugar_browser_class_init(SugarBrowserClass *browser_class) 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); parent_class = (GObjectClass *) g_type_class_peek_parent(browser_class);
gobject_class->get_property = sugar_browser_get_property; gobject_class->get_property = sugar_browser_get_property;
gobject_class->dispose = sugar_browser_dispose;
widget_class->realize = sugar_browser_realize; widget_class->realize = sugar_browser_realize;
signals[MOUSE_CLICK] = g_signal_new ("mouse_click", signals[MOUSE_CLICK] = g_signal_new ("mouse_click",
@ -620,6 +653,9 @@ dom_mouse_click_cb(GtkMozEmbed *embed, nsIDOMMouseEvent *mouseEvent)
static void static void
sugar_browser_init(SugarBrowser *browser) sugar_browser_init(SugarBrowser *browser)
{ {
browser->instance_id = last_instance_id;
last_instance_id++;
browser->title = NULL; browser->title = NULL;
browser->address = NULL; browser->address = NULL;
browser->progress = 0.0; browser->progress = 0.0;
@ -636,6 +672,12 @@ sugar_browser_init(SugarBrowser *browser)
G_CALLBACK(dom_mouse_click_cb), NULL); G_CALLBACK(dom_mouse_click_cb), NULL);
} }
int
sugar_browser_get_instance_id(SugarBrowser *browser)
{
return browser->instance_id;
}
void void
sugar_browser_scroll_pixels(SugarBrowser *browser, sugar_browser_scroll_pixels(SugarBrowser *browser,
int dx, int dx,

View File

@ -39,6 +39,7 @@ typedef struct _SugarBrowserMetadata SugarBrowserMetadata;
struct _SugarBrowser { struct _SugarBrowser {
GtkMozEmbed base_instance; GtkMozEmbed base_instance;
int instance_id;
int total_requests; int total_requests;
int current_requests; int current_requests;
float progress; float progress;
@ -58,25 +59,25 @@ struct _SugarBrowserClass {
SugarBrowser * (* create_window) (SugarBrowser *browser); SugarBrowser * (* create_window) (SugarBrowser *browser);
}; };
GType sugar_browser_get_type (void); GType sugar_browser_get_type (void);
SugarBrowser *sugar_browser_create_window (SugarBrowser *browser); int sugar_browser_get_instance_id (SugarBrowser *browser);
void sugar_browser_scroll_pixels (SugarBrowser *browser, SugarBrowser *sugar_browser_create_window (SugarBrowser *browser);
int dx, void sugar_browser_scroll_pixels (SugarBrowser *browser,
int dy); int dx,
void sugar_browser_grab_focus (SugarBrowser *browser); int dy);
gboolean sugar_browser_save_uri (SugarBrowser *browser, void sugar_browser_grab_focus (SugarBrowser *browser);
const char *uri, gboolean sugar_browser_save_uri (SugarBrowser *browser,
const char *filename); const char *uri,
gboolean sugar_browser_save_document (SugarBrowser *browser, const char *filename);
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, gboolean sugar_browser_startup (const char *profile_path,
const char *profile_name); const char *profile_name);
void sugar_browser_shutdown (void); void sugar_browser_shutdown (void);
char *sugar_browser_get_session (SugarBrowser *browser);
gboolean sugar_browser_set_session (SugarBrowser *browser,
const char *session);
#define SUGAR_TYPE_BROWSER_EVENT (sugar_browser_event_get_type()) #define SUGAR_TYPE_BROWSER_EVENT (sugar_browser_event_get_type())

View File

@ -5,7 +5,26 @@ XUL Runner and gtkmozembed and is produced by the PyGTK
""" """
try: try:
from sugar.browser._sugarbrowser import * from sugar.browser._sugarbrowser import startup, shutdown
from sugar.browser import _sugarbrowser
except ImportError: except ImportError:
from sugar import ltihooks 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)

View File

@ -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 ;; From sugar-key-grabber.h
(define-function sugar_key_grabber_get_type (define-function sugar_key_grabber_get_type

1
sugar/browser/browser.py Normal file
View File

@ -0,0 +1 @@

View File

@ -39,7 +39,7 @@ xulrunner_startup(void)
fprintf(stderr, "Couldn't find a compatible GRE.\n"); fprintf(stderr, "Couldn't find a compatible GRE.\n");
return 1; return 1;
} }
rv = XPCOMGlueStartup(xpcomPath); rv = XPCOMGlueStartup(xpcomPath);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
fprintf(stderr, "Couldn't start XPCOM."); fprintf(stderr, "Couldn't start XPCOM.");

View File

@ -21,9 +21,14 @@ import os
import pygtk import pygtk
pygtk.require('2.0') pygtk.require('2.0')
import gtk import gtk
import gobject
import sugar.browser import sugar.browser
def _print_document():
#print browser.document
pass
def _quit(window): def _quit(window):
sugar.browser.shutdown() sugar.browser.shutdown()
gtk.main_quit() gtk.main_quit()
@ -38,7 +43,9 @@ browser = sugar.browser.Browser()
window.add(browser) window.add(browser)
browser.show() browser.show()
browser.load_url('http://www.google.com') browser.load_url('about:blank')
gobject.idle_add(_print_document)
window.show() window.show()