JavaScriptChild.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #include "JavaScriptChild.h"
  7. #include "mozilla/dom/ContentChild.h"
  8. #include "mozilla/dom/BindingUtils.h"
  9. #include "mozilla/ipc/MessageChannel.h"
  10. #include "nsContentUtils.h"
  11. #include "xpcprivate.h"
  12. #include "jsfriendapi.h"
  13. #include "AccessCheck.h"
  14. using namespace JS;
  15. using namespace mozilla;
  16. using namespace mozilla::jsipc;
  17. using mozilla::AutoSafeJSContext;
  18. static void
  19. UpdateChildWeakPointersBeforeSweepingZoneGroup(JSContext* cx, void* data)
  20. {
  21. static_cast<JavaScriptChild*>(data)->updateWeakPointers();
  22. }
  23. static void
  24. TraceChild(JSTracer* trc, void* data)
  25. {
  26. static_cast<JavaScriptChild*>(data)->trace(trc);
  27. }
  28. JavaScriptChild::~JavaScriptChild()
  29. {
  30. JSContext* cx = dom::danger::GetJSContext();
  31. JS_RemoveWeakPointerZoneGroupCallback(cx, UpdateChildWeakPointersBeforeSweepingZoneGroup);
  32. JS_RemoveExtraGCRootsTracer(cx, TraceChild, this);
  33. }
  34. bool
  35. JavaScriptChild::init()
  36. {
  37. if (!WrapperOwner::init())
  38. return false;
  39. if (!WrapperAnswer::init())
  40. return false;
  41. JSContext* cx = dom::danger::GetJSContext();
  42. JS_AddWeakPointerZoneGroupCallback(cx, UpdateChildWeakPointersBeforeSweepingZoneGroup, this);
  43. JS_AddExtraGCRootsTracer(cx, TraceChild, this);
  44. return true;
  45. }
  46. void
  47. JavaScriptChild::trace(JSTracer* trc)
  48. {
  49. objects_.trace(trc, strongReferenceObjIdMinimum_);
  50. }
  51. void
  52. JavaScriptChild::updateWeakPointers()
  53. {
  54. objects_.sweep();
  55. unwaivedObjectIds_.sweep();
  56. waivedObjectIds_.sweep();
  57. }
  58. JSObject*
  59. JavaScriptChild::scopeForTargetObjects()
  60. {
  61. // CPOWs from the parent need to point into the child's privileged junk
  62. // scope so that they can benefit from XrayWrappers in the child.
  63. return xpc::PrivilegedJunkScope();
  64. }
  65. bool
  66. JavaScriptChild::RecvDropTemporaryStrongReferences(const uint64_t& upToObjId)
  67. {
  68. strongReferenceObjIdMinimum_ = upToObjId + 1;
  69. return true;
  70. }
  71. PJavaScriptChild*
  72. mozilla::jsipc::NewJavaScriptChild()
  73. {
  74. JavaScriptChild* child = new JavaScriptChild();
  75. if (!child->init()) {
  76. delete child;
  77. return nullptr;
  78. }
  79. return child;
  80. }
  81. void
  82. mozilla::jsipc::ReleaseJavaScriptChild(PJavaScriptChild* child)
  83. {
  84. static_cast<JavaScriptChild*>(child)->decref();
  85. }