1/*
2 * Copyright (C) 2009 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef WorkerRunLoop_h
32#define WorkerRunLoop_h
33
34#include "ScriptExecutionContext.h"
35#include <memory>
36#include <wtf/MessageQueue.h>
37
38namespace WebCore {
39
40    class ModePredicate;
41    class WorkerGlobalScope;
42    class WorkerSharedTimer;
43
44    class WorkerRunLoop {
45    public:
46        WorkerRunLoop();
47        ~WorkerRunLoop();
48
49        // Blocking call. Waits for tasks and timers, invokes the callbacks.
50        void run(WorkerGlobalScope*);
51
52        enum WaitMode { WaitForMessage, DontWaitForMessage };
53
54        // Waits for a single task and returns.
55        MessageQueueWaitResult runInMode(WorkerGlobalScope*, const String& mode, WaitMode = WaitForMessage);
56
57        void terminate();
58        bool terminated() const { return m_messageQueue.killed(); }
59
60        void postTask(ScriptExecutionContext::Task);
61        void postTaskAndTerminate(ScriptExecutionContext::Task);
62        void postTaskForMode(ScriptExecutionContext::Task, const String& mode);
63
64        unsigned long createUniqueId() { return ++m_uniqueId; }
65
66        static String defaultMode();
67
68        class Task {
69            WTF_MAKE_NONCOPYABLE(Task); WTF_MAKE_FAST_ALLOCATED;
70        public:
71            static std::unique_ptr<Task> create(ScriptExecutionContext::Task, const String& mode);
72            ~Task() { }
73            const String& mode() const { return m_mode; }
74            void performTask(const WorkerRunLoop&, WorkerGlobalScope*);
75
76        private:
77            Task(ScriptExecutionContext::Task, const String& mode);
78
79            ScriptExecutionContext::Task m_task;
80            String m_mode;
81        };
82
83    private:
84        friend class RunLoopSetup;
85        MessageQueueWaitResult runInMode(WorkerGlobalScope*, const ModePredicate&, WaitMode);
86
87        // Runs any clean up tasks that are currently in the queue and returns.
88        // This should only be called when the context is closed or loop has been terminated.
89        void runCleanupTasks(WorkerGlobalScope*);
90
91        MessageQueue<Task> m_messageQueue;
92        std::unique_ptr<WorkerSharedTimer> m_sharedTimer;
93        int m_nestedCount;
94        unsigned long m_uniqueId;
95    };
96
97} // namespace WebCore
98
99#endif // WorkerRunLoop_h
100