18e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project/*
2635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project * Copyright (C) 2008 Apple Inc. All Rights Reserved.
38e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
48e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Redistribution and use in source and binary forms, with or without
58e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * modification, are permitted provided that the following conditions
68e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * are met:
78e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
88e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
98e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *    documentation and/or other materials provided with the distribution.
128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project *
258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project */
268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
27635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project#ifndef WorkerThread_h
28635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project#define WorkerThread_h
298e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
308e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#if ENABLE(WORKERS)
318e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
328f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian#include "WorkerRunLoop.h"
33dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch#include <wtf/Forward.h>
34635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project#include <wtf/OwnPtr.h>
35635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project#include <wtf/PassRefPtr.h>
36635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project#include <wtf/RefCounted.h>
378e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
388e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectnamespace WebCore {
398e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
40635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    class KURL;
41231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    class NotificationPresenter;
42635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    class WorkerContext;
435f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian    class WorkerLoaderProxy;
44231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    class WorkerReportingProxy;
45635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    struct WorkerThreadStartupData;
46635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project
47635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    class WorkerThread : public RefCounted<WorkerThread> {
488e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    public:
490bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch        virtual ~WorkerThread();
50635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project
51635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        bool start();
52635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        void stop();
538e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
54635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        ThreadIdentifier threadID() const { return m_threadID; }
55635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        WorkerRunLoop& runLoop() { return m_runLoop; }
565f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian        WorkerLoaderProxy& workerLoaderProxy() const { return m_workerLoaderProxy; }
57231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        WorkerReportingProxy& workerReportingProxy() const { return m_workerReportingProxy; }
58231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
59231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        // Number of active worker threads.
60231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        static unsigned workerThreadCount();
61231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
62231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#if ENABLE(NOTIFICATIONS)
63231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        NotificationPresenter* getNotificationPresenter() { return m_notificationPresenter; }
64231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        void setNotificationPresenter(NotificationPresenter* presenter) { m_notificationPresenter = presenter; }
65231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#endif
668e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
670bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch    protected:
68231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        WorkerThread(const KURL&, const String& userAgent, const String& sourceCode, WorkerLoaderProxy&, WorkerReportingProxy&);
690bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
700bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch        // Factory method for creating a new worker context for the thread.
710bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch        virtual PassRefPtr<WorkerContext> createWorkerContext(const KURL& url, const String& userAgent) = 0;
720bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
730bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch        // Executes the event loop for the worker thread. Derived classes can override to perform actions before/after entering the event loop.
740bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch        virtual void runEventLoop();
75635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project
760bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch        WorkerContext* workerContext() { return m_workerContext.get(); }
770bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
780bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch    private:
790bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch        // Static function executed as the core routine on the new thread. Passed a pointer to a WorkerThread object.
80635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        static void* workerThreadStart(void*);
81635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        void* workerThread();
82635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project
83635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        ThreadIdentifier m_threadID;
84635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        WorkerRunLoop m_runLoop;
855f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian        WorkerLoaderProxy& m_workerLoaderProxy;
86231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        WorkerReportingProxy& m_workerReportingProxy;
87635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project
88635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        RefPtr<WorkerContext> m_workerContext;
89635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        Mutex m_threadCreationMutex;
908e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
91635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        OwnPtr<WorkerThreadStartupData> m_startupData;
92231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
93231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#if ENABLE(NOTIFICATIONS)
94231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        NotificationPresenter* m_notificationPresenter;
95231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#endif
96231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
97231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        // Track the number of WorkerThread instances for use in layout tests.
98231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        static unsigned m_threadCount;
998e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    };
1008e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1018e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project} // namespace WebCore
1028e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1038e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#endif // ENABLE(WORKERS)
1048e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
105635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project#endif // WorkerThread_h
1068f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian
107