Remove duplicate API from WKContext
[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
40 using namespace WebCore;
41
42 namespace WebKit {
43
44 WebResourceCacheManager& WebResourceCacheManager::shared()
45 {
46     static WebResourceCacheManager& shared = *new WebResourceCacheManager;
47     return shared;
48 }
49
50 WebResourceCacheManager::WebResourceCacheManager()
51 {
52 }
53
54 WebResourceCacheManager::~WebResourceCacheManager()
55 {
56 }
57
58 void WebResourceCacheManager::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
59 {
60     didReceiveWebResourceCacheManagerMessage(connection, messageID, arguments);
61 }
62
63
64 void WebResourceCacheManager::getCacheOrigins(uint64_t callbackID) const
65 {
66     MemoryCache::SecurityOriginSet origins;
67     memoryCache()->getOriginsWithCache(origins);
68
69 #if USE(CFURLCACHE)
70     RetainPtr<CFArrayRef> cfURLHosts = cfURLCacheHostNames();
71     CFIndex size = cfURLHosts ? CFArrayGetCount(cfURLHosts.get()) : 0;
72
73     String httpString("http");
74     for (CFIndex i = 0; i < size; ++i) {
75         CFStringRef host = static_cast<CFStringRef>(CFArrayGetValueAtIndex(cfURLHosts.get(), i));
76         origins.add(SecurityOrigin::create(httpString, host, 0));
77     }
78 #endif
79
80     // Create a list with the origins in both of the caches.
81     Vector<SecurityOriginData> identifiers;
82     identifiers.reserveCapacity(origins.size());
83
84     MemoryCache::SecurityOriginSet::iterator end = origins.end();
85     for (MemoryCache::SecurityOriginSet::iterator it = origins.begin(); it != end; ++it) {
86         RefPtr<SecurityOrigin> origin = *it;
87         
88         SecurityOriginData originData;
89         originData.protocol = origin->protocol();
90         originData.host = origin->host();
91         originData.port = origin->port();
92
93         identifiers.uncheckedAppend(originData);
94     }
95
96     WebProcess::shared().connection()->send(Messages::WebResourceCacheManagerProxy::DidGetCacheOrigins(identifiers, callbackID), 0);
97     WebProcess::shared().terminateIfPossible();
98 }
99
100 void WebResourceCacheManager::clearCacheForOrigin(SecurityOriginData originData, uint32_t cachesToClear) const
101 {
102     ResourceCachesToClear resourceCachesToClear = static_cast<ResourceCachesToClear>(cachesToClear);
103
104     RefPtr<SecurityOrigin> origin = SecurityOrigin::create(originData.protocol, originData.host, originData.port);
105     if (!origin)
106         return;
107
108     memoryCache()->removeResourcesWithOrigin(origin.get());
109
110 #if USE(CFURLCACHE)
111     if (resourceCachesToClear != InMemoryResourceCachesOnly) { 
112         RetainPtr<CFMutableArrayRef> hostArray(AdoptCF, CFArrayCreateMutable(0, 0, &kCFTypeArrayCallBacks));
113         RetainPtr<CFStringRef> host(AdoptCF, origin->host().createCFString());
114         CFArrayAppendValue(hostArray.get(), host.get());
115
116         clearCFURLCacheForHostNames(hostArray.get());
117     }
118 #endif
119     WebProcess::shared().terminateIfPossible();
120 }
121
122 void WebResourceCacheManager::clearCacheForAllOrigins(uint32_t cachesToClear) const
123 {
124     ResourceCachesToClear resourceCachesToClear = static_cast<ResourceCachesToClear>(cachesToClear);
125
126     WebProcess::shared().clearResourceCaches(resourceCachesToClear);
127     WebProcess::shared().terminateIfPossible();
128 }
129
130 } // namespace WebKit