netwerk/base/src/PrivateBrowsingChannel.h

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:6c0c4e568c8f
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
17 namespace mozilla {
18 namespace net {
19
20 template <class Channel>
21 class PrivateBrowsingChannel : public nsIPrivateBrowsingChannel
22 {
23 public:
24 PrivateBrowsingChannel() :
25 mPrivateBrowsingOverriden(false),
26 mPrivateBrowsing(false)
27 {
28 }
29
30 NS_IMETHOD SetPrivate(bool aPrivate)
31 {
32 // Make sure that we don't have a load context
33 // This is a fatal error in debug builds, and a runtime error in release
34 // builds.
35 nsCOMPtr<nsILoadContext> loadContext;
36 NS_QueryNotificationCallbacks(static_cast<Channel*>(this), loadContext);
37 MOZ_ASSERT(!loadContext);
38 if (loadContext) {
39 return NS_ERROR_FAILURE;
40 }
41
42 mPrivateBrowsingOverriden = true;
43 mPrivateBrowsing = aPrivate;
44 return NS_OK;
45 }
46
47 NS_IMETHOD GetIsChannelPrivate(bool *aResult)
48 {
49 NS_ENSURE_ARG_POINTER(aResult);
50 *aResult = NS_UsePrivateBrowsing(static_cast<Channel*>(this));
51 return NS_OK;
52 }
53
54 NS_IMETHOD IsPrivateModeOverriden(bool* aValue, bool *aResult)
55 {
56 NS_ENSURE_ARG_POINTER(aValue);
57 NS_ENSURE_ARG_POINTER(aResult);
58 *aResult = mPrivateBrowsingOverriden;
59 if (mPrivateBrowsingOverriden) {
60 *aValue = mPrivateBrowsing;
61 }
62 return NS_OK;
63 }
64
65 bool CanSetCallbacks(nsIInterfaceRequestor* aCallbacks) const
66 {
67 // Make sure that the private bit override flag is not set.
68 // This is a fatal error in debug builds, and a runtime error in release
69 // builds.
70 if (!aCallbacks) {
71 return true;
72 }
73 nsCOMPtr<nsILoadContext> loadContext = do_GetInterface(aCallbacks);
74 if (!loadContext) {
75 return true;
76 }
77 MOZ_ASSERT(!mPrivateBrowsingOverriden);
78 return !mPrivateBrowsingOverriden;
79 }
80
81 bool CanSetLoadGroup(nsILoadGroup* aLoadGroup) const
82 {
83 // Make sure that the private bit override flag is not set.
84 // This is a fatal error in debug builds, and a runtime error in release
85 // builds.
86 if (!aLoadGroup) {
87 return true;
88 }
89 nsCOMPtr<nsIInterfaceRequestor> callbacks;
90 aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
91 // From this point on, we just hand off the work to CanSetCallbacks,
92 // because the logic is exactly the same.
93 return CanSetCallbacks(callbacks);
94 }
95
96 protected:
97 bool mPrivateBrowsingOverriden;
98 bool mPrivateBrowsing;
99 };
100
101 }
102 }
103
104 #endif
105

mercurial