1/*
2 * Copyright (C) 2008 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27#ifndef WorkerMessagingProxy_h
28#define WorkerMessagingProxy_h
29
30#if ENABLE(WORKERS)
31
32#include "ScriptExecutionContext.h"
33#include "WorkerContextProxy.h"
34#include "WorkerLoaderProxy.h"
35#include "WorkerObjectProxy.h"
36#include <wtf/Forward.h>
37#include <wtf/Noncopyable.h>
38#include <wtf/PassOwnPtr.h>
39#include <wtf/PassRefPtr.h>
40#include <wtf/RefPtr.h>
41#include <wtf/Vector.h>
42
43namespace WebCore {
44
45    class DedicatedWorkerThread;
46    class ScriptExecutionContext;
47    class Worker;
48
49    class WorkerMessagingProxy : public WorkerContextProxy, public WorkerObjectProxy, public WorkerLoaderProxy {
50        WTF_MAKE_NONCOPYABLE(WorkerMessagingProxy); WTF_MAKE_FAST_ALLOCATED;
51    public:
52        WorkerMessagingProxy(Worker*);
53
54        // Implementations of WorkerContextProxy.
55        // (Only use these methods in the worker object thread.)
56        virtual void startWorkerContext(const KURL& scriptURL, const String& userAgent, const String& sourceCode);
57        virtual void terminateWorkerContext();
58        virtual void postMessageToWorkerContext(PassRefPtr<SerializedScriptValue>, PassOwnPtr<MessagePortChannelArray>);
59        virtual bool hasPendingActivity() const;
60        virtual void workerObjectDestroyed();
61
62        // Implementations of WorkerObjectProxy.
63        // (Only use these methods in the worker context thread.)
64        virtual void postMessageToWorkerObject(PassRefPtr<SerializedScriptValue>, PassOwnPtr<MessagePortChannelArray>);
65        virtual void postExceptionToWorkerObject(const String& errorMessage, int lineNumber, const String& sourceURL);
66        virtual void postConsoleMessageToWorkerObject(MessageSource, MessageType, MessageLevel, const String& message, int lineNumber, const String& sourceURL);
67        virtual void confirmMessageFromWorkerObject(bool hasPendingActivity);
68        virtual void reportPendingActivity(bool hasPendingActivity);
69        virtual void workerContextClosed();
70        virtual void workerContextDestroyed();
71
72        // Implementation of WorkerLoaderProxy.
73        // These methods are called on different threads to schedule loading
74        // requests and to send callbacks back to WorkerContext.
75        virtual void postTaskToLoader(PassOwnPtr<ScriptExecutionContext::Task>);
76        virtual void postTaskForModeToWorkerContext(PassOwnPtr<ScriptExecutionContext::Task>, const String& mode);
77
78        void workerThreadCreated(PassRefPtr<DedicatedWorkerThread>);
79
80        // Only use this method on the worker object thread.
81        bool askedToTerminate() const { return m_askedToTerminate; }
82
83    private:
84        friend class MessageWorkerTask;
85        friend class WorkerContextDestroyedTask;
86        friend class WorkerExceptionTask;
87        friend class WorkerThreadActivityReportTask;
88
89        virtual ~WorkerMessagingProxy();
90
91        void workerContextDestroyedInternal();
92        void reportPendingActivityInternal(bool confirmingMessage, bool hasPendingActivity);
93        Worker* workerObject() const { return m_workerObject; }
94
95        RefPtr<ScriptExecutionContext> m_scriptExecutionContext;
96        Worker* m_workerObject;
97        RefPtr<DedicatedWorkerThread> m_workerThread;
98
99        unsigned m_unconfirmedMessageCount; // Unconfirmed messages from worker object to worker thread.
100        bool m_workerThreadHadPendingActivity; // The latest confirmation from worker thread reported that it was still active.
101
102        bool m_askedToTerminate;
103
104        Vector<OwnPtr<ScriptExecutionContext::Task> > m_queuedEarlyTasks; // Tasks are queued here until there's a thread object created.
105    };
106
107} // namespace WebCore
108
109#endif // ENABLE(WORKERS)
110
111#endif // WorkerMessagingProxy_h
112