Replace uses of ArgumentEncoder/ArgumentDecoder with MessageEncoder/MessageDecoder
[WebKit-https.git] / Source / WebKit2 / WebProcess / ResourceCache / WebResourceCacheManager.cpp
1 /*
2  * Copyright (C) 2011 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 "WebResourceCacheManager.h"
28
29 #include "Connection.h"
30 #include "MessageID.h"
31 #include "ResourceCachesToClear.h"
32 #include "SecurityOriginData.h"
33 #include "WebCoreArgumentCoders.h"
34 #include "WebResourceCacheManagerProxyMessages.h"
35 #include "WebProcess.h"
36 #include <WebCore/MemoryCache.h>
37 #include <WebCore/SecurityOrigin.h>
38 #include <WebCore/SecurityOriginHash.h>
39 #include <wtf/UnusedParam.h>
40
41 using namespace WebCore;
42
43 namespace WebKit {
44
45 WebResourceCacheManager& WebResourceCacheManager::shared()
46 {
47     static WebResourceCacheManager& shared = *new WebResourceCacheManager;
48     return shared;
49 }
50
51 WebResourceCacheManager::WebResourceCacheManager()
52 {
53 }
54
55 WebResourceCacheManager::~WebResourceCacheManager()
56 {
57 }
58
59 void WebResourceCacheManager::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::MessageDecoder& decoder)
60 {
61     didReceiveWebResourceCacheManagerMessage(connection, messageID, decoder);
62 }
63
64
65 void WebResourceCacheManager::getCacheOrigins(uint64_t callbackID) const
66 {
67     WebProcess::LocalTerminationDisabler terminationDisabler(WebProcess::shared());
68
69     MemoryCache::SecurityOriginSet origins;
70     memoryCache()->getOriginsWithCache(origins);
71
72 #if USE(CFURLCACHE)
73     RetainPtr<CFArrayRef> cfURLHosts = cfURLCacheHostNames();
74     CFIndex size = cfURLHosts ? CFArrayGetCount(cfURLHosts.get()) : 0;
75
76     String httpString("http");
77     for (CFIndex i = 0; i < size; ++i) {
78         CFStringRef host = static_cast<CFStringRef>(CFArrayGetValueAtIndex(cfURLHosts.get(), i));
79         origins.add(SecurityOrigin::create(httpString, host, 0));
80     }
81 #endif
82
83     // Create a list with the origins in both of the caches.
84     Vector<SecurityOriginData> identifiers;
85     identifiers.reserveCapacity(origins.size());
86
87     MemoryCache::SecurityOriginSet::iterator end = origins.end();
88     for (MemoryCache::SecurityOriginSet::iterator it = origins.begin(); it != end; ++it) {
89         RefPtr<SecurityOrigin> origin = *it;
90         
91         SecurityOriginData originData;
92         originData.protocol = origin->protocol();
93         originData.host = origin->host();
94         originData.port = origin->port();
95
96         identifiers.uncheckedAppend(originData);
97     }
98
99     WebProcess::shared().connection()->send(Messages::WebResourceCacheManagerProxy::DidGetCacheOrigins(identifiers, callbackID), 0);
100 }
101
102 void WebResourceCacheManager::clearCacheForOrigin(SecurityOriginData originData, uint32_t cachesToClear) const
103 {
104     WebProcess::LocalTerminationDisabler terminationDisabler(WebProcess::shared());
105
106 #if USE(CFURLCACHE)
107     ResourceCachesToClear resourceCachesToClear = static_cast<ResourceCachesToClear>(cachesToClear);
108 #else
109     UNUSED_PARAM(cachesToClear);
110 #endif
111
112     RefPtr<SecurityOrigin> origin = SecurityOrigin::create(originData.protocol, originData.host, originData.port);
113     if (!origin)
114         return;
115
116     memoryCache()->removeResourcesWithOrigin(origin.get());
117
118 #if USE(CFURLCACHE)
119     if (resourceCachesToClear != InMemoryResourceCachesOnly) { 
120         RetainPtr<CFMutableArrayRef> hostArray(AdoptCF, CFArrayCreateMutable(0, 0, &kCFTypeArrayCallBacks));
121         RetainPtr<CFStringRef> host(AdoptCF, origin->host().createCFString());
122         CFArrayAppendValue(hostArray.get(), host.get());
123
124         clearCFURLCacheForHostNames(hostArray.get());
125     }
126 #endif
127 }
128
129 void WebResourceCacheManager::clearCacheForAllOrigins(uint32_t cachesToClear) const
130 {
131     WebProcess::LocalTerminationDisabler terminationDisabler(WebProcess::shared());
132
133     ResourceCachesToClear resourceCachesToClear = static_cast<ResourceCachesToClear>(cachesToClear);
134
135     WebProcess::shared().clearResourceCaches(resourceCachesToClear);
136 }
137
138 } // namespace WebKit