DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (5b81998bb7ab)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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 "mozilla/dom/Future.h"
#include "mozilla/dom/FutureBinding.h"
#include "mozilla/dom/FutureResolver.h"
#include "mozilla/Preferences.h"
#include "FutureCallback.h"
#include "nsContentUtils.h"
#include "nsPIDOMWindow.h"

namespace mozilla {
namespace dom {

// FutureTask

// This class processes the future's callbacks with future's result.
class FutureTask MOZ_FINAL : public nsRunnable
{
public:
  FutureTask(Future* aFuture)
    : mFuture(aFuture)
  {
    MOZ_ASSERT(aFuture);
    MOZ_COUNT_CTOR(FutureTask);
  }

  ~FutureTask()
  {
    MOZ_COUNT_DTOR(FutureTask);
  }

  NS_IMETHOD Run()
  {
    mFuture->mTaskPending = false;
    mFuture->RunTask();
    return NS_OK;
  }

private:
  nsRefPtr<Future> mFuture;
};

// Future

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Future)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mResolver)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mResolveCallbacks);
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mRejectCallbacks);
  tmp->mResult = JSVAL_VOID;
  NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Future)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResolver)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResolveCallbacks);
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRejectCallbacks);
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Future)
  NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mResult)
  NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_TRACE_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(Future)
NS_IMPL_CYCLE_COLLECTING_RELEASE(Future)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Future)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

Future::Future(nsPIDOMWindow* aWindow)
  : mWindow(aWindow)
  , mResult(JS::UndefinedValue())
  , mState(Pending)
  , mTaskPending(false)
{
  MOZ_COUNT_CTOR(Future);
  NS_HOLD_JS_OBJECTS(this, Future);
  SetIsDOMBinding();

  mResolver = new FutureResolver(this);
}

Future::~Future()
{
  mResult = JSVAL_VOID;
  NS_DROP_JS_OBJECTS(this, Future);
  MOZ_COUNT_DTOR(Future);
}

JSObject*
Future::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
{
  return FutureBinding::Wrap(aCx, aScope, this);
}

/* static */ bool
Future::PrefEnabled()
{
  return Preferences::GetBool("dom.future.enabled", false);
}

/* static */ already_AddRefed<Future>
Future::Constructor(const GlobalObject& aGlobal, JSContext* aCx,
                    FutureInit& aInit, ErrorResult& aRv)
{
  MOZ_ASSERT(PrefEnabled());

  nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.Get());
  if (!window) {
    aRv.Throw(NS_ERROR_UNEXPECTED);
    return nullptr;
  }

  nsRefPtr<Future> future = new Future(window);

  aInit.Call(future, *future->mResolver, aRv,
             CallbackObject::eRethrowExceptions);
  aRv.WouldReportJSException();

  if (aRv.IsJSException()) {
    Optional<JS::Handle<JS::Value> > value(aCx);
    aRv.StealJSException(aCx, &value.Value());
    future->mResolver->Reject(aCx, value);
  }

  return future.forget();
}

/* static */ already_AddRefed<Future>
Future::Resolve(const GlobalObject& aGlobal, JSContext* aCx,
                JS::Handle<JS::Value> aValue, ErrorResult& aRv)
{
  MOZ_ASSERT(PrefEnabled());

  nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.Get());
  if (!window) {
    aRv.Throw(NS_ERROR_UNEXPECTED);
    return nullptr;
  }

  nsRefPtr<Future> future = new Future(window);

  Optional<JS::Handle<JS::Value> > value(aCx, aValue);
  future->mResolver->Resolve(aCx, value);
  return future.forget();
}

/* static */ already_AddRefed<Future>
Future::Reject(const GlobalObject& aGlobal, JSContext* aCx,
               JS::Handle<JS::Value> aValue, ErrorResult& aRv)
{
  MOZ_ASSERT(PrefEnabled());

  nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.Get());
  if (!window) {
    aRv.Throw(NS_ERROR_UNEXPECTED);
    return nullptr;
  }

  nsRefPtr<Future> future = new Future(window);

  Optional<JS::Handle<JS::Value> > value(aCx, aValue);
  future->mResolver->Reject(aCx, value);
  return future.forget();
}

already_AddRefed<Future>
Future::Then(AnyCallback* aResolveCallback, AnyCallback* aRejectCallback)
{
  nsRefPtr<Future> future = new Future(GetParentObject());

  nsRefPtr<FutureCallback> resolveCb =
    FutureCallback::Factory(future->mResolver,
                            aResolveCallback,
                            FutureCallback::Resolve);

  nsRefPtr<FutureCallback> rejectCb =
    FutureCallback::Factory(future->mResolver,
                            aRejectCallback,
                            FutureCallback::Reject);

  AppendCallbacks(resolveCb, rejectCb);

  return future.forget();
}

already_AddRefed<Future>
Future::Catch(AnyCallback* aRejectCallback)
{
  return Then(nullptr, aRejectCallback);
}

void
Future::Done(AnyCallback* aResolveCallback, AnyCallback* aRejectCallback)
{
  if (!aResolveCallback && !aRejectCallback) {
    return;
  }

  nsRefPtr<FutureCallback> resolveCb;
  if (aResolveCallback) {
    resolveCb = new SimpleWrapperFutureCallback(this, aResolveCallback);
  }

  nsRefPtr<FutureCallback> rejectCb;
  if (aRejectCallback) {
    rejectCb = new SimpleWrapperFutureCallback(this, aRejectCallback);
  }

  AppendCallbacks(resolveCb, rejectCb);
}

void
Future::AppendCallbacks(FutureCallback* aResolveCallback,
                        FutureCallback* aRejectCallback)
{
  if (aResolveCallback) {
    mResolveCallbacks.AppendElement(aResolveCallback);
  }

  if (aRejectCallback) {
    mRejectCallbacks.AppendElement(aRejectCallback);
  }

  // If future's state is resolved, queue a task to process future's resolve
  // callbacks with future's result. If future's state is rejected, queue a task
  // to process future's reject callbacks with future's result.
  if (mState != Pending && !mTaskPending) {
    nsRefPtr<FutureTask> task = new FutureTask(this);
    NS_DispatchToCurrentThread(task);
    mTaskPending = true;
  }
}

void
Future::RunTask()
{
  MOZ_ASSERT(mState != Pending);

  nsTArray<nsRefPtr<FutureCallback> > callbacks;
  callbacks.SwapElements(mState == Resolved ? mResolveCallbacks
                                            : mRejectCallbacks);
  mResolveCallbacks.Clear();
  mRejectCallbacks.Clear();

  JSAutoRequest ar(nsContentUtils::GetSafeJSContext());
  Optional<JS::Handle<JS::Value> > value(nsContentUtils::GetSafeJSContext(), mResult);

  for (uint32_t i = 0; i < callbacks.Length(); ++i) {
    callbacks[i]->Call(value);
  }
}

} // namespace dom
} // namespace mozilla