CookieServiceChild.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/CookieServiceChild.h"
  6. #include "mozilla/LoadInfo.h"
  7. #include "mozilla/BasePrincipal.h"
  8. #include "mozilla/ipc/URIUtils.h"
  9. #include "mozilla/net/NeckoChild.h"
  10. #include "nsIChannel.h"
  11. #include "nsIURI.h"
  12. #include "nsIPrefService.h"
  13. #include "nsIPrefBranch.h"
  14. #include "nsServiceManagerUtils.h"
  15. using namespace mozilla::ipc;
  16. namespace mozilla {
  17. namespace net {
  18. // Pref string constants
  19. static const char kPrefCookieBehavior[] = "network.cookie.cookieBehavior";
  20. static const char kPrefThirdPartySession[] =
  21. "network.cookie.thirdparty.sessionOnly";
  22. static CookieServiceChild *gCookieService;
  23. CookieServiceChild*
  24. CookieServiceChild::GetSingleton()
  25. {
  26. if (!gCookieService)
  27. gCookieService = new CookieServiceChild();
  28. NS_ADDREF(gCookieService);
  29. return gCookieService;
  30. }
  31. NS_IMPL_ISUPPORTS(CookieServiceChild,
  32. nsICookieService,
  33. nsIObserver,
  34. nsISupportsWeakReference)
  35. CookieServiceChild::CookieServiceChild()
  36. : mCookieBehavior(nsICookieService::BEHAVIOR_ACCEPT)
  37. , mThirdPartySession(false)
  38. {
  39. NS_ASSERTION(IsNeckoChild(), "not a child process");
  40. // This corresponds to Release() in DeallocPCookieService.
  41. NS_ADDREF_THIS();
  42. // Create a child PCookieService actor.
  43. NeckoChild::InitNeckoChild();
  44. gNeckoChild->SendPCookieServiceConstructor(this);
  45. // Init our prefs and observer.
  46. nsCOMPtr<nsIPrefBranch> prefBranch =
  47. do_GetService(NS_PREFSERVICE_CONTRACTID);
  48. NS_WARNING_ASSERTION(prefBranch, "no prefservice");
  49. if (prefBranch) {
  50. prefBranch->AddObserver(kPrefCookieBehavior, this, true);
  51. prefBranch->AddObserver(kPrefThirdPartySession, this, true);
  52. PrefChanged(prefBranch);
  53. }
  54. }
  55. CookieServiceChild::~CookieServiceChild()
  56. {
  57. gCookieService = nullptr;
  58. }
  59. void
  60. CookieServiceChild::PrefChanged(nsIPrefBranch *aPrefBranch)
  61. {
  62. int32_t val;
  63. if (NS_SUCCEEDED(aPrefBranch->GetIntPref(kPrefCookieBehavior, &val)))
  64. mCookieBehavior =
  65. val >= nsICookieService::BEHAVIOR_ACCEPT &&
  66. val <= nsICookieService::BEHAVIOR_LIMIT_FOREIGN
  67. ? val : nsICookieService::BEHAVIOR_ACCEPT;
  68. bool boolval;
  69. if (NS_SUCCEEDED(aPrefBranch->GetBoolPref(kPrefThirdPartySession, &boolval)))
  70. mThirdPartySession = !!boolval;
  71. if (!mThirdPartyUtil && RequireThirdPartyCheck()) {
  72. mThirdPartyUtil = do_GetService(THIRDPARTYUTIL_CONTRACTID);
  73. NS_ASSERTION(mThirdPartyUtil, "require ThirdPartyUtil service");
  74. }
  75. }
  76. bool
  77. CookieServiceChild::RequireThirdPartyCheck()
  78. {
  79. return mCookieBehavior == nsICookieService::BEHAVIOR_REJECT_FOREIGN ||
  80. mCookieBehavior == nsICookieService::BEHAVIOR_LIMIT_FOREIGN ||
  81. mThirdPartySession;
  82. }
  83. nsresult
  84. CookieServiceChild::GetCookieStringInternal(nsIURI *aHostURI,
  85. nsIChannel *aChannel,
  86. char **aCookieString,
  87. bool aFromHttp)
  88. {
  89. NS_ENSURE_ARG(aHostURI);
  90. NS_ENSURE_ARG_POINTER(aCookieString);
  91. *aCookieString = nullptr;
  92. // Fast past: don't bother sending IPC messages about nullprincipal'd
  93. // documents.
  94. nsAutoCString scheme;
  95. aHostURI->GetScheme(scheme);
  96. if (scheme.EqualsLiteral("moz-nullprincipal"))
  97. return NS_OK;
  98. // Determine whether the request is foreign. Failure is acceptable.
  99. bool isForeign = true;
  100. if (RequireThirdPartyCheck())
  101. mThirdPartyUtil->IsThirdPartyChannel(aChannel, aHostURI, &isForeign);
  102. URIParams uriParams;
  103. SerializeURI(aHostURI, uriParams);
  104. mozilla::NeckoOriginAttributes attrs;
  105. if (aChannel) {
  106. nsCOMPtr<nsILoadInfo> loadInfo = aChannel->GetLoadInfo();
  107. if (loadInfo) {
  108. attrs = loadInfo->GetOriginAttributes();
  109. }
  110. }
  111. // Synchronously call the parent.
  112. nsAutoCString result;
  113. SendGetCookieString(uriParams, !!isForeign, aFromHttp, attrs, &result);
  114. if (!result.IsEmpty())
  115. *aCookieString = ToNewCString(result);
  116. return NS_OK;
  117. }
  118. nsresult
  119. CookieServiceChild::SetCookieStringInternal(nsIURI *aHostURI,
  120. nsIChannel *aChannel,
  121. const char *aCookieString,
  122. const char *aServerTime,
  123. bool aFromHttp)
  124. {
  125. NS_ENSURE_ARG(aHostURI);
  126. NS_ENSURE_ARG_POINTER(aCookieString);
  127. // Fast past: don't bother sending IPC messages about nullprincipal'd
  128. // documents.
  129. nsAutoCString scheme;
  130. aHostURI->GetScheme(scheme);
  131. if (scheme.EqualsLiteral("moz-nullprincipal"))
  132. return NS_OK;
  133. // Determine whether the request is foreign. Failure is acceptable.
  134. bool isForeign = true;
  135. if (RequireThirdPartyCheck())
  136. mThirdPartyUtil->IsThirdPartyChannel(aChannel, aHostURI, &isForeign);
  137. nsDependentCString cookieString(aCookieString);
  138. nsDependentCString serverTime;
  139. if (aServerTime)
  140. serverTime.Rebind(aServerTime);
  141. URIParams uriParams;
  142. SerializeURI(aHostURI, uriParams);
  143. mozilla::NeckoOriginAttributes attrs;
  144. if (aChannel) {
  145. nsCOMPtr<nsILoadInfo> loadInfo = aChannel->GetLoadInfo();
  146. if (loadInfo) {
  147. attrs = loadInfo->GetOriginAttributes();
  148. }
  149. }
  150. // Synchronously call the parent.
  151. SendSetCookieString(uriParams, !!isForeign, cookieString, serverTime,
  152. aFromHttp, attrs);
  153. return NS_OK;
  154. }
  155. NS_IMETHODIMP
  156. CookieServiceChild::Observe(nsISupports *aSubject,
  157. const char *aTopic,
  158. const char16_t *aData)
  159. {
  160. NS_ASSERTION(strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0,
  161. "not a pref change topic!");
  162. nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(aSubject);
  163. if (prefBranch)
  164. PrefChanged(prefBranch);
  165. return NS_OK;
  166. }
  167. NS_IMETHODIMP
  168. CookieServiceChild::GetCookieString(nsIURI *aHostURI,
  169. nsIChannel *aChannel,
  170. char **aCookieString)
  171. {
  172. return GetCookieStringInternal(aHostURI, aChannel, aCookieString, false);
  173. }
  174. NS_IMETHODIMP
  175. CookieServiceChild::GetCookieStringFromHttp(nsIURI *aHostURI,
  176. nsIURI *aFirstURI,
  177. nsIChannel *aChannel,
  178. char **aCookieString)
  179. {
  180. return GetCookieStringInternal(aHostURI, aChannel, aCookieString, true);
  181. }
  182. NS_IMETHODIMP
  183. CookieServiceChild::SetCookieString(nsIURI *aHostURI,
  184. nsIPrompt *aPrompt,
  185. const char *aCookieString,
  186. nsIChannel *aChannel)
  187. {
  188. return SetCookieStringInternal(aHostURI, aChannel, aCookieString,
  189. nullptr, false);
  190. }
  191. NS_IMETHODIMP
  192. CookieServiceChild::SetCookieStringFromHttp(nsIURI *aHostURI,
  193. nsIURI *aFirstURI,
  194. nsIPrompt *aPrompt,
  195. const char *aCookieString,
  196. const char *aServerTime,
  197. nsIChannel *aChannel)
  198. {
  199. return SetCookieStringInternal(aHostURI, aChannel, aCookieString,
  200. aServerTime, true);
  201. }
  202. } // namespace net
  203. } // namespace mozilla