summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/webidl-api/ExtensionEventManager.cpp
blob: 2a5f9e0978b33d4b4e3e22e80c0eedefe608046d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ExtensionEventManager.h"

#include "ExtensionAPIAddRemoveListener.h"

#include "mozilla/dom/ExtensionEventManagerBinding.h"
#include "nsIGlobalObject.h"
#include "ExtensionEventListener.h"

namespace mozilla {
namespace extensions {

NS_IMPL_CYCLE_COLLECTION_CLASS(ExtensionEventManager);
NS_IMPL_CYCLE_COLLECTING_ADDREF(ExtensionEventManager);
NS_IMPL_CYCLE_COLLECTING_RELEASE(ExtensionEventManager)

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ExtensionEventManager)
  tmp->mListeners.clear();
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mExtensionBrowser)
  NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ExtensionEventManager)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mExtensionBrowser)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ExtensionEventManager)
  for (auto iter = tmp->mListeners.iter(); !iter.done(); iter.next()) {
    aCallbacks.Trace(&iter.get().mutableKey(), "mListeners key", aClosure);
  }
  NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_TRACE_END

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ExtensionEventManager)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

ExtensionEventManager::ExtensionEventManager(
    nsIGlobalObject* aGlobal, ExtensionBrowser* aExtensionBrowser,
    const nsAString& aNamespace, const nsAString& aEventName,
    const nsAString& aObjectType, const nsAString& aObjectId)
    : mGlobal(aGlobal),
      mExtensionBrowser(aExtensionBrowser),
      mAPINamespace(aNamespace),
      mEventName(aEventName),
      mAPIObjectType(aObjectType),
      mAPIObjectId(aObjectId) {
  MOZ_DIAGNOSTIC_ASSERT(mGlobal);
  MOZ_DIAGNOSTIC_ASSERT(mExtensionBrowser);

  RefPtr<ExtensionEventManager> self = this;
  mozilla::HoldJSObjects(this);
}

ExtensionEventManager::~ExtensionEventManager() {
  ReleaseListeners();
  mozilla::DropJSObjects(this);
};

void ExtensionEventManager::ReleaseListeners() {
  if (mListeners.empty()) {
    return;
  }

  for (auto iter = mListeners.iter(); !iter.done(); iter.next()) {
    iter.get().value()->Cleanup();
  }

  mListeners.clear();
}

JSObject* ExtensionEventManager::WrapObject(JSContext* aCx,
                                            JS::Handle<JSObject*> aGivenProto) {
  return dom::ExtensionEventManager_Binding::Wrap(aCx, this, aGivenProto);
}

nsIGlobalObject* ExtensionEventManager::GetParentObject() const {
  return mGlobal;
}

void ExtensionEventManager::AddListener(
    JSContext* aCx, dom::Function& aCallback,
    const dom::Optional<JS::Handle<JSObject*>>& aOptions, ErrorResult& aRv) {
  JS::Rooted<JSObject*> cb(aCx, aCallback.CallbackOrNull());
  if (cb == nullptr) {
    ThrowUnexpectedError(aCx, aRv);
    return;
  }

  RefPtr<ExtensionEventManager> self = this;

  IgnoredErrorResult rv;
  RefPtr<ExtensionEventListener> wrappedCb = ExtensionEventListener::Create(
      mGlobal, mExtensionBrowser, &aCallback,
      [self = std::move(self)]() { self->ReleaseListeners(); }, rv);

  if (NS_WARN_IF(rv.Failed())) {
    ThrowUnexpectedError(aCx, aRv);
    return;
  }

  RefPtr<ExtensionEventListener> storedWrapper = wrappedCb;
  if (!mListeners.put(cb, std::move(storedWrapper))) {
    ThrowUnexpectedError(aCx, aRv);
    return;
  }

  auto request = SendAddListener(mEventName);
  request->Run(mGlobal, aCx, {}, wrappedCb, aRv);

  if (!aRv.Failed() && mAPIObjectType.IsEmpty()) {
    mExtensionBrowser->TrackWakeupEventListener(aCx, mAPINamespace, mEventName);
  }
}

void ExtensionEventManager::RemoveListener(dom::Function& aCallback,
                                           ErrorResult& aRv) {
  dom::AutoJSAPI jsapi;
  if (NS_WARN_IF(!jsapi.Init(mGlobal))) {
    aRv.Throw(NS_ERROR_UNEXPECTED);
    return;
  }

  JSContext* cx = jsapi.cx();
  JS::Rooted<JSObject*> cb(cx, aCallback.CallbackOrNull());
  const auto& ptr = mListeners.lookup(cb);

  // Return earlier if the listener wasn't found
  if (!ptr) {
    return;
  }

  RefPtr<ExtensionEventListener> wrappedCb = ptr->value();
  auto request = SendRemoveListener(mEventName);
  request->Run(mGlobal, cx, {}, wrappedCb, aRv);

  if (NS_WARN_IF(aRv.Failed())) {
    return;
  }

  if (mAPIObjectType.IsEmpty()) {
    mExtensionBrowser->UntrackWakeupEventListener(cx, mAPINamespace,
                                                  mEventName);
  }

  mListeners.remove(cb);

  wrappedCb->Cleanup();
}

bool ExtensionEventManager::HasListener(dom::Function& aCallback,
                                        ErrorResult& aRv) const {
  return mListeners.has(aCallback.CallbackOrNull());
}

bool ExtensionEventManager::HasListeners(ErrorResult& aRv) const {
  return !mListeners.empty();
}

}  // namespace extensions
}  // namespace mozilla