Bug 1845134 - Part 4: Update existing ui-icons to use the latest source from acorn...
[gecko.git] / netwerk / base / PrivateBrowsingChannel.h
bloba2e224f092e3d8aef3114081efb53b4ac22f3369
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 = channel->LoadInfo();
77 OriginAttributes attrs = loadInfo->GetOriginAttributes();
78 mPrivateBrowsing = attrs.mPrivateBrowsingId > 0;
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;
88 nsCOMPtr<nsILoadContext> loadContext = do_GetInterface(aCallbacks);
89 if (!loadContext) {
90 return true;
92 MOZ_ASSERT(!mPrivateBrowsingOverriden);
93 return !mPrivateBrowsingOverriden;
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;
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);
110 protected:
111 bool mPrivateBrowsingOverriden;
112 bool mPrivateBrowsing;
115 } // namespace net
116 } // namespace mozilla
118 #endif