Home | History | Annotate | Download | only in ResourceCache
      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     WebProcess::LocalTerminationDisabler terminationDisabler(WebProcess::shared());
     67 
     68     MemoryCache::SecurityOriginSet origins;
     69     memoryCache()->getOriginsWithCache(origins);
     70 
     71 #if USE(CFURLCACHE)
     72     RetainPtr<CFArrayRef> cfURLHosts = cfURLCacheHostNames();
     73     CFIndex size = cfURLHosts ? CFArrayGetCount(cfURLHosts.get()) : 0;
     74 
     75     String httpString("http");
     76     for (CFIndex i = 0; i < size; ++i) {
     77         CFStringRef host = static_cast<CFStringRef>(CFArrayGetValueAtIndex(cfURLHosts.get(), i));
     78         origins.add(SecurityOrigin::create(httpString, host, 0));
     79     }
     80 #endif
     81 
     82     // Create a list with the origins in both of the caches.
     83     Vector<SecurityOriginData> identifiers;
     84     identifiers.reserveCapacity(origins.size());
     85 
     86     MemoryCache::SecurityOriginSet::iterator end = origins.end();
     87     for (MemoryCache::SecurityOriginSet::iterator it = origins.begin(); it != end; ++it) {
     88         RefPtr<SecurityOrigin> origin = *it;
     89 
     90         SecurityOriginData originData;
     91         originData.protocol = origin->protocol();
     92         originData.host = origin->host();
     93         originData.port = origin->port();
     94 
     95         identifiers.uncheckedAppend(originData);
     96     }
     97 
     98     WebProcess::shared().connection()->send(Messages::WebResourceCacheManagerProxy::DidGetCacheOrigins(identifiers, callbackID), 0);
     99 }
    100 
    101 void WebResourceCacheManager::clearCacheForOrigin(SecurityOriginData originData, uint32_t cachesToClear) const
    102 {
    103     WebProcess::LocalTerminationDisabler terminationDisabler(WebProcess::shared());
    104 
    105 #if USE(CFURLCACHE)
    106     ResourceCachesToClear resourceCachesToClear = static_cast<ResourceCachesToClear>(cachesToClear);
    107 #else
    108     UNUSED_PARAM(cachesToClear);
    109 #endif
    110 
    111     RefPtr<SecurityOrigin> origin = SecurityOrigin::create(originData.protocol, originData.host, originData.port);
    112     if (!origin)
    113         return;
    114 
    115     memoryCache()->removeResourcesWithOrigin(origin.get());
    116 
    117 #if USE(CFURLCACHE)
    118     if (resourceCachesToClear != InMemoryResourceCachesOnly) {
    119         RetainPtr<CFMutableArrayRef> hostArray(AdoptCF, CFArrayCreateMutable(0, 0, &kCFTypeArrayCallBacks));
    120         RetainPtr<CFStringRef> host(AdoptCF, origin->host().createCFString());
    121         CFArrayAppendValue(hostArray.get(), host.get());
    122 
    123         clearCFURLCacheForHostNames(hostArray.get());
    124     }
    125 #endif
    126 }
    127 
    128 void WebResourceCacheManager::clearCacheForAllOrigins(uint32_t cachesToClear) const
    129 {
    130     WebProcess::LocalTerminationDisabler terminationDisabler(WebProcess::shared());
    131 
    132     ResourceCachesToClear resourceCachesToClear = static_cast<ResourceCachesToClear>(cachesToClear);
    133 
    134     WebProcess::shared().clearResourceCaches(resourceCachesToClear);
    135 }
    136 
    137 } // namespace WebKit
    138