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/Noncopyable.h>
37 #include <wtf/PassRefPtr.h>
38 #include <wtf/RefPtr.h>
39 #include <wtf/Vector.h>
43 class ScriptExecutionContext;
48 class WorkerMessagingProxy : public WorkerContextProxy, public WorkerObjectProxy, public WorkerLoaderProxy, Noncopyable {
50 WorkerMessagingProxy(Worker*);
52 // Implementations of WorkerContextProxy.
53 // (Only use these methods in the worker object thread.)
54 virtual void startWorkerContext(const KURL& scriptURL, const String& userAgent, const String& sourceCode);
55 virtual void terminateWorkerContext();
56 virtual void postMessageToWorkerContext(const String& message);
57 virtual bool hasPendingActivity() const;
58 virtual void workerObjectDestroyed();
60 // Implementations of WorkerObjectProxy.
61 // (Only use these methods in the worker context thread.)
62 virtual void postMessageToWorkerObject(const String& message);
63 virtual void postExceptionToWorkerObject(const String& errorMessage, int lineNumber, const String& sourceURL);
64 virtual void postConsoleMessageToWorkerObject(MessageDestination, MessageSource, MessageLevel, const String& message, int lineNumber, const String& sourceURL);
65 virtual void confirmMessageFromWorkerObject(bool hasPendingActivity);
66 virtual void reportPendingActivity(bool hasPendingActivity);
67 virtual void workerContextDestroyed();
69 // Implementation of WorkerLoaderProxy.
70 // These methods are called on different threads to schedule loading
71 // requests and to send callbacks back to WorkerContext.
72 virtual void postTaskToLoader(PassRefPtr<ScriptExecutionContext::Task>);
73 virtual void postTaskForModeToWorkerContext(PassRefPtr<ScriptExecutionContext::Task>, const String& mode);
75 void workerThreadCreated(PassRefPtr<WorkerThread>);
77 // Only use this method on the worker object thread.
78 bool askedToTerminate() const { return m_askedToTerminate; }
81 friend class MessageWorkerTask;
82 friend class WorkerContextDestroyedTask;
83 friend class WorkerThreadActivityReportTask;
85 virtual ~WorkerMessagingProxy();
87 void workerContextDestroyedInternal();
88 void reportPendingActivityInternal(bool confirmingMessage, bool hasPendingActivity);
89 Worker* workerObject() const { return m_workerObject; }
91 RefPtr<ScriptExecutionContext> m_scriptExecutionContext;
92 Worker* m_workerObject;
93 RefPtr<WorkerThread> m_workerThread;
95 unsigned m_unconfirmedMessageCount; // Unconfirmed messages from worker object to worker thread.
96 bool m_workerThreadHadPendingActivity; // The latest confirmation from worker thread reported that it was still active.
98 bool m_askedToTerminate;
100 Vector<RefPtr<ScriptExecutionContext::Task> > m_queuedEarlyTasks; // Tasks are queued here until there's a thread object created.
103 } // namespace WebCore
105 #endif // ENABLE(WORKERS)
107 #endif // WorkerMessagingProxy_h