tor-browser

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

PrivateBrowsingChannel.h (3591B)


      1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=4 sts=2 sw=2 et cin: */
      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 #ifndef mozilla_net_PrivateBrowsingChannel_h__
      8 #define mozilla_net_PrivateBrowsingChannel_h__
      9 
     10 #include "nsIPrivateBrowsingChannel.h"
     11 #include "nsCOMPtr.h"
     12 #include "nsILoadGroup.h"
     13 #include "nsILoadContext.h"
     14 #include "nsIInterfaceRequestorUtils.h"
     15 #include "nsIInterfaceRequestor.h"
     16 #include "nsNetUtil.h"
     17 
     18 namespace mozilla {
     19 namespace net {
     20 
     21 template <class Channel>
     22 class PrivateBrowsingChannel : public nsIPrivateBrowsingChannel {
     23 public:
     24  PrivateBrowsingChannel()
     25      : mPrivateBrowsingOverriden(false), mPrivateBrowsing(false) {}
     26 
     27  NS_IMETHOD SetPrivate(bool aPrivate) override {
     28    // Make sure that we don't have a load context
     29    // This is a fatal error in debug builds, and a runtime error in release
     30    // builds.
     31    nsCOMPtr<nsILoadContext> loadContext;
     32    NS_QueryNotificationCallbacks(static_cast<Channel*>(this), loadContext);
     33    MOZ_ASSERT(!loadContext);
     34    if (loadContext) {
     35      return NS_ERROR_FAILURE;
     36    }
     37 
     38    mPrivateBrowsingOverriden = true;
     39    mPrivateBrowsing = aPrivate;
     40    return NS_OK;
     41  }
     42 
     43  NS_IMETHOD GetIsChannelPrivate(bool* aResult) override {
     44    NS_ENSURE_ARG_POINTER(aResult);
     45    *aResult = mPrivateBrowsing;
     46    return NS_OK;
     47  }
     48 
     49  NS_IMETHOD IsPrivateModeOverriden(bool* aValue, bool* aResult) override {
     50    NS_ENSURE_ARG_POINTER(aValue);
     51    NS_ENSURE_ARG_POINTER(aResult);
     52    *aResult = mPrivateBrowsingOverriden;
     53    if (mPrivateBrowsingOverriden) {
     54      *aValue = mPrivateBrowsing;
     55    }
     56    return NS_OK;
     57  }
     58 
     59  // Must be called every time the channel's callbacks or loadGroup is updated
     60  void UpdatePrivateBrowsing() {
     61    // once marked as private we never go un-private
     62    if (mPrivateBrowsing) {
     63      return;
     64    }
     65 
     66    auto channel = static_cast<Channel*>(this);
     67 
     68    nsCOMPtr<nsILoadContext> loadContext;
     69    NS_QueryNotificationCallbacks(channel, loadContext);
     70    if (loadContext) {
     71      mPrivateBrowsing = loadContext->UsePrivateBrowsing();
     72      return;
     73    }
     74 
     75    nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
     76    OriginAttributes attrs = loadInfo->GetOriginAttributes();
     77    mPrivateBrowsing = attrs.IsPrivateBrowsing();
     78  }
     79 
     80  bool CanSetCallbacks(nsIInterfaceRequestor* aCallbacks) const {
     81    // Make sure that the private bit override flag is not set.
     82    // This is a fatal error in debug builds, and a runtime error in release
     83    // builds.
     84    if (!aCallbacks) {
     85      return true;
     86    }
     87    nsCOMPtr<nsILoadContext> loadContext = do_GetInterface(aCallbacks);
     88    if (!loadContext) {
     89      return true;
     90    }
     91    MOZ_ASSERT(!mPrivateBrowsingOverriden);
     92    return !mPrivateBrowsingOverriden;
     93  }
     94 
     95  bool CanSetLoadGroup(nsILoadGroup* aLoadGroup) const {
     96    // Make sure that the private bit override flag is not set.
     97    // This is a fatal error in debug builds, and a runtime error in release
     98    // builds.
     99    if (!aLoadGroup) {
    100      return true;
    101    }
    102    nsCOMPtr<nsIInterfaceRequestor> callbacks;
    103    aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
    104    // From this point on, we just hand off the work to CanSetCallbacks,
    105    // because the logic is exactly the same.
    106    return CanSetCallbacks(callbacks);
    107  }
    108 
    109 protected:
    110  bool mPrivateBrowsingOverriden;
    111  bool mPrivateBrowsing;
    112 };
    113 
    114 }  // namespace net
    115 }  // namespace mozilla
    116 
    117 #endif