michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim:set ts=4 sts=4 sw=4 et cin: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_net_PrivateBrowsingChannel_h__ michael@0: #define mozilla_net_PrivateBrowsingChannel_h__ michael@0: michael@0: #include "nsIPrivateBrowsingChannel.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsILoadGroup.h" michael@0: #include "nsILoadContext.h" michael@0: #include "nsIInterfaceRequestorUtils.h" michael@0: #include "nsIInterfaceRequestor.h" michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: template michael@0: class PrivateBrowsingChannel : public nsIPrivateBrowsingChannel michael@0: { michael@0: public: michael@0: PrivateBrowsingChannel() : michael@0: mPrivateBrowsingOverriden(false), michael@0: mPrivateBrowsing(false) michael@0: { michael@0: } michael@0: michael@0: NS_IMETHOD SetPrivate(bool aPrivate) michael@0: { michael@0: // Make sure that we don't have a load context michael@0: // This is a fatal error in debug builds, and a runtime error in release michael@0: // builds. michael@0: nsCOMPtr loadContext; michael@0: NS_QueryNotificationCallbacks(static_cast(this), loadContext); michael@0: MOZ_ASSERT(!loadContext); michael@0: if (loadContext) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: mPrivateBrowsingOverriden = true; michael@0: mPrivateBrowsing = aPrivate; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHOD GetIsChannelPrivate(bool *aResult) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: *aResult = NS_UsePrivateBrowsing(static_cast(this)); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHOD IsPrivateModeOverriden(bool* aValue, bool *aResult) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aValue); michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: *aResult = mPrivateBrowsingOverriden; michael@0: if (mPrivateBrowsingOverriden) { michael@0: *aValue = mPrivateBrowsing; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool CanSetCallbacks(nsIInterfaceRequestor* aCallbacks) const michael@0: { michael@0: // Make sure that the private bit override flag is not set. michael@0: // This is a fatal error in debug builds, and a runtime error in release michael@0: // builds. michael@0: if (!aCallbacks) { michael@0: return true; michael@0: } michael@0: nsCOMPtr loadContext = do_GetInterface(aCallbacks); michael@0: if (!loadContext) { michael@0: return true; michael@0: } michael@0: MOZ_ASSERT(!mPrivateBrowsingOverriden); michael@0: return !mPrivateBrowsingOverriden; michael@0: } michael@0: michael@0: bool CanSetLoadGroup(nsILoadGroup* aLoadGroup) const michael@0: { michael@0: // Make sure that the private bit override flag is not set. michael@0: // This is a fatal error in debug builds, and a runtime error in release michael@0: // builds. michael@0: if (!aLoadGroup) { michael@0: return true; michael@0: } michael@0: nsCOMPtr callbacks; michael@0: aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks)); michael@0: // From this point on, we just hand off the work to CanSetCallbacks, michael@0: // because the logic is exactly the same. michael@0: return CanSetCallbacks(callbacks); michael@0: } michael@0: michael@0: protected: michael@0: bool mPrivateBrowsingOverriden; michael@0: bool mPrivateBrowsing; michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif michael@0: