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 OwnPtr<BlobData> blobData = BlobData::create(); |
| 69 String filePath = m_response.downloadedFilePath(); |
| 70 if (!filePath.isEmpty() && m_downloadedBlobLength) { |
| 71 blobData->appendFile(filePath); |
| 72 // TODO(horo): Set the ContentType correctly. |
| 73 } |
| 74 Dictionary options; |
| 75 // TODO(horo): fill options. |
| 76 RefPtrWillBeRawPtr<Blob> blob = Blob::create(BlobDataHandle::create(blobData
.release(), m_downloadedBlobLength)); |
| 77 // TODO(horo): Handle response status correctly. |
| 78 m_resolver->resolve(Response::create(blob.get(), options)); |
| 79 notiyfyFinished(); |
| 80 } |
| 81 |
| 82 void FetchManager::Loader::didFail(const ResourceError& error) |
| 83 { |
| 84 failed(); |
| 85 } |
| 86 |
| 87 void FetchManager::Loader::didFailAccessControlCheck(const ResourceError& error) |
| 88 { |
| 89 failed(); |
| 90 } |
| 91 |
| 92 void FetchManager::Loader::didFailRedirectCheck() |
| 93 { |
| 94 failed(); |
| 95 } |
| 96 |
| 97 void FetchManager::Loader::didDownloadData(int dataLength) |
| 98 { |
| 99 m_downloadedBlobLength += dataLength; |
| 100 } |
| 101 |
| 102 void FetchManager::Loader::start() |
| 103 { |
| 104 m_request->setDownloadToFile(true); |
| 105 ThreadableLoaderOptions options; |
| 106 // TODO(horo): Fill options. |
| 107 ResourceLoaderOptions resourceLoaderOptions; |
| 108 resourceLoaderOptions.dataBufferingPolicy = DoNotBufferData; |
| 109 // TODO(horo): Fill resourceLoaderOptions. |
| 110 m_loader = ThreadableLoader::create(*m_executionContext, this, *m_request, o
ptions, resourceLoaderOptions); |
| 111 if (!m_loader) |
| 112 m_resolver->reject(DOMException::create(InvalidStateError)); |
| 113 } |
| 114 |
| 115 void FetchManager::Loader::cleanup() |
| 116 { |
| 117 m_fetchManager = 0; |
| 118 if (m_loader) { |
| 119 m_loader->cancel(); |
| 120 m_loader.clear(); |
| 121 } |
| 122 } |
| 123 |
| 124 void FetchManager::Loader::failed() |
| 125 { |
| 126 m_resolver->reject(DOMException::create(NetworkError)); |
| 127 notiyfyFinished(); |
| 128 } |
| 129 |
| 130 void FetchManager::Loader::notiyfyFinished() |
| 131 { |
| 132 if (m_fetchManager) |
| 133 m_fetchManager->removeLoader(this); |
| 134 } |
| 135 |
| 136 FetchManager::FetchManager(ExecutionContext* executionContext) |
| 137 : m_executionContext(executionContext) |
| 138 { |
| 139 } |
| 140 |
| 141 FetchManager::~FetchManager() |
| 142 { |
| 143 for (HashSet<OwnPtr<Loader> >::iterator it = m_loaders.begin(); it != m_load
ers.end(); ++it) { |
| 144 (*it)->cleanup(); |
| 145 } |
| 146 } |
| 147 |
| 148 ScriptPromise FetchManager::fetch(ScriptState* scriptState, PassOwnPtr<ResourceR
equest> request) |
| 149 { |
| 150 RefPtr<ScriptPromiseResolverWithContext> resolver = ScriptPromiseResolverWit
hContext::create(scriptState); |
| 151 ScriptPromise promise = resolver->promise(); |
| 152 |
| 153 OwnPtr<Loader> loader(adoptPtr(new Loader(m_executionContext, this, resolver
, request))); |
| 154 (*m_loaders.add(loader.release()).storedValue)->start(); |
| 155 return promise; |
| 156 } |
| 157 |
| 158 void FetchManager::removeLoader(Loader* loader) |
| 159 { |
| 160 m_loaders.remove(loader); |
| 161 } |
| 162 |
| 163 } // namespace WebCore |
OLD | NEW |