2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #ifndef WorkerMessagingProxy_h
28 #define WorkerMessagingProxy_h
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>
45 class DedicatedWorkerThread;
46 class ScriptExecutionContext;
49 class WorkerMessagingProxy : public WorkerContextProxy, public WorkerObjectProxy, public WorkerLoaderProxy {
50 WTF_MAKE_NONCOPYABLE(WorkerMessagingProxy); WTF_MAKE_FAST_ALLOCATED;
52 WorkerMessagingProxy(Worker*);
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, WorkerThreadStartMode);
57 virtual void terminateWorkerContext();
58 virtual void postMessageToWorkerContext(PassRefPtr<SerializedScriptValue>, PassOwnPtr<MessagePortChannelArray>);
59 virtual bool hasPendingActivity() const;
60 virtual void workerObjectDestroyed();
62 virtual void connectToInspector(WorkerContextProxy::PageInspector*);
63 virtual void disconnectFromInspector();
64 virtual void sendMessageToInspector(const String&);
67 // Implementations of WorkerObjectProxy.
68 // (Only use these methods in the worker context thread.)
69 virtual void postMessageToWorkerObject(PassRefPtr<SerializedScriptValue>, PassOwnPtr<MessagePortChannelArray>);
70 virtual void postExceptionToWorkerObject(const String& errorMessage, int lineNumber, const String& sourceURL);
71 virtual void postConsoleMessageToWorkerObject(MessageSource, MessageType, MessageLevel, const String& message, int lineNumber, const String& sourceURL);
73 virtual void postMessageToPageInspector(const String&);
74 virtual void updateInspectorStateCookie(const String&);
76 virtual void confirmMessageFromWorkerObject(bool hasPendingActivity);
77 virtual void reportPendingActivity(bool hasPendingActivity);
78 virtual void workerContextClosed();
79 virtual void workerContextDestroyed();
81 // Implementation of WorkerLoaderProxy.
82 // These methods are called on different threads to schedule loading
83 // requests and to send callbacks back to WorkerContext.
84 virtual void postTaskToLoader(PassOwnPtr<ScriptExecutionContext::Task>);
85 virtual void postTaskForModeToWorkerContext(PassOwnPtr<ScriptExecutionContext::Task>, const String& mode);
87 void workerThreadCreated(PassRefPtr<DedicatedWorkerThread>);
89 // Only use this method on the worker object thread.
90 bool askedToTerminate() const { return m_askedToTerminate; }
93 friend class MessageWorkerTask;
94 friend class PostMessageToPageInspectorTask;
95 friend class WorkerContextDestroyedTask;
96 friend class WorkerExceptionTask;
97 friend class WorkerThreadActivityReportTask;
99 virtual ~WorkerMessagingProxy();
101 void workerContextDestroyedInternal();
102 void reportPendingActivityInternal(bool confirmingMessage, bool hasPendingActivity);
103 Worker* workerObject() const { return m_workerObject; }
105 RefPtr<ScriptExecutionContext> m_scriptExecutionContext;
106 Worker* m_workerObject;
107 RefPtr<DedicatedWorkerThread> m_workerThread;
109 unsigned m_unconfirmedMessageCount; // Unconfirmed messages from worker object to worker thread.
110 bool m_workerThreadHadPendingActivity; // The latest confirmation from worker thread reported that it was still active.
112 bool m_askedToTerminate;
114 Vector<OwnPtr<ScriptExecutionContext::Task> > m_queuedEarlyTasks; // Tasks are queued here until there's a thread object created.
115 #if ENABLE(INSPECTOR)
116 WorkerContextProxy::PageInspector* m_pageInspector;
120 } // namespace WebCore
122 #endif // ENABLE(WORKERS)
124 #endif // WorkerMessagingProxy_h