Use sendWithAsyncReply for cookie getting/clearing functions
[WebKit.git] / Source / WebKit / UIProcess / WebCookieManagerProxy.h
1 /*
2  * Copyright (C) 2011, 2013 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 #pragma once
27
28 #include "APIObject.h"
29 #include "GenericCallback.h"
30 #include "MessageReceiver.h"
31 #include "WebContextSupplement.h"
32 #include "WebCookieManagerProxyClient.h"
33 #include <pal/SessionID.h>
34 #include <wtf/Forward.h>
35 #include <wtf/HashMap.h>
36 #include <wtf/RefPtr.h>
37
38 #if USE(SOUP)
39 #include "SoupCookiePersistentStorageType.h"
40 #endif
41
42 namespace API {
43 class Array;
44 }
45
46 namespace WebCore {
47 struct Cookie;
48 }
49
50 namespace WebKit {
51
52 class WebProcessPool;
53 class WebProcessProxy;
54
55 typedef GenericCallback<API::Array*> ArrayCallback;
56 typedef GenericCallback<HTTPCookieAcceptPolicy> HTTPCookieAcceptPolicyCallback;
57 typedef GenericCallback<const Vector<WebCore::Cookie>&> GetCookiesCallback;
58
59 class WebCookieManagerProxy : public API::ObjectImpl<API::Object::Type::CookieManager>, public WebContextSupplement, private IPC::MessageReceiver {
60 public:
61     static const char* supplementName();
62
63     static Ref<WebCookieManagerProxy> create(WebProcessPool*);
64     virtual ~WebCookieManagerProxy();
65
66     void initializeClient(const WKCookieManagerClientBase*);
67     
68     void getHostnamesWithCookies(PAL::SessionID, CompletionHandler<void(Vector<String>&&)>&&);
69     void deleteCookie(PAL::SessionID, const WebCore::Cookie&, CompletionHandler<void()>&&);
70     void deleteCookiesForHostnames(PAL::SessionID, const Vector<String>&);
71     void deleteAllCookies(PAL::SessionID);
72     void deleteAllCookiesModifiedSince(PAL::SessionID, WallTime, CompletionHandler<void()>&&);
73
74     void setCookies(PAL::SessionID, const Vector<WebCore::Cookie>&, CompletionHandler<void()>&&);
75     void setCookies(PAL::SessionID, const Vector<WebCore::Cookie>&, const URL&, const URL& mainDocumentURL, CompletionHandler<void()>&&);
76
77     void getAllCookies(PAL::SessionID, CompletionHandler<void(Vector<WebCore::Cookie>&&)>&&);
78     void getCookies(PAL::SessionID, const URL&, CompletionHandler<void(Vector<WebCore::Cookie>&&)>&&);
79
80     void setHTTPCookieAcceptPolicy(PAL::SessionID, HTTPCookieAcceptPolicy, CompletionHandler<void()>&&);
81     void getHTTPCookieAcceptPolicy(PAL::SessionID, CompletionHandler<void(HTTPCookieAcceptPolicy)>&&);
82
83     void startObservingCookieChanges(PAL::SessionID);
84     void stopObservingCookieChanges(PAL::SessionID);
85
86     void setCookieObserverCallback(PAL::SessionID, WTF::Function<void ()>&&);
87
88     class Observer {
89     public:
90         virtual ~Observer() { }
91         virtual void cookiesDidChange() = 0;
92         virtual void managerDestroyed() = 0;
93     };
94
95     void registerObserver(PAL::SessionID, Observer&);
96     void unregisterObserver(PAL::SessionID, Observer&);
97
98 #if USE(SOUP)
99     void setCookiePersistentStorage(PAL::SessionID, const String& storagePath, SoupCookiePersistentStorageType);
100     void getCookiePersistentStorage(PAL::SessionID, String& storagePath, SoupCookiePersistentStorageType&) const;
101 #endif
102
103     using API::Object::ref;
104     using API::Object::deref;
105
106 private:
107     WebCookieManagerProxy(WebProcessPool*);
108
109     void cookiesDidChange(PAL::SessionID);
110
111     // WebContextSupplement
112     void processPoolDestroyed() override;
113     void processDidClose(WebProcessProxy*) override;
114     void processDidClose(NetworkProcessProxy*) override;
115     void refWebContextSupplement() override;
116     void derefWebContextSupplement() override;
117
118     // IPC::MessageReceiver
119     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
120
121 #if PLATFORM(COCOA)
122     void persistHTTPCookieAcceptPolicy(HTTPCookieAcceptPolicy);
123 #endif
124
125     HashMap<PAL::SessionID, WTF::Function<void ()>> m_legacyCookieObservers;
126     HashMap<PAL::SessionID, HashSet<Observer*>> m_cookieObservers;
127
128     WebCookieManagerProxyClient m_client;
129
130 #if USE(SOUP)
131     using CookiePersistentStorageMap = HashMap<PAL::SessionID, std::pair<String, SoupCookiePersistentStorageType>>;
132     CookiePersistentStorageMap m_cookiePersistentStorageMap;
133 #endif
134 };
135
136 } // namespace WebKit