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