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/. */
7 #ifndef mozilla_net_PrivateBrowsingChannel_h__
8 #define mozilla_net_PrivateBrowsingChannel_h__
10 #include "nsIPrivateBrowsingChannel.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"
22 template <class Channel
>
23 class PrivateBrowsingChannel
: public nsIPrivateBrowsingChannel
{
25 PrivateBrowsingChannel()
26 : mPrivateBrowsingOverriden(false), mPrivateBrowsing(false) {}
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
32 nsCOMPtr
<nsILoadContext
> loadContext
;
33 NS_QueryNotificationCallbacks(static_cast<Channel
*>(this), loadContext
);
34 MOZ_ASSERT(!loadContext
);
36 return NS_ERROR_FAILURE
;
39 mPrivateBrowsingOverriden
= true;
40 mPrivateBrowsing
= aPrivate
;
44 NS_IMETHOD
GetIsChannelPrivate(bool* aResult
) override
{
45 NS_ENSURE_ARG_POINTER(aResult
);
46 *aResult
= mPrivateBrowsing
;
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
;
60 // Must be called every time the channel's callbacks or loadGroup is updated
61 void UpdatePrivateBrowsing() {
62 // once marked as private we never go un-private
63 if (mPrivateBrowsing
) {
67 auto channel
= static_cast<Channel
*>(this);
69 nsCOMPtr
<nsILoadContext
> loadContext
;
70 NS_QueryNotificationCallbacks(channel
, loadContext
);
72 mPrivateBrowsing
= loadContext
->UsePrivateBrowsing();
76 nsCOMPtr
<nsILoadInfo
> loadInfo
;
77 Unused
<< channel
->GetLoadInfo(getter_AddRefs(loadInfo
));
79 OriginAttributes attrs
= loadInfo
->GetOriginAttributes();
80 mPrivateBrowsing
= attrs
.mPrivateBrowsingId
> 0;
84 bool CanSetCallbacks(nsIInterfaceRequestor
* aCallbacks
) const {
85 // Make sure that the private bit override flag is not set.
86 // This is a fatal error in debug builds, and a runtime error in release
91 nsCOMPtr
<nsILoadContext
> loadContext
= do_GetInterface(aCallbacks
);
95 MOZ_ASSERT(!mPrivateBrowsingOverriden
);
96 return !mPrivateBrowsingOverriden
;
99 bool CanSetLoadGroup(nsILoadGroup
* aLoadGroup
) const {
100 // Make sure that the private bit override flag is not set.
101 // This is a fatal error in debug builds, and a runtime error in release
106 nsCOMPtr
<nsIInterfaceRequestor
> callbacks
;
107 aLoadGroup
->GetNotificationCallbacks(getter_AddRefs(callbacks
));
108 // From this point on, we just hand off the work to CanSetCallbacks,
109 // because the logic is exactly the same.
110 return CanSetCallbacks(callbacks
);
114 bool mPrivateBrowsingOverriden
;
115 bool mPrivateBrowsing
;
119 } // namespace mozilla