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" | |
9 #include "core/dom/DOMError.h" | |
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" | |
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 notiyfyFinished(); | |
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 }; | |
45 | |
46 FetchManager::Loader::Loader(ExecutionContext* executionContext, FetchManager* f etchManager, PassRefPtr<ScriptPromiseResolverWithContext> resolver, PassOwnPtr<R esourceRequest> request) | |
47 : m_executionContext(executionContext) | |
48 , m_fetchManager(fetchManager) | |
49 , m_resolver(resolver) | |
50 , m_request(request) | |
51 , m_downloadedBlobLength(0) | |
52 { | |
53 } | |
54 | |
55 FetchManager::Loader::~Loader() | |
56 { | |
57 if (m_loader) | |
58 m_loader->cancel(); | |
59 } | |
60 | |
61 void FetchManager::Loader::didReceiveResponse(unsigned long, const ResourceRespo nse& response) | |
62 { | |
63 m_response = response; | |
64 } | |
65 | |
66 void FetchManager::Loader::didFinishLoading(unsigned long, double) | |
67 { | |
68 if (!m_resolver) | |
69 return; | |
70 OwnPtr<BlobData> blobData = BlobData::create(); | |
71 String filePath = m_response.downloadedFilePath(); | |
72 if (!filePath.isEmpty() && m_downloadedBlobLength) { | |
73 blobData->appendFile(filePath); | |
74 // TODO(horo): Set the ContentType correctly. | |
75 } | |
76 Dictionary options; | |
77 // TODO(horo): fill options. | |
78 RefPtrWillBeRawPtr<Blob> blob = Blob::create(BlobDataHandle::create(blobData .release(), m_downloadedBlobLength)); | |
79 // TODO(horo): Handle response status correctly. | |
80 m_resolver->resolve(Response::create(blob.get(), options)); | |
81 m_resolver.clear(); | |
yhirano
2014/06/10 04:57:53
Is this clear needed?
horo
2014/06/10 06:20:45
Done.
| |
82 notiyfyFinished(); | |
83 } | |
84 | |
85 void FetchManager::Loader::didFail(const ResourceError& error) | |
86 { | |
87 failed(); | |
88 } | |
89 | |
90 void FetchManager::Loader::didFailAccessControlCheck(const ResourceError& error) | |
91 { | |
92 failed(); | |
93 } | |
94 | |
95 void FetchManager::Loader::didFailRedirectCheck() | |
96 { | |
97 failed(); | |
98 } | |
99 | |
100 void FetchManager::Loader::didDownloadData(int dataLength) | |
101 { | |
102 m_downloadedBlobLength += dataLength; | |
103 } | |
104 | |
105 void FetchManager::Loader::start() | |
106 { | |
107 m_request->setDownloadToFile(true); | |
108 ThreadableLoaderOptions options; | |
109 // TODO(horo): Fill options. | |
110 ResourceLoaderOptions resourceLoaderOptions; | |
111 resourceLoaderOptions.dataBufferingPolicy = DoNotBufferData; | |
112 // TODO(horo): Fill resourceLoaderOptions. | |
113 m_loader = ThreadableLoader::create(*m_executionContext, this, *m_request, o ptions, resourceLoaderOptions); | |
114 if (!m_loader) { | |
dominicc (has gone to gerrit)
2014/06/09 20:11:00
No {s for single-statement bodies.
horo
2014/06/10 03:34:52
Done.
| |
115 m_resolver->reject(DOMError::create(InvalidStateError)); | |
yhirano
2014/06/10 04:57:53
DOMException?
horo
2014/06/10 06:20:45
Done.
| |
116 } | |
117 } | |
118 | |
119 void FetchManager::Loader::cleanup() | |
120 { | |
121 m_fetchManager = 0; | |
122 } | |
123 | |
124 void FetchManager::Loader::failed() | |
125 { | |
126 if (!m_resolver) | |
127 return; | |
128 m_resolver->reject(DOMError::create(NetworkError)); | |
yhirano
2014/06/10 04:57:54
DOMException
horo
2014/06/10 06:20:46
Done.
| |
129 m_resolver.clear(); | |
yhirano
2014/06/10 04:57:53
Is this statement needed?
horo
2014/06/10 06:20:46
Done removed.
It was intended to avoid calling m_
yhirano
2014/06/11 06:51:30
It is safe to call resolve or reject more than onc
| |
130 notiyfyFinished(); | |
131 } | |
132 | |
133 void FetchManager::Loader::notiyfyFinished() | |
134 { | |
135 if (m_fetchManager) | |
136 m_fetchManager->removeLoader(this); | |
137 } | |
138 | |
139 FetchManager::FetchManager(ExecutionContext* executionContext) | |
140 : m_executionContext(executionContext) | |
dominicc (has gone to gerrit)
2014/06/09 20:11:00
Who manages the lifetime of the FetchManager? What
horo
2014/06/10 03:34:52
I added ServiceWorkerGlobalScope::stopFetch() to s
| |
141 { | |
142 } | |
143 | |
144 FetchManager::~FetchManager() | |
145 { | |
146 for (HashSet<OwnPtr<Loader> >::iterator it = m_loaders.begin(); it != m_load ers.end(); ++it) { | |
147 (*it)->cleanup(); | |
148 } | |
149 } | |
150 | |
151 ScriptPromise FetchManager::fetch(ScriptState* scriptState, PassOwnPtr<ResourceR equest> request) | |
152 { | |
153 RefPtr<ScriptPromiseResolverWithContext> resolver = ScriptPromiseResolverWit hContext::create(scriptState); | |
154 ScriptPromise promise = resolver->promise(); | |
155 | |
156 OwnPtr<Loader> loader(adoptPtr(new Loader(m_executionContext, this, resolver , request))); | |
157 Loader* tmpLoader = loader.get(); | |
dominicc (has gone to gerrit)
2014/06/09 20:11:00
Maybe instead of loader/tmpLoader it is neater to
horo
2014/06/10 03:34:52
Done.
yhirano
2014/06/10 04:57:53
You can get a pointer to the ownptr from the resul
horo
2014/06/10 06:20:46
Done.
| |
158 m_loaders.add(loader.release()); | |
159 tmpLoader->start(); | |
160 return promise; | |
161 } | |
162 | |
163 void FetchManager::removeLoader(Loader* loader) | |
164 { | |
165 m_loaders.remove(loader); | |
166 } | |
167 | |
168 } // namespace WebCore | |
OLD | NEW |