CookieServiceParent.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/net/CookieServiceParent.h"
  6. #include "mozilla/dom/PContentParent.h"
  7. #include "mozilla/net/NeckoParent.h"
  8. #include "mozilla/BasePrincipal.h"
  9. #include "mozilla/ipc/URIUtils.h"
  10. #include "nsCookieService.h"
  11. #include "nsIChannel.h"
  12. #include "nsIScriptSecurityManager.h"
  13. #include "nsIPrivateBrowsingChannel.h"
  14. #include "nsNetCID.h"
  15. #include "nsPrintfCString.h"
  16. using namespace mozilla::ipc;
  17. using mozilla::BasePrincipal;
  18. using mozilla::NeckoOriginAttributes;
  19. using mozilla::PrincipalOriginAttributes;
  20. using mozilla::dom::PContentParent;
  21. using mozilla::net::NeckoParent;
  22. namespace {
  23. // Ignore failures from this function, as they only affect whether we do or
  24. // don't show a dialog box in private browsing mode if the user sets a pref.
  25. void
  26. CreateDummyChannel(nsIURI* aHostURI, NeckoOriginAttributes& aAttrs, bool aIsPrivate,
  27. nsIChannel** aChannel)
  28. {
  29. MOZ_ASSERT(aAttrs.mAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID);
  30. PrincipalOriginAttributes attrs;
  31. attrs.InheritFromNecko(aAttrs);
  32. nsCOMPtr<nsIPrincipal> principal =
  33. BasePrincipal::CreateCodebasePrincipal(aHostURI, attrs);
  34. if (!principal) {
  35. return;
  36. }
  37. nsCOMPtr<nsIURI> dummyURI;
  38. nsresult rv = NS_NewURI(getter_AddRefs(dummyURI), "about:blank");
  39. if (NS_FAILED(rv)) {
  40. return;
  41. }
  42. // The following channel is never openend, so it does not matter what
  43. // securityFlags we pass; let's follow the principle of least privilege.
  44. nsCOMPtr<nsIChannel> dummyChannel;
  45. NS_NewChannel(getter_AddRefs(dummyChannel), dummyURI, principal,
  46. nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED,
  47. nsIContentPolicy::TYPE_INVALID);
  48. nsCOMPtr<nsIPrivateBrowsingChannel> pbChannel = do_QueryInterface(dummyChannel);
  49. if (!pbChannel) {
  50. return;
  51. }
  52. pbChannel->SetPrivate(aIsPrivate);
  53. dummyChannel.forget(aChannel);
  54. return;
  55. }
  56. }
  57. namespace mozilla {
  58. namespace net {
  59. CookieServiceParent::CookieServiceParent()
  60. {
  61. // Instantiate the cookieservice via the service manager, so it sticks around
  62. // until shutdown.
  63. nsCOMPtr<nsICookieService> cs = do_GetService(NS_COOKIESERVICE_CONTRACTID);
  64. // Get the nsCookieService instance directly, so we can call internal methods.
  65. mCookieService =
  66. already_AddRefed<nsCookieService>(nsCookieService::GetSingleton());
  67. NS_ASSERTION(mCookieService, "couldn't get nsICookieService");
  68. }
  69. CookieServiceParent::~CookieServiceParent()
  70. {
  71. }
  72. void
  73. CookieServiceParent::ActorDestroy(ActorDestroyReason aWhy)
  74. {
  75. // Nothing needed here. Called right before destructor since this is a
  76. // non-refcounted class.
  77. }
  78. bool
  79. CookieServiceParent::RecvGetCookieString(const URIParams& aHost,
  80. const bool& aIsForeign,
  81. const bool& aFromHttp,
  82. const NeckoOriginAttributes& aAttrs,
  83. nsCString* aResult)
  84. {
  85. if (!mCookieService)
  86. return true;
  87. // Deserialize URI. Having a host URI is mandatory and should always be
  88. // provided by the child; thus we consider failure fatal.
  89. nsCOMPtr<nsIURI> hostURI = DeserializeURI(aHost);
  90. if (!hostURI)
  91. return false;
  92. bool isPrivate = aAttrs.mPrivateBrowsingId > 0;
  93. mCookieService->GetCookieStringInternal(hostURI, aIsForeign, aFromHttp, aAttrs,
  94. isPrivate, *aResult);
  95. return true;
  96. }
  97. bool
  98. CookieServiceParent::RecvSetCookieString(const URIParams& aHost,
  99. const bool& aIsForeign,
  100. const nsCString& aCookieString,
  101. const nsCString& aServerTime,
  102. const bool& aFromHttp,
  103. const NeckoOriginAttributes& aAttrs)
  104. {
  105. if (!mCookieService)
  106. return true;
  107. // Deserialize URI. Having a host URI is mandatory and should always be
  108. // provided by the child; thus we consider failure fatal.
  109. nsCOMPtr<nsIURI> hostURI = DeserializeURI(aHost);
  110. if (!hostURI)
  111. return false;
  112. bool isPrivate = aAttrs.mPrivateBrowsingId > 0;
  113. // This is a gross hack. We've already computed everything we need to know
  114. // for whether to set this cookie or not, but we need to communicate all of
  115. // this information through to nsICookiePermission, which indirectly
  116. // computes the information from the channel. We only care about the
  117. // aIsPrivate argument as nsCookieService::SetCookieStringInternal deals
  118. // with aIsForeign before we have to worry about nsCookiePermission trying
  119. // to use the channel to inspect it.
  120. nsCOMPtr<nsIChannel> dummyChannel;
  121. CreateDummyChannel(hostURI, const_cast<NeckoOriginAttributes&>(aAttrs),
  122. isPrivate, getter_AddRefs(dummyChannel));
  123. // NB: dummyChannel could be null if something failed in CreateDummyChannel.
  124. nsDependentCString cookieString(aCookieString, 0);
  125. mCookieService->SetCookieStringInternal(hostURI, aIsForeign, cookieString,
  126. aServerTime, aFromHttp, aAttrs,
  127. isPrivate, dummyChannel);
  128. return true;
  129. }
  130. } // namespace net
  131. } // namespace mozilla