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: */
|
2013-11-19 23:14:07 +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 "ServiceWorker.h"
|
|
|
|
|
2015-07-02 12:54:00 +00:00
|
|
|
#include "nsIDocument.h"
|
2013-11-19 23:14:07 +00:00
|
|
|
#include "nsPIDOMWindow.h"
|
2015-03-16 20:23:42 +00:00
|
|
|
#include "ServiceWorkerClient.h"
|
2015-02-19 16:40:21 +00:00
|
|
|
#include "ServiceWorkerManager.h"
|
2015-09-30 23:11:03 +00:00
|
|
|
#include "ServiceWorkerPrivate.h"
|
2013-11-19 23:14:07 +00:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
2014-11-06 15:57:57 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-11-19 23:14:07 +00:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2014-11-06 15:57:57 +00:00
|
|
|
#include "mozilla/dom/ServiceWorkerGlobalScopeBinding.h"
|
2013-11-19 23:14:07 +00:00
|
|
|
|
2015-02-09 17:42:00 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#undef PostMessage
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using mozilla::ErrorResult;
|
2013-11-19 23:14:07 +00:00
|
|
|
using namespace mozilla::dom;
|
2014-11-06 15:57:57 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace workers {
|
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceWorkerVisible(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return Preferences::GetBool("dom.serviceWorkers.enabled", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorkerGlobalScope* scope = nullptr;
|
|
|
|
nsresult rv = UnwrapObject<prototypes::id::ServiceWorkerGlobalScope_workers,
|
|
|
|
mozilla::dom::ServiceWorkerGlobalScopeBinding_workers::NativeType>(aObj, scope);
|
|
|
|
return NS_SUCCEEDED(rv);
|
|
|
|
}
|
2013-11-19 23:14:07 +00:00
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
ServiceWorker::ServiceWorker(nsPIDOMWindowInner* aWindow,
|
2015-09-30 23:11:03 +00:00
|
|
|
ServiceWorkerInfo* aInfo)
|
2013-11-19 23:14:07 +00:00
|
|
|
: DOMEventTargetHelper(aWindow),
|
2015-09-30 23:11:03 +00:00
|
|
|
mInfo(aInfo)
|
2013-11-19 23:14:07 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-02-19 16:40:21 +00:00
|
|
|
MOZ_ASSERT(aInfo);
|
|
|
|
|
|
|
|
// This will update our state too.
|
|
|
|
mInfo->AppendWorker(this);
|
2013-11-19 23:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorker::~ServiceWorker()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-02-19 16:40:21 +00:00
|
|
|
mInfo->RemoveWorker(this);
|
2013-11-19 23:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ServiceWorker, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ServiceWorker, DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ServiceWorker)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
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
|
|
|
ServiceWorker::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-11-19 23:14:07 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
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 ServiceWorkerBinding::Wrap(aCx, this, aGivenProto);
|
2013-11-19 23:14:07 +00:00
|
|
|
}
|
2013-11-19 23:15:02 +00:00
|
|
|
|
2015-02-19 16:40:21 +00:00
|
|
|
void
|
|
|
|
ServiceWorker::GetScriptURL(nsString& aURL) const
|
|
|
|
{
|
|
|
|
CopyUTF8toUTF16(mInfo->ScriptSpec(), aURL);
|
|
|
|
}
|
|
|
|
|
2015-02-09 17:42:00 +00:00
|
|
|
void
|
|
|
|
ServiceWorker::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2015-08-14 13:00:59 +00:00
|
|
|
if (State() == ServiceWorkerState::Redundant) {
|
2015-08-13 20:35:10 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(GetParentObject());
|
2015-08-17 17:38:34 +00:00
|
|
|
if (!window || !window->GetExtantDoc()) {
|
|
|
|
NS_WARNING("Trying to call post message from an invalid dom object.");
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-30 23:11:03 +00:00
|
|
|
UniquePtr<ServiceWorkerClientInfo> clientInfo(new ServiceWorkerClientInfo(window->GetExtantDoc()));
|
|
|
|
ServiceWorkerPrivate* workerPrivate = mInfo->WorkerPrivate();
|
|
|
|
aRv = workerPrivate->SendMessageEvent(aCx, aMessage, aTransferable, Move(clientInfo));
|
2013-11-19 23:15:02 +00:00
|
|
|
}
|
2014-11-06 15:57:57 +00:00
|
|
|
|
|
|
|
} // namespace workers
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|