2011-07-17 12:09:13 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2014-02-24 05:57:15 -08:00
|
|
|
#include "DataStore.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2014-02-24 05:57:15 -08:00
|
|
|
#include "mozilla/dom/DataStore.h"
|
|
|
|
#include "mozilla/dom/DataStoreBinding.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "mozilla/dom/PromiseWorkerProxy.h"
|
2013-09-29 21:15:37 -07:00
|
|
|
#include "mozilla/dom/WorkerNavigatorBinding.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2014-02-24 05:57:15 -08:00
|
|
|
#include "Navigator.h"
|
|
|
|
#include "nsProxyRelease.h"
|
2013-06-05 07:04:23 -07:00
|
|
|
#include "RuntimeService.h"
|
|
|
|
|
2014-09-24 09:09:50 -07:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
|
2014-02-24 05:57:15 -08:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "WorkerRunnable.h"
|
|
|
|
#include "WorkerScope.h"
|
|
|
|
|
2013-07-24 00:38:23 -07:00
|
|
|
BEGIN_WORKERS_NAMESPACE
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-09-29 21:15:37 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WorkerNavigator)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WorkerNavigator, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WorkerNavigator, Release)
|
|
|
|
|
2013-07-24 00:38:23 -07:00
|
|
|
/* static */ already_AddRefed<WorkerNavigator>
|
2013-11-19 15:08:50 -08:00
|
|
|
WorkerNavigator::Create(bool aOnLine)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-07-24 00:38:23 -07:00
|
|
|
RuntimeService* rts = RuntimeService::GetService();
|
|
|
|
MOZ_ASSERT(rts);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-11-19 15:08:50 -08:00
|
|
|
const RuntimeService::NavigatorProperties& properties =
|
|
|
|
rts->GetNavigatorProperties();
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-07-24 00:38:23 -07:00
|
|
|
nsRefPtr<WorkerNavigator> navigator =
|
2014-09-23 12:11:18 -07:00
|
|
|
new WorkerNavigator(properties, aOnLine);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-07-24 00:38:23 -07:00
|
|
|
return navigator.forget();
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-09-29 21:15:37 -07:00
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
WorkerNavigator::WrapObject(JSContext* aCx)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return WorkerNavigatorBinding_workers::Wrap(aCx, this);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2014-02-24 05:57:34 -08:00
|
|
|
// A WorkerMainThreadRunnable to synchronously add DataStoreChangeEventProxy on
|
|
|
|
// the main thread. We need this because we have to access |mBackingStore| on
|
|
|
|
// the main thread.
|
|
|
|
class DataStoreAddEventListenerRunnable : public WorkerMainThreadRunnable
|
|
|
|
{
|
|
|
|
nsMainThreadPtrHandle<DataStore> mBackingStore;
|
|
|
|
DataStoreChangeEventProxy* mEventProxy;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
// Add |mEventProxy| as an event listner to DataStore.
|
|
|
|
if (NS_FAILED(mBackingStore->AddEventListener(NS_LITERAL_STRING("change"),
|
|
|
|
mEventProxy,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
2))) {
|
|
|
|
MOZ_ASSERT(false, "failed to add event listener!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
DataStoreAddEventListenerRunnable(
|
|
|
|
WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsMainThreadPtrHandle<DataStore>& aBackingStore,
|
|
|
|
DataStoreChangeEventProxy* aEventProxy)
|
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate)
|
|
|
|
, mBackingStore(aBackingStore)
|
|
|
|
, mEventProxy(aEventProxy)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-24 05:57:15 -08:00
|
|
|
#define WORKER_DATA_STORES_TAG JS_SCTAG_USER_MIN
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
GetDataStoresStructuredCloneCallbacksRead(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
uint32_t aData,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
workerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (aTag != WORKER_DATA_STORES_TAG) {
|
|
|
|
MOZ_ASSERT(false, "aTag must be WORKER_DATA_STORES_TAG!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!aData, "aData should be empty");
|
|
|
|
|
|
|
|
// Read the holder from the buffer, which points to the data store.
|
|
|
|
nsMainThreadPtrHolder<DataStore>* dataStoreholder;
|
|
|
|
if (!JS_ReadBytes(aReader, &dataStoreholder, sizeof(dataStoreholder))) {
|
|
|
|
MOZ_ASSERT(false, "cannot read bytes for dataStoreholder!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-06-25 15:35:36 -07:00
|
|
|
// Protect workerStoreObj from moving GC during ~nsRefPtr.
|
|
|
|
JS::Rooted<JSObject*> workerStoreObj(aCx, nullptr);
|
|
|
|
{
|
|
|
|
nsRefPtr<WorkerDataStore> workerStore =
|
|
|
|
new WorkerDataStore(workerPrivate->GlobalScope());
|
2014-07-29 17:43:56 -07:00
|
|
|
nsMainThreadPtrHandle<DataStore> backingStore(dataStoreholder);
|
2014-06-25 15:35:36 -07:00
|
|
|
|
|
|
|
// When we're on the worker thread, prepare a DataStoreChangeEventProxy.
|
|
|
|
nsRefPtr<DataStoreChangeEventProxy> eventProxy =
|
|
|
|
new DataStoreChangeEventProxy(workerPrivate, workerStore);
|
|
|
|
|
|
|
|
// Add the DataStoreChangeEventProxy as an event listener on the main thread.
|
|
|
|
nsRefPtr<DataStoreAddEventListenerRunnable> runnable =
|
|
|
|
new DataStoreAddEventListenerRunnable(workerPrivate,
|
|
|
|
backingStore,
|
|
|
|
eventProxy);
|
|
|
|
runnable->Dispatch(aCx);
|
|
|
|
|
|
|
|
// Point WorkerDataStore to DataStore.
|
|
|
|
workerStore->SetBackingDataStore(backingStore);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
if (!global) {
|
|
|
|
MOZ_ASSERT(false, "cannot get global!");
|
|
|
|
} else {
|
|
|
|
workerStoreObj = workerStore->WrapObject(aCx);
|
|
|
|
if (!JS_WrapObject(aCx, &workerStoreObj)) {
|
|
|
|
MOZ_ASSERT(false, "cannot wrap object for workerStoreObj!");
|
|
|
|
workerStoreObj = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2014-02-24 05:57:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return workerStoreObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetDataStoresStructuredCloneCallbacksWrite(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
PromiseWorkerProxy* proxy = static_cast<PromiseWorkerProxy*>(aClosure);
|
|
|
|
NS_ASSERTION(proxy, "must have proxy!");
|
|
|
|
|
|
|
|
if (!JS_WriteUint32Pair(aWriter, WORKER_DATA_STORES_TAG, 0)) {
|
|
|
|
MOZ_ASSERT(false, "cannot write pair for WORKER_DATA_STORES_TAG!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> storeObj(aCx, aObj);
|
|
|
|
|
|
|
|
DataStore* store = nullptr;
|
|
|
|
nsresult rv = UNWRAP_OBJECT(DataStore, storeObj, store);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_ASSERT(false, "cannot unwrap the DataStore object!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We keep the data store alive here.
|
|
|
|
proxy->StoreISupports(store);
|
|
|
|
|
|
|
|
// Construct the nsMainThreadPtrHolder pointing to the data store.
|
|
|
|
nsMainThreadPtrHolder<DataStore>* dataStoreholder =
|
|
|
|
new nsMainThreadPtrHolder<DataStore>(store);
|
|
|
|
|
|
|
|
// And write the dataStoreholder into the buffer.
|
|
|
|
if (!JS_WriteBytes(aWriter, &dataStoreholder, sizeof(dataStoreholder))) {
|
|
|
|
MOZ_ASSERT(false, "cannot write bytes for dataStoreholder!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSStructuredCloneCallbacks kGetDataStoresStructuredCloneCallbacks = {
|
|
|
|
GetDataStoresStructuredCloneCallbacksRead,
|
|
|
|
GetDataStoresStructuredCloneCallbacksWrite,
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
// A WorkerMainThreadRunnable to run WorkerNavigator::GetDataStores(...) on the
|
|
|
|
// main thread.
|
|
|
|
class NavigatorGetDataStoresRunnable MOZ_FINAL : public WorkerMainThreadRunnable
|
|
|
|
{
|
|
|
|
nsRefPtr<PromiseWorkerProxy> mPromiseWorkerProxy;
|
|
|
|
const nsString mName;
|
2014-07-16 20:53:23 -07:00
|
|
|
const nsString mOwner;
|
2014-02-24 05:57:15 -08:00
|
|
|
ErrorResult& mRv;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NavigatorGetDataStoresRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
Promise* aWorkerPromise,
|
|
|
|
const nsAString& aName,
|
2014-07-16 20:53:23 -07:00
|
|
|
const nsAString& aOwner,
|
2014-02-24 05:57:15 -08:00
|
|
|
ErrorResult& aRv)
|
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate)
|
|
|
|
, mName(aName)
|
2014-07-16 20:53:23 -07:00
|
|
|
, mOwner(aOwner)
|
2014-02-24 05:57:15 -08:00
|
|
|
, mRv(aRv)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
mPromiseWorkerProxy =
|
|
|
|
new PromiseWorkerProxy(aWorkerPrivate,
|
|
|
|
aWorkerPromise,
|
|
|
|
&kGetDataStoresStructuredCloneCallbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool
|
|
|
|
MainThreadRun() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
// Walk up to the containing window.
|
|
|
|
WorkerPrivate* wp = mWorkerPrivate;
|
|
|
|
while (wp->GetParent()) {
|
|
|
|
wp = wp->GetParent();
|
|
|
|
}
|
|
|
|
nsPIDOMWindow* window = wp->GetWindow();
|
|
|
|
|
|
|
|
// TODO SharedWorker has null window. Don't need to worry about at this
|
|
|
|
// point, though.
|
|
|
|
if (!window) {
|
|
|
|
mRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-16 20:53:23 -07:00
|
|
|
nsRefPtr<Promise> promise =
|
|
|
|
Navigator::GetDataStores(window, mName, mOwner, mRv);
|
2014-02-24 05:57:15 -08:00
|
|
|
promise->AppendNativeHandler(mPromiseWorkerProxy);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
WorkerNavigator::GetDataStores(JSContext* aCx,
|
|
|
|
const nsAString& aName,
|
2014-07-16 20:53:23 -07:00
|
|
|
const nsAString& aOwner,
|
2014-02-24 05:57:15 -08:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
workerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2014-07-18 18:31:11 -07:00
|
|
|
nsRefPtr<Promise> promise = Promise::Create(workerPrivate->GlobalScope(), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-02-24 05:57:15 -08:00
|
|
|
|
|
|
|
nsRefPtr<NavigatorGetDataStoresRunnable> runnable =
|
2014-07-16 20:53:23 -07:00
|
|
|
new NavigatorGetDataStoresRunnable(workerPrivate, promise, aName, aOwner, aRv);
|
2014-02-24 05:57:15 -08:00
|
|
|
runnable->Dispatch(aCx);
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2014-09-05 07:26:34 -07:00
|
|
|
void
|
|
|
|
WorkerNavigator::SetLanguages(const nsTArray<nsString>& aLanguages)
|
|
|
|
{
|
|
|
|
WorkerNavigatorBinding_workers::ClearCachedLanguagesValue(this);
|
2014-09-23 12:11:18 -07:00
|
|
|
mProperties.mLanguages = aLanguages;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerNavigator::GetAppName(nsString& aAppName) const
|
|
|
|
{
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
|
|
|
if (!mProperties.mAppNameOverridden.IsEmpty() &&
|
|
|
|
!workerPrivate->UsesSystemPrincipal()) {
|
|
|
|
aAppName = mProperties.mAppNameOverridden;
|
|
|
|
} else {
|
|
|
|
aAppName = mProperties.mAppName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerNavigator::GetAppVersion(nsString& aAppVersion) const
|
|
|
|
{
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
|
|
|
if (!mProperties.mAppVersionOverridden.IsEmpty() &&
|
|
|
|
!workerPrivate->UsesSystemPrincipal()) {
|
|
|
|
aAppVersion = mProperties.mAppVersionOverridden;
|
|
|
|
} else {
|
|
|
|
aAppVersion = mProperties.mAppVersion;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerNavigator::GetPlatform(nsString& aPlatform) const
|
|
|
|
{
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
|
|
|
if (!mProperties.mPlatformOverridden.IsEmpty() &&
|
|
|
|
!workerPrivate->UsesSystemPrincipal()) {
|
|
|
|
aPlatform = mProperties.mPlatformOverridden;
|
|
|
|
} else {
|
|
|
|
aPlatform = mProperties.mPlatform;
|
|
|
|
}
|
2014-09-05 07:26:34 -07:00
|
|
|
}
|
|
|
|
|
2014-09-24 09:09:50 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class GetUserAgentRunnable MOZ_FINAL : public WorkerMainThreadRunnable
|
|
|
|
{
|
|
|
|
nsString& mUA;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GetUserAgentRunnable(WorkerPrivate* aWorkerPrivate, nsString& aUA)
|
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate)
|
|
|
|
, mUA(aUA)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool MainThreadRun() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = mWorkerPrivate->GetWindow();
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
if (window && window->GetDocShell()) {
|
|
|
|
nsIDocument* doc = window->GetExtantDoc();
|
|
|
|
if (doc) {
|
|
|
|
doc->NodePrincipal()->GetURI(getter_AddRefs(uri));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isCallerChrome = mWorkerPrivate->UsesSystemPrincipal();
|
|
|
|
nsresult rv = dom::Navigator::GetUserAgent(window, uri,
|
|
|
|
isCallerChrome, mUA);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to retrieve user-agent from the worker thread.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerNavigator::GetUserAgent(nsString& aUserAgent) const
|
|
|
|
{
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
|
|
|
nsRefPtr<GetUserAgentRunnable> runnable =
|
|
|
|
new GetUserAgentRunnable(workerPrivate, aUserAgent);
|
|
|
|
|
|
|
|
if (!runnable->Dispatch(workerPrivate->GetJSContext())) {
|
|
|
|
JS_ReportPendingException(workerPrivate->GetJSContext());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
END_WORKERS_NAMESPACE
|