/usr/share/sugar/activities/Browse.activity/browser.py is in sugar-browse-activity-0.86 120-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | # Copyright (C) 2006, Red Hat, Inc.
# Copyright (C) 2007, One Laptop Per Child
# Copyright (C) 2009, Tomeu Vizoso, Simon Schampijer
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
import time
import logging
from gettext import gettext as _
import gobject
import gtk
import hulahop
import xpcom
from xpcom.nsError import *
from xpcom import components
from xpcom.components import interfaces
from hulahop.webview import WebView
from sugar.datastore import datastore
from sugar import profile
from sugar import env
from sugar.activity import activity
from sugar.graphics import style
import sessionstore
from palettes import ContentInvoker
from sessionhistory import HistoryListener
from progresslistener import ProgressListener
_ZOOM_AMOUNT = 0.1
class GetSourceListener(object):
_com_interfaces_ = interfaces.nsIWebProgressListener
def __init__(self, file_path, async_cb, async_err_cb):
self._file_path = file_path
self._async_cb = async_cb
self._async_err_cb = async_err_cb
def onStateChange(self, webProgress, request, stateFlags, status):
if stateFlags & interfaces.nsIWebProgressListener.STATE_IS_REQUEST and \
stateFlags & interfaces.nsIWebProgressListener.STATE_STOP:
self._async_cb(self._file_path)
def onProgressChange(self, progress, request, curSelfProgress,
maxSelfProgress, curTotalProgress, maxTotalProgress):
pass
def onLocationChange(self, progress, request, location):
pass
def onStatusChange(self, progress, request, status, message):
pass
def onSecurityChange(self, progress, request, state):
pass
class CommandListener(object):
_com_interfaces_ = interfaces.nsIDOMEventListener
def __init__(self, window):
self._window = window
def handleEvent(self, event):
if not event.isTrusted:
return
uri = event.originalTarget.ownerDocument.documentURI
if not uri.startswith('about:neterror?e=nssBadCert'):
return
cls = components.classes['@sugarlabs.org/add-cert-exception;1']
cert_exception = cls.createInstance(interfaces.hulahopAddCertException)
cert_exception.showDialog(self._window)
class TabbedView(gtk.Notebook):
__gtype_name__ = 'TabbedView'
_com_interfaces_ = interfaces.nsIWindowCreator
AGENT_SHEET = os.path.join(activity.get_bundle_path(),
'agent-stylesheet.css')
USER_SHEET = os.path.join(env.get_profile_path(), 'gecko',
'user-stylesheet.css')
def __init__(self):
gobject.GObject.__init__(self)
self.props.show_border = False
self.props.scrollable = True
io_service_class = components.classes[ \
"@mozilla.org/network/io-service;1"]
io_service = io_service_class.getService(interfaces.nsIIOService)
# Use xpcom to turn off "offline mode" detection, which disables
# access to localhost for no good reason. (Trac #6250.)
io_service2 = io_service_class.getService(interfaces.nsIIOService2)
io_service2.manageOfflineStatus = False
cls = components.classes['@mozilla.org/content/style-sheet-service;1']
style_sheet_service = cls.getService(interfaces.nsIStyleSheetService)
if os.path.exists(TabbedView.AGENT_SHEET):
agent_sheet_uri = io_service.newURI('file:///' +
TabbedView.AGENT_SHEET,
None, None)
style_sheet_service.loadAndRegisterSheet(agent_sheet_uri,
interfaces.nsIStyleSheetService.AGENT_SHEET)
if os.path.exists(TabbedView.USER_SHEET):
user_sheet_uri = io_service.newURI('file:///' + TabbedView.USER_SHEET,
None, None)
style_sheet_service.loadAndRegisterSheet(user_sheet_uri,
interfaces.nsIStyleSheetService.USER_SHEET)
cls = components.classes['@mozilla.org/embedcomp/window-watcher;1']
window_watcher = cls.getService(interfaces.nsIWindowWatcher)
window_creator = xpcom.server.WrapObject(self,
interfaces.nsIWindowCreator)
window_watcher.setWindowCreator(window_creator)
browser = Browser()
self._append_tab(browser)
def createChromeWindow(self, parent, flags):
if flags & interfaces.nsIWebBrowserChrome.CHROME_OPENAS_CHROME:
dialog = PopupDialog()
dialog.view.is_chrome = True
parent_dom_window = parent.webBrowser.contentDOMWindow
parent_view = hulahop.get_view_for_window(parent_dom_window)
if parent_view:
dialog.set_transient_for(parent_view.get_toplevel())
browser = dialog.view.browser
item = browser.queryInterface(interfaces.nsIDocShellTreeItem)
item.itemType = interfaces.nsIDocShellTreeItem.typeChromeWrapper
return browser.containerWindow
else:
browser = Browser()
self._append_tab(browser)
return browser.browser.containerWindow
def _append_tab(self, browser):
label = TabLabel(browser)
label.connect('tab-close', self.__tab_close_cb)
self.append_page(browser, label)
browser.show()
self.set_current_page(-1)
self.props.show_tabs = self.get_n_pages() > 1
def __tab_close_cb(self, label, browser):
self.remove_page(self.page_num(browser))
browser.destroy()
self.props.show_tabs = self.get_n_pages() > 1
def _get_current_browser(self):
return self.get_nth_page(self.get_current_page())
current_browser = gobject.property(type=object, getter=_get_current_browser)
def get_session(self):
tab_sessions = []
for index in xrange(0, self.get_n_pages()):
browser = self.get_nth_page(index)
tab_sessions.append(sessionstore.get_session(browser))
return tab_sessions
def set_session(self, tab_sessions):
if tab_sessions and isinstance(tab_sessions[0], dict):
# Old format, no tabs
tab_sessions = [tab_sessions]
while self.get_n_pages():
self.remove_page(self.get_n_pages() - 1)
for tab_session in tab_sessions:
browser = Browser()
self._append_tab(browser)
sessionstore.set_session(browser, tab_session)
gtk.rc_parse_string('''
style "browse-tab-close" {
xthickness = 0
ythickness = 0
}
widget "*browse-tab-close" style "browse-tab-close"''')
class TabLabel(gtk.HBox):
__gtype_name__ = 'TabLabel'
__gsignals__ = {
'tab-close': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([object]))
}
def __init__(self, browser):
gobject.GObject.__init__(self)
self._browser = browser
self._browser.connect('is-setup', self.__browser_is_setup_cb)
self._label = gtk.Label('')
self.pack_start(self._label)
self._label.show()
button = gtk.Button()
button.connect('clicked', self.__button_clicked_cb)
button.set_name('browse-tab-close')
button.props.relief = gtk.RELIEF_NONE
button.props.focus_on_click = False
self.pack_start(button)
button.show()
close_image = gtk.image_new_from_stock(gtk.STOCK_CLOSE,
gtk.ICON_SIZE_MENU)
button.add(close_image)
close_image.show()
def __button_clicked_cb(self, button):
self.emit('tab-close', self._browser)
def __browser_is_setup_cb(self, browser):
browser.progress.connect('notify::location', self.__location_changed_cb)
browser.connect('notify::title', self.__title_changed_cb)
def __location_changed_cb(self, progress_listener, pspec):
self._label.set_text(self._browser.get_url_from_nsiuri(progress_listener.location))
def __title_changed_cb(self, browser, pspec):
self._label.set_text(browser.props.title)
class Browser(WebView):
__gtype_name__ = 'Browser'
__gsignals__ = {
'is-setup': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([]))
}
def __init__(self):
WebView.__init__(self)
self.history = HistoryListener()
self.progress = ProgressListener()
cls = components.classes["@mozilla.org/typeaheadfind;1"]
self.typeahead = cls.createInstance(interfaces.nsITypeAheadFind)
def do_setup(self):
WebView.do_setup(self)
listener = xpcom.server.WrapObject(ContentInvoker(self),
interfaces.nsIDOMEventListener)
self.window_root.addEventListener('click', listener, False)
listener = xpcom.server.WrapObject(CommandListener(self.dom_window),
interfaces.nsIDOMEventListener)
self.window_root.addEventListener('command', listener, False)
self.progress.setup(self)
self.history.setup(self.web_navigation)
self.typeahead.init(self.doc_shell)
self.emit('is-setup')
def get_url_from_nsiuri(self, uri):
"""
get a nsIURI object and return a string with the url
"""
if uri == None:
return ''
cls = components.classes['@mozilla.org/intl/texttosuburi;1']
texttosuburi = cls.getService(interfaces.nsITextToSubURI)
return texttosuburi.unEscapeURIForUI(uri.originCharset, uri.spec)
def get_session(self):
return sessionstore.get_session(self)
def set_session(self, data):
return sessionstore.set_session(self, data)
def get_source(self, async_cb, async_err_cb):
cls = components.classes[ \
'@mozilla.org/embedding/browser/nsWebBrowserPersist;1']
persist = cls.createInstance(interfaces.nsIWebBrowserPersist)
# get the source from the cache
persist.persistFlags = \
interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_FROM_CACHE
temp_path = os.path.join(activity.get_activity_root(), 'instance')
file_path = os.path.join(temp_path, '%i' % time.time())
cls = components.classes["@mozilla.org/file/local;1"]
local_file = cls.createInstance(interfaces.nsILocalFile)
local_file.initWithPath(file_path)
progresslistener = GetSourceListener(file_path, async_cb, async_err_cb)
persist.progressListener = xpcom.server.WrapObject(
progresslistener, interfaces.nsIWebProgressListener)
uri = self.web_navigation.currentURI
persist.saveURI(uri, self.doc_shell, None, None, None, local_file)
def zoom_in(self):
contentViewer = self.doc_shell.queryInterface( \
interfaces.nsIDocShell).contentViewer
if contentViewer is not None:
markupDocumentViewer = contentViewer.queryInterface( \
interfaces.nsIMarkupDocumentViewer)
markupDocumentViewer.fullZoom += _ZOOM_AMOUNT
def zoom_out(self):
contentViewer = self.doc_shell.queryInterface( \
interfaces.nsIDocShell).contentViewer
if contentViewer is not None:
markupDocumentViewer = contentViewer.queryInterface( \
interfaces.nsIMarkupDocumentViewer)
markupDocumentViewer.fullZoom -= _ZOOM_AMOUNT
class PopupDialog(gtk.Window):
def __init__(self):
gtk.Window.__init__(self)
self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
border = style.GRID_CELL_SIZE
self.set_default_size(gtk.gdk.screen_width() - border * 2,
gtk.gdk.screen_height() - border * 2)
self.view = WebView()
self.view.connect('notify::visibility', self.__notify_visibility_cb)
self.add(self.view)
self.view.realize()
def __notify_visibility_cb(self, web_view, pspec):
if self.view.props.visibility:
self.view.show()
self.show()
|