1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/base/src/PrivateBrowsingChannel.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* vim:set ts=4 sts=4 sw=4 et cin: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozilla_net_PrivateBrowsingChannel_h__ 1.11 +#define mozilla_net_PrivateBrowsingChannel_h__ 1.12 + 1.13 +#include "nsIPrivateBrowsingChannel.h" 1.14 +#include "nsCOMPtr.h" 1.15 +#include "nsILoadGroup.h" 1.16 +#include "nsILoadContext.h" 1.17 +#include "nsIInterfaceRequestorUtils.h" 1.18 +#include "nsIInterfaceRequestor.h" 1.19 + 1.20 +namespace mozilla { 1.21 +namespace net { 1.22 + 1.23 +template <class Channel> 1.24 +class PrivateBrowsingChannel : public nsIPrivateBrowsingChannel 1.25 +{ 1.26 +public: 1.27 + PrivateBrowsingChannel() : 1.28 + mPrivateBrowsingOverriden(false), 1.29 + mPrivateBrowsing(false) 1.30 + { 1.31 + } 1.32 + 1.33 + NS_IMETHOD SetPrivate(bool aPrivate) 1.34 + { 1.35 + // Make sure that we don't have a load context 1.36 + // This is a fatal error in debug builds, and a runtime error in release 1.37 + // builds. 1.38 + nsCOMPtr<nsILoadContext> loadContext; 1.39 + NS_QueryNotificationCallbacks(static_cast<Channel*>(this), loadContext); 1.40 + MOZ_ASSERT(!loadContext); 1.41 + if (loadContext) { 1.42 + return NS_ERROR_FAILURE; 1.43 + } 1.44 + 1.45 + mPrivateBrowsingOverriden = true; 1.46 + mPrivateBrowsing = aPrivate; 1.47 + return NS_OK; 1.48 + } 1.49 + 1.50 + NS_IMETHOD GetIsChannelPrivate(bool *aResult) 1.51 + { 1.52 + NS_ENSURE_ARG_POINTER(aResult); 1.53 + *aResult = NS_UsePrivateBrowsing(static_cast<Channel*>(this)); 1.54 + return NS_OK; 1.55 + } 1.56 + 1.57 + NS_IMETHOD IsPrivateModeOverriden(bool* aValue, bool *aResult) 1.58 + { 1.59 + NS_ENSURE_ARG_POINTER(aValue); 1.60 + NS_ENSURE_ARG_POINTER(aResult); 1.61 + *aResult = mPrivateBrowsingOverriden; 1.62 + if (mPrivateBrowsingOverriden) { 1.63 + *aValue = mPrivateBrowsing; 1.64 + } 1.65 + return NS_OK; 1.66 + } 1.67 + 1.68 + bool CanSetCallbacks(nsIInterfaceRequestor* aCallbacks) const 1.69 + { 1.70 + // Make sure that the private bit override flag is not set. 1.71 + // This is a fatal error in debug builds, and a runtime error in release 1.72 + // builds. 1.73 + if (!aCallbacks) { 1.74 + return true; 1.75 + } 1.76 + nsCOMPtr<nsILoadContext> loadContext = do_GetInterface(aCallbacks); 1.77 + if (!loadContext) { 1.78 + return true; 1.79 + } 1.80 + MOZ_ASSERT(!mPrivateBrowsingOverriden); 1.81 + return !mPrivateBrowsingOverriden; 1.82 + } 1.83 + 1.84 + bool CanSetLoadGroup(nsILoadGroup* aLoadGroup) const 1.85 + { 1.86 + // Make sure that the private bit override flag is not set. 1.87 + // This is a fatal error in debug builds, and a runtime error in release 1.88 + // builds. 1.89 + if (!aLoadGroup) { 1.90 + return true; 1.91 + } 1.92 + nsCOMPtr<nsIInterfaceRequestor> callbacks; 1.93 + aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks)); 1.94 + // From this point on, we just hand off the work to CanSetCallbacks, 1.95 + // because the logic is exactly the same. 1.96 + return CanSetCallbacks(callbacks); 1.97 + } 1.98 + 1.99 +protected: 1.100 + bool mPrivateBrowsingOverriden; 1.101 + bool mPrivateBrowsing; 1.102 +}; 1.103 + 1.104 +} 1.105 +} 1.106 + 1.107 +#endif 1.108 +