Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 1408333 Get rid of nsIIPCBackgroundChildCreateCallback - part 1 -…
Browse files Browse the repository at this point in the history
… BroadcastChannel, r=asuth
  • Loading branch information
bakulf committed Oct 24, 2017
1 parent 9bb2a79 commit ffcbfd2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 73 deletions.
88 changes: 23 additions & 65 deletions dom/broadcastchannel/BroadcastChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,6 @@ BroadcastChannel::BroadcastChannel(nsPIDOMWindowInner* aWindow,
const nsAString& aChannel)
: DOMEventTargetHelper(aWindow)
, mWorkerHolder(nullptr)
, mPrincipalInfo(new PrincipalInfo(aPrincipalInfo))
, mOrigin(aOrigin)
, mChannel(aChannel)
, mInnerID(0)
, mState(StateActive)
Expand Down Expand Up @@ -346,13 +344,20 @@ BroadcastChannel::Constructor(const GlobalObject& aGlobal,
new BroadcastChannel(window, principalInfo, origin, aChannel);

// Register this component to PBackground.
PBackgroundChild* actor = BackgroundChild::GetForCurrentThread();
if (actor) {
bc->ActorCreated(actor);
} else {
BackgroundChild::GetOrCreateForCurrentThread(bc);
PBackgroundChild* actorChild = BackgroundChild::GetOrCreateForCurrentThread();
if (NS_WARN_IF(!actorChild)) {
MOZ_CRASH("Failed to create a PBackgroundChild actor!");
}

PBroadcastChannelChild* actor =
actorChild->SendPBroadcastChannelConstructor(principalInfo, origin,
nsString(aChannel));

bc->mActor = static_cast<BroadcastChannelChild*>(actor);
MOZ_ASSERT(bc->mActor);

bc->mActor->SetParent(bc);

if (!workerPrivate) {
MOZ_ASSERT(window);
MOZ_ASSERT(window->IsInnerWindow());
Expand Down Expand Up @@ -407,18 +412,12 @@ BroadcastChannel::PostMessageData(BroadcastChannelMessage* aData)
{
RemoveDocFromBFCache();

if (mActor) {
RefPtr<BCPostMessageRunnable> runnable =
new BCPostMessageRunnable(mActor, aData);

if (NS_FAILED(NS_DispatchToCurrentThread(runnable))) {
NS_WARNING("Failed to dispatch to the current thread!");
}
RefPtr<BCPostMessageRunnable> runnable =
new BCPostMessageRunnable(mActor, aData);

return;
if (NS_FAILED(NS_DispatchToCurrentThread(runnable))) {
NS_WARNING("Failed to dispatch to the current thread!");
}

mPendingMessages.AppendElement(aData);
}

void
Expand All @@ -428,55 +427,15 @@ BroadcastChannel::Close()
return;
}

if (mPendingMessages.IsEmpty()) {
// We cannot call Shutdown() immediatelly because we could have some
// postMessage runnable already dispatched. Instead, we change the state to
// StateClosed and we shutdown the actor asynchrounsly.

mState = StateClosed;
RefPtr<CloseRunnable> runnable = new CloseRunnable(this);

if (NS_FAILED(NS_DispatchToCurrentThread(runnable))) {
NS_WARNING("Failed to dispatch to the current thread!");
}
} else {
MOZ_ASSERT(!mActor);
mState = StateClosing;
}
}

void
BroadcastChannel::ActorFailed()
{
MOZ_CRASH("Failed to create a PBackgroundChild actor!");
}

void
BroadcastChannel::ActorCreated(PBackgroundChild* aActor)
{
MOZ_ASSERT(aActor);

if (mState == StateClosed) {
return;
}

PBroadcastChannelChild* actor =
aActor->SendPBroadcastChannelConstructor(*mPrincipalInfo, mOrigin, mChannel);

mActor = static_cast<BroadcastChannelChild*>(actor);
MOZ_ASSERT(mActor);

mActor->SetParent(this);
// We cannot call Shutdown() immediatelly because we could have some
// postMessage runnable already dispatched. Instead, we change the state to
// StateClosed and we shutdown the actor asynchrounsly.

// Flush pending messages.
for (uint32_t i = 0; i < mPendingMessages.Length(); ++i) {
PostMessageData(mPendingMessages[i]);
}

mPendingMessages.Clear();
mState = StateClosed;
RefPtr<CloseRunnable> runnable = new CloseRunnable(this);

if (mState == StateClosing) {
Shutdown();
if (NS_FAILED(NS_DispatchToCurrentThread(runnable))) {
NS_WARNING("Failed to dispatch to the current thread!");
}
}

Expand Down Expand Up @@ -565,7 +524,6 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BroadcastChannel,
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BroadcastChannel)
NS_INTERFACE_MAP_ENTRY(nsIIPCBackgroundChildCreateCallback)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)

Expand Down
8 changes: 0 additions & 8 deletions dom/broadcastchannel/BroadcastChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "mozilla/Attributes.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "nsAutoPtr.h"
#include "nsIIPCBackgroundChildCreateCallback.h"
#include "nsIObserver.h"
#include "nsTArray.h"
#include "mozilla/RefPtr.h"
Expand All @@ -34,12 +33,10 @@ class BroadcastChannelMessage;

class BroadcastChannel final
: public DOMEventTargetHelper
, public nsIIPCBackgroundChildCreateCallback
, public nsIObserver
{
friend class BroadcastChannelChild;

NS_DECL_NSIIPCBACKGROUNDCHILDCREATECALLBACK
NS_DECL_NSIOBSERVER

typedef mozilla::ipc::PrincipalInfo PrincipalInfo;
Expand Down Expand Up @@ -88,20 +85,15 @@ class BroadcastChannel final
void RemoveDocFromBFCache();

RefPtr<BroadcastChannelChild> mActor;
nsTArray<RefPtr<BroadcastChannelMessage>> mPendingMessages;

nsAutoPtr<workers::WorkerHolder> mWorkerHolder;

nsAutoPtr<PrincipalInfo> mPrincipalInfo;

nsCString mOrigin;
nsString mChannel;

uint64_t mInnerID;

enum {
StateActive,
StateClosing,
StateClosed
} mState;
};
Expand Down

0 comments on commit ffcbfd2

Please sign in to comment.