Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* vim:set ts=4 sts=4 sw=4 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 : {
25 : public:
26 1193 : PrivateBrowsingChannel() :
27 : mPrivateBrowsingOverriden(false),
28 1193 : mPrivateBrowsing(false)
29 : {
30 1193 : }
31 :
32 1 : NS_IMETHOD SetPrivate(bool aPrivate)
33 : {
34 : // Make sure that we don't have a load context
35 : // This is a fatal error in debug builds, and a runtime error in release
36 : // builds.
37 2 : nsCOMPtr<nsILoadContext> loadContext;
38 1 : NS_QueryNotificationCallbacks(static_cast<Channel*>(this), loadContext);
39 1 : MOZ_ASSERT(!loadContext);
40 1 : if (loadContext) {
41 0 : return NS_ERROR_FAILURE;
42 : }
43 :
44 1 : mPrivateBrowsingOverriden = true;
45 1 : mPrivateBrowsing = aPrivate;
46 1 : return NS_OK;
47 : }
48 :
49 93 : NS_IMETHOD GetIsChannelPrivate(bool *aResult)
50 : {
51 93 : NS_ENSURE_ARG_POINTER(aResult);
52 93 : *aResult = mPrivateBrowsing;
53 93 : return NS_OK;
54 : }
55 :
56 0 : NS_IMETHOD IsPrivateModeOverriden(bool* aValue, bool *aResult)
57 : {
58 0 : NS_ENSURE_ARG_POINTER(aValue);
59 0 : NS_ENSURE_ARG_POINTER(aResult);
60 0 : *aResult = mPrivateBrowsingOverriden;
61 0 : if (mPrivateBrowsingOverriden) {
62 0 : *aValue = mPrivateBrowsing;
63 : }
64 0 : return NS_OK;
65 : }
66 :
67 : // Must be called every time the channel's callbacks or loadGroup is updated
68 256 : void UpdatePrivateBrowsing()
69 : {
70 : // once marked as private we never go un-private
71 256 : if (mPrivateBrowsing) {
72 254 : return;
73 : }
74 :
75 256 : auto channel = static_cast<Channel*>(this);
76 :
77 258 : nsCOMPtr<nsILoadContext> loadContext;
78 256 : NS_QueryNotificationCallbacks(channel, loadContext);
79 256 : if (loadContext) {
80 254 : mPrivateBrowsing = loadContext->UsePrivateBrowsing();
81 254 : return;
82 : }
83 :
84 4 : nsCOMPtr<nsILoadInfo> loadInfo;
85 2 : Unused << channel->GetLoadInfo(getter_AddRefs(loadInfo));
86 2 : if (loadInfo) {
87 4 : OriginAttributes attrs = loadInfo->GetOriginAttributes();
88 2 : mPrivateBrowsing = attrs.mPrivateBrowsingId > 0;
89 : }
90 : }
91 :
92 256 : bool CanSetCallbacks(nsIInterfaceRequestor* aCallbacks) const
93 : {
94 : // Make sure that the private bit override flag is not set.
95 : // This is a fatal error in debug builds, and a runtime error in release
96 : // builds.
97 256 : if (!aCallbacks) {
98 43 : return true;
99 : }
100 426 : nsCOMPtr<nsILoadContext> loadContext = do_GetInterface(aCallbacks);
101 213 : if (!loadContext) {
102 5 : return true;
103 : }
104 208 : MOZ_ASSERT(!mPrivateBrowsingOverriden);
105 208 : return !mPrivateBrowsingOverriden;
106 : }
107 :
108 72 : bool CanSetLoadGroup(nsILoadGroup* aLoadGroup) const
109 : {
110 : // Make sure that the private bit override flag is not set.
111 : // This is a fatal error in debug builds, and a runtime error in release
112 : // builds.
113 72 : if (!aLoadGroup) {
114 0 : return true;
115 : }
116 144 : nsCOMPtr<nsIInterfaceRequestor> callbacks;
117 72 : aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
118 : // From this point on, we just hand off the work to CanSetCallbacks,
119 : // because the logic is exactly the same.
120 72 : return CanSetCallbacks(callbacks);
121 : }
122 :
123 : protected:
124 : bool mPrivateBrowsingOverriden;
125 : bool mPrivateBrowsing;
126 : };
127 :
128 : } // namespace net
129 : } // namespace mozilla
130 :
131 : #endif
132 :
|