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/frame/LocalFrame.h"
36 #include "core/loader/FrameLoaderClient.h"
37 #include "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 blink {
45
toCookieJar(const Document * document)46 static blink::WebCookieJar* toCookieJar(const Document* document)
47 {
48 if (!document || !document->frame())
49 return 0;
50 return document->frame()->loader().client()->cookieJar();
51 }
52
cookies(const Document * document,const KURL & url)53 String cookies(const Document* document, const KURL& url)
54 {
55 blink::WebCookieJar* cookieJar = toCookieJar(document);
56 if (!cookieJar)
57 return String();
58 return cookieJar->cookies(url, document->firstPartyForCookies());
59 }
60
setCookies(Document * document,const KURL & url,const String & cookieString)61 void setCookies(Document* document, const KURL& url, const String& cookieString)
62 {
63 blink::WebCookieJar* cookieJar = toCookieJar(document);
64 if (!cookieJar)
65 return;
66 cookieJar->setCookie(url, document->firstPartyForCookies(), cookieString);
67 }
68
cookiesEnabled(const Document * document)69 bool cookiesEnabled(const Document* document)
70 {
71 blink::WebCookieJar* cookieJar = toCookieJar(document);
72 if (!cookieJar)
73 return false;
74 return cookieJar->cookiesEnabled(document->cookieURL(), document->firstPartyForCookies());
75 }
76
cookieRequestHeaderFieldValue(const Document * document,const KURL & url)77 String cookieRequestHeaderFieldValue(const Document* document, const KURL& url)
78 {
79 blink::WebCookieJar* cookieJar = toCookieJar(document);
80 if (!cookieJar)
81 return String();
82 return cookieJar->cookieRequestHeaderFieldValue(url, document->firstPartyForCookies());
83 }
84
getRawCookies(const Document * document,const KURL & url,Vector<Cookie> & cookies)85 bool getRawCookies(const Document* document, const KURL& url, Vector<Cookie>& cookies)
86 {
87 cookies.clear();
88 blink::WebCookieJar* cookieJar = toCookieJar(document);
89 if (!cookieJar)
90 return false;
91 blink::WebVector<blink::WebCookie> webCookies;
92 cookieJar->rawCookies(url, document->firstPartyForCookies(), webCookies);
93 for (unsigned i = 0; i < webCookies.size(); ++i) {
94 const blink::WebCookie& webCookie = webCookies[i];
95 cookies.append(Cookie(webCookie.name, webCookie.value, webCookie.domain, webCookie.path,
96 webCookie.expires, webCookie.httpOnly, webCookie.secure, webCookie.session));
97 }
98 return true;
99 }
100
deleteCookie(const Document * document,const KURL & url,const String & cookieName)101 void deleteCookie(const Document* document, const KURL& url, const String& cookieName)
102 {
103 blink::WebCookieJar* cookieJar = toCookieJar(document);
104 if (!cookieJar)
105 return;
106 cookieJar->deleteCookie(url, cookieName);
107 }
108
109 }
110