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-02-24 17:56:54 +04: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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_PromiseWorkerProxy_h
|
|
|
|
#define mozilla_dom_PromiseWorkerProxy_h
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
// Required for Promise::PromiseTaskSync.
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2014-02-24 17:56:54 +04:00
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
2015-09-30 15:22:08 +03:00
|
|
|
#include "mozilla/dom/StructuredCloneHolder.h"
|
2014-02-24 17:56:54 +04:00
|
|
|
#include "mozilla/dom/workers/bindings/WorkerFeature.h"
|
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
|
2015-04-07 16:25:08 +03:00
|
|
|
#include "WorkerRunnable.h"
|
|
|
|
|
2014-02-24 17:56:54 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class Promise;
|
|
|
|
|
|
|
|
namespace workers {
|
|
|
|
class WorkerPrivate;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace workers
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
// A proxy to (eventually) mirror a resolved/rejected Promise's result from the
|
|
|
|
// main thread to a Promise on the worker thread.
|
2014-02-24 17:56:54 +04:00
|
|
|
//
|
|
|
|
// How to use:
|
|
|
|
//
|
|
|
|
// 1. Create a Promise on the worker thread and return it to the content
|
|
|
|
// script:
|
|
|
|
//
|
2015-10-18 08:24:48 +03:00
|
|
|
// RefPtr<Promise> promise = Promise::Create(workerPrivate->GlobalScope(), aRv);
|
2014-07-19 05:31:11 +04:00
|
|
|
// if (aRv.Failed()) {
|
|
|
|
// return nullptr;
|
|
|
|
// }
|
2014-02-24 17:56:54 +04:00
|
|
|
//
|
2015-09-02 20:07:26 +03:00
|
|
|
// 2. Create a PromiseWorkerProxy wrapping the Promise. If this fails, the
|
|
|
|
// worker is shutting down and you should fail the original call. This is
|
|
|
|
// only likely to happen in (Gecko-specific) worker onclose handlers.
|
|
|
|
//
|
2015-10-18 08:24:48 +03:00
|
|
|
// RefPtr<PromiseWorkerProxy> proxy =
|
2015-09-02 20:07:26 +03:00
|
|
|
// PromiseWorkerProxy::Create(workerPrivate, promise);
|
|
|
|
// if (!proxy) {
|
|
|
|
// // You may also reject the Promise with an AbortError or similar.
|
|
|
|
// return nullptr;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// 3. Dispatch a runnable to the main thread, with a reference to the proxy to
|
|
|
|
// perform the main thread operation. PromiseWorkerProxy is thread-safe
|
|
|
|
// refcounted.
|
|
|
|
//
|
|
|
|
// 4. Return the worker thread promise to the JS caller:
|
|
|
|
//
|
|
|
|
// return promise.forget();
|
2014-02-24 17:56:54 +04:00
|
|
|
//
|
2015-09-02 20:07:26 +03:00
|
|
|
// 5. In your main thread runnable Run(), obtain a Promise on
|
2014-02-24 17:56:54 +04:00
|
|
|
// the main thread and call its AppendNativeHandler(PromiseNativeHandler*)
|
|
|
|
// to bind the PromiseWorkerProxy created at #2.
|
|
|
|
//
|
|
|
|
// 4. Then the Promise results returned by ResolvedCallback/RejectedCallback
|
|
|
|
// will be dispatched as a WorkerRunnable to the worker thread to
|
|
|
|
// resolve/reject the Promise created at #1.
|
2014-10-06 22:01:20 +04:00
|
|
|
//
|
|
|
|
// PromiseWorkerProxy can also be used in situations where there is no main
|
|
|
|
// thread Promise, or where special handling is required on the worker thread
|
2015-09-02 20:07:26 +03:00
|
|
|
// for promise resolution. Create a PromiseWorkerProxy as in steps 1 to 3
|
|
|
|
// above. When the main thread is ready to resolve the worker thread promise:
|
|
|
|
//
|
|
|
|
// 1. Acquire the mutex before attempting to access the worker private.
|
|
|
|
//
|
|
|
|
// AssertIsOnMainThread();
|
|
|
|
// MutexAutoLock lock(proxy->Lock());
|
|
|
|
// if (proxy->CleanedUp()) {
|
|
|
|
// // Worker has already shut down, can't access worker private.
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// 2. Dispatch a runnable to the worker. Use GetWorkerPrivate() to acquire the
|
|
|
|
// worker.
|
|
|
|
//
|
2015-10-18 08:24:48 +03:00
|
|
|
// RefPtr<FinishTaskWorkerRunnable> runnable =
|
2015-09-02 20:07:26 +03:00
|
|
|
// new FinishTaskWorkerRunnable(proxy->GetWorkerPrivate(), proxy, result);
|
2016-02-26 23:23:12 +03:00
|
|
|
// if (!r->Dispatch()) {
|
2015-09-02 20:07:26 +03:00
|
|
|
// // Worker is alive but not Running any more, so the Promise can't
|
|
|
|
// // be resolved, give up. The proxy will get Release()d at some
|
|
|
|
// // point.
|
|
|
|
//
|
|
|
|
// // Usually do nothing, but you may want to log the fact.
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// 3. In the WorkerRunnable's WorkerRun() use WorkerPromise() to access the
|
|
|
|
// Promise and resolve/reject it. Then call CleanUp().
|
|
|
|
//
|
|
|
|
// bool
|
|
|
|
// WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
// {
|
|
|
|
// aWorkerPrivate->AssertIsOnWorkerThread();
|
2015-10-18 08:24:48 +03:00
|
|
|
// RefPtr<Promise> promise = mProxy->WorkerPromise();
|
2015-09-02 20:07:26 +03:00
|
|
|
// promise->MaybeResolve(mResult);
|
2016-02-29 22:52:42 +03:00
|
|
|
// mProxy->CleanUp();
|
2015-09-02 20:07:26 +03:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Note: If a PromiseWorkerProxy is not cleaned up by a WorkerRunnable - this
|
|
|
|
// can happen if the main thread Promise is never fulfilled - it will
|
|
|
|
// stay alive till the worker reaches a Canceling state, even if all external
|
|
|
|
// references to it are dropped.
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2015-09-05 12:22:13 +03:00
|
|
|
class PromiseWorkerProxy : public PromiseNativeHandler
|
|
|
|
, public workers::WorkerFeature
|
2015-09-30 15:22:08 +03:00
|
|
|
, public StructuredCloneHolderBase
|
2014-02-24 17:56:54 +04:00
|
|
|
{
|
|
|
|
friend class PromiseWorkerProxyRunnable;
|
|
|
|
|
2015-07-09 09:56:00 +03:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2014-02-24 17:56:54 +04:00
|
|
|
|
|
|
|
public:
|
2015-09-05 12:22:13 +03:00
|
|
|
typedef JSObject* (*ReadCallbackOp)(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
const PromiseWorkerProxy* aProxy,
|
|
|
|
uint32_t aTag,
|
|
|
|
uint32_t aData);
|
|
|
|
typedef bool (*WriteCallbackOp)(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWorker,
|
|
|
|
PromiseWorkerProxy* aProxy,
|
|
|
|
JS::HandleObject aObj);
|
|
|
|
|
|
|
|
struct PromiseWorkerProxyStructuredCloneCallbacks
|
|
|
|
{
|
|
|
|
ReadCallbackOp Read;
|
|
|
|
WriteCallbackOp Write;
|
|
|
|
};
|
|
|
|
|
2014-12-17 17:49:36 +03:00
|
|
|
static already_AddRefed<PromiseWorkerProxy>
|
|
|
|
Create(workers::WorkerPrivate* aWorkerPrivate,
|
|
|
|
Promise* aWorkerPromise,
|
2015-09-05 12:22:13 +03:00
|
|
|
const PromiseWorkerProxyStructuredCloneCallbacks* aCallbacks = nullptr);
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
// Main thread callers must hold Lock() and check CleanUp() before calling this.
|
|
|
|
// Worker thread callers, this will assert that the proxy has not been cleaned
|
|
|
|
// up.
|
2014-02-24 17:56:54 +04:00
|
|
|
workers::WorkerPrivate* GetWorkerPrivate() const;
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
// This should only be used within WorkerRunnable::WorkerRun() running on the
|
|
|
|
// worker thread! Do not call this after calling CleanUp().
|
|
|
|
Promise* WorkerPromise() const;
|
2014-02-24 17:56:54 +04:00
|
|
|
|
|
|
|
void StoreISupports(nsISupports* aSupports);
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
// Worker thread only. Calling this invalidates several assumptions, so be
|
|
|
|
// sure this is the last thing you do.
|
|
|
|
// 1. WorkerPrivate() will no longer return a valid worker.
|
|
|
|
// 2. WorkerPromise() will crash!
|
2016-02-29 22:52:42 +03:00
|
|
|
void CleanUp();
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
Mutex& Lock()
|
2015-04-07 16:25:08 +03:00
|
|
|
{
|
|
|
|
return mCleanUpLock;
|
|
|
|
}
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
bool CleanedUp() const
|
2015-04-07 16:25:08 +03:00
|
|
|
{
|
|
|
|
mCleanUpLock.AssertCurrentThreadOwns();
|
|
|
|
return mCleanedUp;
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
// StructuredCloneHolderBase
|
2015-09-05 12:22:13 +03:00
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
JSObject* CustomReadHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
uint32_t aIndex) override;
|
2015-09-05 12:22:13 +03:00
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
bool CustomWriteHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj) override;
|
2015-09-05 12:22:13 +03:00
|
|
|
|
2014-02-24 17:56:54 +04:00
|
|
|
protected:
|
|
|
|
virtual void ResolvedCallback(JSContext* aCx,
|
2015-03-21 19:28:04 +03:00
|
|
|
JS::Handle<JS::Value> aValue) override;
|
2014-02-24 17:56:54 +04:00
|
|
|
|
|
|
|
virtual void RejectedCallback(JSContext* aCx,
|
2015-03-21 19:28:04 +03:00
|
|
|
JS::Handle<JS::Value> aValue) override;
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool Notify(JSContext* aCx, workers::Status aStatus) override;
|
2014-02-24 17:56:54 +04:00
|
|
|
|
|
|
|
private:
|
2014-12-17 17:49:36 +03:00
|
|
|
PromiseWorkerProxy(workers::WorkerPrivate* aWorkerPrivate,
|
|
|
|
Promise* aWorkerPromise,
|
2015-09-05 12:22:13 +03:00
|
|
|
const PromiseWorkerProxyStructuredCloneCallbacks* aCallbacks = nullptr);
|
2014-12-17 17:49:36 +03:00
|
|
|
|
2014-02-24 17:56:54 +04:00
|
|
|
virtual ~PromiseWorkerProxy();
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
bool AddRefObject();
|
|
|
|
|
|
|
|
// If not called from Create(), be sure to hold Lock().
|
|
|
|
void CleanProperties();
|
|
|
|
|
2014-02-24 17:56:54 +04:00
|
|
|
// Function pointer for calling Promise::{ResolveInternal,RejectInternal}.
|
|
|
|
typedef void (Promise::*RunCallbackFunc)(JSContext*,
|
2014-10-28 15:08:19 +03:00
|
|
|
JS::Handle<JS::Value>);
|
2014-02-24 17:56:54 +04:00
|
|
|
|
|
|
|
void RunCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
RunCallbackFunc aFunc);
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
// Any thread with appropriate checks.
|
2014-02-24 17:56:54 +04:00
|
|
|
workers::WorkerPrivate* mWorkerPrivate;
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
// Worker thread only.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> mWorkerPromise;
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
// Modified on the worker thread.
|
|
|
|
// It is ok to *read* this without a lock on the worker.
|
|
|
|
// Main thread must always acquire a lock.
|
2014-02-24 17:56:54 +04:00
|
|
|
bool mCleanedUp; // To specify if the cleanUp() has been done.
|
|
|
|
|
2015-09-05 12:22:13 +03:00
|
|
|
const PromiseWorkerProxyStructuredCloneCallbacks* mCallbacks;
|
2014-02-24 17:56:54 +04:00
|
|
|
|
|
|
|
// Aimed to keep objects alive when doing the structured-clone read/write,
|
|
|
|
// which can be added by calling StoreISupports() on the main thread.
|
|
|
|
nsTArray<nsMainThreadPtrHandle<nsISupports>> mSupportsArray;
|
|
|
|
|
|
|
|
// Ensure the worker and the main thread won't race to access |mCleanedUp|.
|
|
|
|
Mutex mCleanUpLock;
|
2015-04-07 16:25:08 +03:00
|
|
|
|
2016-02-26 18:52:06 +03:00
|
|
|
#ifdef DEBUG
|
2015-09-02 20:07:26 +03:00
|
|
|
// Maybe get rid of this entirely and rely on mCleanedUp
|
2016-02-26 18:52:06 +03:00
|
|
|
bool mFeatureAdded;
|
|
|
|
#endif
|
2015-04-07 16:25:08 +03:00
|
|
|
};
|
2014-02-24 17:56:54 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_PromiseWorkerProxy_h
|