addons.py (2686B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 import os 6 7 from . import errors 8 9 __all__ = ["Addons", "AddonInstallException"] 10 11 12 class AddonInstallException(errors.MarionetteException): 13 pass 14 15 16 class Addons: 17 """An API for installing and inspecting addons during Gecko 18 runtime. This is a partially implemented wrapper around Gecko's 19 `AddonManager API`_. 20 21 For example:: 22 23 from marionette_driver.addons import Addons 24 addons = Addons(marionette) 25 addons.install("/path/to/extension.xpi") 26 27 .. _AddonManager API: https://developer.mozilla.org/en-US/Add-ons/Add-on_Manager 28 29 """ 30 31 def __init__(self, marionette): 32 self._mn = marionette 33 34 def install(self, path=None, temp=False, data=None): 35 """Install a Firefox addon, which can be used right away. 36 37 :param path: A file path to the extension to be installed. 38 :param temp: Install a temporary addon. Temporary addons will 39 automatically be uninstalled on shutdown and do not need 40 to be signed. 41 :param data: A base64-encoded string of a zip-packed addon. 42 43 :returns: The addon ID string of the newly installed addon. 44 45 :raises: :exc:`AddonInstallException` 46 47 """ 48 49 if (path and data) or (not path and not data): 50 raise AddonInstallException("Must use either path or data argument.") 51 52 body = {"temporary": temp} 53 if path: 54 # On windows we can end up with a path with mixed \ and / 55 # which Firefox doesn't like 56 path = path.replace("/", os.path.sep) 57 body.update({"path": path}) 58 59 if data: 60 body.update({"addon": data}) 61 62 try: 63 return self._mn._send_message("Addon:Install", body, key="value") 64 except errors.UnknownException as e: 65 raise AddonInstallException(e) 66 67 def uninstall(self, addon_id): 68 """Uninstall a Firefox addon. 69 70 If the addon is restartless, it will be uninstalled right away. 71 Otherwise a restart using :func:`~marionette_driver.marionette.Marionette.restart` 72 will be needed. 73 74 If the call to uninstall is resulting in a `ScriptTimeoutException`, 75 an invalid ID is likely being passed in. Unfortunately due to 76 AddonManager's implementation, it's hard to retrieve this error from 77 Python. 78 79 :param addon_id: The addon ID string to uninstall. 80 81 """ 82 self._mn._send_message("Addon:Uninstall", {"id": addon_id})