CallbackFunction.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. * A common base class for representing WebIDL callback function types in C++.
  7. *
  8. * This class implements common functionality like lifetime
  9. * management, initialization with the callable, and setup of the call
  10. * environment. Subclasses corresponding to particular callback
  11. * function types should provide a Call() method that actually does
  12. * the call.
  13. */
  14. #ifndef mozilla_dom_CallbackFunction_h
  15. #define mozilla_dom_CallbackFunction_h
  16. #include "mozilla/dom/CallbackObject.h"
  17. namespace mozilla {
  18. namespace dom {
  19. class CallbackFunction : public CallbackObject
  20. {
  21. public:
  22. // See CallbackObject for an explanation of the arguments.
  23. explicit CallbackFunction(JSContext* aCx, JS::Handle<JSObject*> aCallable,
  24. nsIGlobalObject* aIncumbentGlobal)
  25. : CallbackObject(aCx, aCallable, aIncumbentGlobal)
  26. {
  27. }
  28. // See CallbackObject for an explanation of the arguments.
  29. explicit CallbackFunction(JS::Handle<JSObject*> aCallable,
  30. JS::Handle<JSObject*> aAsyncStack,
  31. nsIGlobalObject* aIncumbentGlobal)
  32. : CallbackObject(aCallable, aAsyncStack, aIncumbentGlobal)
  33. {
  34. }
  35. JS::Handle<JSObject*> Callable() const
  36. {
  37. return Callback();
  38. }
  39. JS::Handle<JSObject*> CallablePreserveColor() const
  40. {
  41. return CallbackPreserveColor();
  42. }
  43. bool HasGrayCallable() const
  44. {
  45. // Play it safe in case this gets called after unlink.
  46. return mCallback && JS::ObjectIsMarkedGray(mCallback);
  47. }
  48. protected:
  49. explicit CallbackFunction(CallbackFunction* aCallbackFunction)
  50. : CallbackObject(aCallbackFunction)
  51. {
  52. }
  53. // See CallbackObject for an explanation of the arguments.
  54. CallbackFunction(JSContext* aCx, JS::Handle<JSObject*> aCallable,
  55. nsIGlobalObject* aIncumbentGlobal,
  56. const FastCallbackConstructor&)
  57. : CallbackObject(aCx, aCallable, aIncumbentGlobal,
  58. FastCallbackConstructor())
  59. {
  60. }
  61. };
  62. } // namespace dom
  63. } // namespace mozilla
  64. #endif // mozilla_dom_CallbackFunction_h