Index: src/mlx/gui/cef.py
===================================================================
--- src/mlx/gui/cef.py	(revision 648)
+++ src/mlx/gui/cef.py	(revision 682)
@@ -1,11 +1,18 @@
 from common import *
+
+from mlx.util import secondaryInstallation
+
+from cefpython3 import cefpython
+from selenium import webdriver
+from selenium.webdriver.chrome.options import Options
 
 import platform
 import json
-
-from cefpython3 import cefpython
-
+import time
 import os
 import re
+import threading
+import tempfile
+import traceback
 
 #------------------------------------------------------------------------------
@@ -20,12 +27,140 @@
 _toQuit = False
 
-#------------------------------------------------------------------------------
-
-def initialize():
+# The Selenium thread
+_seleniumHandler = None
+
+#------------------------------------------------------------------------------
+
+def getArgsFilePath():
+    """Get the path of the argument file."""
+    if os.name=="nt":
+        return os.path.join(tempfile.gettempdir(),
+                            "mlxcef.args" +
+                            (".secondary" if secondaryInstallation else ""))
+    else:
+        import pwd
+        return os.path.join(tempfile.gettempdir(),
+                            "mlxcef." + pwd.getpwuid(os.getuid())[0] + ".args" +
+                            (".secondary" if secondaryInstallation else ""))
+
+#------------------------------------------------------------------------------
+
+class ArgsFileWaiter(threading.Thread):
+    """A thread to wait for the appearance of the arguments file."""
+    def __init__(self, initializedCallback):
+        """Construct the thread."""
+        threading.Thread.__init__(self)
+        self.daemon = True
+
+        self._initializedCallback = initializedCallback
+
+    def run(self):
+        """Repeatedly check for the existence of the arguments file.
+
+        If it is found, read it, extract the arguments and insert a job into
+        the GUI loop to perform the actual initialization of CEF."""
+        argsFilePath = getArgsFilePath()
+        print "Waiting for the arguments file '%s' to appear" % (argsFilePath,)
+
+        while not os.path.exists(argsFilePath):
+            time.sleep(0.1)
+
+        print "Got arguments, reading them."""
+
+        with open(argsFilePath, "rt") as f:
+            args = f.read().split()
+
+        gobject.idle_add(_initializeCEF, args, self._initializedCallback)
+
+#------------------------------------------------------------------------------
+
+class SeleniumHandler(threading.Thread):
+    """Thread to handle Selenium operations."""
+    def __init__(self, programDirectory):
+        """Construct the thread."""
+        threading.Thread.__init__(self)
+        self.daemon = False
+
+        self._programDirectory = programDirectory
+
+        self._commandsCondition = threading.Condition()
+        self._commands = []
+
+        self._toQuit = False
+
+    def run(self):
+        """Create the Selenium driver and the perform any operations
+        requested."""
+        scriptName = "mlx_cef_caller"
+        if secondaryInstallation:
+            scriptName += "_secondary"
+        scriptName += ".bat" if os.name=="nt" else ".sh"
+
+        scriptPath = os.path.join(self._programDirectory, scriptName)
+        print "Creating the Selenium driver to call script", scriptPath
+
+        options = Options()
+        options.binary_location = scriptPath
+        driver = webdriver.Chrome(chrome_options = options)
+        # try:
+        # except:
+        #     traceback.print_exc()
+
+        print "Created Selenium driver."
+        while not self._toQuit:
+            with self._commandsCondition:
+                while not self._commands:
+                    self._commandsCondition.wait()
+
+                command = self._commands[0]
+                del self._commands[0]
+
+            command()
+
+        driver.quit()
+
+    def quit(self):
+        """Instruct the thread to quit and then join it."""
+        self._enqueue(self._quit)
+        self.join()
+
+    def _enqueue(self, command):
+        """Enqueue the given command.
+
+        command should be a function to be executed in the thread."""
+        with self._commandsCondition:
+            self._commands.append(command)
+            self._commandsCondition.notify()
+
+    def _quit(self):
+        """Set the _toQuit member variable to indicate that the thread should
+        quit."""
+        self._toQuit = True
+
+#------------------------------------------------------------------------------
+
+def initialize(programDirectory, initializedCallback):
     """Initialize the Chrome Embedded Framework."""
-    global _toQuit
+    global _toQuit, _seleniumHandler
     _toQuit = False
 
     gobject.threads_init()
+
+    argsFilePath = getArgsFilePath()
+    try:
+        os.unlink(argsFilePath)
+    except:
+        pass
+
+    _seleniumHandler = SeleniumHandler(programDirectory)
+    _seleniumHandler.start()
+
+    ArgsFileWaiter(initializedCallback).start()
+
+#------------------------------------------------------------------------------
+
+def _initializeCEF(args, initializedCallback):
+    """Perform the actual initialization of CEF using the given arguments."""
+    print "Initializing CEF with args:", args
 
     settings = {
@@ -41,7 +176,22 @@
     }
 
-    cefpython.Initialize(settings, {})
+    switches={}
+    for arg in args:
+        if arg.startswith("--"):
+            if arg != "--enable-logging":
+                assignIndex = arg.find("=")
+                if assignIndex<0:
+                    switches[arg[2:]] = ""
+                else:
+                    switches[arg[2:assignIndex]] = arg[assignIndex+1:]
+        else:
+            print "Unhandled switch", arg
+
+    cefpython.Initialize(settings, switches)
 
     gobject.timeout_add(10, _handleTimeout)
+
+    print "Initialized, executing callback..."
+    initializedCallback()
 
 #------------------------------------------------------------------------------
@@ -83,6 +233,7 @@
 def finalize():
     """Finalize the Chrome Embedded Framework."""
-    global _toQuit
+    global _toQuit, _seleniumHandler
     toQuit = True
+    _seleniumHandler.quit()
     cefpython.Shutdown()
 
