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-02-09 17:43:00 +00:00
|
|
|
#include "nsGlobalWindow.h"
|
2015-03-06 13:04:49 +00:00
|
|
|
#include "nsIDocument.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-03-13 06:15:25 +00:00
|
|
|
nsresult rv = aDoc->GetId(mClientId);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to get the UUID of the document.");
|
|
|
|
}
|
|
|
|
|
2015-05-20 20:14:49 +00:00
|
|
|
nsRefPtr<nsGlobalWindow> innerWindow = static_cast<nsGlobalWindow*>(aDoc->GetInnerWindow());
|
|
|
|
if (innerWindow) {
|
|
|
|
// XXXcatalinb: The inner window can be null if the document is navigating
|
|
|
|
// and was detached.
|
|
|
|
mWindowId = innerWindow->WindowID();
|
|
|
|
}
|
|
|
|
|
2015-03-06 13:04:49 +00:00
|
|
|
aDoc->GetURL(mUrl);
|
|
|
|
mVisibilityState = aDoc->VisibilityState();
|
|
|
|
|
|
|
|
ErrorResult result;
|
|
|
|
mFocused = aDoc->HasFocus(result);
|
|
|
|
if (result.Failed()) {
|
|
|
|
NS_WARNING("Failed to get focus information.");
|
|
|
|
}
|
|
|
|
|
2015-08-17 17:38:34 +00:00
|
|
|
nsRefPtr<nsGlobalWindow> outerWindow = static_cast<nsGlobalWindow*>(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
|
|
|
|
, public StructuredCloneHelper
|
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-02 16:20:30 +00:00
|
|
|
: StructuredCloneHelper(CloningSupported, TransferringSupported,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<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();
|
|
|
|
|
|
|
|
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-08-27 16:19:13 +00:00
|
|
|
nsRefPtr<MessageEvent> event = new MessageEvent(aTargetContainer,
|
|
|
|
nullptr, nullptr);
|
|
|
|
rv = event->InitMessageEvent(NS_LITERAL_STRING("message"),
|
|
|
|
false /* non-bubbling */,
|
|
|
|
false /* not cancelable */,
|
|
|
|
messageData,
|
|
|
|
EmptyString(),
|
|
|
|
EmptyString(),
|
|
|
|
nullptr);
|
|
|
|
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-16 03:27:56 +00:00
|
|
|
nsTArray<nsRefPtr<MessagePort>> ports = TakeTransferredPorts();
|
2015-08-27 16:19:13 +00:00
|
|
|
|
|
|
|
nsRefPtr<MessagePortList> portList =
|
|
|
|
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-08-27 16:19:13 +00:00
|
|
|
nsRefPtr<ServiceWorkerClientPostMessageRunnable> runnable =
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|