Line data Source code
1 : /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 : /* vim: set ts=2 et sw=2 tw=80: */
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 file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "DCPresentationChannelDescription.h"
8 : #include "nsComponentManagerUtils.h"
9 : #include "nsGlobalWindow.h"
10 : #include "PresentationBuilderChild.h"
11 : #include "PresentationIPCService.h"
12 : #include "nsServiceManagerUtils.h"
13 : #include "mozilla/Unused.h"
14 :
15 : namespace mozilla {
16 : namespace dom {
17 :
18 0 : NS_IMPL_ISUPPORTS(PresentationBuilderChild,
19 : nsIPresentationSessionTransportBuilderListener)
20 :
21 0 : PresentationBuilderChild::PresentationBuilderChild(const nsString& aSessionId,
22 0 : uint8_t aRole)
23 : : mSessionId(aSessionId)
24 0 : , mRole(aRole)
25 : {
26 0 : }
27 :
28 0 : nsresult PresentationBuilderChild::Init()
29 : {
30 0 : mBuilder = do_CreateInstance("@mozilla.org/presentation/datachanneltransportbuilder;1");
31 0 : if (NS_WARN_IF(!mBuilder)) {
32 0 : return NS_ERROR_NOT_AVAILABLE;
33 : }
34 :
35 0 : uint64_t windowId = 0;
36 :
37 : nsCOMPtr<nsIPresentationService> service =
38 0 : do_GetService(PRESENTATION_SERVICE_CONTRACTID);
39 0 : if (NS_WARN_IF(!service)) {
40 0 : return NS_ERROR_NOT_AVAILABLE;
41 : }
42 :
43 0 : if (NS_WARN_IF(NS_FAILED(service->GetWindowIdBySessionId(
44 : mSessionId,
45 : mRole,
46 : &windowId)))) {
47 0 : return NS_ERROR_NOT_AVAILABLE;
48 : }
49 :
50 0 : nsPIDOMWindowInner* window = nsGlobalWindow::GetInnerWindowWithId(windowId)->AsInner();
51 0 : if (NS_WARN_IF(!window)) {
52 0 : return NS_ERROR_NOT_AVAILABLE;
53 : }
54 :
55 0 : return mBuilder->BuildDataChannelTransport(mRole, window, this);
56 : }
57 :
58 : void
59 0 : PresentationBuilderChild::ActorDestroy(ActorDestroyReason aWhy)
60 : {
61 0 : mBuilder = nullptr;
62 0 : mActorDestroyed = true;
63 0 : }
64 :
65 : mozilla::ipc::IPCResult
66 0 : PresentationBuilderChild::RecvOnOffer(const nsString& aSDP)
67 : {
68 0 : if (NS_WARN_IF(!mBuilder)) {
69 0 : return IPC_FAIL_NO_REASON(this);
70 : }
71 : RefPtr<DCPresentationChannelDescription> description =
72 0 : new DCPresentationChannelDescription(aSDP);
73 :
74 0 : if (NS_WARN_IF(NS_FAILED(mBuilder->OnOffer(description)))) {
75 0 : return IPC_FAIL_NO_REASON(this);
76 : }
77 0 : return IPC_OK();
78 : }
79 :
80 : mozilla::ipc::IPCResult
81 0 : PresentationBuilderChild::RecvOnAnswer(const nsString& aSDP)
82 : {
83 0 : if (NS_WARN_IF(!mBuilder)) {
84 0 : return IPC_FAIL_NO_REASON(this);
85 : }
86 : RefPtr<DCPresentationChannelDescription> description =
87 0 : new DCPresentationChannelDescription(aSDP);
88 :
89 0 : if (NS_WARN_IF(NS_FAILED(mBuilder->OnAnswer(description)))) {
90 0 : return IPC_FAIL_NO_REASON(this);
91 : }
92 0 : return IPC_OK();
93 : }
94 :
95 : mozilla::ipc::IPCResult
96 0 : PresentationBuilderChild::RecvOnIceCandidate(const nsString& aCandidate)
97 : {
98 0 : if (NS_WARN_IF(mBuilder && NS_FAILED(mBuilder->OnIceCandidate(aCandidate)))) {
99 0 : return IPC_FAIL_NO_REASON(this);
100 : }
101 0 : return IPC_OK();
102 : }
103 :
104 : // nsPresentationSessionTransportBuilderListener
105 : NS_IMETHODIMP
106 0 : PresentationBuilderChild::OnSessionTransport(nsIPresentationSessionTransport* aTransport)
107 : {
108 0 : if (NS_WARN_IF(mActorDestroyed || !SendOnSessionTransport())){
109 0 : return NS_ERROR_FAILURE;
110 : }
111 :
112 : nsCOMPtr<nsIPresentationService> service =
113 0 : do_GetService(PRESENTATION_SERVICE_CONTRACTID);
114 0 : NS_WARNING_ASSERTION(service, "no presentation service");
115 0 : if (service) {
116 0 : Unused << NS_WARN_IF(NS_FAILED(static_cast<PresentationIPCService*>(service.get())->
117 : NotifySessionTransport(mSessionId, mRole, aTransport)));
118 : }
119 0 : mBuilder = nullptr;
120 0 : return NS_OK;
121 : }
122 :
123 : NS_IMETHODIMP
124 0 : PresentationBuilderChild::OnError(nsresult reason)
125 : {
126 0 : mBuilder = nullptr;
127 :
128 0 : if (NS_WARN_IF(mActorDestroyed || !SendOnSessionTransportError(reason))){
129 0 : return NS_ERROR_FAILURE;
130 : }
131 0 : return NS_OK;
132 : }
133 :
134 : NS_IMETHODIMP
135 0 : PresentationBuilderChild::SendOffer(nsIPresentationChannelDescription* aOffer)
136 : {
137 0 : nsAutoString SDP;
138 0 : nsresult rv = aOffer->GetDataChannelSDP(SDP);
139 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
140 0 : return rv;
141 : }
142 :
143 0 : if (NS_WARN_IF(mActorDestroyed || !SendSendOffer(SDP))){
144 0 : return NS_ERROR_FAILURE;
145 : }
146 0 : return NS_OK;
147 : }
148 :
149 : NS_IMETHODIMP
150 0 : PresentationBuilderChild::SendAnswer(nsIPresentationChannelDescription* aAnswer)
151 : {
152 0 : nsAutoString SDP;
153 0 : nsresult rv = aAnswer->GetDataChannelSDP(SDP);
154 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
155 0 : return rv;
156 : }
157 :
158 0 : if (NS_WARN_IF(mActorDestroyed || !SendSendAnswer(SDP))){
159 0 : return NS_ERROR_FAILURE;
160 : }
161 0 : return NS_OK;
162 : }
163 :
164 : NS_IMETHODIMP
165 0 : PresentationBuilderChild::SendIceCandidate(const nsAString& candidate)
166 : {
167 0 : if (NS_WARN_IF(mActorDestroyed || !SendSendIceCandidate(nsString(candidate)))) {
168 0 : return NS_ERROR_FAILURE;
169 : }
170 0 : return NS_OK;
171 : }
172 :
173 : NS_IMETHODIMP
174 0 : PresentationBuilderChild::Close(nsresult reason)
175 : {
176 0 : if (NS_WARN_IF(mActorDestroyed || !SendClose(reason))) {
177 0 : return NS_ERROR_FAILURE;
178 : }
179 0 : return NS_OK;
180 : }
181 :
182 : } // namespace dom
183 : } // namespace mozilla
184 :
|