2015-05-03 22:32:37 +03: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 14:03:00 +03: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 22:38:37 +03:00
|
|
|
#include "ServiceWorkerContainer.h"
|
2014-10-27 14:03:00 +03:00
|
|
|
|
2015-02-09 20:43:00 +03:00
|
|
|
#include "mozilla/dom/MessageEvent.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "mozilla/dom/Navigator.h"
|
2015-09-22 11:56:48 +03:00
|
|
|
#include "mozilla/dom/ServiceWorkerMessageEvent.h"
|
|
|
|
#include "mozilla/dom/ServiceWorkerMessageEventBinding.h"
|
2015-02-09 20:43:00 +03:00
|
|
|
#include "nsGlobalWindow.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
#include "nsIBrowserDOMWindow.h"
|
2015-03-06 16:04:49 +03:00
|
|
|
#include "nsIDocument.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
#include "ServiceWorker.h"
|
|
|
|
#include "ServiceWorkerPrivate.h"
|
2015-02-09 20:43:00 +03:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2014-10-27 14:03:00 +03: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 20:38:34 +03:00
|
|
|
ServiceWorkerClientInfo::ServiceWorkerClientInfo(nsIDocument* aDoc)
|
2015-05-20 23:14:49 +03:00
|
|
|
: mWindowId(0)
|
2016-04-22 00:14:45 +03:00
|
|
|
, mFrameType(FrameType::None)
|
2015-03-06 16:04:49 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDoc);
|
2015-11-26 02:03:59 +03:00
|
|
|
nsresult rv = aDoc->GetOrCreateId(mClientId);
|
2015-03-13 09:15:25 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to get the UUID of the document.");
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
RefPtr<nsGlobalWindow> innerWindow = nsGlobalWindow::Cast(aDoc->GetInnerWindow());
|
2015-05-20 23:14:49 +03:00
|
|
|
if (innerWindow) {
|
|
|
|
// XXXcatalinb: The inner window can be null if the document is navigating
|
|
|
|
// and was detached.
|
|
|
|
mWindowId = innerWindow->WindowID();
|
|
|
|
}
|
|
|
|
|
2016-01-07 23:48:56 +03:00
|
|
|
nsCOMPtr<nsIURI> originalURI = aDoc->GetOriginalURI();
|
|
|
|
if (originalURI) {
|
|
|
|
nsAutoCString spec;
|
|
|
|
originalURI->GetSpec(spec);
|
|
|
|
CopyUTF8toUTF16(spec, mUrl);
|
|
|
|
}
|
2015-03-06 16:04:49 +03:00
|
|
|
mVisibilityState = aDoc->VisibilityState();
|
|
|
|
|
|
|
|
ErrorResult result;
|
|
|
|
mFocused = aDoc->HasFocus(result);
|
|
|
|
if (result.Failed()) {
|
|
|
|
NS_WARNING("Failed to get focus information.");
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
RefPtr<nsGlobalWindow> outerWindow = nsGlobalWindow::Cast(aDoc->GetWindow());
|
2016-04-22 00:14:45 +03:00
|
|
|
if (!outerWindow) {
|
|
|
|
MOZ_ASSERT(mFrameType == FrameType::None);
|
|
|
|
} else if (!outerWindow->IsTopLevelWindow()) {
|
2015-03-06 16:04:49 +03:00
|
|
|
mFrameType = FrameType::Nested;
|
|
|
|
} else if (outerWindow->HadOriginalOpener()) {
|
|
|
|
mFrameType = FrameType::Auxiliary;
|
|
|
|
} else {
|
|
|
|
mFrameType = FrameType::Top_level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:03:00 +03: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 17:13:33 +03:00
|
|
|
ServiceWorkerClient::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-10-27 14:03:00 +03: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 17:13:33 +03:00
|
|
|
return ClientBinding::Wrap(aCx, this, aGivenProto);
|
2014-10-27 14:03:00 +03:00
|
|
|
}
|
|
|
|
|
2015-02-09 20:43:00 +03:00
|
|
|
namespace {
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
class ServiceWorkerClientPostMessageRunnable final
|
2016-04-26 03:23:21 +03:00
|
|
|
: public Runnable
|
2015-09-30 15:22:08 +03:00
|
|
|
, public StructuredCloneHolder
|
2015-02-09 20:43:00 +03:00
|
|
|
{
|
2015-03-13 09:15:25 +03:00
|
|
|
uint64_t mWindowId;
|
2015-02-09 20:43:00 +03:00
|
|
|
|
|
|
|
public:
|
2015-08-27 19:19:13 +03:00
|
|
|
explicit ServiceWorkerClientPostMessageRunnable(uint64_t aWindowId)
|
2015-09-30 15:22:08 +03:00
|
|
|
: StructuredCloneHolder(CloningSupported, TransferringSupported,
|
2015-09-02 19:20:30 +03:00
|
|
|
SameProcessDifferentThread)
|
2015-08-27 19:19:13 +03:00
|
|
|
, mWindowId(aWindowId)
|
|
|
|
{}
|
2015-02-09 20:43:00 +03:00
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-05-20 23:14:49 +03:00
|
|
|
nsGlobalWindow* window = nsGlobalWindow::GetInnerWindowWithId(mWindowId);
|
2015-02-09 20:43:00 +03:00
|
|
|
if (!window) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-02-25 22:38:37 +03:00
|
|
|
ErrorResult result;
|
|
|
|
dom::Navigator* navigator = window->GetNavigator(result);
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2015-02-25 22:38:37 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ServiceWorkerContainer> container = navigator->ServiceWorker();
|
2015-02-09 20:43:00 +03:00
|
|
|
AutoJSAPI jsapi;
|
2015-06-29 18:50:04 +03:00
|
|
|
if (NS_WARN_IF(!jsapi.Init(window))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-02-09 20:43:00 +03:00
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
|
2015-02-25 22:38:37 +03:00
|
|
|
return DispatchDOMEvent(cx, container);
|
2015-02-09 20:43:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
NS_IMETHOD
|
2015-02-25 22:38:37 +03:00
|
|
|
DispatchDOMEvent(JSContext* aCx, ServiceWorkerContainer* aTargetContainer)
|
2015-02-09 20:43:00 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2016-03-28 17:27:20 +03:00
|
|
|
MOZ_ASSERT(aTargetContainer->GetParentObject(),
|
|
|
|
"How come we don't have a window here?!");
|
|
|
|
|
2015-02-09 20:43:00 +03:00
|
|
|
JS::Rooted<JS::Value> messageData(aCx);
|
2015-08-27 19:19:13 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
Read(aTargetContainer->GetParentObject(), aCx, &messageData, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
xpc::Throw(aCx, rv.StealNSResult());
|
2015-02-09 20:43:00 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-09-22 11:56:48 +03:00
|
|
|
RootedDictionary<ServiceWorkerMessageEventInit> init(aCx);
|
|
|
|
|
2016-03-28 17:27:20 +03: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 11:56:48 +03:00
|
|
|
init.mData = messageData;
|
2016-03-28 17:27:20 +03:00
|
|
|
nsAutoCString origin;
|
|
|
|
if (principal && !isNullPrincipal && !isSystemPrincipal) {
|
|
|
|
principal->GetOrigin(origin);
|
|
|
|
}
|
|
|
|
init.mOrigin.Construct(NS_ConvertUTF8toUTF16(origin));
|
2015-09-22 11:56:48 +03:00
|
|
|
init.mLastEventId.Construct(EmptyString());
|
|
|
|
init.mPorts.Construct();
|
|
|
|
init.mPorts.Value().SetNull();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ServiceWorker> serviceWorker = aTargetContainer->GetController();
|
2015-09-22 11:56:48 +03:00
|
|
|
init.mSource.Construct();
|
|
|
|
if (serviceWorker) {
|
|
|
|
init.mSource.Value().SetValue().SetAsServiceWorker() = serviceWorker;
|
|
|
|
} else {
|
|
|
|
init.mSource.Value().SetNull();
|
2015-02-09 20:43:00 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ServiceWorkerMessageEvent> event =
|
2015-09-22 11:56:48 +03:00
|
|
|
ServiceWorkerMessageEvent::Constructor(aTargetContainer,
|
|
|
|
NS_LITERAL_STRING("message"), init, rv);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<MessagePort>> ports = TakeTransferredPorts();
|
2015-08-27 19:19:13 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessagePortList> portList =
|
2015-08-27 19:19:13 +03:00
|
|
|
new MessagePortList(static_cast<dom::Event*>(event.get()),
|
|
|
|
ports);
|
|
|
|
event->SetPorts(portList);
|
|
|
|
|
2015-02-09 20:43:00 +03:00
|
|
|
event->SetTrusted(true);
|
|
|
|
bool status = false;
|
2015-08-27 19:19:13 +03:00
|
|
|
aTargetContainer->DispatchEvent(static_cast<dom::Event*>(event.get()),
|
|
|
|
&status);
|
2015-02-09 20:43:00 +03:00
|
|
|
|
|
|
|
if (!status) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-02-09 20:43:00 +03: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 08:24:48 +03:00
|
|
|
RefPtr<ServiceWorkerClientPostMessageRunnable> runnable =
|
2015-08-27 19:19:13 +03:00
|
|
|
new ServiceWorkerClientPostMessageRunnable(mWindowId);
|
2015-02-09 20:43:00 +03:00
|
|
|
|
2015-09-02 19:20:30 +03:00
|
|
|
runnable->Write(aCx, aMessage, transferable, aRv);
|
2015-08-27 19:19:13 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-02-09 20:43:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-27 19:19:13 +03:00
|
|
|
aRv = NS_DispatchToMainThread(runnable);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
2015-02-09 20:43:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|