<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[225450] trunk/Source</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/225450">225450</a></dd>
<dt>Author</dt> <dd>beidson@apple.com</dd>
<dt>Date</dt> <dd>2017-12-02 10:00:19 -0800 (Sat, 02 Dec 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>Factor out the "databaseTaskQueue" parts of IDBServer into something reusable.
https://bugs.webkit.org/show_bug.cgi?id=180298

Reviewed by Chris Dumez.

Source/WebCore:

No new tests (Refactor only).

* Modules/indexeddb/server/IDBServer.cpp:
(WebCore::IDBServer::IDBServer::IDBServer):
(WebCore::IDBServer::IDBServer::postDatabaseTask):
(WebCore::IDBServer::IDBServer::postDatabaseTaskReply):
(WebCore::IDBServer::IDBServer::databaseRunLoop): Deleted.
(WebCore::IDBServer::IDBServer::handleTaskRepliesOnMainThread): Deleted.
* Modules/indexeddb/server/IDBServer.h:
(): Deleted.

Source/WTF:

* WTF.xcodeproj/project.pbxproj:
* wtf/CMakeLists.txt:

* wtf/CrossThreadTaskHandler.cpp: Added.
(WTF::CrossThreadTaskHandler::CrossThreadTaskHandler):
(WTF::CrossThreadTaskHandler::~CrossThreadTaskHandler):
(WTF::CrossThreadTaskHandler::postTask):
(WTF::CrossThreadTaskHandler::postTaskReply):
(WTF::CrossThreadTaskHandler::taskRunLoop):
(WTF::CrossThreadTaskHandler::handleTaskRepliesOnMainThread):
* wtf/CrossThreadTaskHandler.h: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFWTFxcodeprojprojectpbxproj">trunk/Source/WTF/WTF.xcodeproj/project.pbxproj</a></li>
<li><a href="#trunkSourceWTFwtfCMakeListstxt">trunk/Source/WTF/wtf/CMakeLists.txt</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreModulesindexeddbserverIDBServercpp">trunk/Source/WebCore/Modules/indexeddb/server/IDBServer.cpp</a></li>
<li><a href="#trunkSourceWebCoreModulesindexeddbserverIDBServerh">trunk/Source/WebCore/Modules/indexeddb/server/IDBServer.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceWTFwtfCrossThreadTaskHandlercpp">trunk/Source/WTF/wtf/CrossThreadTaskHandler.cpp</a></li>
<li><a href="#trunkSourceWTFwtfCrossThreadTaskHandlerh">trunk/Source/WTF/wtf/CrossThreadTaskHandler.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (225449 => 225450)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog       2017-12-02 17:55:24 UTC (rev 225449)
+++ trunk/Source/WTF/ChangeLog  2017-12-02 18:00:19 UTC (rev 225450)
</span><span class="lines">@@ -1,3 +1,22 @@
</span><ins>+2017-12-02  Brady Eidson  <beidson@apple.com>
+
+        Factor out the "databaseTaskQueue" parts of IDBServer into something reusable.
+        https://bugs.webkit.org/show_bug.cgi?id=180298
+
+        Reviewed by Chris Dumez.
+
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/CMakeLists.txt:
+
+        * wtf/CrossThreadTaskHandler.cpp: Added.
+        (WTF::CrossThreadTaskHandler::CrossThreadTaskHandler):
+        (WTF::CrossThreadTaskHandler::~CrossThreadTaskHandler):
+        (WTF::CrossThreadTaskHandler::postTask):
+        (WTF::CrossThreadTaskHandler::postTaskReply):
+        (WTF::CrossThreadTaskHandler::taskRunLoop):
+        (WTF::CrossThreadTaskHandler::handleTaskRepliesOnMainThread):
+        * wtf/CrossThreadTaskHandler.h: Added.
+
</ins><span class="cx"> 2017-12-01  Mark Lam  <mark.lam@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Let's scramble ClassInfo pointers in cells.
</span></span></pre></div>
<a id="trunkSourceWTFWTFxcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/WTF.xcodeproj/project.pbxproj (225449 => 225450)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/WTF.xcodeproj/project.pbxproj   2017-12-02 17:55:24 UTC (rev 225449)
+++ trunk/Source/WTF/WTF.xcodeproj/project.pbxproj      2017-12-02 18:00:19 UTC (rev 225450)
</span><span class="lines">@@ -65,6 +65,7 @@
</span><span class="cx">          2CDED0F318115C85004DBA70 /* RunLoop.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2CDED0F118115C85004DBA70 /* RunLoop.cpp */; };
</span><span class="cx">          3337DB9CE743410FAF076E17 /* StackTrace.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 313EDEC9778E49C9BEA91CFC /* StackTrace.cpp */; };
</span><span class="cx">          515F794E1CFC9F4A00CCED93 /* CrossThreadCopier.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 515F794B1CFC9F4A00CCED93 /* CrossThreadCopier.cpp */; };
</span><ins>+               517F82D71FD22F3000DA3DEA /* CrossThreadTaskHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 517F82D51FD22F2F00DA3DEA /* CrossThreadTaskHandler.cpp */; };
</ins><span class="cx">           51F1752B1F3D486000C74950 /* PersistentCoders.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 51F175261F3D486000C74950 /* PersistentCoders.cpp */; };
</span><span class="cx">          51F1752C1F3D486000C74950 /* PersistentDecoder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 51F175271F3D486000C74950 /* PersistentDecoder.cpp */; };
</span><span class="cx">          51F1752D1F3D486000C74950 /* PersistentEncoder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 51F175291F3D486000C74950 /* PersistentEncoder.cpp */; };
</span><span class="lines">@@ -322,6 +323,8 @@
</span><span class="cx">          515F794D1CFC9F4A00CCED93 /* CrossThreadTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CrossThreadTask.h; sourceTree = "<group>"; };
</span><span class="cx">          515F79551CFD3A6900CCED93 /* CrossThreadQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CrossThreadQueue.h; sourceTree = "<group>"; };
</span><span class="cx">          517A53571F5734B700DCDC0A /* Identified.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Identified.h; sourceTree = "<group>"; };
</span><ins>+               517F82D51FD22F2F00DA3DEA /* CrossThreadTaskHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CrossThreadTaskHandler.cpp; sourceTree = "<group>"; };
+               517F82D61FD22F2F00DA3DEA /* CrossThreadTaskHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CrossThreadTaskHandler.h; sourceTree = "<group>"; };
</ins><span class="cx">           5182C22C1F2BC7E60059BA7C /* InstanceCounted.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = InstanceCounted.h; sourceTree = "<group>"; };
</span><span class="cx">          51F175241F3D486000C74950 /* PersistentCoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PersistentCoder.h; sourceTree = "<group>"; };
</span><span class="cx">          51F175251F3D486000C74950 /* PersistentCoders.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PersistentCoders.h; sourceTree = "<group>"; };
</span><span class="lines">@@ -804,6 +807,8 @@
</span><span class="cx">                          515F794C1CFC9F4A00CCED93 /* CrossThreadCopier.h */,
</span><span class="cx">                          515F79551CFD3A6900CCED93 /* CrossThreadQueue.h */,
</span><span class="cx">                          515F794D1CFC9F4A00CCED93 /* CrossThreadTask.h */,
</span><ins>+                               517F82D51FD22F2F00DA3DEA /* CrossThreadTaskHandler.cpp */,
+                               517F82D61FD22F2F00DA3DEA /* CrossThreadTaskHandler.h */,
</ins><span class="cx">                           A8A47273151A825A004123FF /* CryptographicallyRandomNumber.cpp */,
</span><span class="cx">                          A8A47274151A825A004123FF /* CryptographicallyRandomNumber.h */,
</span><span class="cx">                          E15556F318A0CC18006F48FB /* CryptographicUtilities.cpp */,
</span><span class="lines">@@ -1386,6 +1391,7 @@
</span><span class="cx">                          E38C41281EB4E0680042957D /* CPUTime.cpp in Sources */,
</span><span class="cx">                          E38C41251EB4E04C0042957D /* CPUTimeCocoa.mm in Sources */,
</span><span class="cx">                          515F794E1CFC9F4A00CCED93 /* CrossThreadCopier.cpp in Sources */,
</span><ins>+                               517F82D71FD22F3000DA3DEA /* CrossThreadTaskHandler.cpp in Sources */,
</ins><span class="cx">                           A8A4739A151A825B004123FF /* CryptographicallyRandomNumber.cpp in Sources */,
</span><span class="cx">                          E15556F518A0CC18006F48FB /* CryptographicUtilities.cpp in Sources */,
</span><span class="cx">                          A8A47439151A825B004123FF /* CString.cpp in Sources */,
</span><span class="lines">@@ -1444,6 +1450,7 @@
</span><span class="cx">                          1469419316EAAF6D0024E146 /* RunLoopTimerCF.cpp in Sources */,
</span><span class="cx">                          1469419916EAB0410024E146 /* SchedulePairCF.cpp in Sources */,
</span><span class="cx">                          1469419716EAAFF80024E146 /* SchedulePairMac.mm in Sources */,
</span><ins>+                               FE85416E1FBE285D008DA5DA /* ScrambledPtr.cpp in Sources */,
</ins><span class="cx">                           0F66B28E1DC97BAB004A1D3F /* Seconds.cpp in Sources */,
</span><span class="cx">                          A8A47421151A825B004123FF /* SHA1.cpp in Sources */,
</span><span class="cx">                          5311BD531EA71CAD00525281 /* Signals.cpp in Sources */,
</span><span class="lines">@@ -1470,7 +1477,6 @@
</span><span class="cx">                          1C181C7F1D3078DA00F5FA16 /* TextBreakIterator.cpp in Sources */,
</span><span class="cx">                          1C181C961D30800A00F5FA16 /* TextBreakIteratorInternalICUMac.mm in Sources */,
</span><span class="cx">                          A3E4DD931F3A803400DED0B4 /* TextStream.cpp in Sources */,
</span><del>-                               FE85416E1FBE285D008DA5DA /* ScrambledPtr.cpp in Sources */,
</del><span class="cx">                           E311FB171F0A568B003C08DE /* ThreadGroup.cpp in Sources */,
</span><span class="cx">                          A8A4744A151A825B004123FF /* Threading.cpp in Sources */,
</span><span class="cx">                          A8A4744E151A825B004123FF /* ThreadingPthreads.cpp in Sources */,
</span></span></pre></div>
<a id="trunkSourceWTFwtfCMakeListstxt"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/CMakeLists.txt (225449 => 225450)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/CMakeLists.txt      2017-12-02 17:55:24 UTC (rev 225449)
+++ trunk/Source/WTF/wtf/CMakeLists.txt 2017-12-02 18:00:19 UTC (rev 225450)
</span><span class="lines">@@ -21,6 +21,7 @@
</span><span class="cx">     Condition.h
</span><span class="cx">     CrossThreadCopier.h
</span><span class="cx">     CrossThreadTask.h
</span><ins>+    CrossThreadTaskHandler.h
</ins><span class="cx">     CryptographicUtilities.h
</span><span class="cx">     CryptographicallyRandomNumber.h
</span><span class="cx">     CurrentTime.h
</span><span class="lines">@@ -219,6 +220,7 @@
</span><span class="cx">     ClockType.cpp
</span><span class="cx">     CompilationThread.cpp
</span><span class="cx">     CrossThreadCopier.cpp
</span><ins>+    CrossThreadTaskHandler.cpp
</ins><span class="cx">     CryptographicUtilities.cpp
</span><span class="cx">     CryptographicallyRandomNumber.cpp
</span><span class="cx">     CurrentTime.cpp
</span></span></pre></div>
<a id="trunkSourceWTFwtfCrossThreadTaskHandlercpp"></a>
<div class="addfile"><h4>Added: trunk/Source/WTF/wtf/CrossThreadTaskHandler.cpp (0 => 225450)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/CrossThreadTaskHandler.cpp                          (rev 0)
+++ trunk/Source/WTF/wtf/CrossThreadTaskHandler.cpp     2017-12-02 18:00:19 UTC (rev 225450)
</span><span class="lines">@@ -0,0 +1,87 @@
</span><ins>+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "CrossThreadTaskHandler.h"
+
+namespace WTF {
+
+CrossThreadTaskHandler::CrossThreadTaskHandler(const char* threadName)
+{
+    ASSERT(isMainThread());
+    Locker<Lock> locker(m_taskThreadCreationLock);
+    m_thread = Thread::create(threadName, [this] {
+        taskRunLoop();
+    });
+}
+
+CrossThreadTaskHandler::~CrossThreadTaskHandler()
+{
+    ASSERT(isMainThread());
+}
+
+void CrossThreadTaskHandler::postTask(CrossThreadTask&& task)
+{
+    m_taskQueue.append(WTFMove(task));
+}
+
+void CrossThreadTaskHandler::postTaskReply(CrossThreadTask&& task)
+{
+    m_taskReplyQueue.append(WTFMove(task));
+
+    Locker<Lock> locker(m_mainThreadReplyLock);
+    if (m_mainThreadReplyScheduled)
+        return;
+
+    m_mainThreadReplyScheduled = true;
+    callOnMainThread([this] {
+        handleTaskRepliesOnMainThread();
+    });
+}
+
+void CrossThreadTaskHandler::taskRunLoop()
+{
+    ASSERT(!isMainThread());
+    {
+        Locker<Lock> locker(m_taskThreadCreationLock);
+    }
+
+    while (!m_taskQueue.isKilled())
+        m_taskQueue.waitForMessage().performTask();
+}
+
+void CrossThreadTaskHandler::handleTaskRepliesOnMainThread()
+{
+    {
+        Locker<Lock> locker(m_mainThreadReplyLock);
+        m_mainThreadReplyScheduled = false;
+    }
+
+    while (auto task = m_taskReplyQueue.tryGetMessage())
+        task->performTask();
+}
+
+
+} // namespace WTF
</ins></span></pre></div>
<a id="trunkSourceWTFwtfCrossThreadTaskHandlerh"></a>
<div class="addfile"><h4>Added: trunk/Source/WTF/wtf/CrossThreadTaskHandler.h (0 => 225450)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/CrossThreadTaskHandler.h                            (rev 0)
+++ trunk/Source/WTF/wtf/CrossThreadTaskHandler.h       2017-12-02 18:00:19 UTC (rev 225450)
</span><span class="lines">@@ -0,0 +1,64 @@
</span><ins>+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <wtf/CrossThreadQueue.h>
+#include <wtf/CrossThreadTask.h>
+#include <wtf/Lock.h>
+#include <wtf/Threading.h>
+
+namespace WTF {
+
+class RegistrationStore;
+class SQLiteDatabase;
+
+class CrossThreadTaskHandler {
+public:
+    WTF_EXPORT_PRIVATE virtual ~CrossThreadTaskHandler();
+
+protected:
+    WTF_EXPORT_PRIVATE CrossThreadTaskHandler(const char* threadName);
+
+    WTF_EXPORT_PRIVATE void postTask(CrossThreadTask&&);
+    WTF_EXPORT_PRIVATE void postTaskReply(CrossThreadTask&&);
+
+private:
+    void handleTaskRepliesOnMainThread();
+    void taskRunLoop();
+
+    RefPtr<Thread> m_thread { nullptr };
+    Lock m_taskThreadCreationLock;
+    Lock m_mainThreadReplyLock;
+    bool m_mainThreadReplyScheduled { false };
+
+    CrossThreadQueue<CrossThreadTask> m_taskQueue;
+    CrossThreadQueue<CrossThreadTask> m_taskReplyQueue;
+};
+
+} // namespace WTF
+
+using WTF::CrossThreadTaskHandler;
+
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (225449 => 225450)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2017-12-02 17:55:24 UTC (rev 225449)
+++ trunk/Source/WebCore/ChangeLog      2017-12-02 18:00:19 UTC (rev 225450)
</span><span class="lines">@@ -1,3 +1,21 @@
</span><ins>+2017-12-02  Brady Eidson  <beidson@apple.com>
+
+        Factor out the "databaseTaskQueue" parts of IDBServer into something reusable.
+        https://bugs.webkit.org/show_bug.cgi?id=180298
+
+        Reviewed by Chris Dumez.
+
+        No new tests (Refactor only).
+
+        * Modules/indexeddb/server/IDBServer.cpp:
+        (WebCore::IDBServer::IDBServer::IDBServer):
+        (WebCore::IDBServer::IDBServer::postDatabaseTask):
+        (WebCore::IDBServer::IDBServer::postDatabaseTaskReply):
+        (WebCore::IDBServer::IDBServer::databaseRunLoop): Deleted.
+        (WebCore::IDBServer::IDBServer::handleTaskRepliesOnMainThread): Deleted.
+        * Modules/indexeddb/server/IDBServer.h:
+        (): Deleted.
+
</ins><span class="cx"> 2017-12-02  Simon Fraser  <simon.fraser@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Add a log channel for viewports
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesindexeddbserverIDBServercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/indexeddb/server/IDBServer.cpp (225449 => 225450)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/indexeddb/server/IDBServer.cpp      2017-12-02 17:55:24 UTC (rev 225449)
+++ trunk/Source/WebCore/Modules/indexeddb/server/IDBServer.cpp 2017-12-02 18:00:19 UTC (rev 225450)
</span><span class="lines">@@ -53,24 +53,17 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> IDBServer::IDBServer(IDBBackingStoreTemporaryFileHandler& fileHandler)
</span><del>-    : m_backingStoreTemporaryFileHandler(fileHandler)
</del><ins>+    : CrossThreadTaskHandler("IndexedDatabase Server")
+    , m_backingStoreTemporaryFileHandler(fileHandler)
</ins><span class="cx"> {
</span><del>-    Locker<Lock> locker(m_databaseThreadCreationLock);
-    m_thread = Thread::create("IndexedDatabase Server", [this] {
-        databaseRunLoop();
-    });
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> IDBServer::IDBServer(const String& databaseDirectoryPath, IDBBackingStoreTemporaryFileHandler& fileHandler)
</span><del>-    : m_databaseDirectoryPath(databaseDirectoryPath)
</del><ins>+    : CrossThreadTaskHandler("IndexedDatabase Server")
+    , m_databaseDirectoryPath(databaseDirectoryPath)
</ins><span class="cx">     , m_backingStoreTemporaryFileHandler(fileHandler)
</span><span class="cx"> {
</span><span class="cx">     LOG(IndexedDB, "IDBServer created at path %s", databaseDirectoryPath.utf8().data());
</span><del>-
-    Locker<Lock> locker(m_databaseThreadCreationLock);
-    m_thread = Thread::create("IndexedDatabase Server", [this] {
-        databaseRunLoop();
-    });
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void IDBServer::registerConnection(IDBConnectionToClient& connection)
</span><span class="lines">@@ -488,45 +481,14 @@
</span><span class="cx"> 
</span><span class="cx"> void IDBServer::postDatabaseTask(CrossThreadTask&& task)
</span><span class="cx"> {
</span><del>-    m_databaseQueue.append(WTFMove(task));
</del><ins>+    postTask(WTFMove(task));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void IDBServer::postDatabaseTaskReply(CrossThreadTask&& task)
</span><span class="cx"> {
</span><del>-    m_databaseReplyQueue.append(WTFMove(task));
-
-    Locker<Lock> locker(m_mainThreadReplyLock);
-    if (m_mainThreadReplyScheduled)
-        return;
-
-    m_mainThreadReplyScheduled = true;
-    callOnMainThread([this] {
-        handleTaskRepliesOnMainThread();
-    });
</del><ins>+    postTaskReply(WTFMove(task));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-void IDBServer::databaseRunLoop()
-{
-    ASSERT(!isMainThread());
-    {
-        Locker<Lock> locker(m_databaseThreadCreationLock);
-    }
-
-    while (!m_databaseQueue.isKilled())
-        m_databaseQueue.waitForMessage().performTask();
-}
-
-void IDBServer::handleTaskRepliesOnMainThread()
-{
-    {
-        Locker<Lock> locker(m_mainThreadReplyLock);
-        m_mainThreadReplyScheduled = false;
-    }
-
-    while (auto task = m_databaseReplyQueue.tryGetMessage())
-        task->performTask();
-}
-
</del><span class="cx"> static uint64_t generateDeleteCallbackID()
</span><span class="cx"> {
</span><span class="cx">     ASSERT(isMainThread());
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesindexeddbserverIDBServerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/indexeddb/server/IDBServer.h (225449 => 225450)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/indexeddb/server/IDBServer.h        2017-12-02 17:55:24 UTC (rev 225449)
+++ trunk/Source/WebCore/Modules/indexeddb/server/IDBServer.h   2017-12-02 18:00:19 UTC (rev 225450)
</span><span class="lines">@@ -31,8 +31,7 @@
</span><span class="cx"> #include "IDBDatabaseIdentifier.h"
</span><span class="cx"> #include "UniqueIDBDatabase.h"
</span><span class="cx"> #include "UniqueIDBDatabaseConnection.h"
</span><del>-#include <wtf/CrossThreadQueue.h>
-#include <wtf/CrossThreadTask.h>
</del><ins>+#include <wtf/CrossThreadTaskHandler.h>
</ins><span class="cx"> #include <wtf/HashMap.h>
</span><span class="cx"> #include <wtf/Lock.h>
</span><span class="cx"> #include <wtf/Ref.h>
</span><span class="lines">@@ -51,7 +50,7 @@
</span><span class="cx"> 
</span><span class="cx"> class IDBBackingStoreTemporaryFileHandler;
</span><span class="cx"> 
</span><del>-class IDBServer : public RefCounted<IDBServer> {
</del><ins>+class IDBServer : public RefCounted<IDBServer>, public CrossThreadTaskHandler {
</ins><span class="cx"> public:
</span><span class="cx">     static Ref<IDBServer> create(IDBBackingStoreTemporaryFileHandler&);
</span><span class="cx">     WEBCORE_EXPORT static Ref<IDBServer> create(const String& databaseDirectoryPath, IDBBackingStoreTemporaryFileHandler&);
</span><span class="lines">@@ -118,20 +117,9 @@
</span><span class="cx">     void performCloseAndDeleteDatabasesForOrigins(const Vector<SecurityOriginData>&, uint64_t callbackID);
</span><span class="cx">     void didPerformCloseAndDeleteDatabases(uint64_t callbackID);
</span><span class="cx"> 
</span><del>-    void databaseRunLoop();
-    void handleTaskRepliesOnMainThread();
-
</del><span class="cx">     HashMap<uint64_t, RefPtr<IDBConnectionToClient>> m_connectionMap;
</span><span class="cx">     HashMap<IDBDatabaseIdentifier, std::unique_ptr<UniqueIDBDatabase>> m_uniqueIDBDatabaseMap;
</span><span class="cx"> 
</span><del>-    RefPtr<Thread> m_thread { nullptr };
-    Lock m_databaseThreadCreationLock;
-    Lock m_mainThreadReplyLock;
-    bool m_mainThreadReplyScheduled { false };
-
-    CrossThreadQueue<CrossThreadTask> m_databaseQueue;
-    CrossThreadQueue<CrossThreadTask> m_databaseReplyQueue;
-
</del><span class="cx">     HashMap<uint64_t, UniqueIDBDatabaseConnection*> m_databaseConnections;
</span><span class="cx">     HashMap<IDBResourceIdentifier, UniqueIDBDatabaseTransaction*> m_transactions;
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>