Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim:set ts=4 sts=4 sw=4 et cin: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef mozilla_net_PrivateBrowsingChannel_h__ |
michael@0 | 8 | #define mozilla_net_PrivateBrowsingChannel_h__ |
michael@0 | 9 | |
michael@0 | 10 | #include "nsIPrivateBrowsingChannel.h" |
michael@0 | 11 | #include "nsCOMPtr.h" |
michael@0 | 12 | #include "nsILoadGroup.h" |
michael@0 | 13 | #include "nsILoadContext.h" |
michael@0 | 14 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 15 | #include "nsIInterfaceRequestor.h" |
michael@0 | 16 | |
michael@0 | 17 | namespace mozilla { |
michael@0 | 18 | namespace net { |
michael@0 | 19 | |
michael@0 | 20 | template <class Channel> |
michael@0 | 21 | class PrivateBrowsingChannel : public nsIPrivateBrowsingChannel |
michael@0 | 22 | { |
michael@0 | 23 | public: |
michael@0 | 24 | PrivateBrowsingChannel() : |
michael@0 | 25 | mPrivateBrowsingOverriden(false), |
michael@0 | 26 | mPrivateBrowsing(false) |
michael@0 | 27 | { |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | NS_IMETHOD SetPrivate(bool aPrivate) |
michael@0 | 31 | { |
michael@0 | 32 | // Make sure that we don't have a load context |
michael@0 | 33 | // This is a fatal error in debug builds, and a runtime error in release |
michael@0 | 34 | // builds. |
michael@0 | 35 | nsCOMPtr<nsILoadContext> loadContext; |
michael@0 | 36 | NS_QueryNotificationCallbacks(static_cast<Channel*>(this), loadContext); |
michael@0 | 37 | MOZ_ASSERT(!loadContext); |
michael@0 | 38 | if (loadContext) { |
michael@0 | 39 | return NS_ERROR_FAILURE; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | mPrivateBrowsingOverriden = true; |
michael@0 | 43 | mPrivateBrowsing = aPrivate; |
michael@0 | 44 | return NS_OK; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | NS_IMETHOD GetIsChannelPrivate(bool *aResult) |
michael@0 | 48 | { |
michael@0 | 49 | NS_ENSURE_ARG_POINTER(aResult); |
michael@0 | 50 | *aResult = NS_UsePrivateBrowsing(static_cast<Channel*>(this)); |
michael@0 | 51 | return NS_OK; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | NS_IMETHOD IsPrivateModeOverriden(bool* aValue, bool *aResult) |
michael@0 | 55 | { |
michael@0 | 56 | NS_ENSURE_ARG_POINTER(aValue); |
michael@0 | 57 | NS_ENSURE_ARG_POINTER(aResult); |
michael@0 | 58 | *aResult = mPrivateBrowsingOverriden; |
michael@0 | 59 | if (mPrivateBrowsingOverriden) { |
michael@0 | 60 | *aValue = mPrivateBrowsing; |
michael@0 | 61 | } |
michael@0 | 62 | return NS_OK; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | bool CanSetCallbacks(nsIInterfaceRequestor* aCallbacks) const |
michael@0 | 66 | { |
michael@0 | 67 | // Make sure that the private bit override flag is not set. |
michael@0 | 68 | // This is a fatal error in debug builds, and a runtime error in release |
michael@0 | 69 | // builds. |
michael@0 | 70 | if (!aCallbacks) { |
michael@0 | 71 | return true; |
michael@0 | 72 | } |
michael@0 | 73 | nsCOMPtr<nsILoadContext> loadContext = do_GetInterface(aCallbacks); |
michael@0 | 74 | if (!loadContext) { |
michael@0 | 75 | return true; |
michael@0 | 76 | } |
michael@0 | 77 | MOZ_ASSERT(!mPrivateBrowsingOverriden); |
michael@0 | 78 | return !mPrivateBrowsingOverriden; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | bool CanSetLoadGroup(nsILoadGroup* aLoadGroup) const |
michael@0 | 82 | { |
michael@0 | 83 | // Make sure that the private bit override flag is not set. |
michael@0 | 84 | // This is a fatal error in debug builds, and a runtime error in release |
michael@0 | 85 | // builds. |
michael@0 | 86 | if (!aLoadGroup) { |
michael@0 | 87 | return true; |
michael@0 | 88 | } |
michael@0 | 89 | nsCOMPtr<nsIInterfaceRequestor> callbacks; |
michael@0 | 90 | aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks)); |
michael@0 | 91 | // From this point on, we just hand off the work to CanSetCallbacks, |
michael@0 | 92 | // because the logic is exactly the same. |
michael@0 | 93 | return CanSetCallbacks(callbacks); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | protected: |
michael@0 | 97 | bool mPrivateBrowsingOverriden; |
michael@0 | 98 | bool mPrivateBrowsing; |
michael@0 | 99 | }; |
michael@0 | 100 | |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | #endif |
michael@0 | 105 |