Bug 1526591 - Remove devtools.inspector.shapesHighlighter.enabled pref. r=rcaliman
[gecko.git] / netwerk / base / PrivateBrowsingChannel.h
blobbfd4644d4131b393202846df9b7efeff6a2bee17
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"
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"
19 namespace mozilla {
20 namespace net {
22 template <class Channel>
23 class PrivateBrowsingChannel : public nsIPrivateBrowsingChannel {
24 public:
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
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;
39 mPrivateBrowsingOverriden = true;
40 mPrivateBrowsing = aPrivate;
41 return NS_OK;
44 NS_IMETHOD GetIsChannelPrivate(bool* aResult) override {
45 NS_ENSURE_ARG_POINTER(aResult);
46 *aResult = mPrivateBrowsing;
47 return NS_OK;
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;
57 return NS_OK;
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) {
64 return;
67 auto channel = static_cast<Channel*>(this);
69 nsCOMPtr<nsILoadContext> loadContext;
70 NS_QueryNotificationCallbacks(channel, loadContext);
71 if (loadContext) {
72 mPrivateBrowsing = loadContext->UsePrivateBrowsing();
73 return;
76 nsCOMPtr<nsILoadInfo> loadInfo;
77 Unused << channel->GetLoadInfo(getter_AddRefs(loadInfo));
78 if (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
87 // builds.
88 if (!aCallbacks) {
89 return true;
91 nsCOMPtr<nsILoadContext> loadContext = do_GetInterface(aCallbacks);
92 if (!loadContext) {
93 return true;
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
102 // builds.
103 if (!aLoadGroup) {
104 return true;
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);
113 protected:
114 bool mPrivateBrowsingOverriden;
115 bool mPrivateBrowsing;
118 } // namespace net
119 } // namespace mozilla
121 #endif