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 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) {

View File

@ -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);
};

View File

@ -64,6 +64,7 @@
#include <nsIClipboardDragDropHooks.h>
#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,6 +369,8 @@ 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);
@ -373,6 +378,14 @@ sugar_browser_realize(GtkWidget *widget)
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->RegisterBrowser(browser->instance_id, webBrowser);
} else {
g_warning ("Failed to get nsIBrowserHelper");
}
nsCOMPtr<nsICommandManager> commandManager = do_GetInterface(webBrowser);
if (commandManager) {
nsresult rv;
@ -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
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,

View File

@ -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())

View File

@ -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)

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
(define-function sugar_key_grabber_get_type

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

@ -0,0 +1 @@

View File

@ -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()