shutdown.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Utilities for registering functions to be called at xpcom shutdown.
  2. #
  3. # Pass xpcom.shutdown.register a function (and optionally args) that should
  4. # be called as xpcom shutsdown. Relies on xpcom itself sending the
  5. # standard shutdown notification.
  6. # ***** BEGIN LICENSE BLOCK *****
  7. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  8. #
  9. # The contents of this file are subject to the Mozilla Public License Version
  10. # 1.1 (the "License"); you may not use this file except in compliance with
  11. # the License. You may obtain a copy of the License at
  12. # http://www.mozilla.org/MPL/
  13. #
  14. # Software distributed under the License is distributed on an "AS IS" basis,
  15. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. # for the specific language governing rights and limitations under the
  17. # License.
  18. #
  19. # The Original Code is the Python XPCOM language bindings.
  20. #
  21. # The Initial Developer of the Original Code is Mark Hammond.
  22. # Portions created by the Initial Developer are Copyright (C) 2000
  23. # the Initial Developer. All Rights Reserved.
  24. #
  25. # Contributor(s):
  26. #
  27. # Alternatively, the contents of this file may be used under the terms of
  28. # either the GNU General Public License Version 2 or later (the "GPL"), or
  29. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30. # in which case the provisions of the GPL or the LGPL are applicable instead
  31. # of those above. If you wish to allow use of your version of this file only
  32. # under the terms of either the GPL or the LGPL, and not to allow others to
  33. # use your version of this file under the terms of the MPL, indicate your
  34. # decision by deleting the provisions above and replace them with the notice
  35. # and other provisions required by the GPL or the LGPL. If you do not delete
  36. # the provisions above, a recipient may use your version of this file under
  37. # the terms of any one of the MPL, the GPL or the LGPL.
  38. #
  39. # ***** END LICENSE BLOCK *****
  40. #
  41. import xpcom.server
  42. from xpcom import _xpcom
  43. from xpcom.components import interfaces
  44. import logging
  45. _handlers = []
  46. class _ShutdownObserver:
  47. _com_interfaces_ = interfaces.nsIObserver
  48. def observe(self, service, topic, extra):
  49. logger = logging.getLogger('xpcom')
  50. # Remove the observer first, we can't touch things afterwards
  51. svc = service.QueryInterface(interfaces.nsIServiceManager)\
  52. .getServiceByContractID(
  53. "@mozilla.org/observer-service;1",
  54. interfaces.nsIObserverService)
  55. svc.removeObserver(self, topic)
  56. while _handlers:
  57. func, args, kw = _handlers.pop()
  58. try:
  59. logger.debug("Calling shutdown handler '%s'(*%s, **%s)",
  60. func, args, kw)
  61. func(*args, **kw)
  62. except:
  63. logger.exception("Shutdown handler '%s' failed", func)
  64. def register(func, *args, **kw):
  65. _handlers.append( (func, args, kw) )
  66. # Register
  67. svc = _xpcom.GetServiceManager().getServiceByContractID(
  68. "@mozilla.org/observer-service;1",
  69. interfaces.nsIObserverService)
  70. svc.addObserver(_ShutdownObserver(), "xpcom-shutdown", 0)
  71. del svc, _ShutdownObserver