michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "CellBroadcast.h" michael@0: #include "mozilla/dom/MozCellBroadcastBinding.h" michael@0: #include "nsIDOMMozCellBroadcastEvent.h" michael@0: #include "nsIDOMMozCellBroadcastMessage.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "GeneratedEvents.h" michael@0: michael@0: #define NS_RILCONTENTHELPER_CONTRACTID "@mozilla.org/ril/content-helper;1" michael@0: michael@0: using namespace mozilla::dom; michael@0: michael@0: /** michael@0: * CellBroadcast::Listener Implementation. michael@0: */ michael@0: michael@0: class CellBroadcast::Listener : public nsICellBroadcastListener michael@0: { michael@0: private: michael@0: CellBroadcast* mCellBroadcast; michael@0: michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_FORWARD_SAFE_NSICELLBROADCASTLISTENER(mCellBroadcast) michael@0: michael@0: Listener(CellBroadcast* aCellBroadcast) michael@0: : mCellBroadcast(aCellBroadcast) michael@0: { michael@0: MOZ_ASSERT(mCellBroadcast); michael@0: } michael@0: michael@0: void Disconnect() michael@0: { michael@0: MOZ_ASSERT(mCellBroadcast); michael@0: mCellBroadcast = nullptr; michael@0: } michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(CellBroadcast::Listener, nsICellBroadcastListener) michael@0: michael@0: /** michael@0: * CellBroadcast Implementation. michael@0: */ michael@0: michael@0: // static michael@0: already_AddRefed michael@0: CellBroadcast::Create(nsPIDOMWindow* aWindow, ErrorResult& aRv) michael@0: { michael@0: MOZ_ASSERT(aWindow); michael@0: MOZ_ASSERT(aWindow->IsInnerWindow()); michael@0: michael@0: nsCOMPtr provider = michael@0: do_GetService(NS_RILCONTENTHELPER_CONTRACTID); michael@0: if (!provider) { michael@0: aRv.Throw(NS_ERROR_UNEXPECTED); michael@0: return nullptr; michael@0: } michael@0: michael@0: nsRefPtr cb = new CellBroadcast(aWindow, provider); michael@0: return cb.forget(); michael@0: } michael@0: michael@0: CellBroadcast::CellBroadcast(nsPIDOMWindow *aWindow, michael@0: nsICellBroadcastProvider *aProvider) michael@0: : DOMEventTargetHelper(aWindow) michael@0: , mProvider(aProvider) michael@0: { michael@0: mListener = new Listener(this); michael@0: DebugOnly rv = mProvider->RegisterCellBroadcastMsg(mListener); michael@0: NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), michael@0: "Failed registering Cell Broadcast callback with provider"); michael@0: } michael@0: michael@0: CellBroadcast::~CellBroadcast() michael@0: { michael@0: MOZ_ASSERT(mProvider && mListener); michael@0: michael@0: mListener->Disconnect(); michael@0: mProvider->UnregisterCellBroadcastMsg(mListener); michael@0: } michael@0: michael@0: JSObject* michael@0: CellBroadcast::WrapObject(JSContext* aCx) michael@0: { michael@0: return MozCellBroadcastBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: // Forwarded nsICellBroadcastListener methods michael@0: michael@0: NS_IMETHODIMP michael@0: CellBroadcast::NotifyMessageReceived(nsIDOMMozCellBroadcastMessage* aMessage) michael@0: { michael@0: nsCOMPtr event; michael@0: NS_NewDOMMozCellBroadcastEvent(getter_AddRefs(event), this, nullptr, nullptr); michael@0: michael@0: nsCOMPtr ce = do_QueryInterface(event); michael@0: nsresult rv = ce->InitMozCellBroadcastEvent(NS_LITERAL_STRING("received"), michael@0: true, false, aMessage); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return DispatchTrustedEvent(ce); michael@0: }