Home | History | Annotate | Download | only in loader
      1 /*
      2  * Copyright (C) 2013 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "core/loader/CookieJar.h"
     33 
     34 #include "core/dom/Document.h"
     35 #include "core/loader/FrameLoaderClient.h"
     36 #include "core/page/Frame.h"
     37 #include "core/platform/Cookie.h"
     38 #include "public/platform/Platform.h"
     39 #include "public/platform/WebCookie.h"
     40 #include "public/platform/WebCookieJar.h"
     41 #include "public/platform/WebURL.h"
     42 #include "public/platform/WebVector.h"
     43 
     44 namespace WebCore {
     45 
     46 static WebKit::WebCookieJar* toCookieJar(const Document* document)
     47 {
     48     if (!document || !document->frame())
     49         return 0;
     50     WebKit::WebCookieJar* cookieJar = document->frame()->loader()->client()->cookieJar();
     51     // FIXME: DRT depends on being able to get a cookie jar from Platform rather than
     52     // FrameLoaderClient. Delete this when DRT is deleted.
     53     if (!cookieJar)
     54         cookieJar = WebKit::Platform::current()->cookieJar();
     55     return cookieJar;
     56 }
     57 
     58 String cookies(const Document* document, const KURL& url)
     59 {
     60     WebKit::WebCookieJar* cookieJar = toCookieJar(document);
     61     if (!cookieJar)
     62         return String();
     63     return cookieJar->cookies(url, document->firstPartyForCookies());
     64 }
     65 
     66 void setCookies(Document* document, const KURL& url, const String& cookieString)
     67 {
     68     WebKit::WebCookieJar* cookieJar = toCookieJar(document);
     69     if (!cookieJar)
     70         return;
     71     cookieJar->setCookie(url, document->firstPartyForCookies(), cookieString);
     72 }
     73 
     74 bool cookiesEnabled(const Document* document)
     75 {
     76     WebKit::WebCookieJar* cookieJar = toCookieJar(document);
     77     if (!cookieJar)
     78         return false;
     79     return cookieJar->cookiesEnabled(document->cookieURL(), document->firstPartyForCookies());
     80 }
     81 
     82 String cookieRequestHeaderFieldValue(const Document* document, const KURL& url)
     83 {
     84     WebKit::WebCookieJar* cookieJar = toCookieJar(document);
     85     if (!cookieJar)
     86         return String();
     87     return cookieJar->cookieRequestHeaderFieldValue(url, document->firstPartyForCookies());
     88 }
     89 
     90 bool getRawCookies(const Document* document, const KURL& url, Vector<Cookie>& cookies)
     91 {
     92     cookies.clear();
     93     WebKit::WebCookieJar* cookieJar = toCookieJar(document);
     94     if (!cookieJar)
     95         return false;
     96     WebKit::WebVector<WebKit::WebCookie> webCookies;
     97     cookieJar->rawCookies(url, document->firstPartyForCookies(), webCookies);
     98     for (unsigned i = 0; i < webCookies.size(); ++i) {
     99         const WebKit::WebCookie& webCookie = webCookies[i];
    100         cookies.append(Cookie(webCookie.name, webCookie.value, webCookie.domain, webCookie.path,
    101                               webCookie.expires, webCookie.httpOnly, webCookie.secure, webCookie.session));
    102     }
    103     return true;
    104 }
    105 
    106 void deleteCookie(const Document* document, const KURL& url, const String& cookieName)
    107 {
    108     WebKit::WebCookieJar* cookieJar = toCookieJar(document);
    109     if (!cookieJar)
    110         return;
    111     cookieJar->deleteCookie(url, cookieName);
    112 }
    113 
    114 }
    115