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

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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et 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 "nsContentSecurityManager.h"
#include "nsContentUtils.h"
#include "RtspChannelChild.h"
#include "mozilla/ipc/URIUtils.h"
#include "nsServiceManagerUtils.h"

using namespace mozilla::ipc;

namespace mozilla {
namespace net {

//-----------------------------------------------------------------------------
// RtspChannelChild
//-----------------------------------------------------------------------------
RtspChannelChild::RtspChannelChild(nsIURI *aUri)
  : mIPCOpen(false)
  , mCanceled(false)
{
  nsBaseChannel::SetURI(aUri);
  DisallowThreadRetargeting();
}

RtspChannelChild::~RtspChannelChild()
{
}

nsIStreamingProtocolController*
RtspChannelChild::GetController()
{
  return mMediaStreamController;
}

void
RtspChannelChild::ReleaseController()
{
  if (mMediaStreamController) {
    mMediaStreamController = nullptr;
  }
}

//-----------------------------------------------------------------------------
// IPDL
//-----------------------------------------------------------------------------
void
RtspChannelChild::AddIPDLReference()
{
  MOZ_ASSERT(!mIPCOpen,
             "Attempt to retain more than one IPDL reference");
  mIPCOpen = true;
  AddRef();
}

void
RtspChannelChild::ReleaseIPDLReference()
{
  MOZ_ASSERT(mIPCOpen, "Attempt to release nonexistent IPDL reference");
  mIPCOpen = false;
  Release();
}

//-----------------------------------------------------------------------------
// nsISupports
//-----------------------------------------------------------------------------
NS_IMPL_ISUPPORTS_INHERITED(RtspChannelChild,
                            nsBaseChannel,
                            nsIChannel,
                            nsIChildChannel)

//-----------------------------------------------------------------------------
// nsBaseChannel::nsIChannel
//-----------------------------------------------------------------------------
NS_IMETHODIMP
RtspChannelChild::GetContentType(nsACString& aContentType)
{
  aContentType.AssignLiteral("RTSP");
  return NS_OK;
}

class CallListenerOnStartRequestEvent : public nsRunnable
{
public:
  CallListenerOnStartRequestEvent(nsIStreamListener *aListener,
                                  nsIRequest *aRequest, nsISupports *aContext)
    : mListener(aListener)
    , mRequest(aRequest)
    , mContext(aContext)
  {
    MOZ_RELEASE_ASSERT(aListener);
  }
  NS_IMETHOD Run()
  {
    MOZ_ASSERT(NS_IsMainThread());
    mListener->OnStartRequest(mRequest, mContext);
    return NS_OK;
  }
private:
  RefPtr<nsIStreamListener> mListener;
  RefPtr<nsIRequest> mRequest;
  RefPtr<nsISupports> mContext;
};

NS_IMETHODIMP
RtspChannelChild::AsyncOpen(nsIStreamListener *aListener, nsISupports *aContext)
{
  MOZ_ASSERT(!mLoadInfo ||
             mLoadInfo->GetSecurityMode() == 0 ||
             mLoadInfo->GetInitialSecurityCheckDone() ||
             (mLoadInfo->GetSecurityMode() == nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL &&
              nsContentUtils::IsSystemPrincipal(mLoadInfo->LoadingPrincipal())),
             "security flags in loadInfo but asyncOpen2() not called");

  // Precondition checks.
  MOZ_ASSERT(aListener);
  nsCOMPtr<nsIURI> uri = nsBaseChannel::URI();
  NS_ENSURE_TRUE(uri, NS_ERROR_ILLEGAL_VALUE);

  // Create RtspController.
  nsCOMPtr<nsIStreamingProtocolControllerService> mediaControllerService =
    do_GetService(MEDIASTREAMCONTROLLERSERVICE_CONTRACTID);
  MOZ_RELEASE_ASSERT(mediaControllerService,
    "Cannot proceed if media controller service is unavailable!");
  mediaControllerService->Create(this, getter_AddRefs(mMediaStreamController));
  MOZ_ASSERT(mMediaStreamController);

  // Add ourselves to the load group.
  if (mLoadGroup) {
    mLoadGroup->AddRequest(this, nullptr);
  }

  // Dispatch mListener's OnStartRequest directly. mListener is expected to
  // create an RtspMediaResource and use the RtspController we just created to
  // manage the control and data streams to and from the network.
  mListener = aListener;
  mListenerContext = aContext;
  NS_DispatchToMainThread(
    new CallListenerOnStartRequestEvent(mListener, this, mListenerContext));

  return NS_OK;
}

NS_IMETHODIMP
RtspChannelChild::AsyncOpen2(nsIStreamListener *aListener)
{
  nsCOMPtr<nsIStreamListener> listener = aListener;
  nsresult rv = nsContentSecurityManager::doContentSecurityCheck(this, listener);
  NS_ENSURE_SUCCESS(rv, rv);
  return AsyncOpen(listener, nullptr);
}

//-----------------------------------------------------------------------------
// nsBaseChannel::nsIStreamListener::nsIRequestObserver
//-----------------------------------------------------------------------------
NS_IMETHODIMP
RtspChannelChild::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext)
{
  MOZ_CRASH("Should never be called");
}

NS_IMETHODIMP
RtspChannelChild::OnStopRequest(nsIRequest *aRequest, nsISupports *aContext,
                                nsresult aStatusCode)
{
  MOZ_CRASH("Should never be called");
}

//-----------------------------------------------------------------------------
// nsBaseChannel::nsIStreamListener
//-----------------------------------------------------------------------------
NS_IMETHODIMP
RtspChannelChild::OnDataAvailable(nsIRequest *aRequest,
                                  nsISupports *aContext,
                                  nsIInputStream *aInputStream,
                                  uint64_t aOffset,
                                  uint32_t aCount)
{
  MOZ_CRASH("Should never be called");
}

//-----------------------------------------------------------------------------
// nsBaseChannel::nsIChannel::nsIRequest
//-----------------------------------------------------------------------------
class CallListenerOnStopRequestEvent : public nsRunnable
{
public:
  CallListenerOnStopRequestEvent(nsIStreamListener *aListener,
                                 nsIRequest *aRequest,
                                 nsISupports *aContext, nsresult aStatus)
    : mListener(aListener)
    , mRequest(aRequest)
    , mContext(aContext)
    , mStatus(aStatus)
  {
    MOZ_RELEASE_ASSERT(aListener);
  }
  NS_IMETHOD Run()
  {
    MOZ_ASSERT(NS_IsMainThread());
    mListener->OnStopRequest(mRequest, mContext, mStatus);
    return NS_OK;
  }
private:
  RefPtr<nsIStreamListener> mListener;
  RefPtr<nsIRequest> mRequest;
  RefPtr<nsISupports> mContext;
  nsresult mStatus;
};

NS_IMETHODIMP
RtspChannelChild::Cancel(nsresult status)
{
  if (mCanceled) {
    return NS_OK;
  }

  mCanceled = true;
  // Stop RtspController.
  if (mMediaStreamController) {
    mMediaStreamController->Stop();
  }

  // Call mListener's OnStopRequest to do clean up.
  NS_DispatchToMainThread(
    new CallListenerOnStopRequestEvent(mListener, this,
                                       mListenerContext, status));
  mListener = nullptr;
  mListenerContext = nullptr;

  // Remove ourselves from the load group.
  if (mLoadGroup) {
    mLoadGroup->RemoveRequest(this, nullptr, status);
  }

  return NS_OK;
}

NS_IMETHODIMP
RtspChannelChild::Suspend()
{
  MOZ_CRASH("Should never be called");
}

NS_IMETHODIMP
RtspChannelChild::Resume()
{
  MOZ_CRASH("Should never be called");
}

//-----------------------------------------------------------------------------
// nsBaseChannel
//-----------------------------------------------------------------------------
NS_IMETHODIMP
RtspChannelChild::OpenContentStream(bool aAsync,
                                    nsIInputStream **aStream,
                                    nsIChannel **aChannel)
{
  MOZ_CRASH("Should never be called");
}

//-----------------------------------------------------------------------------
// nsIChildChannel
//-----------------------------------------------------------------------------
NS_IMETHODIMP
RtspChannelChild::ConnectParent(uint32_t id)
{
  // Create RtspChannelParent for redirection.
  AddIPDLReference();
  RtspChannelConnectArgs connectArgs;
  SerializeURI(nsBaseChannel::URI(), connectArgs.uri());
  connectArgs.channelId() = id;
  if (!gNeckoChild->SendPRtspChannelConstructor(this, connectArgs)) {
    return NS_ERROR_FAILURE;
  }

  return NS_OK;
}

NS_IMETHODIMP
RtspChannelChild::CompleteRedirectSetup(nsIStreamListener *aListener,
                                        nsISupports *aContext)
{
  if (mLoadInfo && mLoadInfo->GetEnforceSecurity()) {
    MOZ_ASSERT(!aContext, "aContext should be null!");
    return AsyncOpen2(aListener);
  }
  return AsyncOpen(aListener, aContext);
}

} // namespace net
} // namespace mozilla