DiskCacheMonitor.mm 4.3 KB

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