tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ChannelInfo.cpp (2979B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=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
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "mozilla/dom/ChannelInfo.h"
      8 
      9 #include "mozilla/BasePrincipal.h"
     10 #include "mozilla/dom/Document.h"
     11 #include "mozilla/net/HttpBaseChannel.h"
     12 #include "nsCOMPtr.h"
     13 #include "nsContentUtils.h"
     14 #include "nsIChannel.h"
     15 #include "nsIGlobalObject.h"
     16 #include "nsIHttpChannel.h"
     17 #include "nsNetUtil.h"
     18 #include "nsSerializationHelper.h"
     19 
     20 using namespace mozilla;
     21 using namespace mozilla::dom;
     22 
     23 void ChannelInfo::InitFromDocument(Document* aDoc) {
     24  MOZ_ASSERT(NS_IsMainThread());
     25  MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
     26 
     27  nsCOMPtr<nsITransportSecurityInfo> securityInfo(aDoc->GetSecurityInfo());
     28  if (securityInfo) {
     29    SetSecurityInfo(securityInfo);
     30  }
     31 
     32  mInited = true;
     33 }
     34 
     35 void ChannelInfo::InitFromChannel(nsIChannel* aChannel) {
     36  MOZ_ASSERT(NS_IsMainThread());
     37  MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
     38 
     39  nsCOMPtr<nsITransportSecurityInfo> securityInfo;
     40  aChannel->GetSecurityInfo(getter_AddRefs(securityInfo));
     41  if (securityInfo) {
     42    SetSecurityInfo(securityInfo);
     43  }
     44 
     45  mInited = true;
     46 }
     47 
     48 void ChannelInfo::InitFromChromeGlobal(nsIGlobalObject* aGlobal) {
     49  MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
     50  MOZ_ASSERT(aGlobal);
     51 
     52  MOZ_RELEASE_ASSERT(aGlobal->PrincipalOrNull()->IsSystemPrincipal());
     53 
     54  mSecurityInfo = nullptr;
     55  mInited = true;
     56 }
     57 
     58 void ChannelInfo::InitFromTransportSecurityInfo(
     59    nsITransportSecurityInfo* aSecurityInfo) {
     60  MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
     61 
     62  mSecurityInfo = aSecurityInfo;
     63  mInited = true;
     64 }
     65 
     66 void ChannelInfo::SetSecurityInfo(nsITransportSecurityInfo* aSecurityInfo) {
     67  MOZ_ASSERT(!mSecurityInfo, "security info should only be set once");
     68  mSecurityInfo = aSecurityInfo;
     69 }
     70 
     71 nsresult ChannelInfo::ResurrectInfoOnChannel(nsIChannel* aChannel) {
     72  MOZ_ASSERT(NS_IsMainThread());
     73  MOZ_ASSERT(mInited);
     74 
     75  if (mSecurityInfo) {
     76    nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aChannel);
     77    MOZ_ASSERT(httpChannel);
     78    net::HttpBaseChannel* httpBaseChannel =
     79        static_cast<net::HttpBaseChannel*>(httpChannel.get());
     80    nsresult rv = httpBaseChannel->OverrideSecurityInfo(mSecurityInfo);
     81    if (NS_WARN_IF(NS_FAILED(rv))) {
     82      return rv;
     83    }
     84  }
     85 
     86  return NS_OK;
     87 }
     88 
     89 already_AddRefed<nsITransportSecurityInfo> ChannelInfo::SecurityInfo() const {
     90  // This may be called when mInited is false, for example if we try to store
     91  // a synthesized Response object into the Cache.  Uninitialized and empty
     92  // ChannelInfo objects are indistinguishable at the IPC level, so this is
     93  // fine.
     94  nsCOMPtr<nsITransportSecurityInfo> securityInfo(mSecurityInfo);
     95  return securityInfo.forget();
     96 }