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 (d1ed7de67f5a)

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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "MobileMessageCursorCallback.h"
#include "mozilla/dom/ScriptSettings.h"
#include "nsIDOMDOMRequest.h"
#include "nsIDOMMozSmsMessage.h"
#include "nsIMobileMessageCallback.h"
#include "nsServiceManagerUtils.h"      // for do_GetService

namespace mozilla {
namespace dom {

NS_IMPL_CYCLE_COLLECTION_INHERITED(MobileMessageCursor, DOMCursor,
                                   mPendingResults)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MobileMessageCursor)
NS_INTERFACE_MAP_END_INHERITING(DOMCursor)

NS_IMPL_ADDREF_INHERITED(MobileMessageCursor, DOMCursor)
NS_IMPL_RELEASE_INHERITED(MobileMessageCursor, DOMCursor)

MobileMessageCursor::MobileMessageCursor(nsPIDOMWindow* aWindow,
                                         nsICursorContinueCallback* aCallback)
  : DOMCursor(aWindow, aCallback)
{
}

NS_IMETHODIMP
MobileMessageCursor::Continue()
{
  // We have originally:
  //
  //   DOMCursor::Continue()
  //   +-> DOMCursor::Continue(ErrorResult& aRv)
  //
  // Now it becomes:
  //
  //   MobileMessageCursor::Continue()
  //   +-> DOMCursor::Continue()
  //       +-> MobileMessageCursor::Continue(ErrorResult& aRv)
  //           o-> DOMCursor::Continue(ErrorResult& aRv)
  return DOMCursor::Continue();
}

void
MobileMessageCursor::Continue(ErrorResult& aRv)
{
  // An ordinary DOMCursor works in following flow:
  //
  //   DOMCursor::Continue()
  //   +-> DOMCursor::Reset()
  //   +-> nsICursorContinueCallback::HandleContinue()
  //       +-> nsIMobileMessageCursorCallback::NotifyCursorResult()
  //           +-> DOMCursor::FireSuccess()
  //
  // With no pending result, we call to |DOMCursor::Continue()| as usual.
  if (!mPendingResults.Length()) {
    DOMCursor::Continue(aRv);
    return;
  }

  // Otherwise, reset current result and fire a success event with the last
  // pending one.
  Reset();

  nsresult rv = FireSuccessWithNextPendingResult();
  if (NS_FAILED(rv)) {
    aRv.Throw(rv);
  }
}

nsresult
MobileMessageCursor::FireSuccessWithNextPendingResult()
{
  // We're going to pop the last element from mPendingResults, so it must not
  // be empty.
  MOZ_ASSERT(mPendingResults.Length());

  AutoJSAPI jsapi;
  if (NS_WARN_IF(!jsapi.Init(GetOwner()))) {
    return NS_ERROR_FAILURE;
  }

  JSContext* cx = jsapi.cx();
  JS::Rooted<JS::Value> val(cx);
  nsresult rv =
    nsContentUtils::WrapNative(cx, mPendingResults.LastElement(), &val);
  NS_ENSURE_SUCCESS(rv, rv);

  mPendingResults.RemoveElementAt(mPendingResults.Length() - 1);

  FireSuccess(val);
  return NS_OK;
}

namespace mobilemessage {

NS_IMPL_CYCLE_COLLECTION(MobileMessageCursorCallback, mDOMCursor)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MobileMessageCursorCallback)
  NS_INTERFACE_MAP_ENTRY(nsIMobileMessageCursorCallback)
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(MobileMessageCursorCallback)
NS_IMPL_CYCLE_COLLECTING_RELEASE(MobileMessageCursorCallback)

// nsIMobileMessageCursorCallback

NS_IMETHODIMP
MobileMessageCursorCallback::NotifyCursorError(int32_t aError)
{
  MOZ_ASSERT(mDOMCursor);

  nsRefPtr<DOMCursor> cursor = mDOMCursor.forget();

  switch (aError) {
    case nsIMobileMessageCallback::NO_SIGNAL_ERROR:
      cursor->FireError(NS_LITERAL_STRING("NoSignalError"));
      break;
    case nsIMobileMessageCallback::NOT_FOUND_ERROR:
      cursor->FireError(NS_LITERAL_STRING("NotFoundError"));
      break;
    case nsIMobileMessageCallback::UNKNOWN_ERROR:
      cursor->FireError(NS_LITERAL_STRING("UnknownError"));
      break;
    case nsIMobileMessageCallback::INTERNAL_ERROR:
      cursor->FireError(NS_LITERAL_STRING("InternalError"));
      break;
    default: // SUCCESS_NO_ERROR is handled above.
      MOZ_CRASH("Should never get here!");
  }

  return NS_OK;
}

NS_IMETHODIMP
MobileMessageCursorCallback::NotifyCursorResult(nsISupports** aResults,
                                                uint32_t aSize)
{
  MOZ_ASSERT(mDOMCursor);
  // We should only be notified with valid results. Or, either
  // |NotifyCursorDone()| or |NotifyCursorError()| should be called instead.
  MOZ_ASSERT(aResults && *aResults && aSize);
  // There shouldn't be unexpected notifications before |Continue()| is called.
  nsTArray<nsCOMPtr<nsISupports>>& pending = mDOMCursor->mPendingResults;
  MOZ_ASSERT(pending.Length() == 0);

  // Push pending results in reversed order.
  pending.SetCapacity(pending.Length() + aSize);
  while (aSize) {
    --aSize;
    pending.AppendElement(aResults[aSize]);
  }

  nsresult rv = mDOMCursor->FireSuccessWithNextPendingResult();
  if (NS_FAILED(rv)) {
    NotifyCursorError(nsIMobileMessageCallback::INTERNAL_ERROR);
  }

  return NS_OK;
}

NS_IMETHODIMP
MobileMessageCursorCallback::NotifyCursorDone()
{
  MOZ_ASSERT(mDOMCursor);

  nsRefPtr<DOMCursor> cursor = mDOMCursor.forget();
  cursor->FireDone();

  return NS_OK;
}

} // namespace mobilemessage
} // namespace dom
} // namespace mozilla