Bug 1345943 P1 Set the MessageEvent.source to ServiceWorker that called Client.postMessage(). r=asuth

This commit is contained in:
Ben Kelly 2017-03-10 12:15:07 -05:00
Родитель 767a13c3cf
Коммит 12ef9421b4
3 изменённых файлов: 51 добавлений и 8 удалений

Просмотреть файл

@ -129,12 +129,18 @@ class ServiceWorkerClientPostMessageRunnable final
: public Runnable
, public StructuredCloneHolder
{
uint64_t mWindowId;
const uint64_t mSourceID;
const nsCString mSourceScope;
const uint64_t mWindowId;
public:
explicit ServiceWorkerClientPostMessageRunnable(uint64_t aWindowId)
ServiceWorkerClientPostMessageRunnable(uint64_t aSourceID,
const nsACString& aSourceScope,
uint64_t aWindowId)
: StructuredCloneHolder(CloningSupported, TransferringSupported,
StructuredCloneScope::SameProcessDifferentThread)
, mSourceID(aSourceID)
, mSourceScope(aSourceScope)
, mWindowId(aWindowId)
{}
@ -160,12 +166,13 @@ public:
}
JSContext* cx = jsapi.cx();
return DispatchDOMEvent(cx, container);
return DispatchDOMEvent(cx, window->AsInner(), container);
}
private:
NS_IMETHOD
DispatchDOMEvent(JSContext* aCx, ServiceWorkerContainer* aTargetContainer)
DispatchDOMEvent(JSContext* aCx, nsPIDOMWindowInner* aWindow,
ServiceWorkerContainer* aTargetContainer)
{
AssertIsOnMainThread();
@ -201,9 +208,18 @@ private:
}
init.mOrigin = NS_ConvertUTF8toUTF16(origin);
RefPtr<ServiceWorker> serviceWorker = aTargetContainer->GetController();
if (serviceWorker) {
init.mSource.SetValue().SetAsServiceWorker() = serviceWorker;
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
if (swm) {
RefPtr<ServiceWorkerRegistrationInfo> reg =
swm->GetRegistration(principal, mSourceScope);
if (reg) {
RefPtr<ServiceWorkerInfo> serviceWorker = reg->GetByID(mSourceID);
if (serviceWorker) {
init.mSource.SetValue().SetAsServiceWorker() =
serviceWorker->GetOrCreateInstance(aWindow);
}
}
}
if (!TakeTransferredPortsAsSequence(init.mPorts)) {
@ -246,8 +262,14 @@ ServiceWorkerClient::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
return;
}
// At the moment we only expose Client on ServiceWorker globals.
MOZ_ASSERT(workerPrivate->IsServiceWorker());
uint32_t serviceWorkerID = workerPrivate->ServiceWorkerID();
nsCString scope = workerPrivate->WorkerName();
RefPtr<ServiceWorkerClientPostMessageRunnable> runnable =
new ServiceWorkerClientPostMessageRunnable(mWindowId);
new ServiceWorkerClientPostMessageRunnable(serviceWorkerID, scope,
mWindowId);
runnable->Write(aCx, aMessage, transferable, JS::CloneDataPolicy().denySharedArrayBuffer(),
aRv);

Просмотреть файл

@ -439,6 +439,24 @@ ServiceWorkerRegistrationInfo::GetActive() const
return mActiveWorker;
}
ServiceWorkerInfo*
ServiceWorkerRegistrationInfo::GetByID(uint64_t aID) const
{
if (mActiveWorker && mActiveWorker->ID() == aID) {
return mActiveWorker;
}
if (mWaitingWorker && mWaitingWorker->ID() == aID) {
return mWaitingWorker;
}
if (mInstallingWorker && mInstallingWorker->ID() == aID) {
return mInstallingWorker;
}
if (mEvaluatingWorker && mEvaluatingWorker->ID() == aID) {
return mEvaluatingWorker;
}
return nullptr;
}
void
ServiceWorkerRegistrationInfo::SetEvaluating(ServiceWorkerInfo* aServiceWorker)
{

Просмотреть файл

@ -134,6 +134,9 @@ public:
ServiceWorkerInfo*
GetActive() const;
ServiceWorkerInfo*
GetByID(uint64_t aID) const;
// Set the given worker as the evaluating service worker. The worker
// state is not changed.
void