1/*
2 * Copyright (C) 2012 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "CookieJar.h"
28
29#include "CookiesStrategy.h"
30#include "Document.h"
31#include "Frame.h"
32#include "FrameLoader.h"
33#include "NetworkingContext.h"
34#include "PlatformCookieJar.h"
35#include "PlatformStrategies.h"
36
37namespace WebCore {
38
39static NetworkingContext* networkingContext(const Document* document)
40{
41    // FIXME: Returning 0 means falling back to default context. That's not a choice that is appropriate to do at runtime
42    if (!document)
43        return 0;
44    Frame* frame = document->frame();
45    if (!frame)
46        return 0;
47
48    return frame->loader().networkingContext();
49}
50
51#if PLATFORM(COCOA) || USE(CFNETWORK) || USE(SOUP)
52inline NetworkStorageSession& storageSession(const Document* document)
53{
54    NetworkingContext* context = networkingContext(document);
55    return context ? context->storageSession() : NetworkStorageSession::defaultStorageSession();
56}
57#define LOCAL_SESSION(document) NetworkStorageSession& session = storageSession(document);
58#else
59#define LOCAL_SESSION(document) NetworkStorageSession session(networkingContext(document));
60#endif
61
62String cookies(const Document* document, const URL& url)
63{
64    LOCAL_SESSION(document)
65    return platformStrategies()->cookiesStrategy()->cookiesForDOM(session, document->firstPartyForCookies(), url);
66}
67
68void setCookies(Document* document, const URL& url, const String& cookieString)
69{
70    LOCAL_SESSION(document)
71    platformStrategies()->cookiesStrategy()->setCookiesFromDOM(session, document->firstPartyForCookies(), url, cookieString);
72}
73
74bool cookiesEnabled(const Document* document)
75{
76    LOCAL_SESSION(document)
77    return platformStrategies()->cookiesStrategy()->cookiesEnabled(session, document->firstPartyForCookies(), document->cookieURL());
78}
79
80String cookieRequestHeaderFieldValue(const Document* document, const URL& url)
81{
82    LOCAL_SESSION(document)
83    return platformStrategies()->cookiesStrategy()->cookieRequestHeaderFieldValue(session, document->firstPartyForCookies(), url);
84}
85
86bool getRawCookies(const Document* document, const URL& url, Vector<Cookie>& cookies)
87{
88    LOCAL_SESSION(document)
89    return platformStrategies()->cookiesStrategy()->getRawCookies(session, document->firstPartyForCookies(), url, cookies);
90}
91
92void deleteCookie(const Document* document, const URL& url, const String& cookieName)
93{
94    LOCAL_SESSION(document)
95    platformStrategies()->cookiesStrategy()->deleteCookie(session, url, cookieName);
96}
97
98}
99