1 | /* |
2 | * Copyright (C) 2017 Igalia S.L. |
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 | * |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
17 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
18 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "FileMonitor.h" |
28 | |
29 | #include <wtf/FileSystem.h> |
30 | #include <wtf/glib/GUniquePtr.h> |
31 | #include <wtf/threads/BinarySemaphore.h> |
32 | |
33 | namespace WebCore { |
34 | |
35 | FileMonitor::FileMonitor(const String& path, Ref<WorkQueue>&& handlerQueue, WTF::Function<void(FileChangeType)>&& modificationHandler) |
36 | : m_handlerQueue(WTFMove(handlerQueue)) |
37 | , m_modificationHandler(WTFMove(modificationHandler)) |
38 | { |
39 | if (path.isEmpty() || !m_modificationHandler) |
40 | return; |
41 | |
42 | Function<void ()> createPlatformMonitor = [&] { |
43 | auto file = adoptGRef(g_file_new_for_path(FileSystem::fileSystemRepresentation(path).data())); |
44 | GUniqueOutPtr<GError> error; |
45 | m_platformMonitor = adoptGRef(g_file_monitor(file.get(), G_FILE_MONITOR_NONE, nullptr, &error.outPtr())); |
46 | if (m_platformMonitor) |
47 | g_signal_connect(m_platformMonitor.get(), "changed" , G_CALLBACK(fileChangedCallback), this); |
48 | else |
49 | WTFLogAlways("Failed to create a monitor for path %s: %s" , path.utf8().data(), error->message); |
50 | }; |
51 | |
52 | // The monitor can be created in the work queue thread. |
53 | if (&m_handlerQueue->runLoop() == &RunLoop::current()) { |
54 | createPlatformMonitor(); |
55 | return; |
56 | } |
57 | |
58 | BinarySemaphore semaphore; |
59 | m_handlerQueue->dispatch([createPlatformMonitor = WTFMove(createPlatformMonitor), &semaphore] { |
60 | createPlatformMonitor(); |
61 | semaphore.signal(); |
62 | }); |
63 | semaphore.wait(); |
64 | } |
65 | |
66 | FileMonitor::~FileMonitor() |
67 | { |
68 | // The monitor can be destroyed in the work queue thread. |
69 | if (&m_handlerQueue->runLoop() == &RunLoop::current()) |
70 | return; |
71 | |
72 | BinarySemaphore semaphore; |
73 | m_handlerQueue->dispatch([&] { |
74 | m_platformMonitor = nullptr; |
75 | semaphore.signal(); |
76 | }); |
77 | semaphore.wait(); |
78 | } |
79 | |
80 | void FileMonitor::fileChangedCallback(GFileMonitor*, GFile*, GFile*, GFileMonitorEvent event, FileMonitor* monitor) |
81 | { |
82 | switch (event) { |
83 | case G_FILE_MONITOR_EVENT_DELETED: |
84 | monitor->didChange(FileChangeType::Removal); |
85 | break; |
86 | case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT: |
87 | case G_FILE_MONITOR_EVENT_CREATED: |
88 | monitor->didChange(FileChangeType::Modification); |
89 | break; |
90 | default: |
91 | break; |
92 | } |
93 | } |
94 | |
95 | void FileMonitor::didChange(FileChangeType type) |
96 | { |
97 | ASSERT(!isMainThread()); |
98 | if (type == FileChangeType::Removal) |
99 | m_platformMonitor = nullptr; |
100 | m_modificationHandler(type); |
101 | } |
102 | |
103 | } // namespace WebCore |
104 | |