Files
tubestation/toolkit/components/extensions/webidl-api/ExtensionEventManager.cpp
Luca Greco 0165b6aa79 Bug 1688040 - part9: Implement setting/clearing and reporting browser.runtime.lastError from ChromeCompatCallbackHandler::RejectedCallback. r=baku
This patch does:
- add to ExtensionBrowser two new data members to keep track of value for the browser.runtime.lastError and if it was checked while
  the chrome compatible callback was being executed
- add to ExtensionBrowser 3 new methods to set, get and clear the lastError value
- add a reference to the ExtensionBrowser to all API namespaces and API objects classes, because it has to be then propagated to the
  ChromeCompatCallbackHandler instances that are being attached to the promise result of the async API methods calls if the caller did
  pass the optional callback parameter
- tweak the ChromeCompatCallbackHandler class to set the lastError value before calling the callback in ChromeCompatCallbackHAndler::RejectedCallback
  and then clear it after the call has been completed and report it to the console if it wasn't checked
- change the ExtensionRuntime::GetLastError methhod to restrieve and return the value from the ExtensionBrowser instance

Differential Revision: https://phabricator.services.mozilla.com/D107327
2021-10-06 12:28:23 +00:00

158 lines
4.7 KiB
C++

/* 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 "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);
}
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;
}
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