PrivateBrowsingChannel.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #ifndef mozilla_net_PrivateBrowsingChannel_h__
  7. #define mozilla_net_PrivateBrowsingChannel_h__
  8. #include "nsIPrivateBrowsingChannel.h"
  9. #include "nsCOMPtr.h"
  10. #include "nsILoadGroup.h"
  11. #include "nsILoadContext.h"
  12. #include "nsIInterfaceRequestorUtils.h"
  13. #include "nsIInterfaceRequestor.h"
  14. #include "nsNetUtil.h"
  15. #include "mozilla/Unused.h"
  16. namespace mozilla {
  17. namespace net {
  18. template <class Channel>
  19. class PrivateBrowsingChannel : public nsIPrivateBrowsingChannel
  20. {
  21. public:
  22. PrivateBrowsingChannel() :
  23. mPrivateBrowsingOverriden(false),
  24. mPrivateBrowsing(false)
  25. {
  26. }
  27. NS_IMETHOD SetPrivate(bool aPrivate)
  28. {
  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;
  37. }
  38. mPrivateBrowsingOverriden = true;
  39. mPrivateBrowsing = aPrivate;
  40. return NS_OK;
  41. }
  42. NS_IMETHOD GetIsChannelPrivate(bool *aResult)
  43. {
  44. NS_ENSURE_ARG_POINTER(aResult);
  45. *aResult = mPrivateBrowsing;
  46. return NS_OK;
  47. }
  48. NS_IMETHOD IsPrivateModeOverriden(bool* aValue, bool *aResult)
  49. {
  50. NS_ENSURE_ARG_POINTER(aValue);
  51. NS_ENSURE_ARG_POINTER(aResult);
  52. *aResult = mPrivateBrowsingOverriden;
  53. if (mPrivateBrowsingOverriden) {
  54. *aValue = mPrivateBrowsing;
  55. }
  56. return NS_OK;
  57. }
  58. // Must be called every time the channel's callbacks or loadGroup is updated
  59. void UpdatePrivateBrowsing()
  60. {
  61. // once marked as private we never go un-private
  62. if (mPrivateBrowsing) {
  63. return;
  64. }
  65. auto channel = static_cast<Channel*>(this);
  66. nsCOMPtr<nsILoadContext> loadContext;
  67. NS_QueryNotificationCallbacks(channel, loadContext);
  68. if (loadContext) {
  69. mPrivateBrowsing = loadContext->UsePrivateBrowsing();
  70. return;
  71. }
  72. nsCOMPtr<nsILoadInfo> loadInfo;
  73. Unused << channel->GetLoadInfo(getter_AddRefs(loadInfo));
  74. if (loadInfo) {
  75. NeckoOriginAttributes attrs = loadInfo->GetOriginAttributes();
  76. mPrivateBrowsing = attrs.mPrivateBrowsingId > 0;
  77. }
  78. }
  79. bool CanSetCallbacks(nsIInterfaceRequestor* aCallbacks) const
  80. {
  81. // Make sure that the private bit override flag is not set.
  82. // This is a fatal error in debug builds, and a runtime error in release
  83. // builds.
  84. if (!aCallbacks) {
  85. return true;
  86. }
  87. nsCOMPtr<nsILoadContext> loadContext = do_GetInterface(aCallbacks);
  88. if (!loadContext) {
  89. return true;
  90. }
  91. MOZ_ASSERT(!mPrivateBrowsingOverriden);
  92. return !mPrivateBrowsingOverriden;
  93. }
  94. bool CanSetLoadGroup(nsILoadGroup* aLoadGroup) const
  95. {
  96. // Make sure that the private bit override flag is not set.
  97. // This is a fatal error in debug builds, and a runtime error in release
  98. // builds.
  99. if (!aLoadGroup) {
  100. return true;
  101. }
  102. nsCOMPtr<nsIInterfaceRequestor> callbacks;
  103. aLoadGroup->GetNotificationCallbacks(getter_AddRefs(callbacks));
  104. // From this point on, we just hand off the work to CanSetCallbacks,
  105. // because the logic is exactly the same.
  106. return CanSetCallbacks(callbacks);
  107. }
  108. protected:
  109. bool mPrivateBrowsingOverriden;
  110. bool mPrivateBrowsing;
  111. };
  112. } // namespace net
  113. } // namespace mozilla
  114. #endif