2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-10-27 11:03:00 +00:00
|
|
|
/* 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 "ServiceWorkerClient.h"
|
2015-02-25 19:38:37 +00:00
|
|
|
#include "ServiceWorkerContainer.h"
|
2014-10-27 11:03:00 +00:00
|
|
|
|
2015-02-09 17:43:00 +00:00
|
|
|
#include "mozilla/dom/MessageEvent.h"
|
2015-07-02 12:54:00 +00:00
|
|
|
#include "mozilla/dom/Navigator.h"
|
2015-09-22 08:56:48 +00:00
|
|
|
#include "mozilla/dom/ServiceWorkerMessageEvent.h"
|
|
|
|
#include "mozilla/dom/ServiceWorkerMessageEventBinding.h"
|
2015-02-09 17:43:00 +00:00
|
|
|
#include "nsGlobalWindow.h"
|
2016-03-16 18:51:11 +00:00
|
|
|
#include "nsIBrowserDOMWindow.h"
|
2015-03-06 13:04:49 +00:00
|
|
|
#include "nsIDocument.h"
|
2016-03-16 18:51:11 +00:00
|
|
|
#include "ServiceWorker.h"
|
|
|
|
#include "ServiceWorkerPrivate.h"
|
2015-02-09 17:43:00 +00:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2014-10-27 11:03:00 +00:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
using namespace mozilla::dom::workers;
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(ServiceWorkerClient, mOwner)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(ServiceWorkerClient)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(ServiceWorkerClient)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ServiceWorkerClient)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2015-08-17 17:38:34 +00:00
|
|
|
ServiceWorkerClientInfo::ServiceWorkerClientInfo(nsIDocument* aDoc)
|
2015-05-20 20:14:49 +00:00
|
|
|
: mWindowId(0)
|
2015-03-06 13:04:49 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDoc);
|
2015-11-25 23:03:59 +00:00
|
|
|
nsresult rv = aDoc->GetOrCreateId(mClientId);
|
2015-03-13 06:15:25 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to get the UUID of the document.");
|
|
|
|
}
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
RefPtr<nsGlobalWindow> innerWindow = nsGlobalWindow::Cast(aDoc->GetInnerWindow());
|
2015-05-20 20:14:49 +00:00
|
|
|
if (innerWindow) {
|
|
|
|
// XXXcatalinb: The inner window can be null if the document is navigating
|
|
|
|
// and was detached.
|
|
|
|
mWindowId = innerWindow->WindowID();
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:48:56 +00:00
|
|
|
nsCOMPtr<nsIURI> originalURI = aDoc->GetOriginalURI();
|
|
|
|
if (originalURI) {
|
|
|
|
nsAutoCString spec;
|
|
|
|
originalURI->GetSpec(spec);
|
|
|
|
CopyUTF8toUTF16(spec, mUrl);
|
|
|
|
}
|
2015-03-06 13:04:49 +00:00
|
|
|
mVisibilityState = aDoc->VisibilityState();
|
|
|
|
|
|
|
|
ErrorResult result;
|
|
|
|
mFocused = aDoc->HasFocus(result);
|
|
|
|
if (result.Failed()) {
|
|
|
|
NS_WARNING("Failed to get focus information.");
|
|
|
|
}
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
RefPtr<nsGlobalWindow> outerWindow = nsGlobalWindow::Cast(aDoc->GetWindow());
|
2015-05-20 20:14:49 +00:00
|
|
|
MOZ_ASSERT(outerWindow);
|
2015-03-06 13:04:49 +00:00
|
|
|
if (!outerWindow->IsTopLevelWindow()) {
|
|
|
|
mFrameType = FrameType::Nested;
|
|
|
|
} else if (outerWindow->HadOriginalOpener()) {
|
|
|
|
mFrameType = FrameType::Auxiliary;
|
|
|
|
} else {
|
|
|
|
mFrameType = FrameType::Top_level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 11:03:00 +00:00
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 14:13:33 +00:00
|
|
|
ServiceWorkerClient::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-10-27 11:03:00 +00:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 14:13:33 +00:00
|
|
|
return ClientBinding::Wrap(aCx, this, aGivenProto);
|
2014-10-27 11:03:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 17:43:00 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
class ServiceWorkerClientPostMessageRunnable final
|
|
|
|
: public nsRunnable
|
2015-09-30 12:22:08 +00:00
|
|
|
, public StructuredCloneHolder
|
2015-02-09 17:43:00 +00:00
|
|
|
{
|
2015-03-13 06:15:25 +00:00
|
|
|
uint64_t mWindowId;
|
2015-02-09 17:43:00 +00:00
|
|
|
|
|
|
|
public:
|
2015-08-27 16:19:13 +00:00
|
|
|
explicit ServiceWorkerClientPostMessageRunnable(uint64_t aWindowId)
|
2015-09-30 12:22:08 +00:00
|
|
|
: StructuredCloneHolder(CloningSupported, TransferringSupported,
|
2015-09-02 16:20:30 +00:00
|
|
|
SameProcessDifferentThread)
|
2015-08-27 16:19:13 +00:00
|
|
|
, mWindowId(aWindowId)
|
|
|
|
{}
|
2015-02-09 17:43:00 +00:00
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-05-20 20:14:49 +00:00
|
|
|
nsGlobalWindow* window = nsGlobalWindow::GetInnerWindowWithId(mWindowId);
|
2015-02-09 17:43:00 +00:00
|
|
|
if (!window) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-02-25 19:38:37 +00:00
|
|
|
ErrorResult result;
|
|
|
|
dom::Navigator* navigator = window->GetNavigator(result);
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
2015-04-27 13:18:51 +00:00
|
|
|
return result.StealNSResult();
|
2015-02-25 19:38:37 +00:00
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ServiceWorkerContainer> container = navigator->ServiceWorker();
|
2015-02-09 17:43:00 +00:00
|
|
|
AutoJSAPI jsapi;
|
2015-06-29 15:50:04 +00:00
|
|
|
if (NS_WARN_IF(!jsapi.Init(window))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-02-09 17:43:00 +00:00
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
|
2015-02-25 19:38:37 +00:00
|
|
|
return DispatchDOMEvent(cx, container);
|
2015-02-09 17:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
NS_IMETHOD
|
2015-02-25 19:38:37 +00:00
|
|
|
DispatchDOMEvent(JSContext* aCx, ServiceWorkerContainer* aTargetContainer)
|
2015-02-09 17:43:00 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2016-03-28 14:27:20 +00:00
|
|
|
MOZ_ASSERT(aTargetContainer->GetParentObject(),
|
|
|
|
"How come we don't have a window here?!");
|
|
|
|
|
2015-02-09 17:43:00 +00:00
|
|
|
JS::Rooted<JS::Value> messageData(aCx);
|
2015-08-27 16:19:13 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
Read(aTargetContainer->GetParentObject(), aCx, &messageData, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
xpc::Throw(aCx, rv.StealNSResult());
|
2015-02-09 17:43:00 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-09-22 08:56:48 +00:00
|
|
|
RootedDictionary<ServiceWorkerMessageEventInit> init(aCx);
|
|
|
|
|
2016-03-28 14:27:20 +00:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = aTargetContainer->GetParentObject()->PrincipalOrNull();
|
|
|
|
NS_WARN_IF_FALSE(principal, "Why is the principal null here?");
|
|
|
|
|
|
|
|
bool isNullPrincipal = false;
|
|
|
|
bool isSystemPrincipal = false;
|
|
|
|
if (principal) {
|
|
|
|
principal->GetIsNullPrincipal(&isNullPrincipal);
|
|
|
|
MOZ_ASSERT(!isNullPrincipal);
|
|
|
|
principal->GetIsSystemPrincipal(&isSystemPrincipal);
|
|
|
|
MOZ_ASSERT(!isSystemPrincipal);
|
|
|
|
}
|
|
|
|
|
2015-09-22 08:56:48 +00:00
|
|
|
init.mData = messageData;
|
2016-03-28 14:27:20 +00:00
|
|
|
nsAutoCString origin;
|
|
|
|
if (principal && !isNullPrincipal && !isSystemPrincipal) {
|
|
|
|
principal->GetOrigin(origin);
|
|
|
|
}
|
|
|
|
init.mOrigin.Construct(NS_ConvertUTF8toUTF16(origin));
|
2015-09-22 08:56:48 +00:00
|
|
|
init.mLastEventId.Construct(EmptyString());
|
|
|
|
init.mPorts.Construct();
|
|
|
|
init.mPorts.Value().SetNull();
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ServiceWorker> serviceWorker = aTargetContainer->GetController();
|
2015-09-22 08:56:48 +00:00
|
|
|
init.mSource.Construct();
|
|
|
|
if (serviceWorker) {
|
|
|
|
init.mSource.Value().SetValue().SetAsServiceWorker() = serviceWorker;
|
|
|
|
} else {
|
|
|
|
init.mSource.Value().SetNull();
|
2015-02-09 17:43:00 +00:00
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ServiceWorkerMessageEvent> event =
|
2015-09-22 08:56:48 +00:00
|
|
|
ServiceWorkerMessageEvent::Constructor(aTargetContainer,
|
|
|
|
NS_LITERAL_STRING("message"), init, rv);
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
nsTArray<RefPtr<MessagePort>> ports = TakeTransferredPorts();
|
2015-08-27 16:19:13 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MessagePortList> portList =
|
2015-08-27 16:19:13 +00:00
|
|
|
new MessagePortList(static_cast<dom::Event*>(event.get()),
|
|
|
|
ports);
|
|
|
|
event->SetPorts(portList);
|
|
|
|
|
2015-02-09 17:43:00 +00:00
|
|
|
event->SetTrusted(true);
|
|
|
|
bool status = false;
|
2015-08-27 16:19:13 +00:00
|
|
|
aTargetContainer->DispatchEvent(static_cast<dom::Event*>(event.get()),
|
|
|
|
&status);
|
2015-02-09 17:43:00 +00:00
|
|
|
|
|
|
|
if (!status) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2015-02-09 17:43:00 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerClient::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
workerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());
|
|
|
|
if (aTransferable.WasPassed()) {
|
|
|
|
const Sequence<JS::Value>& realTransferable = aTransferable.Value();
|
|
|
|
|
|
|
|
JS::HandleValueArray elements =
|
|
|
|
JS::HandleValueArray::fromMarkedLocation(realTransferable.Length(),
|
|
|
|
realTransferable.Elements());
|
|
|
|
|
|
|
|
JSObject* array = JS_NewArrayObject(aCx, elements);
|
|
|
|
if (!array) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
transferable.setObject(*array);
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ServiceWorkerClientPostMessageRunnable> runnable =
|
2015-08-27 16:19:13 +00:00
|
|
|
new ServiceWorkerClientPostMessageRunnable(mWindowId);
|
2015-02-09 17:43:00 +00:00
|
|
|
|
2015-09-02 16:20:30 +00:00
|
|
|
runnable->Write(aCx, aMessage, transferable, aRv);
|
2015-08-27 16:19:13 +00:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-02-09 17:43:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:19:13 +00:00
|
|
|
aRv = NS_DispatchToMainThread(runnable);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
2015-02-09 17:43:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|