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 FrameLoader* loader = frame->loader(); 48 if (!loader) 49 return 0; 50 return loader->networkingContext(); 51} 52 53#if PLATFORM(MAC) || USE(CFNETWORK) || USE(SOUP) 54inline NetworkStorageSession& storageSession(const Document* document) 55{ 56 NetworkingContext* context = networkingContext(document); 57 return context ? context->storageSession() : NetworkStorageSession::defaultStorageSession(); 58} 59#define LOCAL_SESSION(document) NetworkStorageSession& session = storageSession(document); 60#else 61#define LOCAL_SESSION(document) NetworkStorageSession session(networkingContext(document)); 62#endif 63 64String cookies(const Document* document, const KURL& url) 65{ 66 LOCAL_SESSION(document) 67 return platformStrategies()->cookiesStrategy()->cookiesForDOM(session, document->firstPartyForCookies(), url); 68} 69 70void setCookies(Document* document, const KURL& url, const String& cookieString) 71{ 72 LOCAL_SESSION(document) 73 platformStrategies()->cookiesStrategy()->setCookiesFromDOM(session, document->firstPartyForCookies(), url, cookieString); 74} 75 76bool cookiesEnabled(const Document* document) 77{ 78 LOCAL_SESSION(document) 79 return platformStrategies()->cookiesStrategy()->cookiesEnabled(session, document->firstPartyForCookies(), document->cookieURL()); 80} 81 82String cookieRequestHeaderFieldValue(const Document* document, const KURL& url) 83{ 84 LOCAL_SESSION(document) 85 return platformStrategies()->cookiesStrategy()->cookieRequestHeaderFieldValue(session, document->firstPartyForCookies(), url); 86} 87 88bool getRawCookies(const Document* document, const KURL& url, Vector<Cookie>& cookies) 89{ 90 LOCAL_SESSION(document) 91 return platformStrategies()->cookiesStrategy()->getRawCookies(session, document->firstPartyForCookies(), url, cookies); 92} 93 94void deleteCookie(const Document* document, const KURL& url, const String& cookieName) 95{ 96 LOCAL_SESSION(document) 97 platformStrategies()->cookiesStrategy()->deleteCookie(session, url, cookieName); 98} 99 100} 101