def restore_default_bookmarks(self): """Restores the default bookmarks for the current profile.""" retval = self.marionette.execute_async_script(""" Components.utils.import("resource://gre/modules/BookmarkHTMLUtils.jsm"); Components.utils.import("resource://gre/modules/Services.jsm"); // Default bookmarks.html file is stored inside omni.jar, // so get it via a resource URI let defaultBookmarks = 'resource:///defaults/profile/bookmarks.html'; let observer = { observe: function (aSubject, aTopic, aData) { Services.obs.removeObserver(observer, "bookmarks-restore-success"); Services.obs.removeObserver(observer, "bookmarks-restore-failed"); marionetteScriptFinished(aTopic == "bookmarks-restore-success"); } }; // Trigger the import of the default bookmarks Services.obs.addObserver(observer, "bookmarks-restore-success", false); Services.obs.addObserver(observer, "bookmarks-restore-failed", false); BookmarkHTMLUtils.importFromURL(defaultBookmarks, true); """, script_timeout=10000) if not retval: raise MarionetteException("Restore Default Bookmarks failed")
def restore_pref(self, pref_name): """Restores a previously set preference to its former value. The first time a preference gets modified a backup of its value is made. By calling this method, exactly this value will be restored, whether how often the preference has been modified again afterward. If the preference did not exist before and has been newly created, it will be reset to its original value. Please see :func:`~Preferences.reset_pref` for details. :param pref_name: The preference to restore """ assert pref_name is not None try: # in case it is a newly set preference, reset it. Otherwise restore # its original value. if self.archive[pref_name] is None: self.reset_pref(pref_name) else: self.set_pref(pref_name, self.archive[pref_name]) del self.archive[pref_name] except KeyError: raise MarionetteException('Nothing to restore for preference "%s"', pref_name)
def remove_all_history(self): """Remove all history items.""" retval = self.marionette.execute_async_script(""" Components.utils.import("resource://gre/modules/PlacesUtils.jsm"); PlacesUtils.history.clear() .then(() => marionetteScriptFinished(true)) .catch(() => marionetteScriptFinished(false)); """, script_timeout=10000) if not retval: raise MarionetteException("Removing all history failed")
def restore_default_bookmarks(self): """Restore the default bookmarks for the current profile.""" retval = self.marionette.execute_async_script(""" Components.utils.import("resource://gre/modules/BookmarkHTMLUtils.jsm"); // Default bookmarks.html file is stored inside omni.jar, // so get it via a resource URI let defaultBookmarks = 'chrome://browser/locale/bookmarks.html'; // Trigger the import of the default bookmarks BookmarkHTMLUtils.importFromURL(defaultBookmarks, true) .then(() => marionetteScriptFinished(true)) .catch(() => marionetteScriptFinished(false)); """, script_timeout=10000) if not retval: raise MarionetteException("Restore Default Bookmarks failed")
def get_entity(self, dtd_urls, entity_id): """Returns the localized string for the specified DTD entity id. To find the entity all given DTD files will be searched for the id. :param dtd_urls: A list of dtd files to search. :param entity_id: The id to retrieve the value from. :returns: The localized string for the requested entity. :raises MarionetteException: When entity id is not found in dtd_urls. """ # Add xhtml11.dtd to prevent missing entity errors with XHTML files dtds = copy.copy(dtd_urls) dtds.append("resource:///res/dtd/xhtml11.dtd") dtd_refs = '' for index, item in enumerate(dtds): dtd_id = 'dtd_%s' % index dtd_refs += '<!ENTITY %% %s SYSTEM "%s">%%%s;' % \ (dtd_id, item, dtd_id) contents = """<?xml version="1.0"?> <!DOCTYPE elem [%s]> <elem id="entity">&%s;</elem>""" % (dtd_refs, entity_id) with self.marionette.using_context('chrome'): value = self.marionette.execute_script(""" var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"] .createInstance(Components.interfaces.nsIDOMParser); var doc = parser.parseFromString(arguments[0], "text/xml"); var node = doc.querySelector("elem[id='entity']"); return node ? node.textContent : null; """, script_args=[contents]) if not value: raise MarionetteException('DTD Entity not found: %s' % entity_id) return value
def sanitize(self, data_type): """Sanitize user data, including cache, cookies, offlineApps, history, formdata, downloads, passwords, sessions, siteSettings. Usage: sanitize(): Clears all user data. sanitize({ "sessions": True }): Clears only session user data. more: https://dxr.mozilla.org/mozilla-central/source/browser/modules/Sanitizer.jsm :param data_type: optional, Information specifying data to be sanitized """ with self.marionette.using_context('chrome'): result = self.marionette.execute_async_script( """ let resolve = arguments[arguments.length - 1]; var {Sanitizer} = Components.utils.import("resource:///modules/Sanitizer.jsm", {}); var data_type = arguments[0]; // Apply options for what to sanitize var itemsToClear = []; for (var pref of Object.keys(data_type)) { if (data_type[pref]) { itemsToClear.push(pref); } }; // Sanitize and wait for the promise to resolve Sanitizer.sanitize(itemsToClear).then(() => { resolve(true); }, aError => { resolve(false); }); """, script_args=[data_type]) if not result: raise MarionetteException('Sanitizing of profile data failed.')
def get_property(self, property_urls, property_id): """Returns the localized string for the specified property id. To find the property all given property files will be searched for the id. :param property_urls: A list of property files to search. :param property_id: The id to retrieve the value from. :returns: The localized string for the requested entity. :raises MarionetteException: When property id is not found in property_urls. """ with self.marionette.using_context('chrome'): value = self.marionette.execute_script( """ let property = null; let property_id = arguments[1]; arguments[0].some(aUrl => { let bundle = Services.strings.createBundle(aUrl); try { property = bundle.GetStringFromName(property_id); return true; } catch (ex) { } }); return property; """, script_args=[property_urls, property_id]) if not value: raise MarionetteException('Property not found: %s' % property_id) return value
def sanitize(self, data_type): """Sanitize user data, including cache, cookies, offlineApps, history, formdata, downloads, passwords, sessions, siteSettings. Usage: sanitize(): Clears all user data. sanitize({ "sessions": True }): Clears only session user data. more: https://dxr.mozilla.org/mozilla-central/source/browser/base/content/sanitize.js :param data_type: optional, Information specifying data to be sanitized """ with self.marionette.using_context('chrome'): result = self.marionette.execute_async_script( """ Components.utils.import("resource://gre/modules/Services.jsm"); var data_type = arguments[0]; var data_type = (typeof data_type === "undefined") ? {} : { cache: data_type.cache || false, cookies: data_type.cookies || false, downloads: data_type.downloads || false, formdata: data_type.formdata || false, history: data_type.history || false, offlineApps: data_type.offlineApps || false, passwords: data_type.passwords || false, sessions: data_type.sessions || false, siteSettings: data_type.siteSettings || false }; // Load the sanitize script var tempScope = {}; Components.classes["@mozilla.org/moz/jssubscript-loader;1"] .getService(Components.interfaces.mozIJSSubScriptLoader) .loadSubScript("chrome://browser/content/sanitize.js", tempScope); // Instantiate the Sanitizer var s = new tempScope.Sanitizer(); s.prefDomain = "privacy.cpd."; var itemPrefs = Services.prefs.getBranch(s.prefDomain); // Apply options for what to sanitize for (var pref in data_type) { itemPrefs.setBoolPref(pref, data_type[pref]); }; // Sanitize and wait for the promise to resolve var finished = false; s.sanitize().then(() => { for (let pref in data_type) { itemPrefs.clearUserPref(pref); }; marionetteScriptFinished(true); }, aError => { for (let pref in data_type) { itemPrefs.clearUserPref(pref); }; marionetteScriptFinished(false); }); """, script_args=[data_type]) if not result: raise MarionetteException('Sanitizing of profile data failed.')