Changeset 218901 in webkit


Ignore:
Timestamp:
Jun 28, 2017 4:21:11 PM (7 years ago)
Author:
Brent Fulgham
Message:

Teach ResourceLoadStatistics to recognize changes in the file system
https://bugs.webkit.org/show_bug.cgi?id=173800
<rdar://problem/32937842>

Reviewed by Chris Dumez.

Source/WebCore:

We want to support the case where multiple UI processes choose to share the same
statistics file. To support this, update the ResourceLoadStatistics logic to be aware
that the statistics data file might change underneath it, and to take appropriate
action when it does.

  • WebCore.xcodeproj/project.pbxproj: Update for new sources.
  • WebCore/CMakeLists.txt: Update for new FileMonitor source file.
  • loader/ResourceLoadStatisticsStore.cpp:

(WebCore::ResourceLoadStatisticsStore::clearInMemoryAndPersistent): Use the new deletion
handler for the data file instead of writing out an empty file.
(WebCore::ResourceLoadStatisticsStore::setDeletePersistentStoreCallback): Added.

  • loader/ResourceLoadStatisticsStore.h:
  • platform/FileMonitor.cpp: Added.

(WebCore::FileMonitor::create):
(WebCore::FileMonitor::FileMonitor): Register handlers and begin monitoring file.
(WebCore::FileMonitor::~FileMonitor): Stop any active file monitoring.
(WebCore::FileMonitor::startMonitoringPath): Stub implementation.
(WebCore::FileMonitor::stopMonitoring): Ditto.

  • platform/FileMonitor.h: Added.
  • platform/FileSystem.h: Export files needed by WebKit2. Add support for O_EVTONLY

Darwin file handles.

  • platform/Logging.h: Add 'ResourceLoadStatistics' category.
  • platform/cocoa/FileMonitorCocoa.mm: Added.

(WebCore::FileMonitor::startMonitoringPath): Create a new VNODE type dispatch_source
to receive notifications when the specified file changes.
(WebCore::FileMonitor::stopMonitoring): Cancel the dispatch_source when we are done
monitoring the file.

  • platform/posix/FileSystemPOSIX.cpp: Update 'openFile' to understand the O_EVTONLY

mode of file handles (Darwin-only).

Source/WebKit2:

We want to support the case where multiple UI processes choose to share the same
statistics file. To support this, update the ResourceLoadStatistics logic to be aware
that the statistics data file might change underneath it, and to take appropriate
action when it does.

  • UIProcess/WebResourceLoadStatisticsStore.cpp:

(WebKit::WebResourceLoadStatisticsStore::setResourceLoadStatisticsEnabled): When we enable
the statistics, read in the data and then watch for changes.
(WebKit::WebResourceLoadStatisticsStore::registerSharedResourceLoadObserver): Add a
'delete data' handler to remove the file on disk, not just write an empty store.
(WebKit::WebResourceLoadStatisticsStore::readDataFromDiskIfNeeded): Use ASCIILiteral.
(WebKit::WebResourceLoadStatisticsStore::refreshFromDisk): Convenience function.
(WebKit::WebResourceLoadStatisticsStore::writeStoreToDisk): Sync with any on-disk
storage before writing out.
(WebKit::openAndLockFile): Convenience function.
(WebKit::closeAndUnlockFile): Ditto.
(WebKit::WebResourceLoadStatisticsStore::writeEncoderToDisk): Revise to lock file (on
platforms that support it) before writing to disk.
(WebKit::WebResourceLoadStatisticsStore::deleteStoreFromDisk): Added convenience function.
(WebKit::WebResourceLoadStatisticsStore::clearInMemoryData): Ditto.
(WebKit::WebResourceLoadStatisticsStore::startMonitoringStatisticsStorage): Create a
file system monitor to watch for file changes.
(WebKit::WebResourceLoadStatisticsStore::syncWithExistingStatisticsStorageIfNeeded): Added
convenience function.
(WebKit::WebResourceLoadStatisticsStore::createDecoderFromDisk): Revise to read data
directly so we can control file locking.

  • UIProcess/WebResourceLoadStatisticsStore.h:

2017-06-28 Wenson Hsieh <Wenson Hsieh>

Followup to r218885: adjust for further UIKit SPI changes
https://bugs.webkit.org/show_bug.cgi?id=173927
<rdar://problem/33020792>

Reviewed by Tim Horton.

On ToT, UIKit now invokes -_dragInteraction:item:shouldDelaySetDownAnimationWithCompletion: before the
completion block of -dragInteraction:willAnimateLiftWithAnimator:session: is called. This means we now need to
store the completion block in -shouldDelaySetDownAnimationWithCompletion: and wait until the UIDragAnimating
completion block in -willAnimateCancelWithAnimator: before invoking it.

  • UIProcess/ios/WKContentViewInteraction.h:
  • UIProcess/ios/WKContentViewInteraction.mm:

(-[WKContentView cleanUpDragSourceSessionState]):

Ensure that the set-down completion block is invoked when a drag session ends (e.g., if the web content process
crashes).

(-[WKContentView _dragInteraction:item:shouldDelaySetDownAnimationWithCompletion:]):
(-[WKContentView _api_dragInteraction:item:willAnimateCancelWithAnimator:]):

Source/WTF:

  • wtf/DispatchPtr.h: Added (moved from WebKit2)
  • wtf/Platform.h: Make sure USE_FILE_LOCK is enabled.

Tools:

  • TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
  • TestWebKitAPI/Tests/WebCore/FileMonitor.cpp: Added.

(TestWebKitAPI::FileMonitorTest::tempFilePath):
(TestWebKitAPI::handleFileModification):
(TestWebKitAPI::handleFileDeletion):
(TestWebKitAPI::resetTestState):
(TestWebKitAPI::TEST_F):

Location:
trunk
Files:
5 added
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r218872 r218901  
     12017-06-28  Brent Fulgham  <bfulgham@apple.com>
     2
     3        Teach ResourceLoadStatistics to recognize changes in the file system
     4        https://bugs.webkit.org/show_bug.cgi?id=173800
     5        <rdar://problem/32937842>
     6
     7        Reviewed by Chris Dumez.
     8
     9        * wtf/DispatchPtr.h: Added (moved from WebKit2)
     10        * wtf/Platform.h: Make sure USE_FILE_LOCK is enabled.
     11
    1122017-06-28  David Kilzer  <ddkilzer@apple.com>
    213
  • trunk/Source/WTF/WTF.xcodeproj/project.pbxproj

    r218151 r218901  
    383383                A8A4727E151A825A004123FF /* Deque.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Deque.h; sourceTree = "<group>"; };
    384384                A8A4727F151A825A004123FF /* DisallowCType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DisallowCType.h; sourceTree = "<group>"; };
     385                A8A4727F151A825A004123FF /* DispatchPtr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DispatchPtr.h; sourceTree = "<group>"; };
    385386                A8A47280151A825A004123FF /* DoublyLinkedList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DoublyLinkedList.h; sourceTree = "<group>"; };
    386387                A8A47282151A825A004123FF /* bignum-dtoa.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "bignum-dtoa.cc"; sourceTree = "<group>"; };
     
    773774                                A8A4727E151A825A004123FF /* Deque.h */,
    774775                                A8A4727F151A825A004123FF /* DisallowCType.h */,
     776                A8A4727F151A825A004123FF /* DispatchPtr.h */,
    775777                                0F4570421BE5B58F0062A629 /* Dominators.h */,
    776778                                A8A47280151A825A004123FF /* DoublyLinkedList.h */,
  • trunk/Source/WTF/wtf/Platform.h

    r218782 r218901  
    546546#define HAVE_OUT_OF_PROCESS_LAYER_HOSTING 1
    547547#define HAVE_DTRACE 0
     548#define USE_FILE_LOCK 1
    548549
    549550#if !PLATFORM(WATCHOS) && !PLATFORM(APPLETV)
  • trunk/Source/WebCore/CMakeLists.txt

    r218843 r218901  
    21782178    platform/FileChooser.cpp
    21792179    platform/FileHandle.cpp
     2180    platform/FileMonitor.cpp
    21802181    platform/FileStream.cpp
    21812182    platform/FileSystem.cpp
  • trunk/Source/WebCore/ChangeLog

    r218896 r218901  
     12017-06-28  Brent Fulgham  <bfulgham@apple.com>
     2
     3        Teach ResourceLoadStatistics to recognize changes in the file system
     4        https://bugs.webkit.org/show_bug.cgi?id=173800
     5        <rdar://problem/32937842>
     6
     7        Reviewed by Chris Dumez.
     8
     9        We want to support the case where multiple UI processes choose to share the same
     10        statistics file. To support this, update the ResourceLoadStatistics logic to be aware
     11        that the statistics data file might change underneath it, and to take appropriate
     12        action when it does.
     13
     14        * WebCore.xcodeproj/project.pbxproj: Update for new sources.
     15        * WebCore/CMakeLists.txt: Update for new FileMonitor source file.
     16        * loader/ResourceLoadStatisticsStore.cpp:
     17        (WebCore::ResourceLoadStatisticsStore::clearInMemoryAndPersistent): Use the new deletion
     18        handler for the data file instead of writing out an empty file.
     19        (WebCore::ResourceLoadStatisticsStore::setDeletePersistentStoreCallback): Added.
     20        * loader/ResourceLoadStatisticsStore.h:
     21        * platform/FileMonitor.cpp: Added.
     22        (WebCore::FileMonitor::create):
     23        (WebCore::FileMonitor::FileMonitor): Register handlers and begin monitoring file.
     24        (WebCore::FileMonitor::~FileMonitor): Stop any active file monitoring.
     25        (WebCore::FileMonitor::startMonitoringPath): Stub implementation.
     26        (WebCore::FileMonitor::stopMonitoring): Ditto.
     27        * platform/FileMonitor.h: Added.
     28        * platform/FileSystem.h: Export files needed by WebKit2. Add support for O_EVTONLY
     29        Darwin file handles.
     30        * platform/Logging.h: Add 'ResourceLoadStatistics' category.
     31        * platform/cocoa/FileMonitorCocoa.mm: Added.
     32        (WebCore::FileMonitor::startMonitoringPath): Create a new VNODE type dispatch_source
     33        to receive notifications when the specified file changes.
     34        (WebCore::FileMonitor::stopMonitoring): Cancel the dispatch_source when we are done
     35        monitoring the file.
     36        * platform/posix/FileSystemPOSIX.cpp: Update 'openFile' to understand the O_EVTONLY
     37        mode of file handles (Darwin-only).
     38
    1392017-06-28  Brady Eidson  <beidson@apple.com>
    240
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r218746 r218901  
    30673067                79F2F5A11091939A000D87CB /* CompositionEvent.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 79F2F59E1091939A000D87CB /* CompositionEvent.cpp */; };
    30683068                79F2F5A21091939A000D87CB /* CompositionEvent.h in Headers */ = {isa = PBXBuildFile; fileRef = 79F2F59F1091939A000D87CB /* CompositionEvent.h */; };
     3069                7A09CEF11F02069B00E93BDB /* FileMonitor.h in Headers */ = {isa = PBXBuildFile; fileRef = 7A09CEEC1F01CC9300E93BDB /* FileMonitor.h */; settings = {ATTRIBUTES = (Private, ); }; };
    30693070                7A0E770E10C00A8800A0276E /* InspectorFrontendHost.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7A0E770B10C00A8800A0276E /* InspectorFrontendHost.cpp */; };
    30703071                7A0E770F10C00A8800A0276E /* InspectorFrontendHost.h in Headers */ = {isa = PBXBuildFile; fileRef = 7A0E770C10C00A8800A0276E /* InspectorFrontendHost.h */; settings = {ATTRIBUTES = (Private, ); }; };
     
    30773078                7A29BA6A187B7C1D00F29CEB /* TemporaryOpenGLSetting.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7A29BA69187B781C00F29CEB /* TemporaryOpenGLSetting.cpp */; };
    30783079                7A29F57218C69514004D0F81 /* OutOfBandTextTrackPrivateAVF.h in Headers */ = {isa = PBXBuildFile; fileRef = 7A29F57118C69514004D0F81 /* OutOfBandTextTrackPrivateAVF.h */; };
     3080                7A32D7471F020EED00162C44 /* FileMonitorCocoa.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7A09CEF01F01D1F700E93BDB /* FileMonitorCocoa.mm */; };
     3081                7A32D7481F020EFF00162C44 /* FileMonitor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7A09CEEB1F01CC9300E93BDB /* FileMonitor.cpp */; };
    30793082                7A45032F18DB717200377B34 /* BufferedLineReader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7A45032D18DB717200377B34 /* BufferedLineReader.cpp */; };
    30803083                7A45033018DB717200377B34 /* BufferedLineReader.h in Headers */ = {isa = PBXBuildFile; fileRef = 7A45032E18DB717200377B34 /* BufferedLineReader.h */; };
     
    1113011133                79F2F59F1091939A000D87CB /* CompositionEvent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CompositionEvent.h; sourceTree = "<group>"; };
    1113111134                79F2F5A01091939A000D87CB /* CompositionEvent.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = CompositionEvent.idl; sourceTree = "<group>"; };
     11135                7A09CEEB1F01CC9300E93BDB /* FileMonitor.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = FileMonitor.cpp; sourceTree = "<group>"; };
     11136                7A09CEEC1F01CC9300E93BDB /* FileMonitor.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FileMonitor.h; sourceTree = "<group>"; };
     11137                7A09CEF01F01D1F700E93BDB /* FileMonitorCocoa.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = FileMonitorCocoa.mm; sourceTree = "<group>"; };
    1113211138                7A0E770B10C00A8800A0276E /* InspectorFrontendHost.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InspectorFrontendHost.cpp; sourceTree = "<group>"; };
    1113311139                7A0E770C10C00A8800A0276E /* InspectorFrontendHost.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InspectorFrontendHost.h; sourceTree = "<group>"; };
     
    2126121267                                7C7941E31C56C29300A4C58E /* DataDetectorsCoreSoftLink.h */,
    2126221268                                7C7941E21C56C29300A4C58E /* DataDetectorsCoreSoftLink.mm */,
     21269                                7A09CEF01F01D1F700E93BDB /* FileMonitorCocoa.mm */,
    2126321270                                514B3F750C722055000530DF /* FileSystemCocoa.mm */,
    2126421271                                A5C974CF11485FF10066F2AB /* KeyEventCocoa.h */,
     
    2413924146                                5351D4AF1DF916AC008010CD /* FileHandle.h */,
    2414024147                                4689F1AE1267BAE100E8D380 /* FileMetadata.h */,
     24148                                7A09CEEB1F01CC9300E93BDB /* FileMonitor.cpp */,
     24149                                7A09CEEC1F01CC9300E93BDB /* FileMonitor.h */,
    2414124150                                2EF1BFE8121C9F4200C27627 /* FileStream.cpp */,
    2414224151                                2EF1BFE9121C9F4200C27627 /* FileStream.h */,
     
    2656426573                                76CDD2F31103DA6600680521 /* AccessibilityMenuList.h in Headers */,
    2656526574                                76CDD2F71103DA6600680521 /* AccessibilityMenuListOption.h in Headers */,
     26575                                7A09CEF11F02069B00E93BDB /* FileMonitor.h in Headers */,
    2656626576                                76CDD2F51103DA6600680521 /* AccessibilityMenuListPopup.h in Headers */,
    2656726577                                29ACB212143E7128006BCA5F /* AccessibilityMockObject.h in Headers */,
     
    3084730857                                0719427F1D088F21002AA51D /* AVFoundationMIMETypeCache.mm in Sources */,
    3084830858                                070363E5181A1CDC00C074A5 /* AVMediaCaptureSource.mm in Sources */,
     30859                                7A32D7481F020EFF00162C44 /* FileMonitor.cpp in Sources */,
    3084930860                                CD336F6117F9F64700DDDCD0 /* AVTrackPrivateAVFObjCImpl.mm in Sources */,
    3085030861                                070363E7181A1CDC00C074A5 /* AVVideoCaptureSource.mm in Sources */,
     
    3396233973                                8AA61CFF144D595B00F37350 /* WebKitCSSRegionRule.cpp in Sources */,
    3396333974                                3FFFF9A8159D9A550020BBD5 /* WebKitCSSViewportRule.cpp in Sources */,
     33975                                7A32D7471F020EED00162C44 /* FileMonitorCocoa.mm in Sources */,
    3396433976                                1A1414B513A0F0500019996C /* WebKitFontFamilyNames.cpp in Sources */,
    3396533977                                2D0621441DA639B600A7FB26 /* WebKitMediaKeyMessageEvent.cpp in Sources */,
  • trunk/Source/WebCore/loader/ResourceLoadStatisticsStore.cpp

    r218865 r218901  
    159159    ASSERT(!isMainThread());
    160160    clearInMemory();
    161     if (m_writePersistentStoreHandler)
    162         m_writePersistentStoreHandler();
     161    if (m_deletePersistentStoreHandler)
     162        m_deletePersistentStoreHandler();
    163163    if (m_grandfatherExistingWebsiteDataHandler)
    164164        m_grandfatherExistingWebsiteDataHandler();
     
    221221{
    222222    m_grandfatherExistingWebsiteDataHandler = WTFMove(handler);
     223}
     224
     225void ResourceLoadStatisticsStore::setDeletePersistentStoreCallback(WTF::Function<void()>&& handler)
     226{
     227    m_deletePersistentStoreHandler = WTFMove(handler);
    223228}
    224229
  • trunk/Source/WebCore/loader/ResourceLoadStatisticsStore.h

    r218865 r218901  
    8585    WEBCORE_EXPORT void setShouldPartitionCookiesCallback(WTF::Function<void(const Vector<String>& domainsToRemove, const Vector<String>& domainsToAdd, bool clearFirst)>&&);
    8686    WEBCORE_EXPORT void setWritePersistentStoreCallback(WTF::Function<void()>&&);
     87    WEBCORE_EXPORT void setDeletePersistentStoreCallback(WTF::Function<void()>&&);
    8788    WEBCORE_EXPORT void setGrandfatherExistingWebsiteDataCallback(WTF::Function<void()>&&);
    8889    WEBCORE_EXPORT void setFireTelemetryCallback(WTF::Function<void()>&& handler);
     
    120121    WTF::Function<void()> m_writePersistentStoreHandler;
    121122    WTF::Function<void()> m_grandfatherExistingWebsiteDataHandler;
     123    WTF::Function<void()> m_deletePersistentStoreHandler;
    122124    WTF::Function<void()> m_fireTelemetryHandler;
    123125
  • trunk/Source/WebCore/platform/FileSystem.h

    r218160 r218901  
    2929 */
    3030
    31 #ifndef FileSystem_h
    32 #define FileSystem_h
     31#pragma once
    3332
    3433#include <time.h>
     
    7271enum FileOpenMode {
    7372    OpenForRead = 0,
    74     OpenForWrite
     73    OpenForWrite,
     74#if OS(DARWIN)
     75    OpenForEventsOnly
     76#endif
    7577};
    7678
     
    132134WEBCORE_EXPORT int writeToFile(PlatformFileHandle, const char* data, int length);
    133135// Returns number of bytes actually written if successful, -1 otherwise.
    134 int readFromFile(PlatformFileHandle, char* data, int length);
     136WEBCORE_EXPORT int readFromFile(PlatformFileHandle, char* data, int length);
    135137
    136138// Appends the contents of the file found at 'path' to the open PlatformFileHandle.
     
    142144
    143145#if USE(FILE_LOCK)
    144 bool lockFile(PlatformFileHandle, FileLockMode);
    145 bool unlockFile(PlatformFileHandle);
     146WEBCORE_EXPORT bool lockFile(PlatformFileHandle, FileLockMode);
     147WEBCORE_EXPORT bool unlockFile(PlatformFileHandle);
    146148#endif
    147149
     
    199201} // namespace WebCore
    200202
    201 #endif // FileSystem_h
  • trunk/Source/WebCore/platform/Logging.h

    r218615 r218901  
    11/*
    2  * Copyright (C) 2003, 2006, 2013, 2015, 2016 Apple Inc.  All rights reserved.
     2 * Copyright (C) 2003-2017 Apple Inc.  All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    7777    M(ResourceLoading) \
    7878    M(ResourceLoadObserver) \
     79    M(ResourceLoadStatistics) \
    7980    M(Scrolling) \
    8081    M(Services) \
  • trunk/Source/WebCore/platform/posix/FileSystemPOSIX.cpp

    r212484 r218901  
    8484    else if (mode == OpenForWrite)
    8585        platformFlag |= (O_WRONLY | O_CREAT | O_TRUNC);
     86#if OS(DARWIN)
     87    else if (mode == OpenForEventsOnly)
     88        platformFlag |= O_EVTONLY;
     89#endif
     90
    8691    return open(fsRep.data(), platformFlag, 0666);
    8792}
  • trunk/Source/WebKit2/ChangeLog

    r218899 r218901  
     12017-06-28  Brent Fulgham  <bfulgham@apple.com>
     2
     3        Teach ResourceLoadStatistics to recognize changes in the file system
     4        https://bugs.webkit.org/show_bug.cgi?id=173800
     5        <rdar://problem/32937842>
     6
     7        Reviewed by Chris Dumez.
     8
     9        We want to support the case where multiple UI processes choose to share the same
     10        statistics file. To support this, update the ResourceLoadStatistics logic to be aware
     11        that the statistics data file might change underneath it, and to take appropriate
     12        action when it does.
     13
     14        * UIProcess/WebResourceLoadStatisticsStore.cpp:
     15        (WebKit::WebResourceLoadStatisticsStore::setResourceLoadStatisticsEnabled): When we enable
     16        the statistics, read in the data and then watch for changes.
     17        (WebKit::WebResourceLoadStatisticsStore::registerSharedResourceLoadObserver): Add a
     18        'delete data' handler to remove the file on disk, not just write an empty store.
     19        (WebKit::WebResourceLoadStatisticsStore::readDataFromDiskIfNeeded): Use ASCIILiteral.
     20        (WebKit::WebResourceLoadStatisticsStore::refreshFromDisk): Convenience function.
     21        (WebKit::WebResourceLoadStatisticsStore::writeStoreToDisk): Sync with any on-disk
     22        storage before writing out.
     23        (WebKit::openAndLockFile): Convenience function.
     24        (WebKit::closeAndUnlockFile): Ditto.
     25        (WebKit::WebResourceLoadStatisticsStore::writeEncoderToDisk): Revise to lock file (on
     26        platforms that support it) before writing to disk.
     27        (WebKit::WebResourceLoadStatisticsStore::deleteStoreFromDisk): Added convenience function.
     28        (WebKit::WebResourceLoadStatisticsStore::clearInMemoryData): Ditto.
     29        (WebKit::WebResourceLoadStatisticsStore::startMonitoringStatisticsStorage): Create a
     30        file system monitor to watch for file changes.
     31        (WebKit::WebResourceLoadStatisticsStore::syncWithExistingStatisticsStorageIfNeeded): Added
     32        convenience function.
     33        (WebKit::WebResourceLoadStatisticsStore::createDecoderFromDisk): Revise to read data
     34        directly so we can control file locking.
     35        * UIProcess/WebResourceLoadStatisticsStore.h:
     36
    137 2017-06-28  Wenson Hsieh  <wenson_hsieh@apple.com>
    238
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheData.h

    r218457 r218901  
    11/*
    2  * Copyright (C) 2015 Apple Inc. All rights reserved.
     2 * Copyright (C) 2015-2017 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    3434#include <wtf/text/WTFString.h>
    3535
     36#if PLATFORM(COCOA)
     37#include <wtf/DispatchPtr.h>
     38#endif
     39
    3640#if USE(SOUP)
    3741#include <WebCore/GRefPtrSoup.h>
     
    4347
    4448namespace NetworkCache {
    45 
    46 #if PLATFORM(COCOA)
    47 template <typename T> class DispatchPtr;
    48 template <typename T> DispatchPtr<T> adoptDispatch(T dispatchObject);
    49 
    50 // FIXME: Use OSObjectPtr instead when it works with dispatch_data_t on all platforms.
    51 template<typename T> class DispatchPtr {
    52 public:
    53     DispatchPtr()
    54         : m_ptr(nullptr)
    55     {
    56     }
    57     explicit DispatchPtr(T ptr)
    58         : m_ptr(ptr)
    59     {
    60         if (m_ptr)
    61             dispatch_retain(m_ptr);
    62     }
    63     DispatchPtr(const DispatchPtr& other)
    64         : m_ptr(other.m_ptr)
    65     {
    66         if (m_ptr)
    67             dispatch_retain(m_ptr);
    68     }
    69     ~DispatchPtr()
    70     {
    71         if (m_ptr)
    72             dispatch_release(m_ptr);
    73     }
    74 
    75     DispatchPtr& operator=(const DispatchPtr& other)
    76     {
    77         auto copy = other;
    78         std::swap(m_ptr, copy.m_ptr);
    79         return *this;
    80     }
    81 
    82     T get() const { return m_ptr; }
    83     explicit operator bool() const { return m_ptr; }
    84 
    85     friend DispatchPtr adoptDispatch<T>(T);
    86 
    87 private:
    88     struct Adopt { };
    89     DispatchPtr(Adopt, T data)
    90         : m_ptr(data)
    91     {
    92     }
    93 
    94     T m_ptr;
    95 };
    96 
    97 template <typename T> DispatchPtr<T> adoptDispatch(T dispatchObject)
    98 {
    99     return DispatchPtr<T>(typename DispatchPtr<T>::Adopt { }, dispatchObject);
    100 }
    101 #endif
    10249
    10350class Data {
  • trunk/Source/WebKit2/UIProcess/WebResourceLoadStatisticsStore.cpp

    r218841 r218901  
    2727#include "WebResourceLoadStatisticsStore.h"
    2828
     29#include "Logging.h"
    2930#include "WebProcessMessages.h"
    3031#include "WebProcessPool.h"
     
    3435#include "WebsiteDataFetchOption.h"
    3536#include "WebsiteDataType.h"
     37#include <WebCore/FileMonitor.h>
     38#include <WebCore/FileSystem.h>
    3639#include <WebCore/KeyedCoding.h>
    3740#include <WebCore/ResourceLoadObserver.h>
     
    4346#include <wtf/Seconds.h>
    4447#include <wtf/threads/BinarySemaphore.h>
     48
     49#if PLATFORM(COCOA)
     50#define ENABLE_MULTIPROCESS_ACCESS_TO_STATISTICS_STORE 1
     51#endif
    4552
    4653using namespace WebCore;
     
    151158            });
    152159        }
    153            
     160
     161        stopMonitoringStatisticsStorage();
     162
    154163        writeStoreToDisk();
     164
     165        startMonitoringStatisticsStorage();
    155166    });
    156167}
     
    167178void WebResourceLoadStatisticsStore::setResourceLoadStatisticsEnabled(bool enabled)
    168179{
     180    ASSERT(RunLoop::isMain());
     181
    169182    if (enabled == m_resourceLoadStatisticsEnabled)
    170183        return;
     
    172185    m_resourceLoadStatisticsEnabled = enabled;
    173186
    174     readDataFromDiskIfNeeded();
     187    if (m_resourceLoadStatisticsEnabled) {
     188        readDataFromDiskIfNeeded();
     189        m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this)] () {
     190            startMonitoringStatisticsStorage();
     191        });
     192    } else
     193        m_statisticsQueue->dispatch([statisticsStorageMonitor = WTFMove(m_statisticsStorageMonitor)]  { });
    175194}
    176195
     
    198217    m_resourceLoadStatisticsStore->setGrandfatherExistingWebsiteDataCallback([this, protectedThis = makeRef(*this)]() {
    199218        grandfatherExistingWebsiteData();
     219    });
     220    m_resourceLoadStatisticsStore->setDeletePersistentStoreCallback([this, protectedThis = makeRef(*this)] {
     221        m_statisticsQueue->dispatch([this, protectedThis = protectedThis.copyRef()] {
     222            deleteStoreFromDisk();
     223        });
    200224    });
    201225    m_resourceLoadStatisticsStore->setFireTelemetryCallback([this, protectedThis = makeRef(*this)]() {
     
    240264
    241265    m_statisticsQueue->dispatch([this, protectedThis = makeRef(*this)] {
    242         auto decoder = createDecoderFromDisk("full_browsing_session");
     266        auto decoder = createDecoderFromDisk(ASCIILiteral("full_browsing_session"));
    243267        if (!decoder) {
    244268            grandfatherExistingWebsiteData();
     
    254278    });
    255279}
    256 
     280   
     281void WebResourceLoadStatisticsStore::refreshFromDisk()
     282{
     283    ASSERT(!RunLoop::isMain());
     284    auto decoder = createDecoderFromDisk(ASCIILiteral("full_browsing_session"));
     285    if (!decoder)
     286        return;
     287
     288    auto locker = holdLock(coreStore().statisticsLock());
     289    coreStore().readDataFromDecoder(*decoder);
     290}
     291   
    257292void WebResourceLoadStatisticsStore::processWillOpenConnection(WebProcessProxy&, IPC::Connection& connection)
    258293{
     
    288323    ASSERT(!RunLoop::isMain());
    289324   
     325    syncWithExistingStatisticsStorageIfNeeded();
     326
    290327    auto encoder = coreStore().createEncoderFromData();
    291328    writeEncoderToDisk(*encoder.get(), "full_browsing_session");
     329}
     330
     331static PlatformFileHandle openAndLockFile(const String& path, FileOpenMode mode)
     332{
     333    ASSERT(!RunLoop::isMain());
     334    auto handle = openFile(path, mode);
     335    if (handle == invalidPlatformFileHandle)
     336        return invalidPlatformFileHandle;
     337
     338#if ENABLE(MULTIPROCESS_ACCESS_TO_STATISTICS_STORE)
     339    bool locked = lockFile(handle, WebCore::LockExclusive);
     340    ASSERT_UNUSED(locked, locked);
     341#endif
     342
     343    return handle;
     344}
     345
     346static void closeAndUnlockFile(PlatformFileHandle handle)
     347{
     348    ASSERT(!RunLoop::isMain());
     349#if ENABLE(MULTIPROCESS_ACCESS_TO_STATISTICS_STORE)
     350    bool unlocked = unlockFile(handle);
     351    ASSERT_UNUSED(unlocked, unlocked);
     352#endif
     353    closeFile(handle);
    292354}
    293355
     
    309371    }
    310372
    311     auto handle = openFile(resourceLog, OpenForWrite);
    312     if (!handle)
     373    auto handle = openAndLockFile(resourceLog, OpenForWrite);
     374    if (handle == invalidPlatformFileHandle)
    313375        return;
    314376   
    315377    int64_t writtenBytes = writeToFile(handle, rawData->data(), rawData->size());
    316     closeFile(handle);
     378    closeAndUnlockFile(handle);
    317379
    318380    if (writtenBytes != static_cast<int64_t>(rawData->size()))
    319         WTFLogAlways("WebResourceLoadStatisticsStore: We only wrote %d out of %d bytes to disk", static_cast<unsigned>(writtenBytes), rawData->size());
    320 }
    321 
     381        RELEASE_LOG_ERROR(ResourceLoadStatistics, "WebResourceLoadStatisticsStore: We only wrote %d out of %d bytes to disk", static_cast<unsigned>(writtenBytes), rawData->size());
     382}
     383
     384void WebResourceLoadStatisticsStore::deleteStoreFromDisk()
     385{
     386    ASSERT(!RunLoop::isMain());
     387    String resourceLogPath = persistentStoragePath(ASCIILiteral("full_browsing_session"));
     388    if (resourceLogPath.isEmpty())
     389        return;
     390
     391    stopMonitoringStatisticsStorage();
     392
     393    if (!deleteFile(resourceLogPath))
     394        RELEASE_LOG_ERROR(ResourceLoadStatistics, "Unable to delete statistics file: %s", resourceLogPath.utf8().data());
     395}
     396
     397void WebResourceLoadStatisticsStore::clearInMemoryData()
     398{
     399    auto locker = holdLock(coreStore().statisticsLock());
     400    coreStore().clearInMemory();
     401}
     402
     403void WebResourceLoadStatisticsStore::startMonitoringStatisticsStorage()
     404{
     405    ASSERT(!RunLoop::isMain());
     406    if (m_statisticsStorageMonitor)
     407        return;
     408   
     409    String resourceLogPath = persistentStoragePath(ASCIILiteral("full_browsing_session"));
     410    if (resourceLogPath.isEmpty())
     411        return;
     412   
     413    m_statisticsStorageMonitor = FileMonitor::create(resourceLogPath, m_statisticsQueue.copyRef(), [this] (FileMonitor::FileChangeType type) {
     414        ASSERT(!RunLoop::isMain());
     415        switch (type) {
     416        case FileMonitor::FileChangeType::Modification:
     417            refreshFromDisk();
     418            break;
     419        case FileMonitor::FileChangeType::Removal:
     420            clearInMemoryData();
     421            m_statisticsStorageMonitor = nullptr;
     422            break;
     423        }
     424    });
     425
     426    m_statisticsStorageMonitor->startMonitoring();
     427}
     428
     429void WebResourceLoadStatisticsStore::stopMonitoringStatisticsStorage()
     430{
     431    ASSERT(!RunLoop::isMain());
     432    if (!m_statisticsStorageMonitor)
     433        return;
     434
     435    m_statisticsStorageMonitor = nullptr;
     436}
     437
     438void WebResourceLoadStatisticsStore::syncWithExistingStatisticsStorageIfNeeded()
     439{
     440    ASSERT(!RunLoop::isMain());
     441    if (m_statisticsStorageMonitor)
     442        return;
     443   
     444    refreshFromDisk();
     445   
     446    startMonitoringStatisticsStorage();
     447}
     448   
     449   
    322450#if !PLATFORM(COCOA)
    323451void WebResourceLoadStatisticsStore::platformExcludeFromBackup() const
     
    329457std::unique_ptr<KeyedDecoder> WebResourceLoadStatisticsStore::createDecoderFromDisk(const String& label) const
    330458{
     459    ASSERT(!RunLoop::isMain());
    331460    String resourceLog = persistentStoragePath(label);
    332461    if (resourceLog.isEmpty())
    333462        return nullptr;
    334463
    335     RefPtr<SharedBuffer> rawData = SharedBuffer::createWithContentsOfFile(resourceLog);
    336     if (!rawData)
     464    auto handle = openAndLockFile(resourceLog, OpenForRead);
     465    if (handle == invalidPlatformFileHandle)
    337466        return nullptr;
    338 
    339     return KeyedDecoder::decoder(reinterpret_cast<const uint8_t*>(rawData->data()), rawData->size());
     467   
     468    long long fileSize = 0;
     469    if (!getFileSize(handle, fileSize)) {
     470        closeAndUnlockFile(handle);
     471        return nullptr;
     472    }
     473   
     474    size_t bytesToRead;
     475    if (!WTF::convertSafely(fileSize, bytesToRead)) {
     476        closeAndUnlockFile(handle);
     477        return nullptr;
     478    }
     479
     480    Vector<char> buffer(bytesToRead);
     481    size_t totalBytesRead = readFromFile(handle, buffer.data(), buffer.size());
     482
     483    closeAndUnlockFile(handle);
     484
     485    if (totalBytesRead != bytesToRead)
     486        return nullptr;
     487
     488    return KeyedDecoder::decoder(reinterpret_cast<const uint8_t*>(buffer.data()), buffer.size());
    340489}
    341490
  • trunk/Source/WebKit2/UIProcess/WebResourceLoadStatisticsStore.h

    r218865 r218901  
    4747class KeyedDecoder;
    4848class KeyedEncoder;
     49class FileMonitor;
    4950struct ResourceLoadStatistics;
    5051}
     
    8687    void classifyResource(WebCore::ResourceLoadStatistics&);
    8788    void removeDataRecords();
     89    void startMonitoringStatisticsStorage();
     90    void stopMonitoringStatisticsStorage();
    8891
    8992    String persistentStoragePath(const String& label) const;
     
    98101    std::unique_ptr<WebCore::KeyedDecoder> createDecoderFromDisk(const String& label) const;
    99102    void platformExcludeFromBackup() const;
    100    
     103    void deleteStoreFromDisk();
     104    void clearInMemoryData();
     105    void syncWithExistingStatisticsStorageIfNeeded();
     106    void refreshFromDisk();
    101107    void telemetryTimerFired();
    102108
     
    108114#endif
    109115    Ref<WTF::WorkQueue> m_statisticsQueue;
     116    RefPtr<WebCore::FileMonitor> m_statisticsStorageMonitor;
    110117    String m_statisticsStoragePath;
    111118    bool m_resourceLoadStatisticsEnabled { false };
  • trunk/Tools/ChangeLog

    r218896 r218901  
     12017-06-28  Brent Fulgham  <bfulgham@apple.com>
     2
     3        Teach ResourceLoadStatistics to recognize changes in the file system
     4        https://bugs.webkit.org/show_bug.cgi?id=173800
     5        <rdar://problem/32937842>
     6
     7        Reviewed by Chris Dumez.
     8
     9        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
     10        * TestWebKitAPI/Tests/WebCore/FileMonitor.cpp: Added.
     11        (TestWebKitAPI::FileMonitorTest::tempFilePath):
     12        (TestWebKitAPI::handleFileModification):
     13        (TestWebKitAPI::handleFileDeletion):
     14        (TestWebKitAPI::resetTestState):
     15        (TestWebKitAPI::TEST_F):
     16
    1172017-06-28  Brady Eidson  <beidson@apple.com>
    218
  • trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj

    r218855 r218901  
    232232                7A010BCD1D877C0D00EDE72A /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7A010BCC1D877C0D00EDE72A /* QuartzCore.framework */; };
    233233                7A1458FC1AD5C07000E06772 /* mouse-button-listener.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 7A1458FB1AD5C03500E06772 /* mouse-button-listener.html */; };
     234                7A32D74A1F02151500162C44 /* FileMonitor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7A32D7491F02151500162C44 /* FileMonitor.cpp */; };
    234235                7A66BDB61EAF14EF00CCC924 /* LimitTitleSize.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7A66BDB51EAF14D000CCC924 /* LimitTitleSize.cpp */; };
    235236                7A66BDB81EAF18D500CCC924 /* set-long-title.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 7A66BDB71EAF150100CCC924 /* set-long-title.html */; };
     
    12611262                7A010BCC1D877C0D00EDE72A /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
    12621263                7A1458FB1AD5C03500E06772 /* mouse-button-listener.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "mouse-button-listener.html"; sourceTree = "<group>"; };
     1264                7A32D7491F02151500162C44 /* FileMonitor.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = FileMonitor.cpp; sourceTree = "<group>"; };
    12631265                7A38D7E51C752D5F004F157D /* HashCountedSet.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HashCountedSet.cpp; sourceTree = "<group>"; };
    12641266                7A5623101AD5AF3E0096B920 /* MenuTypesForMouseEvents.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MenuTypesForMouseEvents.cpp; sourceTree = "<group>"; };
     
    19301932                                26F6E1EF1ADC749B00DE696B /* DFAMinimizer.cpp */,
    19311933                                3151180F1DB1ADD500176304 /* ExtendedColor.cpp */,
     1934                                7A32D7491F02151500162C44 /* FileMonitor.cpp */,
    19321935                                41973B5A1AF2286A006C7B36 /* FileSystem.cpp */,
    19331936                                7A909A701D877475007E10F8 /* FloatPoint.cpp */,
     
    30253028                                CD78E11D1DB7EA660014A2DE /* FullscreenDelegate.mm in Sources */,
    30263029                                7C83E0BD1D0A650C00FEBCF3 /* FullscreenTopContentInset.mm in Sources */,
     3030                                7A32D74A1F02151500162C44 /* FileMonitor.cpp in Sources */,
    30273031                                CDBFCC451A9FF45300A7B691 /* FullscreenZoomInitialFrame.mm in Sources */,
    30283032                                7CCE7EF81A411AE600447C4C /* Geolocation.cpp in Sources */,
Note: See TracChangeset for help on using the changeset viewer.