Bug 1131327 - Patch 3 - move event listeners to main thread class. r=baku

This commit is contained in:
Nikhil Marathe 2015-04-07 13:09:07 -07:00
parent dc1c6a0c24
commit 565637850b
2 changed files with 55 additions and 58 deletions

View File

@ -59,51 +59,7 @@ ServiceWorkerRegistrationBase::ServiceWorkerRegistrationBase(nsPIDOMWindow* aWin
const nsAString& aScope)
: DOMEventTargetHelper(aWindow)
, mScope(aScope)
, mListeningForEvents(false)
{
MOZ_ASSERT(aWindow);
MOZ_ASSERT(aWindow->IsInnerWindow());
StartListeningForEvents();
}
ServiceWorkerRegistrationBase::~ServiceWorkerRegistrationBase()
{
StopListeningForEvents();
}
void
ServiceWorkerRegistrationBase::DisconnectFromOwner()
{
StopListeningForEvents();
DOMEventTargetHelper::DisconnectFromOwner();
}
// XXXnsm, maybe this can be optimized to only add when a event handler is
// registered.
void
ServiceWorkerRegistrationBase::StartListeningForEvents()
{
nsCOMPtr<nsIServiceWorkerManager> swm = do_GetService(SERVICEWORKERMANAGER_CONTRACTID);
if (swm) {
swm->AddRegistrationEventListener(mScope, this);
mListeningForEvents = true;
}
}
void
ServiceWorkerRegistrationBase::StopListeningForEvents()
{
if (!mListeningForEvents) {
return;
}
nsCOMPtr<nsIServiceWorkerManager> swm = do_GetService(SERVICEWORKERMANAGER_CONTRACTID);
if (swm) {
swm->RemoveRegistrationEventListener(mScope, this);
mListeningForEvents = false;
}
}
{}
////////////////////////////////////////////////////
// Main Thread implementation
@ -122,13 +78,21 @@ NS_IMPL_CYCLE_COLLECTION_INHERITED(ServiceWorkerRegistrationMainThread, ServiceW
ServiceWorkerRegistrationMainThread::ServiceWorkerRegistrationMainThread(nsPIDOMWindow* aWindow,
const nsAString& aScope)
: ServiceWorkerRegistrationBase(aWindow, aScope)
, mListeningForEvents(false)
{
AssertIsOnMainThread();
MOZ_ASSERT(aWindow);
MOZ_ASSERT(aWindow->IsInnerWindow());
StartListeningForEvents();
}
ServiceWorkerRegistrationMainThread::~ServiceWorkerRegistrationMainThread()
{
StopListeningForEvents();
MOZ_ASSERT(!mListeningForEvents);
}
already_AddRefed<workers::ServiceWorker>
ServiceWorkerRegistrationMainThread::GetWorkerReference(WhichServiceWorker aWhichOne)
{
@ -170,6 +134,35 @@ ServiceWorkerRegistrationMainThread::GetWorkerReference(WhichServiceWorker aWhic
return ref.forget();
}
// XXXnsm, maybe this can be optimized to only add when a event handler is
// registered.
void
ServiceWorkerRegistrationMainThread::StartListeningForEvents()
{
AssertIsOnMainThread();
MOZ_ASSERT(!mListeningForEvents);
nsCOMPtr<nsIServiceWorkerManager> swm = do_GetService(SERVICEWORKERMANAGER_CONTRACTID);
if (swm) {
swm->AddRegistrationEventListener(mScope, this);
mListeningForEvents = true;
}
}
void
ServiceWorkerRegistrationMainThread::StopListeningForEvents()
{
AssertIsOnMainThread();
if (!mListeningForEvents) {
return;
}
nsCOMPtr<nsIServiceWorkerManager> swm = do_GetService(SERVICEWORKERMANAGER_CONTRACTID);
if (swm) {
swm->RemoveRegistrationEventListener(mScope, this);
}
mListeningForEvents = false;
}
JSObject*
ServiceWorkerRegistrationMainThread::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
@ -415,7 +408,6 @@ NS_IMPL_CYCLE_COLLECTION_INHERITED(ServiceWorkerRegistrationWorkerThread,
JSObject*
ServiceWorkerRegistrationWorkerThread::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
AssertIsOnMainThread();
return ServiceWorkerRegistrationBinding_workers::Wrap(aCx, this, aGivenProto);
}

View File

@ -75,22 +75,12 @@ public:
virtual void
InvalidateWorkerReference(WhichServiceWorker aWhichOnes) = 0;
// DOMEventTargethelper
virtual void DisconnectFromOwner() override;
protected:
virtual ~ServiceWorkerRegistrationBase();
virtual ~ServiceWorkerRegistrationBase()
{ }
const nsString mScope;
private:
void
StartListeningForEvents();
void
StopListeningForEvents();
bool mListeningForEvents;
nsCOMPtr<nsISupports> mCCDummy;
};
@ -128,12 +118,27 @@ public:
void
InvalidateWorkerReference(WhichServiceWorker aWhichOnes) override;
// DOMEventTargethelper
void DisconnectFromOwner() override
{
StopListeningForEvents();
ServiceWorkerRegistrationBase::DisconnectFromOwner();
}
private:
~ServiceWorkerRegistrationMainThread();
already_AddRefed<workers::ServiceWorker>
GetWorkerReference(WhichServiceWorker aWhichOne);
void
StartListeningForEvents();
void
StopListeningForEvents();
bool mListeningForEvents;
// The following properties are cached here to ensure JS equality is satisfied
// instead of acquiring a new worker instance from the ServiceWorkerManager
// for every access. A null value is considered a cache miss.