1/*
2 * Copyright (C) 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#import "config.h"
27#import "DiskCacheMonitor.h"
28
29#import "NetworkConnectionToWebProcess.h"
30#import "NetworkProcessConnectionMessages.h"
31#import "NetworkResourceLoader.h"
32#import "WebCoreArgumentCoders.h"
33
34#ifdef __has_include
35#if __has_include(<CFNetwork/CFURLCachePriv.h>)
36#include <CFNetwork/CFURLCachePriv.h>
37#endif
38#endif
39
40#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
41
42typedef void (^CFCachedURLResponseCallBackBlock)(CFCachedURLResponseRef);
43extern "C" void _CFCachedURLResponseSetBecameFileBackedCallBackBlock(CFCachedURLResponseRef, CFCachedURLResponseCallBackBlock, dispatch_queue_t);
44
45using namespace WebCore;
46
47namespace WebKit {
48
49// The maximum number of seconds we'll try to wait for a resource to be disk cached before we forget the request.
50static const double diskCacheMonitorTimeout = 20;
51
52void DiskCacheMonitor::monitorFileBackingStoreCreation(CFCachedURLResponseRef cachedResponse, NetworkResourceLoader* loader)
53{
54    if (!cachedResponse)
55        return;
56
57    ASSERT(loader);
58
59    new DiskCacheMonitor(cachedResponse, loader); // Balanced by adoptPtr in the blocks setup in the constructor, one of which is guaranteed to run.
60}
61
62DiskCacheMonitor::DiskCacheMonitor(CFCachedURLResponseRef cachedResponse, NetworkResourceLoader* loader)
63    : m_connectionToWebProcess(loader->connectionToWebProcess())
64    , m_resourceRequest(loader->request())
65{
66    ASSERT(isMainThread());
67
68    // Set up a delayed callback to cancel this monitor if the resource hasn't been cached yet.
69    __block DiskCacheMonitor* rawMonitor = this;
70
71    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * diskCacheMonitorTimeout), dispatch_get_main_queue(), ^{
72        adoptPtr(rawMonitor); // Balanced by `new DiskCacheMonitor` in monitorFileBackingStoreCreation.
73        rawMonitor = 0;
74    });
75
76    // Set up the disk caching callback to create the ShareableResource and send it to the WebProcess.
77    CFCachedURLResponseCallBackBlock block = ^(CFCachedURLResponseRef cachedResponse)
78    {
79        // If the monitor isn't there then it timed out before this resource was cached to disk.
80        if (!rawMonitor)
81            return;
82
83        OwnPtr<DiskCacheMonitor> monitor = adoptPtr(rawMonitor); // Balanced by `new DiskCacheMonitor` in monitorFileBackingStoreCreation.
84        rawMonitor = 0;
85
86        ShareableResource::Handle handle;
87        NetworkResourceLoader::tryGetShareableHandleFromCFURLCachedResponse(handle, cachedResponse);
88        if (handle.isNull())
89            return;
90
91        monitor->send(Messages::NetworkProcessConnection::DidCacheResource(monitor->resourceRequest(), handle));
92    };
93
94    _CFCachedURLResponseSetBecameFileBackedCallBackBlock(cachedResponse, block, dispatch_get_main_queue());
95}
96
97CoreIPC::Connection* DiskCacheMonitor::messageSenderConnection()
98{
99    return m_connectionToWebProcess->connection();
100}
101
102uint64_t DiskCacheMonitor::messageSenderDestinationID()
103{
104    return 0;
105}
106
107} // namespace WebKit
108
109#endif // #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
110