153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)/*
253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *
453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * modification, are permitted provided that the following conditions are
653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * met:
753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *
853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *     * Redistributions of source code must retain the above copyright
953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * notice, this list of conditions and the following disclaimer.
1053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *     * Redistributions in binary form must reproduce the above
1153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
1253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * in the documentation and/or other materials provided with the
1353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * distribution.
1453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
1553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * contributors may be used to endorse or promote products derived from
1653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * this software without specific prior written permission.
1753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *
1853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) */
3053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
3153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "config.h"
3253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/loader/CookieJar.h"
3353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
3453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/dom/Document.h"
35d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "core/frame/LocalFrame.h"
365267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "core/loader/FrameLoaderClient.h"
3751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/Cookie.h"
385267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "public/platform/Platform.h"
395267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "public/platform/WebCookie.h"
405267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "public/platform/WebCookieJar.h"
415267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "public/platform/WebURL.h"
425267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "public/platform/WebVector.h"
4353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
44c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
4553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
4651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)static blink::WebCookieJar* toCookieJar(const Document* document)
4753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
4853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    if (!document || !document->frame())
4953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        return 0;
5007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    return document->frame()->loader().client()->cookieJar();
5153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
5253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
5353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)String cookies(const Document* document, const KURL& url)
5453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
5551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    blink::WebCookieJar* cookieJar = toCookieJar(document);
5653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    if (!cookieJar)
5753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        return String();
5853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    return cookieJar->cookies(url, document->firstPartyForCookies());
5953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
6053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
6153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)void setCookies(Document* document, const KURL& url, const String& cookieString)
6253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
6351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    blink::WebCookieJar* cookieJar = toCookieJar(document);
6453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    if (!cookieJar)
6553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        return;
6653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    cookieJar->setCookie(url, document->firstPartyForCookies(), cookieString);
6753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
6853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
6953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)bool cookiesEnabled(const Document* document)
7053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
7151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    blink::WebCookieJar* cookieJar = toCookieJar(document);
7253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    if (!cookieJar)
7353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        return false;
7453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    return cookieJar->cookiesEnabled(document->cookieURL(), document->firstPartyForCookies());
7553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
7653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
7753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)String cookieRequestHeaderFieldValue(const Document* document, const KURL& url)
7853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
7951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    blink::WebCookieJar* cookieJar = toCookieJar(document);
8053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    if (!cookieJar)
8153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        return String();
8253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    return cookieJar->cookieRequestHeaderFieldValue(url, document->firstPartyForCookies());
8353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
8453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
8553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)bool getRawCookies(const Document* document, const KURL& url, Vector<Cookie>& cookies)
8653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
8753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    cookies.clear();
8851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    blink::WebCookieJar* cookieJar = toCookieJar(document);
8953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    if (!cookieJar)
9053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        return false;
9151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    blink::WebVector<blink::WebCookie> webCookies;
9253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    cookieJar->rawCookies(url, document->firstPartyForCookies(), webCookies);
9353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    for (unsigned i = 0; i < webCookies.size(); ++i) {
9451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        const blink::WebCookie& webCookie = webCookies[i];
9553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        cookies.append(Cookie(webCookie.name, webCookie.value, webCookie.domain, webCookie.path,
9653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)                              webCookie.expires, webCookie.httpOnly, webCookie.secure, webCookie.session));
9753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    }
9853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    return true;
9953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
10053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
10153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)void deleteCookie(const Document* document, const KURL& url, const String& cookieName)
10253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
10351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    blink::WebCookieJar* cookieJar = toCookieJar(document);
10453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    if (!cookieJar)
10553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)        return;
10653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    cookieJar->deleteCookie(url, cookieName);
10753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
10853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
10953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
110