1
# Miro - an RSS based video player application
2
# Copyright (C) 2005-2010 Participatory Culture Foundation
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
# In addition, as a special exception, the copyright holders give
19
# permission to link the code of portions of this program with the OpenSSL
22
# You must obey the GNU General Public License in all respects for all of
23
# the code used other than OpenSSL. If you modify file(s) with this
24
# exception, you may extend this exception to your version of the file(s),
25
# but you are not obligated to do so. If you do not wish to do so, delete
26
# this exception statement from your version. If you delete this exception
27
# statement from all source files in the program, then also delete it here.
29
"""xulrunnerbrowser -- Pyrex module to implement an embedded XULRunner
35
ctypedef unsigned short wchar_t
38
cdef extern from "Python.h":
39
ctypedef struct PyObject
40
PyObject* PyObject_CallMethod(PyObject *o, char* name, char* format, ...)
41
PyObject* PyObject_CallFunction(PyObject *o, char* format, ...)
42
int PyObject_IsTrue(PyObject *o)
43
void Py_DECREF(PyObject*)
44
void Py_INCREF(PyObject*)
45
object PyString_FromString(char *)
46
object PyString_FromStringAndSize(char *, int)
48
cdef extern from "nscore.h":
49
ctypedef unsigned int nsresult
50
ctypedef unsigned int PRBool
51
cdef enum NS_RESULT_VALUES:
54
cdef extern from "MiroBrowserEmbed.h":
55
ctypedef void(*focusCallback)(PRBool forward, void* data)
56
ctypedef int(*uriCallback)(char* data, void* data)
57
ctypedef void(*networkCallback)(PRBool is_start, void* data)
58
ctypedef struct MiroBrowserEmbed:
59
nsresult (*init)(unsigned long parentWindow, int x, int y, int width,
64
nsresult (*loadURI)(char* uri)
65
nsresult (*getCurrentURI)(char ** uri)
66
nsresult (*getCurrentTitle)(wchar_t ** title)
67
nsresult (*resize)(int x, int y, int width, int height)
75
void (*SetFocusCallback)(focusCallback callback, void* data)
76
void (*SetURICallback)(uriCallback callback, void* data)
77
void (*SetNetworkCallback)(networkCallback callback, void* data)
78
# Trick Pyrex into creating constructor and destructor code
79
MiroBrowserEmbed *new_MiroBrowserEmbed "new MiroBrowserEmbed" ()
80
void del_MiroBrowserEmbed "delete" (MiroBrowserEmbed *rect)
81
void addref(MiroBrowserEmbed* browser)
82
void release(MiroBrowserEmbed* browser)
84
cdef extern from "MiroWindowCreator.h":
85
ctypedef void(*newWindowCallback)(char* data, void* data)
87
ctypedef struct MiroWindowCreator:
89
void (*SetNewWindowCallback)(newWindowCallback, void*)
91
# Trick Pyrex into creating constructor and destructor code
92
MiroWindowCreator *new_MiroWindowCreator "new MiroWindowCreator" ()
93
void del_MiroWindowCreator "delete" (MiroWindowCreator *rect)
95
cdef extern from "HttpObserver.h":
96
nsresult startObserving()
98
cdef extern from "Init.h":
99
nsresult init_xulrunner(char* xul_dir, char* app_dir)
100
nsresult c_setup_user_agent "setup_user_agent" (char* vendor, char* vendor_sub, char* comment)
101
void shutdown_xulrunner()
103
cdef extern from "pythread.h":
104
ctypedef struct PyThreadState
105
PyThreadState *PyEval_SaveThread()
106
void PyEval_RestoreThread(PyThreadState *_save)
108
cdef extern from "string.h":
109
size_t wcslen(wchar_t *str)
111
cdef public log_warning(char* str) with gil:
114
cdef public log_info(char* str) with gil:
117
def initialize(xul_dir, app_dir):
119
rv = init_xulrunner(xul_dir, app_dir)
121
raise XPCOMError("init_xulrunner failed with code: %d" % rv)
122
rv = startObserving()
124
raise XPCOMError("startObserving failed with code: %d" % rv)
126
def setup_user_agent(vendor, vendor_sub, vendor_comment):
128
rv = c_setup_user_agent(vendor, vendor_sub, vendor_comment)
130
raise XPCOMError("setup_user_agent failed with code: %d" % rv)
132
def install_window_creator(new_window_handler):
133
cdef MiroWindowCreator *creator
134
creator = new_MiroWindowCreator()
135
rv = creator.install()
137
raise XPCOMError("MiroWindowCreator.install() failed with code: %d" % rv)
138
creator.SetNewWindowCallback(newWindowCallbackGlue, <void *>new_window_handler)
143
class XPCOMError(Exception):
146
cdef void focusCallbackGlue(PRBool forward, void* data) with gil:
147
cdef PyObject* retval
148
retval = PyObject_CallMethod(<PyObject*>data,
149
"on_browser_focus", "b", forward)
153
cdef int uriCallbackGlue(char *uri, void* data) with gil:
155
cdef PyObject* should_load
158
should_load = PyObject_CallMethod(<PyObject*>data,
159
"on_uri_load", "s", uri)
161
retval = PyObject_IsTrue(should_load)
162
Py_DECREF(should_load)
165
cdef void networkCallbackGlue(PRBool is_start, void* data) with gil:
166
cdef PyObject* retval
169
retval = PyObject_CallMethod(<PyObject*>data, "on_net_start", "")
171
retval = PyObject_CallMethod(<PyObject*>data, "on_net_stop", "")
175
cdef void newWindowCallbackGlue(char* uri, void* data) with gil:
176
cdef PyObject* retval
178
retval = PyObject_CallMethod(<PyObject*>data, "on_new_window", "s", uri)
182
cdef class XULRunnerBrowser:
183
cdef MiroBrowserEmbed* browser
185
def __init__(self, parent, x, y, width, height):
186
self.browser = new_MiroBrowserEmbed()
188
raise XPCOMError("MiroBrowserEmbed Constructor failed")
191
rv = self.browser.init(parent, x, y, width, height)
193
logging.warn("MiroBrowserEmbed.init() failed")
194
release(self.browser)
195
raise XPCOMError("MiroBrowserEmbed.init() failed")
198
rv = self.browser.disable()
200
raise XPCOMError("MiroBrowserEmbed.disable() failed")
203
rv = self.browser.enable()
205
raise XPCOMError("MiroBrowserEmbed.enabled() failed")
208
self.browser.destroy()
210
def set_callback_object(self, handler):
211
self.browser.SetFocusCallback(focusCallbackGlue, <void *>handler)
212
self.browser.SetURICallback(uriCallbackGlue, <void *>handler)
213
self.browser.SetNetworkCallback(networkCallbackGlue, <void *>handler)
215
def _check_result(self, function, result):
217
raise XPCOMError("%s failed with code: %d" % (function, result))
219
def resize(self, x, y, width, height):
221
rv = self.browser.resize(x, y, width, height)
222
self._check_result('MiroBrowserEmbed.resize', rv)
224
def load_uri(self, uri):
226
rv = self.browser.loadURI(uri)
227
self._check_result('MiroBrowserEmbed.loadURI', rv)
229
def get_current_uri(self):
232
rv = self.browser.getCurrentURI(&uri);
233
self._check_result('MiroBrowserEmbed.get_current_uri', rv)
234
return PyString_FromString(uri)
236
def get_current_title(self):
239
cdef size_t title_size
241
rv = self.browser.getCurrentTitle(&title);
243
self._check_result('MiroBrowserEmbed.get_current_title', rv);
245
# using wcslen has safety issues, but since we're using it on
246
# something that comes from XPCOM, i think we're ok
247
title_size = (wcslen(title) + 1) * 2;
249
return PyString_FromStringAndSize(<char *> title, title_size)
253
rv = self.browser.focus()
254
self._check_result('MiroBrowserEmbed.focus', rv)
256
def can_go_back(self):
257
return self.browser.canGoBack()
259
def can_go_forward(self):
260
return self.browser.canGoForward()
263
self.browser.goBack()
265
def go_forward(self):
266
self.browser.goForward()
272
self.browser.reload()