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..97ea913c47f7ba9b35a98bf8036a581fc28cc160 |
| --- /dev/null |
| +++ b/Source/modules/serviceworkers/FetchManager.cpp |
| @@ -0,0 +1,163 @@ |
| +// 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" |
|
yhirano
2014/06/11 06:51:30
DOMError is now not used.
horo
2014/06/11 09:06:23
Done.
|
| +#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) |
| +{ |
| + 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)); |
| + notiyfyFinished(); |
| +} |
| + |
| +void FetchManager::Loader::didFail(const ResourceError& error) |
| +{ |
| + failed(); |
|
tyoshino (SeeGerritForStatus)
2014/06/11 09:12:01
I suggest that you have a flag e.g. m_failed to en
horo
2014/06/11 11:22:12
Done.
|
| +} |
| + |
| +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) |
| + m_resolver->reject(DOMException::create(InvalidStateError)); |
|
tyoshino (SeeGerritForStatus)
2014/06/11 09:12:01
maybe this also should be TypeError. is there any
horo
2014/06/11 11:22:12
Deleted this lines.
m_executionContext->isWorkerGl
|
| +} |
| + |
| +void FetchManager::Loader::cleanup() |
| +{ |
| + m_fetchManager = 0; |
|
tyoshino (SeeGerritForStatus)
2014/06/11 09:12:01
add comment like "prevent notification"
horo
2014/06/11 11:22:11
Done.
|
| + if (m_loader) { |
| + m_loader->cancel(); |
| + m_loader.clear(); |
| + } |
| +} |
| + |
| +void FetchManager::Loader::failed() |
| +{ |
| + m_resolver->reject(DOMException::create(NetworkError)); |
|
tyoshino (SeeGerritForStatus)
2014/06/11 09:12:01
This should be a TypeError. I also had doubts and
horo
2014/06/11 11:22:11
Could you please let me know how to create TypeErr
tyoshino (SeeGerritForStatus)
2014/06/12 03:27:24
snip
How about this?
ScriptState* state = m_reso
horo
2014/06/12 05:21:16
Done.
Thank you.
|
| + notiyfyFinished(); |
| +} |
| + |
| +void FetchManager::Loader::notiyfyFinished() |
|
tyoshino (SeeGerritForStatus)
2014/06/11 09:12:01
notiyfy -> notify
horo
2014/06/11 11:22:12
Done.
|
| +{ |
| + if (m_fetchManager) |
| + m_fetchManager->removeLoader(this); |
| +} |
| + |
| +FetchManager::FetchManager(ExecutionContext* executionContext) |
| + : m_executionContext(executionContext) |
| +{ |
| +} |
| + |
| +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))); |
|
tyoshino (SeeGerritForStatus)
2014/06/11 09:12:01
resolver.release()
horo
2014/06/11 11:22:12
Done.
|
| + (*m_loaders.add(loader.release()).storedValue)->start(); |
| + return promise; |
| +} |
| + |
| +void FetchManager::removeLoader(Loader* loader) |
|
tyoshino (SeeGerritForStatus)
2014/06/11 09:12:01
how about naming this like onLoaderFinished?
horo
2014/06/11 11:22:11
Done.
|
| +{ |
| + m_loaders.remove(loader); |
| +} |
| + |
| +} // namespace WebCore |