Chromium Code Reviews| Index: Source/modules/serviceworkers/FetchManager.cpp |
| diff --git a/Source/modules/serviceworkers/FetchManager.cpp b/Source/modules/serviceworkers/FetchManager.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fb62601c89cb7f3259cabd4473b4b3dfd906c510 |
| --- /dev/null |
| +++ b/Source/modules/serviceworkers/FetchManager.cpp |
| @@ -0,0 +1,168 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "FetchManager.h" |
| + |
| +#include "bindings/v8/ScriptPromiseResolverWithContext.h" |
| +#include "core/dom/DOMError.h" |
| +#include "core/dom/ExceptionCode.h" |
| +#include "core/fileapi/Blob.h" |
| +#include "core/loader/ThreadableLoader.h" |
| +#include "core/loader/ThreadableLoaderClient.h" |
| +#include "modules/serviceworkers/Response.h" |
| +#include "wtf/HashSet.h" |
| + |
| +namespace WebCore { |
| + |
| +class FetchManager::Loader : public ThreadableLoaderClient { |
| +public: |
| + Loader(ExecutionContext*, FetchManager*, PassRefPtr<ScriptPromiseResolverWithContext>, PassOwnPtr<ResourceRequest>); |
| + ~Loader(); |
| + virtual void didReceiveResponse(unsigned long, const ResourceResponse&); |
| + virtual void didFinishLoading(unsigned long, double); |
| + virtual void didFail(const ResourceError&); |
| + virtual void didFailAccessControlCheck(const ResourceError&); |
| + virtual void didFailRedirectCheck(); |
| + virtual void didDownloadData(int); |
| + |
| + void start(); |
| + void cleanup(); |
| + |
| +private: |
| + void failed(); |
| + void notiyfyFinished(); |
| + |
| + ExecutionContext* m_executionContext; |
| + FetchManager* m_fetchManager; |
| + RefPtr<ScriptPromiseResolverWithContext> m_resolver; |
| + OwnPtr<ResourceRequest> m_request; |
| + RefPtr<ThreadableLoader> m_loader; |
| + ResourceResponse m_response; |
| + long long m_downloadedBlobLength; |
| +}; |
| + |
| +FetchManager::Loader::Loader(ExecutionContext* executionContext, FetchManager* fetchManager, PassRefPtr<ScriptPromiseResolverWithContext> resolver, PassOwnPtr<ResourceRequest> request) |
| + : m_executionContext(executionContext) |
| + , m_fetchManager(fetchManager) |
| + , m_resolver(resolver) |
| + , m_request(request) |
| + , m_downloadedBlobLength(0) |
| +{ |
| +} |
| + |
| +FetchManager::Loader::~Loader() |
| +{ |
| + if (m_loader) |
| + m_loader->cancel(); |
| +} |
| + |
| +void FetchManager::Loader::didReceiveResponse(unsigned long, const ResourceResponse& response) |
| +{ |
| + m_response = response; |
| +} |
| + |
| +void FetchManager::Loader::didFinishLoading(unsigned long, double) |
| +{ |
| + if (!m_resolver) |
| + return; |
| + OwnPtr<BlobData> blobData = BlobData::create(); |
| + String filePath = m_response.downloadedFilePath(); |
| + if (!filePath.isEmpty() && m_downloadedBlobLength) { |
| + blobData->appendFile(filePath); |
| + // TODO(horo): Set the ContentType correctly. |
| + } |
| + Dictionary options; |
| + // TODO(horo): fill options. |
| + RefPtrWillBeRawPtr<Blob> blob = Blob::create(BlobDataHandle::create(blobData.release(), m_downloadedBlobLength)); |
| + // TODO(horo): Handle response status correctly. |
| + m_resolver->resolve(Response::create(blob.get(), options)); |
| + m_resolver.clear(); |
|
yhirano
2014/06/10 04:57:53
Is this clear needed?
horo
2014/06/10 06:20:45
Done.
|
| + notiyfyFinished(); |
| +} |
| + |
| +void FetchManager::Loader::didFail(const ResourceError& error) |
| +{ |
| + failed(); |
| +} |
| + |
| +void FetchManager::Loader::didFailAccessControlCheck(const ResourceError& error) |
| +{ |
| + failed(); |
| +} |
| + |
| +void FetchManager::Loader::didFailRedirectCheck() |
| +{ |
| + failed(); |
| +} |
| + |
| +void FetchManager::Loader::didDownloadData(int dataLength) |
| +{ |
| + m_downloadedBlobLength += dataLength; |
| +} |
| + |
| +void FetchManager::Loader::start() |
| +{ |
| + m_request->setDownloadToFile(true); |
| + ThreadableLoaderOptions options; |
| + // TODO(horo): Fill options. |
| + ResourceLoaderOptions resourceLoaderOptions; |
| + resourceLoaderOptions.dataBufferingPolicy = DoNotBufferData; |
| + // TODO(horo): Fill resourceLoaderOptions. |
| + m_loader = ThreadableLoader::create(*m_executionContext, this, *m_request, options, resourceLoaderOptions); |
| + 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.
|
| + m_resolver->reject(DOMError::create(InvalidStateError)); |
|
yhirano
2014/06/10 04:57:53
DOMException?
horo
2014/06/10 06:20:45
Done.
|
| + } |
| +} |
| + |
| +void FetchManager::Loader::cleanup() |
| +{ |
| + m_fetchManager = 0; |
| +} |
| + |
| +void FetchManager::Loader::failed() |
| +{ |
| + if (!m_resolver) |
| + return; |
| + m_resolver->reject(DOMError::create(NetworkError)); |
|
yhirano
2014/06/10 04:57:54
DOMException
horo
2014/06/10 06:20:46
Done.
|
| + 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
|
| + notiyfyFinished(); |
| +} |
| + |
| +void FetchManager::Loader::notiyfyFinished() |
| +{ |
| + if (m_fetchManager) |
| + m_fetchManager->removeLoader(this); |
| +} |
| + |
| +FetchManager::FetchManager(ExecutionContext* executionContext) |
| + : 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
|
| +{ |
| +} |
| + |
| +FetchManager::~FetchManager() |
| +{ |
| + for (HashSet<OwnPtr<Loader> >::iterator it = m_loaders.begin(); it != m_loaders.end(); ++it) { |
| + (*it)->cleanup(); |
| + } |
| +} |
| + |
| +ScriptPromise FetchManager::fetch(ScriptState* scriptState, PassOwnPtr<ResourceRequest> request) |
| +{ |
| + RefPtr<ScriptPromiseResolverWithContext> resolver = ScriptPromiseResolverWithContext::create(scriptState); |
| + ScriptPromise promise = resolver->promise(); |
| + |
| + OwnPtr<Loader> loader(adoptPtr(new Loader(m_executionContext, this, resolver, request))); |
| + 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.
|
| + m_loaders.add(loader.release()); |
| + tmpLoader->start(); |
| + return promise; |
| +} |
| + |
| +void FetchManager::removeLoader(Loader* loader) |
| +{ |
| + m_loaders.remove(loader); |
| +} |
| + |
| +} // namespace WebCore |