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

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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
/* -*- 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 "ParentProcessDocumentChannel.h"

#include "mozilla/StaticPrefs_extensions.h"
#include "nsDocShell.h"
#include "nsIObserverService.h"
#include "nsIClassifiedChannel.h"
#include "nsIRedirectChannelRegistrar.h"
#include "mozilla/net/UrlClassifierCommon.h"

extern mozilla::LazyLogModule gDocumentChannelLog;
#define LOG(fmt) MOZ_LOG(gDocumentChannelLog, mozilla::LogLevel::Verbose, fmt)

namespace mozilla {
namespace net {

using RedirectToRealChannelPromise =
    typename PDocumentChannelParent::RedirectToRealChannelPromise;

class ParentChannelWrapper : public nsIParentChannel {
 public:
  ParentChannelWrapper(nsIChannel* aChannel, nsIStreamListener* aListener)
      : mChannel(aChannel), mListener(aListener) {}

  NS_DECL_ISUPPORTS
  NS_DECL_NSIPARENTCHANNEL
  NS_FORWARD_NSISTREAMLISTENER(mListener->)
  NS_FORWARD_NSIREQUESTOBSERVER(mListener->)

 private:
  virtual ~ParentChannelWrapper() = default;
  const nsCOMPtr<nsIChannel> mChannel;
  const nsCOMPtr<nsIStreamListener> mListener;
};

NS_IMPL_ISUPPORTS(ParentChannelWrapper, nsIParentChannel, nsIStreamListener,
                  nsIRequestObserver);

NS_IMPL_ISUPPORTS_INHERITED(ParentProcessDocumentChannel, DocumentChannel,
                            nsIAsyncVerifyRedirectCallback, nsIObserver)

ParentProcessDocumentChannel::ParentProcessDocumentChannel(
    nsDocShellLoadState* aLoadState, class LoadInfo* aLoadInfo,
    nsLoadFlags aLoadFlags, uint32_t aCacheKey, bool aUriModified,
    bool aIsXFOError)
    : DocumentChannel(aLoadState, aLoadInfo, aLoadFlags, aCacheKey,
                      aUriModified, aIsXFOError) {
  LOG(("ParentProcessDocumentChannel ctor [this=%p]", this));
}

ParentProcessDocumentChannel::~ParentProcessDocumentChannel() {
  LOG(("ParentProcessDocumentChannel dtor [this=%p]", this));
}

RefPtr<RedirectToRealChannelPromise>
ParentProcessDocumentChannel::RedirectToRealChannel(
    nsTArray<ipc::Endpoint<extensions::PStreamFilterParent>>&&
        aStreamFilterEndpoints,
    uint32_t aRedirectFlags, uint32_t aLoadFlags) {
  LOG(("ParentProcessDocumentChannel RedirectToRealChannel [this=%p]", this));
  nsCOMPtr<nsIChannel> channel = mDocumentLoadListener->GetChannel();
  channel->SetLoadFlags(aLoadFlags);
  channel->SetNotificationCallbacks(mCallbacks);

  if (mLoadGroup) {
    channel->SetLoadGroup(mLoadGroup);
  }

  if (XRE_IsE10sParentProcess()) {
    nsCOMPtr<nsIURI> uri;
    MOZ_ALWAYS_SUCCEEDS(NS_GetFinalChannelURI(channel, getter_AddRefs(uri)));
    if (!nsDocShell::CanLoadInParentProcess(uri)) {
      nsAutoCString msg;
      uri->GetSpec(msg);
      msg.Insert(
          "Attempt to load a non-authorised load in the parent process: ", 0);
      NS_ASSERTION(false, msg.get());
      return RedirectToRealChannelPromise::CreateAndResolve(
          NS_ERROR_CONTENT_BLOCKED, __func__);
    }
  }
  mStreamFilterEndpoints = std::move(aStreamFilterEndpoints);

  RefPtr<RedirectToRealChannelPromise> p = mPromise.Ensure(__func__);
  // We make the promise use direct task dispatch in order to reduce the number
  // of event loops iterations.
  mPromise.UseDirectTaskDispatch(__func__);

  nsresult rv =
      gHttpHandler->AsyncOnChannelRedirect(this, channel, aRedirectFlags);
  if (NS_FAILED(rv)) {
    LOG(
        ("ParentProcessDocumentChannel RedirectToRealChannel "
         "AsyncOnChannelRedirect failed [this=%p "
         "aRv=%d]",
         this, int(rv)));
    OnRedirectVerifyCallback(rv);
  }

  return p;
}

NS_IMETHODIMP
ParentProcessDocumentChannel::OnRedirectVerifyCallback(nsresult aResult) {
  LOG(
      ("ParentProcessDocumentChannel OnRedirectVerifyCallback [this=%p "
       "aResult=%d]",
       this, int(aResult)));

  MOZ_ASSERT(mDocumentLoadListener);

  if (NS_FAILED(aResult)) {
    Cancel(aResult);
  } else if (mCanceled) {
    aResult = NS_ERROR_ABORT;
  } else {
    const nsCOMPtr<nsIChannel> channel = mDocumentLoadListener->GetChannel();
    mLoadGroup->AddRequest(channel, nullptr);
    // Adding the channel to the loadgroup could have triggered a status
    // change with an observer being called destroying the docShell, resulting
    // in the PPDC to be canceled.
    if (mCanceled) {
      aResult = NS_ERROR_ABORT;
    } else {
      mLoadGroup->RemoveRequest(this, nullptr, NS_BINDING_REDIRECTED);
      for (auto& endpoint : mStreamFilterEndpoints) {
        extensions::StreamFilterParent::Attach(channel, std::move(endpoint));
      }

      RefPtr<ParentChannelWrapper> wrapper =
          new ParentChannelWrapper(channel, mListener);

      nsCOMPtr<nsIRedirectChannelRegistrar> registrar =
          RedirectChannelRegistrar::GetOrCreate();
      nsCOMPtr<nsIChannel> dummy;
      MOZ_ALWAYS_SUCCEEDS(
          NS_LinkRedirectChannels(mDocumentLoadListener->GetRedirectChannelId(),
                                  wrapper, getter_AddRefs(dummy)));
      MOZ_ASSERT(dummy == channel);
    }
  }

  mPromise.Resolve(aResult, __func__);

  return NS_OK;
}

NS_IMETHODIMP ParentProcessDocumentChannel::AsyncOpen(
    nsIStreamListener* aListener) {
  LOG(("ParentProcessDocumentChannel AsyncOpen [this=%p]", this));
  mDocumentLoadListener = new DocumentLoadListener(
      GetDocShell()->GetBrowsingContext()->Canonical());
  LOG(("Created PPDocumentChannel with listener=%p",
       mDocumentLoadListener.get()));

  // Add observers.
  nsCOMPtr<nsIObserverService> observerService =
      mozilla::services::GetObserverService();
  if (observerService) {
    MOZ_ALWAYS_SUCCEEDS(observerService->AddObserver(
        this, NS_HTTP_ON_MODIFY_REQUEST_TOPIC, false));
  }

  gHttpHandler->OnOpeningDocumentRequest(this);

  nsresult rv = NS_OK;
  Maybe<dom::ClientInfo> initialClientInfo = mInitialClientInfo;
  auto promise = mDocumentLoadListener->Open(
      mLoadState, mCacheKey, Some(mChannelId), mAsyncOpenTime, mTiming,
      std::move(initialClientInfo), GetDocShell()->GetOuterWindowID(),
      GetDocShell()
          ->GetBrowsingContext()
          ->HasValidTransientUserGestureActivation(),
      Some(mUriModified), Some(mIsXFOError), 0 /* ProcessId */, &rv);
  if (NS_FAILED(rv)) {
    MOZ_ASSERT(!promise);
    mDocumentLoadListener = nullptr;
    RemoveObserver();
    return rv;
  }

  mListener = aListener;
  if (mLoadGroup) {
    mLoadGroup->AddRequest(this, nullptr);
  }

  RefPtr<ParentProcessDocumentChannel> self = this;
  promise->Then(
      GetCurrentThreadSerialEventTarget(), __func__,
      [self](DocumentLoadListener::OpenPromiseSucceededType&& aResolveValue) {
        // The DLL is waiting for us to resolve the
        // RedirectToRealChannelPromise given as parameter.
        RefPtr<RedirectToRealChannelPromise> p =
            self->RedirectToRealChannel(
                    std::move(aResolveValue.mStreamFilterEndpoints),
                    aResolveValue.mRedirectFlags, aResolveValue.mLoadFlags)
                ->Then(
                    GetCurrentThreadSerialEventTarget(), __func__,
                    [self](RedirectToRealChannelPromise::ResolveOrRejectValue&&
                               aValue) -> RefPtr<RedirectToRealChannelPromise> {
                      MOZ_ASSERT(aValue.IsResolve());
                      nsresult rv = aValue.ResolveValue();
                      if (NS_FAILED(rv)) {
                        self->DisconnectChildListeners(rv, rv);
                      }
                      self->mLoadGroup = nullptr;
                      self->mListener = nullptr;
                      self->mCallbacks = nullptr;
                      self->RemoveObserver();
                      auto p =
                          MakeRefPtr<RedirectToRealChannelPromise::Private>(
                              __func__);
                      p->UseDirectTaskDispatch(__func__);
                      p->ResolveOrReject(std::move(aValue), __func__);
                      return p;
                    });
        // We chain the promise the DLL is waiting on to the one returned by
        // RedirectToRealChannel. As soon as the promise returned is
        // resolved or rejected, so will the DLL's promise.
        p->ChainTo(aResolveValue.mPromise.forget(), __func__);
      },
      [self](DocumentLoadListener::OpenPromiseFailedType&& aRejectValue) {
        self->DisconnectChildListeners(aRejectValue.mStatus,
                                       aRejectValue.mLoadGroupStatus);
        self->RemoveObserver();
      });
  return NS_OK;
}

NS_IMETHODIMP ParentProcessDocumentChannel::Cancel(nsresult aStatus) {
  LOG(("ParentProcessDocumentChannel Cancel [this=%p]", this));
  if (mCanceled) {
    return NS_OK;
  }

  mCanceled = true;
  // This will force the DocumentListener to abort the promise if there's one
  // pending.
  mDocumentLoadListener->Cancel(aStatus);

  return NS_OK;
}

void ParentProcessDocumentChannel::RemoveObserver() {
  if (nsCOMPtr<nsIObserverService> observerService =
          mozilla::services::GetObserverService()) {
    observerService->RemoveObserver(this, NS_HTTP_ON_MODIFY_REQUEST_TOPIC);
  }
}

////////////////////////////////////////////////////////////////////////////////
// nsIObserver
////////////////////////////////////////////////////////////////////////////////

NS_IMETHODIMP
ParentProcessDocumentChannel::Observe(nsISupports* aSubject, const char* aTopic,
                                      const char16_t* aData) {
  MOZ_ASSERT(NS_IsMainThread());

  if (mRequestObserversCalled) {
    // We have already emitted the event, we don't want to emit it again.
    // We only care about forwarding the first NS_HTTP_ON_MODIFY_REQUEST_TOPIC
    // encountered.
    return NS_OK;
  }
  nsCOMPtr<nsIChannel> channel = do_QueryInterface(aSubject);
  if (!channel || mDocumentLoadListener->GetChannel() != channel) {
    // Not a channel we are interested with.
    return NS_OK;
  }
  LOG(("DocumentChannelParent Observe [this=%p aChannel=%p]", this,
       channel.get()));
  if (!nsCRT::strcmp(aTopic, NS_HTTP_ON_MODIFY_REQUEST_TOPIC)) {
    mRequestObserversCalled = true;
    gHttpHandler->OnModifyDocumentRequest(this);
  }

  return NS_OK;
}

////////////////////////////////////////////////////////////////////////////////
// nsIParentChannel
////////////////////////////////////////////////////////////////////////////////

NS_IMETHODIMP
ParentChannelWrapper::SetParentListener(
    mozilla::net::ParentChannelListener* listener) {
  return NS_OK;
}

NS_IMETHODIMP
ParentChannelWrapper::NotifyFlashPluginStateChanged(
    nsIHttpChannel::FlashPluginState aState) {
  // For now, only HttpChannel use this attribute.
  RefPtr<HttpBaseChannel> httpChannel = do_QueryObject(mChannel.get());
  if (httpChannel) {
    httpChannel->SetFlashPluginState(aState);
  }
  return NS_OK;
}

NS_IMETHODIMP
ParentChannelWrapper::SetClassifierMatchedInfo(const nsACString& aList,
                                               const nsACString& aProvider,
                                               const nsACString& aFullHash) {
  nsCOMPtr<nsIClassifiedChannel> classifiedChannel =
      do_QueryInterface(mChannel);
  if (classifiedChannel) {
    classifiedChannel->SetMatchedInfo(aList, aProvider, aFullHash);
  }
  return NS_OK;
}

NS_IMETHODIMP
ParentChannelWrapper::SetClassifierMatchedTrackingInfo(
    const nsACString& aLists, const nsACString& aFullHash) {
  nsCOMPtr<nsIClassifiedChannel> classifiedChannel =
      do_QueryInterface(mChannel);
  if (classifiedChannel) {
    nsTArray<nsCString> lists, fullhashes;
    for (const nsACString& token : aLists.Split(',')) {
      lists.AppendElement(token);
    }
    for (const nsACString& token : aFullHash.Split(',')) {
      fullhashes.AppendElement(token);
    }
    classifiedChannel->SetMatchedTrackingInfo(lists, fullhashes);
  }
  return NS_OK;
}

NS_IMETHODIMP
ParentChannelWrapper::NotifyClassificationFlags(uint32_t aClassificationFlags,
                                                bool aIsThirdParty) {
  UrlClassifierCommon::SetClassificationFlagsHelper(
      mChannel, aClassificationFlags, aIsThirdParty);
  return NS_OK;
}

NS_IMETHODIMP
ParentChannelWrapper::Delete() { return NS_OK; }

}  // namespace net
}  // namespace mozilla

#undef LOG