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
/* -*- 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/FutureResolver.h"
#include "mozilla/dom/FutureBinding.h"
#include "mozilla/dom/Future.h"
#include "FutureCallback.h"

namespace mozilla {
namespace dom {

// FutureResolverTask

// This class processes the future's callbacks with future's result.
class FutureResolverTask MOZ_FINAL : public nsRunnable
{
public:
  FutureResolverTask(FutureResolver* aResolver,
                     const JS::Handle<JS::Value> aValue,
                     Future::FutureState aState)
    : mResolver(aResolver)
    , mValue(aValue)
    , mState(aState)
  {
    MOZ_ASSERT(aResolver);
    MOZ_ASSERT(mState != Future::Pending);
    MOZ_COUNT_CTOR(FutureResolverTask);

    JSContext* cx = nsContentUtils::GetSafeJSContext();
    JS_AddNamedValueRootRT(JS_GetRuntime(cx), &mValue,
                           "FutureResolverTask.mValue");
  }

  ~FutureResolverTask()
  {
    MOZ_COUNT_DTOR(FutureResolverTask);

    JSContext* cx = nsContentUtils::GetSafeJSContext();
    JS_RemoveValueRootRT(JS_GetRuntime(cx), &mValue);
  }

  NS_IMETHOD Run()
  {
    mResolver->RunTask(JS::Handle<JS::Value>::fromMarkedLocation(&mValue),
                       mState, FutureResolver::SyncTask);
    return NS_OK;
  }

private:
  nsRefPtr<FutureResolver> mResolver;
  JS::Value mValue;
  Future::FutureState mState;
};

// FutureResolver

NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(FutureResolver, mFuture)

NS_IMPL_CYCLE_COLLECTING_ADDREF(FutureResolver)
NS_IMPL_CYCLE_COLLECTING_RELEASE(FutureResolver)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FutureResolver)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

FutureResolver::FutureResolver(Future* aFuture)
  : mFuture(aFuture)
  , mResolvePending(false)
{
  SetIsDOMBinding();
}

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

void
FutureResolver::Resolve(JSContext* aCx,
                        const Optional<JS::Handle<JS::Value> >& aValue,
                        FutureTaskSync aAsynchronous)
{
  if (mResolvePending) {
    return;
  }

  ResolveInternal(aCx, aValue, aAsynchronous);
}

void
FutureResolver::ResolveInternal(JSContext* aCx,
                                const Optional<JS::Handle<JS::Value> >& aValue,
                                FutureTaskSync aAsynchronous)
{
  mResolvePending = true;

  // TODO: Bug 879245 - Then-able objects
  if (aValue.WasPassed() && aValue.Value().isObject()) {
    JS::Rooted<JSObject*> valueObj(aCx, &aValue.Value().toObject());
    Future* nextFuture;
    nsresult rv = UnwrapObject<Future>(aCx, valueObj, nextFuture);

    if (NS_SUCCEEDED(rv)) {
      nsRefPtr<FutureCallback> resolveCb = new ResolveFutureCallback(this);
      nsRefPtr<FutureCallback> rejectCb = new RejectFutureCallback(this);
      nextFuture->AppendCallbacks(resolveCb, rejectCb);
      return;
    }
  }

  // If the synchronous flag is set, process future's resolve callbacks with
  // value. Otherwise, the synchronous flag is unset, queue a task to process
  // future's resolve callbacks with value. Otherwise, the synchronous flag is
  // unset, queue a task to process future's resolve callbacks with value.
  RunTask(aValue.WasPassed() ? aValue.Value() : JS::UndefinedHandleValue,
          Future::Resolved, aAsynchronous);
}

void
FutureResolver::Reject(JSContext* aCx,
                       const Optional<JS::Handle<JS::Value> >& aValue,
                       FutureTaskSync aAsynchronous)
{
  if (mResolvePending) {
    return;
  }

  RejectInternal(aCx, aValue, aAsynchronous);
}

void
FutureResolver::RejectInternal(JSContext* aCx,
                               const Optional<JS::Handle<JS::Value> >& aValue,
                               FutureTaskSync aAsynchronous)
{
  mResolvePending = true;

  // If the synchronous flag is set, process future's reject callbacks with
  // value. Otherwise, the synchronous flag is unset, queue a task to process
  // future's reject callbacks with value.
  RunTask(aValue.WasPassed() ? aValue.Value() : JS::UndefinedHandleValue,
          Future::Rejected, aAsynchronous);
}

void
FutureResolver::RunTask(JS::Handle<JS::Value> aValue,
                        Future::FutureState aState,
                        FutureTaskSync aAsynchronous)
{
  // If the synchronous flag is unset, queue a task to process future's
  // accept callbacks with value.
  if (aAsynchronous == AsyncTask) {
    nsRefPtr<FutureResolverTask> task =
      new FutureResolverTask(this, aValue, aState);
    NS_DispatchToCurrentThread(task);
    return;
  }

  mFuture->SetResult(aValue);
  mFuture->SetState(aState);
  mFuture->RunTask();
  mFuture = nullptr;
}

} // namespace dom
} // namespace mozilla