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.

Mercurial (4a108e94d3e2)

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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim:set ts=4 sts=4 sw=4 et cin: */
/* 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/. */

#ifndef mozilla_net_PrivateBrowsingChannel_h__
#define mozilla_net_PrivateBrowsingChannel_h__

#include "nsIPrivateBrowsingChannel.h"
#include "nsCOMPtr.h"
#include "nsILoadGroup.h"
#include "nsILoadContext.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIInterfaceRequestor.h"

namespace mozilla {
namespace net {

template <class Channel>
class PrivateBrowsingChannel : public nsIPrivateBrowsingChannel
{
public:
  PrivateBrowsingChannel() :
    mPrivateBrowsingOverriden(false),
    mPrivateBrowsing(false)
  {
  }

  NS_IMETHOD SetPrivate(bool aPrivate)
  {
      // Make sure that we don't have a load context
      // This is a fatal error in debug builds, and a runtime error in release
      // builds.
      nsCOMPtr<nsILoadContext> loadContext;
      NS_QueryNotificationCallbacks(static_cast<Channel*>(this), loadContext);
      MOZ_ASSERT(!loadContext);
      if (loadContext) {
          return NS_ERROR_FAILURE;
      }

      mPrivateBrowsingOverriden = true;
      mPrivateBrowsing = aPrivate;
      return NS_OK;
  }

  NS_IMETHOD GetIsChannelPrivate(bool *aResult)
  {
      NS_ENSURE_ARG_POINTER(aResult);
      *aResult = NS_UsePrivateBrowsing(static_cast<Channel*>(this));
      return NS_OK;
  }

  NS_IMETHOD IsPrivateModeOverriden(bool* aValue, bool *aResult)
  {
      NS_ENSURE_ARG_POINTER(aValue);
      NS_ENSURE_ARG_POINTER(aResult);
      *aResult = mPrivateBrowsingOverriden;
      if (mPrivateBrowsingOverriden) {
          *aValue = mPrivateBrowsing;
      }
      return NS_OK;
  }

  bool CanSetCallbacks(nsIInterfaceRequestor* aCallbacks) const
  {
      // Make sure that the private bit override flag is not set.
      // This is a fatal error in debug builds, and a runtime error in release
      // builds.
      if (!aCallbacks) {
          return true;
      }
      nsCOMPtr<nsILoadContext> loadContext = do_GetInterface(aCallbacks);
      if (!loadContext) {
          return true;
      }
      MOZ_ASSERT(!mPrivateBrowsingOverriden);
      return !mPrivateBrowsingOverriden;
  }

  bool CanSetLoadGroup(nsILoadGroup* aLoadGroup) const
  {
      // Make sure that the private bit override flag is not set.
      // This is a fatal error in debug builds, and a runtime error in release
      // builds.
      if (!aLoadGroup) {
          return true;
      }
      nsCOMPtr<nsIInterfaceRequestor> callbacks;
      aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
      // From this point on, we just hand off the work to CanSetCallbacks,
      // because the logic is exactly the same.
      return CanSetCallbacks(callbacks);
  }

protected:
  bool mPrivateBrowsingOverriden;
  bool mPrivateBrowsing;
};

}
}

#endif