OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "config.h" | |
6 #include "FetchManager.h" | |
7 | |
8 #include "bindings/v8/ScriptPromiseResolverWithContext.h" | |
tyoshino (SeeGerritForStatus)
2014/06/12 07:38:19
include bindings/v8/ScriptState.h
horo
2014/06/12 08:32:54
Done.
| |
9 #include "core/dom/DOMException.h" | |
tyoshino (SeeGerritForStatus)
2014/06/12 07:38:19
remove
horo
2014/06/12 08:32:55
Done.
| |
10 #include "core/dom/ExceptionCode.h" | |
11 #include "core/fileapi/Blob.h" | |
12 #include "core/loader/ThreadableLoader.h" | |
13 #include "core/loader/ThreadableLoaderClient.h" | |
14 #include "modules/serviceworkers/Response.h" | |
tyoshino (SeeGerritForStatus)
2014/06/12 07:38:19
include platform/network/ResourceRequest.h
horo
2014/06/12 08:32:54
Done.
| |
15 #include "wtf/HashSet.h" | |
16 | |
17 namespace WebCore { | |
18 | |
19 class FetchManager::Loader : public ThreadableLoaderClient { | |
20 public: | |
21 Loader(ExecutionContext*, FetchManager*, PassRefPtr<ScriptPromiseResolverWit hContext>, PassOwnPtr<ResourceRequest>); | |
22 ~Loader(); | |
23 virtual void didReceiveResponse(unsigned long, const ResourceResponse&); | |
24 virtual void didFinishLoading(unsigned long, double); | |
25 virtual void didFail(const ResourceError&); | |
26 virtual void didFailAccessControlCheck(const ResourceError&); | |
27 virtual void didFailRedirectCheck(); | |
28 virtual void didDownloadData(int); | |
29 | |
30 void start(); | |
31 void cleanup(); | |
32 | |
33 private: | |
34 void failed(); | |
35 void notifyFinished(); | |
36 | |
37 ExecutionContext* m_executionContext; | |
38 FetchManager* m_fetchManager; | |
39 RefPtr<ScriptPromiseResolverWithContext> m_resolver; | |
40 OwnPtr<ResourceRequest> m_request; | |
41 RefPtr<ThreadableLoader> m_loader; | |
42 ResourceResponse m_response; | |
43 long long m_downloadedBlobLength; | |
44 bool m_failed; | |
45 }; | |
46 | |
47 FetchManager::Loader::Loader(ExecutionContext* executionContext, FetchManager* f etchManager, PassRefPtr<ScriptPromiseResolverWithContext> resolver, PassOwnPtr<R esourceRequest> request) | |
48 : m_executionContext(executionContext) | |
49 , m_fetchManager(fetchManager) | |
50 , m_resolver(resolver) | |
51 , m_request(request) | |
52 , m_downloadedBlobLength(0) | |
53 , m_failed(false) | |
54 { | |
55 } | |
56 | |
57 FetchManager::Loader::~Loader() | |
58 { | |
59 if (m_loader) | |
60 m_loader->cancel(); | |
61 } | |
62 | |
63 void FetchManager::Loader::didReceiveResponse(unsigned long, const ResourceRespo nse& response) | |
64 { | |
65 m_response = response; | |
66 } | |
67 | |
68 void FetchManager::Loader::didFinishLoading(unsigned long, double) | |
69 { | |
70 OwnPtr<BlobData> blobData = BlobData::create(); | |
71 String filePath = m_response.downloadedFilePath(); | |
72 if (!filePath.isEmpty() && m_downloadedBlobLength) { | |
73 blobData->appendFile(filePath); | |
74 // FIXME: Set the ContentType correctly. | |
75 } | |
76 Dictionary options; | |
77 // FIXME: fill options. | |
78 RefPtrWillBeRawPtr<Blob> blob = Blob::create(BlobDataHandle::create(blobData .release(), m_downloadedBlobLength)); | |
79 // FIXME: Handle response status correctly. | |
80 m_resolver->resolve(Response::create(blob.get(), options)); | |
81 notifyFinished(); | |
82 } | |
83 | |
84 void FetchManager::Loader::didFail(const ResourceError& error) | |
85 { | |
86 failed(); | |
87 } | |
88 | |
89 void FetchManager::Loader::didFailAccessControlCheck(const ResourceError& error) | |
90 { | |
91 failed(); | |
92 } | |
93 | |
94 void FetchManager::Loader::didFailRedirectCheck() | |
95 { | |
96 failed(); | |
97 } | |
98 | |
99 void FetchManager::Loader::didDownloadData(int dataLength) | |
100 { | |
101 m_downloadedBlobLength += dataLength; | |
102 } | |
103 | |
104 void FetchManager::Loader::start() | |
105 { | |
106 m_request->setDownloadToFile(true); | |
107 ThreadableLoaderOptions options; | |
108 // FIXME: Fill options. | |
109 ResourceLoaderOptions resourceLoaderOptions; | |
110 resourceLoaderOptions.dataBufferingPolicy = DoNotBufferData; | |
111 // FIXME: Fill resourceLoaderOptions. | |
112 m_loader = ThreadableLoader::create(*m_executionContext, this, *m_request, o ptions, resourceLoaderOptions); | |
113 } | |
114 | |
115 void FetchManager::Loader::cleanup() | |
116 { | |
117 // Prevent notification | |
118 m_fetchManager = 0; | |
119 | |
120 if (m_loader) { | |
121 m_loader->cancel(); | |
122 m_loader.clear(); | |
123 } | |
124 } | |
125 | |
126 void FetchManager::Loader::failed() | |
127 { | |
128 if (m_failed) | |
129 return; | |
130 m_failed = true; | |
131 ScriptState* state = m_resolver->scriptState(); | |
132 ScriptState::Scope scope(state); | |
133 m_resolver->reject(V8ThrowException::createTypeError("Failed to fetch", stat e->isolate())); | |
134 notifyFinished(); | |
135 } | |
136 | |
137 void FetchManager::Loader::notifyFinished() | |
138 { | |
139 if (m_fetchManager) | |
140 m_fetchManager->onLoaderFinished(this); | |
141 } | |
142 | |
143 FetchManager::FetchManager(ExecutionContext* executionContext) | |
144 : m_executionContext(executionContext) | |
145 { | |
146 } | |
147 | |
148 FetchManager::~FetchManager() | |
149 { | |
150 for (HashSet<OwnPtr<Loader> >::iterator it = m_loaders.begin(); it != m_load ers.end(); ++it) { | |
151 (*it)->cleanup(); | |
152 } | |
153 } | |
154 | |
155 ScriptPromise FetchManager::fetch(ScriptState* scriptState, PassOwnPtr<ResourceR equest> request) | |
156 { | |
157 RefPtr<ScriptPromiseResolverWithContext> resolver = ScriptPromiseResolverWit hContext::create(scriptState); | |
158 ScriptPromise promise = resolver->promise(); | |
159 | |
160 OwnPtr<Loader> loader(adoptPtr(new Loader(m_executionContext, this, resolver .release(), request))); | |
161 (*m_loaders.add(loader.release()).storedValue)->start(); | |
162 return promise; | |
163 } | |
164 | |
165 void FetchManager::onLoaderFinished(Loader* loader) | |
166 { | |
167 m_loaders.remove(loader); | |
168 } | |
169 | |
170 } // namespace WebCore | |
OLD | NEW |