~ubuntu-branches/ubuntu/natty/miro/natty

« back to all changes in this revision

Viewing changes to platform/windows-xul/plat/frontends/widgets/XULRunnerBrowser/xulrunnerbrowser.pyx

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2011-01-22 02:46:33 UTC
  • mfrom: (1.4.10 upstream) (1.7.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20110122024633-kjme8u93y2il5nmf
Tags: 3.5.1-1ubuntu1
* Merge from debian.  Remaining ubuntu changes:
  - Use python 2.7 instead of python 2.6
  - Relax dependency on python-dbus to >= 0.83.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Miro - an RSS based video player application
2
 
# Copyright (C) 2005-2010 Participatory Culture Foundation
3
 
#
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.
8
 
#
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.
13
 
#
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
17
 
#
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
20
 
# library.
21
 
#
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.
28
 
 
29
 
"""xulrunnerbrowser -- Pyrex module to implement an embedded XULRunner
30
 
broswer.
31
 
"""
32
 
 
33
 
import logging
34
 
 
35
 
ctypedef unsigned short wchar_t
36
 
ctypedef int size_t
37
 
 
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)
47
 
 
48
 
cdef extern from "nscore.h":
49
 
    ctypedef unsigned int nsresult
50
 
    ctypedef unsigned int PRBool
51
 
    cdef enum NS_RESULT_VALUES:
52
 
        NS_OK = 0
53
 
 
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, 
60
 
                int height)
61
 
        nsresult (*disable)()
62
 
        nsresult (*enable)()
63
 
        nsresult (*destroy)()
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)
68
 
        nsresult (*focus)()
69
 
        int (*canGoBack)()
70
 
        int (*canGoForward)()
71
 
        void (*goBack)()
72
 
        void (*goForward)()
73
 
        void (*stop)()
74
 
        void (*reload)()
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)
83
 
 
84
 
cdef extern from "MiroWindowCreator.h":
85
 
    ctypedef void(*newWindowCallback)(char* data, void* data)
86
 
 
87
 
    ctypedef struct MiroWindowCreator:
88
 
        nsresult (*install)()
89
 
        void (*SetNewWindowCallback)(newWindowCallback, void*)
90
 
 
91
 
    # Trick Pyrex into creating constructor and destructor code
92
 
    MiroWindowCreator *new_MiroWindowCreator "new MiroWindowCreator" ()
93
 
    void del_MiroWindowCreator "delete" (MiroWindowCreator *rect)
94
 
 
95
 
cdef extern from "HttpObserver.h":
96
 
    nsresult startObserving()
97
 
 
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()
102
 
 
103
 
cdef extern from "pythread.h":
104
 
    ctypedef struct PyThreadState
105
 
    PyThreadState *PyEval_SaveThread()
106
 
    void PyEval_RestoreThread(PyThreadState *_save)
107
 
 
108
 
cdef extern from "string.h":
109
 
    size_t wcslen(wchar_t *str)
110
 
 
111
 
cdef public log_warning(char* str) with gil:
112
 
    logging.warn(str)
113
 
 
114
 
cdef public log_info(char* str) with gil:
115
 
    logging.info(str)
116
 
 
117
 
def initialize(xul_dir, app_dir):
118
 
    cdef nsresult rv
119
 
    rv = init_xulrunner(xul_dir, app_dir)
120
 
    if rv != NS_OK:
121
 
        raise XPCOMError("init_xulrunner failed with code: %d" % rv)
122
 
    rv = startObserving()
123
 
    if rv != NS_OK:
124
 
        raise XPCOMError("startObserving failed with code: %d" % rv)
125
 
 
126
 
def setup_user_agent(vendor, vendor_sub, vendor_comment):
127
 
    cdef nsresult rv
128
 
    rv = c_setup_user_agent(vendor, vendor_sub, vendor_comment)
129
 
    if rv != NS_OK:
130
 
        raise XPCOMError("setup_user_agent failed with code: %d" % rv)
131
 
 
132
 
def install_window_creator(new_window_handler):
133
 
    cdef MiroWindowCreator *creator
134
 
    creator = new_MiroWindowCreator()
135
 
    rv = creator.install()
136
 
    if rv != NS_OK:
137
 
        raise XPCOMError("MiroWindowCreator.install() failed with code: %d" % rv)
138
 
    creator.SetNewWindowCallback(newWindowCallbackGlue, <void *>new_window_handler)
139
 
 
140
 
def shutdown():
141
 
    shutdown_xulrunner()
142
 
 
143
 
class XPCOMError(Exception):
144
 
    pass
145
 
 
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)
150
 
    if retval:
151
 
        Py_DECREF(retval)
152
 
 
153
 
cdef int uriCallbackGlue(char *uri, void* data) with gil:
154
 
    cdef int retval
155
 
    cdef PyObject* should_load
156
 
 
157
 
    retval = 0
158
 
    should_load = PyObject_CallMethod(<PyObject*>data,
159
 
            "on_uri_load", "s", uri)
160
 
    if should_load:
161
 
        retval = PyObject_IsTrue(should_load)
162
 
        Py_DECREF(should_load)
163
 
    return retval
164
 
 
165
 
cdef void networkCallbackGlue(PRBool is_start, void* data) with gil:
166
 
    cdef PyObject* retval
167
 
 
168
 
    if is_start:
169
 
        retval = PyObject_CallMethod(<PyObject*>data, "on_net_start", "")
170
 
    else:
171
 
        retval = PyObject_CallMethod(<PyObject*>data, "on_net_stop", "")
172
 
    if retval:
173
 
        Py_DECREF(retval)
174
 
 
175
 
cdef void newWindowCallbackGlue(char* uri, void* data) with gil:
176
 
    cdef PyObject* retval
177
 
 
178
 
    retval = PyObject_CallMethod(<PyObject*>data, "on_new_window", "s", uri)
179
 
    if retval:
180
 
        Py_DECREF(retval)
181
 
 
182
 
cdef class XULRunnerBrowser:
183
 
    cdef MiroBrowserEmbed* browser
184
 
 
185
 
    def __init__(self, parent, x, y, width, height):
186
 
        self.browser = new_MiroBrowserEmbed()
187
 
        if not self.browser:
188
 
            raise XPCOMError("MiroBrowserEmbed Constructor failed")
189
 
        addref(self.browser)
190
 
        cdef nsresult rv
191
 
        rv = self.browser.init(parent, x, y, width, height)
192
 
        if rv != NS_OK:
193
 
            logging.warn("MiroBrowserEmbed.init() failed")
194
 
            release(self.browser)
195
 
            raise XPCOMError("MiroBrowserEmbed.init() failed")
196
 
 
197
 
    def disable(self):
198
 
        rv = self.browser.disable()
199
 
        if rv != NS_OK:
200
 
            raise XPCOMError("MiroBrowserEmbed.disable() failed")
201
 
 
202
 
    def enable(self):
203
 
        rv = self.browser.enable()
204
 
        if rv != NS_OK:
205
 
            raise XPCOMError("MiroBrowserEmbed.enabled() failed")
206
 
 
207
 
    def destroy(self):
208
 
        self.browser.destroy()
209
 
 
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)
214
 
 
215
 
    def _check_result(self, function, result):
216
 
        if result != NS_OK:
217
 
            raise XPCOMError("%s failed with code: %d" % (function, result))
218
 
 
219
 
    def resize(self, x, y, width, height):
220
 
        cdef nsresult rv
221
 
        rv = self.browser.resize(x, y, width, height)
222
 
        self._check_result('MiroBrowserEmbed.resize', rv)
223
 
 
224
 
    def load_uri(self, uri):
225
 
        cdef nsresult rv
226
 
        rv = self.browser.loadURI(uri)
227
 
        self._check_result('MiroBrowserEmbed.loadURI', rv)
228
 
 
229
 
    def get_current_uri(self):
230
 
        cdef nsresult rv
231
 
        cdef char *uri
232
 
        rv = self.browser.getCurrentURI(&uri);
233
 
        self._check_result('MiroBrowserEmbed.get_current_uri', rv)
234
 
        return PyString_FromString(uri)
235
 
 
236
 
    def get_current_title(self):
237
 
        cdef nsresult rv
238
 
        cdef wchar_t *title
239
 
        cdef size_t title_size
240
 
 
241
 
        rv = self.browser.getCurrentTitle(&title);
242
 
 
243
 
        self._check_result('MiroBrowserEmbed.get_current_title', rv);
244
 
 
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;
248
 
 
249
 
        return PyString_FromStringAndSize(<char *> title, title_size)
250
 
 
251
 
    def focus(self):
252
 
        cdef nsresult rv
253
 
        rv = self.browser.focus()
254
 
        self._check_result('MiroBrowserEmbed.focus', rv)
255
 
 
256
 
    def can_go_back(self):
257
 
        return self.browser.canGoBack()
258
 
 
259
 
    def can_go_forward(self):
260
 
        return self.browser.canGoForward()
261
 
 
262
 
    def go_back(self):
263
 
        self.browser.goBack()
264
 
 
265
 
    def go_forward(self):
266
 
        self.browser.goForward()
267
 
 
268
 
    def stop(self):
269
 
        self.browser.stop()
270
 
 
271
 
    def reload(self):
272
 
        self.browser.reload()