[Sugar-devel] [Browse] Store web session cookies in SQlite database, for SL #3456

Manuel Quiñones manuq at laptop.org
Wed Apr 18 18:01:06 EDT 2012


There was code in Browse to create a SQlite database under data
directory in the activity profile.  This was for creating a cookie to
authenticate the laptop in a schoolserver.

I have moved the database creation to a new function, that is called
before the one that adds the cookie for the schoolserver.

The database is being attached to the WebKit session so the cookies
are stored there, in a Mozilla compatible SQLite format.

The code is based in Epiphany, embed/ephy-embed-single.c .

Signed-off-by: Manuel Quiñones <manuq at laptop.org>
---
 webactivity.py |   63 ++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/webactivity.py b/webactivity.py
index aadc29a..db0ba97 100644
--- a/webactivity.py
+++ b/webactivity.py
@@ -28,6 +28,7 @@ from gi.repository import Gtk
 from gi.repository import Gdk
 from gi.repository import GdkPixbuf
 from gi.repository import WebKit
+from gi.repository import SoupGNOME
 
 import base64
 import time
@@ -58,6 +59,7 @@ PROFILE_VERSION = 2
 _profile_version = 0
 _profile_path = os.path.join(activity.get_activity_root(), 'data/gecko')
 _version_file = os.path.join(_profile_path, 'version')
+_cookies_db_path = os.path.join(_profile_path, 'cookies.sqlite')
 
 if os.path.exists(_version_file):
     f = open(_version_file)
@@ -76,12 +78,42 @@ if _profile_version < PROFILE_VERSION:
     f.close()
 
 
+def _create_cookies_database():
+    """Creates a SQlite database to store cookies.
+
+    Cookies are stored in the Mozilla format.
+
+    """
+    try:
+        cookies_db = sqlite3.connect(_cookies_db_path)
+        cursor = cookies_db.cursor()
+        cursor.execute('''CREATE TABLE IF NOT EXISTS
+                       moz_cookies
+                       (id INTEGER PRIMARY KEY,
+                        name TEXT,
+                        value TEXT,
+                        host TEXT,
+                        path TEXT,
+                        expiry INTEGER,
+                        lastAccessed INTEGER,
+                        isSecure INTEGER,
+                        isHttpOnly INTEGER)''')
+        cookies_db.commit()
+        cookies_db.close()
+    except sqlite3.Error:
+        _logger.exception('Could not create cookies database')
+
+
 def _seed_xs_cookie():
-    ''' Create a HTTP Cookie to authenticate with the Schoolserver
-    '''
+    """Create a HTTP Cookie to authenticate with the Schoolserver.
+
+    Do nothing if the laptop is not registered with Schoolserver, or
+    if the cookie already exists.
+
+    """
     client = GConf.Client.get_default()
     backup_url = client.get_string('/desktop/sugar/backup_url')
-    if not backup_url:
+    if backup_url == '':
         _logger.debug('seed_xs_cookie: Not registered with Schoolserver')
         return
 
@@ -92,23 +124,9 @@ def _seed_xs_cookie():
     cookie_data = {'color': profile.get_color().to_string(),
                    'pkey_hash': sha1(pubkey).hexdigest()}
 
-    db_path = os.path.join(_profile_path, 'cookies.sqlite')
     try:
-        cookies_db = sqlite3.connect(db_path)
+        cookies_db = sqlite3.connect(_cookies_db_path)
         c = cookies_db.cursor()
-
-        c.execute('''CREATE TABLE IF NOT EXISTS
-                     moz_cookies
-                     (id INTEGER PRIMARY KEY,
-                      name TEXT,
-                      value TEXT,
-                      host TEXT,
-                      path TEXT,
-                      expiry INTEGER,
-                      lastAccessed INTEGER,
-                      isSecure INTEGER,
-                      isHttpOnly INTEGER)''')
-
         c.execute('''SELECT id
                      FROM moz_cookies
                      WHERE name=? AND host=? AND path=?''',
@@ -167,9 +185,16 @@ class WebActivity(activity.Activity):
 
         _logger.debug('Starting the web activity')
 
+        _create_cookies_database()
+        _seed_xs_cookie()
+
         session = WebKit.get_default_session()
         session.set_property('accept-language-auto', True)
 
+        # Attach the cookies database to the web session:
+        jar = SoupGNOME.CookieJarSqlite.new(_cookies_db_path, False)
+        session.add_feature(jar)
+
         # FIXME
         # downloadmanager.remove_old_parts()
 
@@ -177,8 +202,6 @@ class WebActivity(activity.Activity):
         self._tabbed_view = TabbedView()
         self._tabbed_view.connect('focus-url-entry', self._on_focus_url_entry)
 
-        _seed_xs_cookie()
-
         # HACK
         # Currently, the multiple tabs feature crashes the Browse activity
         # on cairo versions 1.8.10 or later. The exact cause for this
-- 
1.7.7.6



More information about the Sugar-devel mailing list