michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 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: #include "mozilla/net/CookieServiceParent.h" michael@0: #include "mozilla/dom/PContentParent.h" michael@0: #include "mozilla/net/NeckoParent.h" michael@0: michael@0: #include "mozilla/ipc/URIUtils.h" michael@0: #include "nsCookieService.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsPrintfCString.h" michael@0: #include "SerializedLoadContext.h" michael@0: michael@0: using namespace mozilla::ipc; michael@0: using mozilla::dom::PContentParent; michael@0: using mozilla::net::NeckoParent; michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: MOZ_WARN_UNUSED_RESULT michael@0: bool michael@0: CookieServiceParent::GetAppInfoFromParams(const IPC::SerializedLoadContext &aLoadContext, michael@0: uint32_t& aAppId, michael@0: bool& aIsInBrowserElement, michael@0: bool& aIsPrivate) michael@0: { michael@0: aAppId = NECKO_NO_APP_ID; michael@0: aIsInBrowserElement = false; michael@0: aIsPrivate = false; michael@0: michael@0: const char* error = NeckoParent::GetValidatedAppInfo(aLoadContext, michael@0: Manager()->Manager(), michael@0: &aAppId, michael@0: &aIsInBrowserElement); michael@0: if (error) { michael@0: NS_WARNING(nsPrintfCString("CookieServiceParent: GetAppInfoFromParams: " michael@0: "FATAL error: %s: KILLING CHILD PROCESS\n", michael@0: error).get()); michael@0: return false; michael@0: } michael@0: michael@0: if (aLoadContext.IsPrivateBitValid()) { michael@0: aIsPrivate = aLoadContext.mUsePrivateBrowsing; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: CookieServiceParent::CookieServiceParent() michael@0: { michael@0: // Instantiate the cookieservice via the service manager, so it sticks around michael@0: // until shutdown. michael@0: nsCOMPtr cs = do_GetService(NS_COOKIESERVICE_CONTRACTID); michael@0: michael@0: // Get the nsCookieService instance directly, so we can call internal methods. michael@0: mCookieService = michael@0: already_AddRefed(nsCookieService::GetSingleton()); michael@0: NS_ASSERTION(mCookieService, "couldn't get nsICookieService"); michael@0: } michael@0: michael@0: CookieServiceParent::~CookieServiceParent() michael@0: { michael@0: } michael@0: michael@0: bool michael@0: CookieServiceParent::RecvGetCookieString(const URIParams& aHost, michael@0: const bool& aIsForeign, michael@0: const bool& aFromHttp, michael@0: const IPC::SerializedLoadContext& michael@0: aLoadContext, michael@0: nsCString* aResult) michael@0: { michael@0: if (!mCookieService) michael@0: return true; michael@0: michael@0: // Deserialize URI. Having a host URI is mandatory and should always be michael@0: // provided by the child; thus we consider failure fatal. michael@0: nsCOMPtr hostURI = DeserializeURI(aHost); michael@0: if (!hostURI) michael@0: return false; michael@0: michael@0: uint32_t appId; michael@0: bool isInBrowserElement, isPrivate; michael@0: bool valid = GetAppInfoFromParams(aLoadContext, appId, michael@0: isInBrowserElement, isPrivate); michael@0: if (!valid) { michael@0: return false; michael@0: } michael@0: michael@0: mCookieService->GetCookieStringInternal(hostURI, aIsForeign, aFromHttp, appId, michael@0: isInBrowserElement, isPrivate, *aResult); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: CookieServiceParent::RecvSetCookieString(const URIParams& aHost, michael@0: const bool& aIsForeign, michael@0: const nsCString& aCookieString, michael@0: const nsCString& aServerTime, michael@0: const bool& aFromHttp, michael@0: const IPC::SerializedLoadContext& michael@0: aLoadContext) michael@0: { michael@0: if (!mCookieService) michael@0: return true; michael@0: michael@0: // Deserialize URI. Having a host URI is mandatory and should always be michael@0: // provided by the child; thus we consider failure fatal. michael@0: nsCOMPtr hostURI = DeserializeURI(aHost); michael@0: if (!hostURI) michael@0: return false; michael@0: michael@0: uint32_t appId; michael@0: bool isInBrowserElement, isPrivate; michael@0: bool valid = GetAppInfoFromParams(aLoadContext, appId, michael@0: isInBrowserElement, isPrivate); michael@0: if (!valid) { michael@0: return false; michael@0: } michael@0: michael@0: nsDependentCString cookieString(aCookieString, 0); michael@0: //TODO: bug 812475, pass a real channel object michael@0: mCookieService->SetCookieStringInternal(hostURI, aIsForeign, cookieString, michael@0: aServerTime, aFromHttp, appId, michael@0: isInBrowserElement, isPrivate, nullptr); michael@0: return true; michael@0: } michael@0: michael@0: mozilla::ipc::IProtocol* michael@0: CookieServiceParent::CloneProtocol(Channel* aChannel, michael@0: mozilla::ipc::ProtocolCloneContext* aCtx) michael@0: { michael@0: NeckoParent* manager = aCtx->GetNeckoParent(); michael@0: nsAutoPtr actor(manager->AllocPCookieServiceParent()); michael@0: if (!actor || !manager->RecvPCookieServiceConstructor(actor)) { michael@0: return nullptr; michael@0: } michael@0: return actor.forget(); michael@0: } michael@0: michael@0: } michael@0: } michael@0: