2 * Copyright (C) 2009 Google 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef WorkerThreadableLoader_h
32 #define WorkerThreadableLoader_h
36 #include "ThreadableLoader.h"
37 #include "ThreadableLoaderClient.h"
38 #include "ThreadableLoaderClientWrapper.h"
41 #include <wtf/PassRefPtr.h>
42 #include <wtf/RefCounted.h>
43 #include <wtf/RefPtr.h>
44 #include <wtf/Threading.h>
49 class ResourceRequest;
51 class WorkerMessagingProxy;
52 struct CrossThreadResourceResponseData;
53 struct CrossThreadResourceRequestData;
55 class WorkerThreadableLoader : public RefCounted<WorkerThreadableLoader>, public ThreadableLoader {
57 static PassRefPtr<WorkerThreadableLoader> create(WorkerContext* worker, ThreadableLoaderClient* client, const ResourceRequest& request, LoadCallbacks callbacksSetting, ContentSniff contentSniff)
59 return adoptRef(new WorkerThreadableLoader(worker, client, request, callbacksSetting, contentSniff));
62 ~WorkerThreadableLoader();
64 virtual void cancel();
66 using RefCounted<WorkerThreadableLoader>::ref;
67 using RefCounted<WorkerThreadableLoader>::deref;
70 virtual void refThreadableLoader() { ref(); }
71 virtual void derefThreadableLoader() { deref(); }
74 // Creates a loader on the main thread and bridges communication between
75 // the main thread and the worker context's thread where WorkerThreadableLoader runs.
77 // Regarding the bridge and lifetimes of items used in callbacks, there are a few cases:
79 // all cases. All tasks posted from the worker context's thread are ok because
80 // the last task posted always is "mainThreadDestroy", so MainThreadBridge is
81 // around for all tasks that use it on the mian thread.
83 // case 1. worker.terminate is called.
84 // In this case, no more tasks are posted from the worker object's thread to the worker
85 // context's thread -- WorkerMessagingProxy enforces this.
87 // case 2. xhr gets aborted and the worker context continues running.
88 // The ThreadableLoaderClientWrapper has the underlying client cleared, so no more calls
89 // go through it. All tasks posted from the worker object's thread to the worker context's
90 // thread do "ThreadableLoaderClientWrapper::ref" (automatically inside of the cross thread copy
91 // done in createCallbackTask), so the ThreadableLoaderClientWrapper instance is there until all
92 // tasks are executed.
93 class MainThreadBridge : ThreadableLoaderClient {
95 // All executed on the worker context's thread.
96 MainThreadBridge(ThreadableLoaderClient*, WorkerMessagingProxy&, const ResourceRequest&, LoadCallbacks, ContentSniff);
101 // Executed on the worker context's thread.
102 void clearClientWrapper();
104 // All executed on the main thread.
105 static void mainThreadDestroy(ScriptExecutionContext*, MainThreadBridge*);
108 static void mainThreadCreateLoader(ScriptExecutionContext*, MainThreadBridge*, std::auto_ptr<CrossThreadResourceRequestData>, LoadCallbacks, ContentSniff);
109 static void mainThreadCancel(ScriptExecutionContext*, MainThreadBridge*);
110 virtual void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
111 virtual void didReceiveResponse(const ResourceResponse&);
112 virtual void didReceiveData(const char*, int lengthReceived);
113 virtual void didFinishLoading(int identifier);
114 virtual void didFail(const ResourceError&);
115 virtual void didFailRedirectCheck();
116 virtual void didReceiveAuthenticationCancellation(const ResourceResponse&);
118 // Only to be used on the main thread.
119 RefPtr<ThreadableLoader> m_mainThreadLoader;
121 // ThreadableLoaderClientWrapper is to be used on the worker context thread.
122 // The ref counting is done on either thread.
123 RefPtr<ThreadSafeShared<ThreadableLoaderClientWrapper> > m_workerClientWrapper;
125 // May be used on either thread.
126 WorkerMessagingProxy& m_messagingProxy;
129 WorkerThreadableLoader(WorkerContext*, ThreadableLoaderClient*, const ResourceRequest&, LoadCallbacks, ContentSniff);
131 RefPtr<WorkerContext> m_workerContext;
132 MainThreadBridge& m_bridge;
135 } // namespace WebCore
137 #endif // ENABLE(WORKERS)
139 #endif // WorkerThreadableLoader_h