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 "PlatformString.h"
37 #include "ThreadableLoader.h"
38 #include "ThreadableLoaderClient.h"
39 #include "ThreadableLoaderClientWrapper.h"
42 #include <wtf/PassRefPtr.h>
43 #include <wtf/RefCounted.h>
44 #include <wtf/RefPtr.h>
45 #include <wtf/Threading.h>
50 class ResourceRequest;
52 class WorkerMessagingProxy;
53 struct CrossThreadResourceResponseData;
54 struct CrossThreadResourceRequestData;
56 class WorkerThreadableLoader : public RefCounted<WorkerThreadableLoader>, public ThreadableLoader {
58 static PassRefPtr<WorkerThreadableLoader> create(WorkerContext* worker, ThreadableLoaderClient* client, const String& taskMode, const ResourceRequest& request, LoadCallbacks callbacksSetting, ContentSniff contentSniff)
60 return adoptRef(new WorkerThreadableLoader(worker, client, taskMode, request, callbacksSetting, contentSniff));
63 ~WorkerThreadableLoader();
65 virtual void cancel();
67 using RefCounted<WorkerThreadableLoader>::ref;
68 using RefCounted<WorkerThreadableLoader>::deref;
71 virtual void refThreadableLoader() { ref(); }
72 virtual void derefThreadableLoader() { deref(); }
75 // Creates a loader on the main thread and bridges communication between
76 // the main thread and the worker context's thread where WorkerThreadableLoader runs.
78 // Regarding the bridge and lifetimes of items used in callbacks, there are a few cases:
80 // all cases. All tasks posted from the worker context's thread are ok because
81 // the last task posted always is "mainThreadDestroy", so MainThreadBridge is
82 // around for all tasks that use it on the mian thread.
84 // case 1. worker.terminate is called.
85 // In this case, no more tasks are posted from the worker object's thread to the worker
86 // context's thread -- WorkerMessagingProxy enforces this.
88 // case 2. xhr gets aborted and the worker context continues running.
89 // The ThreadableLoaderClientWrapper has the underlying client cleared, so no more calls
90 // go through it. All tasks posted from the worker object's thread to the worker context's
91 // thread do "ThreadableLoaderClientWrapper::ref" (automatically inside of the cross thread copy
92 // done in createCallbackTask), so the ThreadableLoaderClientWrapper instance is there until all
93 // tasks are executed.
94 class MainThreadBridge : ThreadableLoaderClient {
96 // All executed on the worker context's thread.
97 MainThreadBridge(ThreadableLoaderClient*, WorkerMessagingProxy&, const String& taskMode, const ResourceRequest&, LoadCallbacks, ContentSniff);
102 // Executed on the worker context's thread.
103 void clearClientWrapper();
105 // All executed on the main thread.
106 static void mainThreadDestroy(ScriptExecutionContext*, MainThreadBridge*);
109 static void mainThreadCreateLoader(ScriptExecutionContext*, MainThreadBridge*, std::auto_ptr<CrossThreadResourceRequestData>, LoadCallbacks, ContentSniff);
110 static void mainThreadCancel(ScriptExecutionContext*, MainThreadBridge*);
111 virtual void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
112 virtual void didReceiveResponse(const ResourceResponse&);
113 virtual void didReceiveData(const char*, int lengthReceived);
114 virtual void didFinishLoading(int identifier);
115 virtual void didFail(const ResourceError&);
116 virtual void didFailRedirectCheck();
117 virtual void didReceiveAuthenticationCancellation(const ResourceResponse&);
119 // Only to be used on the main thread.
120 RefPtr<ThreadableLoader> m_mainThreadLoader;
122 // ThreadableLoaderClientWrapper is to be used on the worker context thread.
123 // The ref counting is done on either thread.
124 RefPtr<ThreadSafeShared<ThreadableLoaderClientWrapper> > m_workerClientWrapper;
126 // May be used on either thread.
127 WorkerMessagingProxy& m_messagingProxy;
129 // For use on the main thread.
133 WorkerThreadableLoader(WorkerContext*, ThreadableLoaderClient*, const String& taskMode, const ResourceRequest&, LoadCallbacks, ContentSniff);
135 RefPtr<WorkerContext> m_workerContext;
136 MainThreadBridge& m_bridge;
139 } // namespace WebCore
141 #endif // ENABLE(WORKERS)
143 #endif // WorkerThreadableLoader_h