dom/bluetooth/BluetoothReplyRunnable.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/bluetooth/BluetoothReplyRunnable.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,96 @@
     1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "base/basictypes.h"
    1.11 +#include "BluetoothReplyRunnable.h"
    1.12 +#include "DOMRequest.h"
    1.13 +#include "mozilla/dom/bluetooth/BluetoothTypes.h"
    1.14 +#include "nsServiceManagerUtils.h"
    1.15 +
    1.16 +USING_BLUETOOTH_NAMESPACE
    1.17 +
    1.18 +BluetoothReplyRunnable::BluetoothReplyRunnable(nsIDOMDOMRequest* aReq)
    1.19 +  : mDOMRequest(aReq)
    1.20 +{}
    1.21 +
    1.22 +void
    1.23 +BluetoothReplyRunnable::SetReply(BluetoothReply* aReply)
    1.24 +{
    1.25 +  mReply = aReply;
    1.26 +}
    1.27 +
    1.28 +void
    1.29 +BluetoothReplyRunnable::ReleaseMembers()
    1.30 +{
    1.31 +  mDOMRequest = nullptr;
    1.32 +}
    1.33 +
    1.34 +BluetoothReplyRunnable::~BluetoothReplyRunnable()
    1.35 +{}
    1.36 +
    1.37 +nsresult
    1.38 +BluetoothReplyRunnable::FireReply(JS::Handle<JS::Value> aVal)
    1.39 +{
    1.40 +  nsCOMPtr<nsIDOMRequestService> rs =
    1.41 +    do_GetService(DOMREQUEST_SERVICE_CONTRACTID);
    1.42 +  NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
    1.43 +
    1.44 +  return mReply->type() == BluetoothReply::TBluetoothReplySuccess ?
    1.45 +    rs->FireSuccessAsync(mDOMRequest, aVal) :
    1.46 +    rs->FireErrorAsync(mDOMRequest, mReply->get_BluetoothReplyError().error());
    1.47 +}
    1.48 +
    1.49 +nsresult
    1.50 +BluetoothReplyRunnable::FireErrorString()
    1.51 +{
    1.52 +  nsCOMPtr<nsIDOMRequestService> rs =
    1.53 +    do_GetService("@mozilla.org/dom/dom-request-service;1");
    1.54 +  NS_ENSURE_TRUE(rs, NS_ERROR_FAILURE);
    1.55 +
    1.56 +  return rs->FireErrorAsync(mDOMRequest, mErrorString);
    1.57 +}
    1.58 +
    1.59 +NS_IMETHODIMP
    1.60 +BluetoothReplyRunnable::Run()
    1.61 +{
    1.62 +  MOZ_ASSERT(NS_IsMainThread());
    1.63 +  MOZ_ASSERT(mDOMRequest);
    1.64 +  MOZ_ASSERT(mReply);
    1.65 +
    1.66 +  nsresult rv;
    1.67 +
    1.68 +  AutoSafeJSContext cx;
    1.69 +  JS::Rooted<JS::Value> v(cx, JSVAL_VOID);
    1.70 +
    1.71 +  if (mReply->type() != BluetoothReply::TBluetoothReplySuccess) {
    1.72 +    rv = FireReply(v);
    1.73 +  } else {
    1.74 +    if (!ParseSuccessfulReply(&v)) {
    1.75 +      rv = FireErrorString();
    1.76 +    } else {
    1.77 +      rv = FireReply(v);
    1.78 +    }
    1.79 +  }
    1.80 +
    1.81 +  if (NS_FAILED(rv)) {
    1.82 +    BT_WARNING("Could not fire DOMRequest!");
    1.83 +  }
    1.84 +
    1.85 +  ReleaseMembers();
    1.86 +  MOZ_ASSERT(!mDOMRequest,
    1.87 +             "mDOMRequest still alive! Deriving class should call "
    1.88 +             "BluetoothReplyRunnable::ReleaseMembers()!");
    1.89 +
    1.90 +  return rv;
    1.91 +}
    1.92 +
    1.93 +BluetoothVoidReplyRunnable::BluetoothVoidReplyRunnable(nsIDOMDOMRequest* aReq)
    1.94 +  : BluetoothReplyRunnable(aReq)
    1.95 +{}
    1.96 +
    1.97 +BluetoothVoidReplyRunnable::~BluetoothVoidReplyRunnable()
    1.98 +{}
    1.99 +

mercurial