summaryrefslogtreecommitdiffstats
path: root/dom/payments/ipc/PaymentRequestChild.cpp
blob: a3a22bf3464ca8753a08d2751a43068956bffe24 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=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 "PaymentRequestChild.h"
#include "mozilla/dom/PaymentRequest.h"
#include "mozilla/dom/PaymentRequestManager.h"

namespace mozilla::dom {

PaymentRequestChild::PaymentRequestChild(PaymentRequest* aRequest)
    : mRequest(aRequest) {
  mRequest->SetIPC(this);
}

nsresult PaymentRequestChild::RequestPayment(
    const IPCPaymentActionRequest& aAction) {
  if (!mRequest) {
    return NS_ERROR_FAILURE;
  }
  if (!SendRequestPayment(aAction)) {
    return NS_ERROR_FAILURE;
  }
  return NS_OK;
}

mozilla::ipc::IPCResult PaymentRequestChild::RecvRespondPayment(
    const IPCPaymentActionResponse& aResponse) {
  if (!mRequest) {
    return IPC_FAIL_NO_REASON(this);
  }
  const IPCPaymentActionResponse& response = aResponse;
  RefPtr<PaymentRequestManager> manager = PaymentRequestManager::GetSingleton();
  MOZ_ASSERT(manager);

  // Hold a strong reference to our request for the entire response.
  RefPtr<PaymentRequest> request(mRequest);
  nsresult rv = manager->RespondPayment(request, response);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return IPC_FAIL_NO_REASON(this);
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult PaymentRequestChild::RecvChangeShippingAddress(
    const nsString& aRequestId, const IPCPaymentAddress& aAddress) {
  if (!mRequest) {
    return IPC_FAIL_NO_REASON(this);
  }
  RefPtr<PaymentRequestManager> manager = PaymentRequestManager::GetSingleton();
  MOZ_ASSERT(manager);
  RefPtr<PaymentRequest> request(mRequest);
  nsresult rv = manager->ChangeShippingAddress(request, aAddress);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return IPC_FAIL_NO_REASON(this);
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult PaymentRequestChild::RecvChangeShippingOption(
    const nsString& aRequestId, const nsString& aOption) {
  if (!mRequest) {
    return IPC_FAIL_NO_REASON(this);
  }
  RefPtr<PaymentRequestManager> manager = PaymentRequestManager::GetSingleton();
  MOZ_ASSERT(manager);
  RefPtr<PaymentRequest> request(mRequest);
  nsresult rv = manager->ChangeShippingOption(request, aOption);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return IPC_FAIL_NO_REASON(this);
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult PaymentRequestChild::RecvChangePayerDetail(
    const nsString& aRequestId, const nsString& aPayerName,
    const nsString& aPayerEmail, const nsString& aPayerPhone) {
  if (!mRequest) {
    return IPC_FAIL_NO_REASON(this);
  }
  RefPtr<PaymentRequestManager> manager = PaymentRequestManager::GetSingleton();
  MOZ_ASSERT(manager);
  RefPtr<PaymentRequest> request(mRequest);
  nsresult rv =
      manager->ChangePayerDetail(request, aPayerName, aPayerEmail, aPayerPhone);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return IPC_FAIL_NO_REASON(this);
  }
  return IPC_OK();
}

mozilla::ipc::IPCResult PaymentRequestChild::RecvChangePaymentMethod(
    const nsString& aRequestId, const nsString& aMethodName,
    const IPCMethodChangeDetails& aMethodDetails) {
  if (!mRequest) {
    return IPC_FAIL_NO_REASON(this);
  }
  RefPtr<PaymentRequestManager> manager = PaymentRequestManager::GetSingleton();
  MOZ_ASSERT(manager);
  RefPtr<PaymentRequest> request(mRequest);
  nsresult rv =
      manager->ChangePaymentMethod(request, aMethodName, aMethodDetails);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return IPC_FAIL_NO_REASON(this);
  }
  return IPC_OK();
}

void PaymentRequestChild::ActorDestroy(ActorDestroyReason aWhy) {
  if (mRequest) {
    DetachFromRequest(true);
  }
}

void PaymentRequestChild::MaybeDelete(bool aCanBeInManager) {
  if (mRequest) {
    DetachFromRequest(aCanBeInManager);
    Send__delete__(this);
  }
}

void PaymentRequestChild::DetachFromRequest(bool aCanBeInManager) {
  MOZ_ASSERT(mRequest);

  if (aCanBeInManager) {
    RefPtr<PaymentRequestManager> manager =
        PaymentRequestManager::GetSingleton();
    MOZ_ASSERT(manager);

    RefPtr<PaymentRequest> request(mRequest);
    manager->RequestIPCOver(request);
  }

  mRequest->SetIPC(nullptr);
  mRequest = nullptr;
}

}  // namespace mozilla::dom