Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "mozilla/dom/PaymentResponse.h"
8 : #include "PaymentRequestUtils.h"
9 :
10 : namespace mozilla {
11 : namespace dom {
12 :
13 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(PaymentResponse, mOwner,
14 : mShippingAddress, mPromise)
15 :
16 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(PaymentResponse)
17 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(PaymentResponse)
18 :
19 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PaymentResponse)
20 0 : NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
21 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
22 0 : NS_INTERFACE_MAP_END
23 :
24 0 : PaymentResponse::PaymentResponse(nsPIDOMWindowInner* aWindow,
25 : const nsAString& aInternalId,
26 : const nsAString& aRequestId,
27 : const nsAString& aMethodName,
28 : const nsAString& aShippingOption,
29 : RefPtr<PaymentAddress> aShippingAddress,
30 : const nsAString& aDetails,
31 : const nsAString& aPayerName,
32 : const nsAString& aPayerEmail,
33 0 : const nsAString& aPayerPhone)
34 : : mOwner(aWindow)
35 : , mCompleteCalled(false)
36 : , mInternalId(aInternalId)
37 : , mRequestId(aRequestId)
38 : , mMethodName(aMethodName)
39 : , mDetails(aDetails)
40 : , mShippingOption(aShippingOption)
41 : , mPayerName(aPayerName)
42 : , mPayerEmail(aPayerEmail)
43 : , mPayerPhone(aPayerPhone)
44 0 : , mShippingAddress(aShippingAddress)
45 : {
46 :
47 : // TODO: from https://github.com/w3c/browser-payment-api/issues/480
48 : // Add payerGivenName + payerFamilyName to PaymentAddress
49 0 : }
50 :
51 0 : PaymentResponse::~PaymentResponse()
52 : {
53 0 : }
54 :
55 : JSObject*
56 0 : PaymentResponse::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
57 : {
58 0 : return PaymentResponseBinding::Wrap(aCx, this, aGivenProto);
59 : }
60 :
61 : void
62 0 : PaymentResponse::GetRequestId(nsString& aRetVal) const
63 : {
64 0 : aRetVal = mRequestId;
65 0 : }
66 :
67 : void
68 0 : PaymentResponse::GetMethodName(nsString& aRetVal) const
69 : {
70 0 : aRetVal = mMethodName;
71 0 : }
72 :
73 : void
74 0 : PaymentResponse::GetDetails(JSContext* aCx, JS::MutableHandle<JSObject*> aRetVal) const
75 : {
76 0 : DeserializeToJSObject(mDetails, aCx, aRetVal);
77 0 : }
78 :
79 : void
80 0 : PaymentResponse::GetShippingOption(nsString& aRetVal) const
81 : {
82 0 : aRetVal = mShippingOption;
83 0 : }
84 :
85 : void
86 0 : PaymentResponse::GetPayerName(nsString& aRetVal) const
87 : {
88 0 : aRetVal = mPayerName;
89 0 : }
90 :
91 0 : void PaymentResponse::GetPayerEmail(nsString& aRetVal) const
92 : {
93 0 : aRetVal = mPayerEmail;
94 0 : }
95 :
96 0 : void PaymentResponse::GetPayerPhone(nsString& aRetVal) const
97 : {
98 0 : aRetVal = mPayerPhone;
99 0 : }
100 :
101 : // TODO:
102 : // Return a raw pointer here to avoid refcounting, but make sure it's safe
103 : // (the object should be kept alive by the callee).
104 : already_AddRefed<PaymentAddress>
105 0 : PaymentResponse::GetShippingAddress() const
106 : {
107 0 : RefPtr<PaymentAddress> address = mShippingAddress;
108 0 : return address.forget();
109 : }
110 :
111 : already_AddRefed<Promise>
112 0 : PaymentResponse::Complete(PaymentComplete result, ErrorResult& aRv)
113 : {
114 0 : if (mCompleteCalled) {
115 0 : aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
116 0 : return nullptr;
117 : }
118 :
119 0 : nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mOwner);
120 0 : ErrorResult errResult;
121 0 : RefPtr<Promise> promise = Promise::Create(global, errResult);
122 0 : if (errResult.Failed()) {
123 0 : aRv.Throw(NS_ERROR_FAILURE);
124 0 : return nullptr;
125 : }
126 :
127 0 : mCompleteCalled = true;
128 :
129 0 : RefPtr<PaymentRequestManager> manager = PaymentRequestManager::GetSingleton();
130 0 : if (NS_WARN_IF(!manager)) {
131 0 : aRv.Throw(NS_ERROR_FAILURE);
132 0 : return nullptr;
133 : }
134 0 : nsresult rv = manager->CompletePayment(mInternalId, result);
135 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
136 0 : promise->MaybeReject(NS_ERROR_FAILURE);
137 0 : return promise.forget();
138 : }
139 :
140 0 : mPromise = promise;
141 0 : return promise.forget();
142 : }
143 :
144 : void
145 0 : PaymentResponse::RespondComplete()
146 : {
147 0 : MOZ_ASSERT(mPromise);
148 :
149 0 : mPromise->MaybeResolve(JS::UndefinedHandleValue);
150 0 : mPromise = nullptr;
151 0 : }
152 :
153 : } // namespace dom
154 9 : } // namespace mozilla
|