gecko-dev/toolkit/components/extensions/webidl-api/ExtensionPort.cpp
Luca Greco 34daf589ca Bug 1682632 - part2.4: ExtensionPort. r=baku
The ExtensionPort represents the webidl definitions of the runtime.Port WebExtensions API object.

A webidl dictionary will represent the serializable definition of the port properties, sent from
the API request handler to provide all the internal properties needed to create an instance
of the ExtensionPort webidl interface on the worker thread (which will be returned to the
extension code as the return value of the runtime.connect/connectNative methods and as a
parameter of the calls to the runtime.onConnect API event listeners).

Depends on D84684

Differential Revision: https://phabricator.services.mozilla.com/D84685
2021-06-11 18:58:09 +00:00

89 lines
2.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 "ExtensionPort.h"
#include "ExtensionEventManager.h"
#include "mozilla/dom/BindingUtils.h" // SequenceRooter
#include "mozilla/dom/ExtensionPortBinding.h"
#include "mozilla/dom/ScriptSettings.h" // AutoEntryScript
#include "nsIGlobalObject.h"
namespace mozilla {
namespace extensions {
NS_IMPL_CYCLE_COLLECTING_ADDREF(ExtensionPort);
NS_IMPL_CYCLE_COLLECTING_RELEASE(ExtensionPort)
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(ExtensionPort, mGlobal,
mOnDisconnectEventMgr,
mOnMessageEventMgr);
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ExtensionPort)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
// static
already_AddRefed<ExtensionPort> ExtensionPort::Create(
nsIGlobalObject* aGlobal, JS::Handle<JS::Value> aDescriptorValue,
ErrorResult& aRv) {
if (!aDescriptorValue.isObject()) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
dom::AutoEntryScript aes(&aDescriptorValue.toObject(), __func__);
JSContext* acx = aes.cx();
auto portDescriptor = MakeUnique<dom::ExtensionPortDescriptor>();
if (!portDescriptor->Init(acx, aDescriptorValue, __func__)) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
RefPtr<ExtensionPort> port =
new ExtensionPort(aGlobal, std::move(portDescriptor));
return port.forget();
}
ExtensionPort::ExtensionPort(
nsIGlobalObject* aGlobal,
UniquePtr<dom::ExtensionPortDescriptor> aPortDescriptor)
: mGlobal(aGlobal), mPortDescriptor(std::move(aPortDescriptor)) {
MOZ_DIAGNOSTIC_ASSERT(mGlobal);
}
nsString ExtensionPort::GetAPIObjectId() const {
return mPortDescriptor->mPortId;
}
JSObject* ExtensionPort::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return dom::ExtensionPort_Binding::Wrap(aCx, this, aGivenProto);
}
nsIGlobalObject* ExtensionPort::GetParentObject() const { return mGlobal; }
ExtensionEventManager* ExtensionPort::OnMessage() {
if (!mOnMessageEventMgr) {
mOnMessageEventMgr = CreateEventManager(u"onMessage"_ns);
}
return mOnMessageEventMgr;
}
ExtensionEventManager* ExtensionPort::OnDisconnect() {
if (!mOnDisconnectEventMgr) {
mOnDisconnectEventMgr = CreateEventManager(u"onDisconnect"_ns);
}
return mOnDisconnectEventMgr;
}
void ExtensionPort::GetName(nsAString& aString) {
aString.Assign(mPortDescriptor->mName);
}
} // namespace extensions
} // namespace mozilla