Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / workers / WorkerMessagingProxy.h
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 #include "core/dom/ExecutionContext.h"
31 #include "core/workers/WorkerGlobalScopeProxy.h"
32 #include "core/workers/WorkerLoaderProxy.h"
33 #include "wtf/Forward.h"
34 #include "wtf/Noncopyable.h"
35 #include "wtf/PassOwnPtr.h"
36 #include "wtf/PassRefPtr.h"
37 #include "wtf/RefPtr.h"
38 #include "wtf/Vector.h"
39
40 namespace blink {
41
42 class WorkerObjectProxy;
43 class DedicatedWorkerThread;
44 class ExecutionContext;
45 class Worker;
46 class WorkerClients;
47
48 class WorkerMessagingProxy FINAL : public WorkerGlobalScopeProxy, public WorkerLoaderProxy {
49     WTF_MAKE_NONCOPYABLE(WorkerMessagingProxy); WTF_MAKE_FAST_ALLOCATED;
50 public:
51     WorkerMessagingProxy(Worker*, PassOwnPtrWillBeRawPtr<WorkerClients>);
52
53     // Implementations of WorkerGlobalScopeProxy.
54     // (Only use these methods in the worker object thread.)
55     virtual void startWorkerGlobalScope(const KURL& scriptURL, const String& userAgent, const String& sourceCode, WorkerThreadStartMode) OVERRIDE;
56     virtual void terminateWorkerGlobalScope() OVERRIDE;
57     virtual void postMessageToWorkerGlobalScope(PassRefPtr<SerializedScriptValue>, PassOwnPtr<MessagePortChannelArray>) OVERRIDE;
58     virtual bool hasPendingActivity() const OVERRIDE;
59     virtual void workerObjectDestroyed() OVERRIDE;
60     virtual void connectToInspector(WorkerGlobalScopeProxy::PageInspector*) OVERRIDE;
61     virtual void disconnectFromInspector() OVERRIDE;
62     virtual void sendMessageToInspector(const String&) OVERRIDE;
63     virtual void writeTimelineStartedEvent(const String& sessionId) OVERRIDE;
64
65     // These methods come from worker context thread via WorkerObjectProxy
66     // and are called on the worker object thread (e.g. main thread).
67     void postMessageToWorkerObject(PassRefPtr<SerializedScriptValue>, PassOwnPtr<MessagePortChannelArray>);
68     void reportException(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL);
69     void reportConsoleMessage(MessageSource, MessageLevel, const String& message, int lineNumber, const String& sourceURL);
70     void postMessageToPageInspector(const String&);
71     void confirmMessageFromWorkerObject(bool hasPendingActivity);
72     void reportPendingActivity(bool hasPendingActivity);
73     void workerGlobalScopeClosed();
74     void workerThreadTerminated();
75
76     // Implementation of WorkerLoaderProxy.
77     // These methods are called on different threads to schedule loading
78     // requests and to send callbacks back to WorkerGlobalScope.
79     virtual void postTaskToLoader(PassOwnPtr<ExecutionContextTask>) OVERRIDE;
80     virtual bool postTaskToWorkerGlobalScope(PassOwnPtr<ExecutionContextTask>) OVERRIDE;
81
82     void workerThreadCreated(PassRefPtr<DedicatedWorkerThread>);
83
84 protected:
85     virtual ~WorkerMessagingProxy();
86
87 private:
88     static void workerObjectDestroyedInternal(ExecutionContext*, WorkerMessagingProxy*);
89     void terminateInternally();
90
91     RefPtrWillBePersistent<ExecutionContext> m_executionContext;
92     OwnPtr<WorkerObjectProxy> m_workerObjectProxy;
93     Worker* m_workerObject;
94     bool m_mayBeDestroyed;
95     RefPtr<DedicatedWorkerThread> m_workerThread;
96
97     unsigned m_unconfirmedMessageCount; // Unconfirmed messages from worker object to worker thread.
98     bool m_workerThreadHadPendingActivity; // The latest confirmation from worker thread reported that it was still active.
99
100     bool m_askedToTerminate;
101
102     Vector<OwnPtr<ExecutionContextTask> > m_queuedEarlyTasks; // Tasks are queued here until there's a thread object created.
103     WorkerGlobalScopeProxy::PageInspector* m_pageInspector;
104
105     OwnPtrWillBePersistent<WorkerClients> m_workerClients;
106 };
107
108 } // namespace blink
109
110 #endif // WorkerMessagingProxy_h