Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(387)

Side by Side Diff: Source/modules/serviceworkers/FetchManager.cpp

Issue 318393002: Initial implementation of ServiceWorkerGlobalScope.fetch() (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: incorporated yhirano and tyoshino's comment Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/DOMException.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 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 m_resolver->reject(DOMException::create(NetworkError));
132 notifyFinished();
133 }
134
135 void FetchManager::Loader::notifyFinished()
136 {
137 if (m_fetchManager)
138 m_fetchManager->onLoaderFinished(this);
139 }
140
141 FetchManager::FetchManager(ExecutionContext* executionContext)
142 : m_executionContext(executionContext)
143 {
144 }
145
146 FetchManager::~FetchManager()
147 {
148 for (HashSet<OwnPtr<Loader> >::iterator it = m_loaders.begin(); it != m_load ers.end(); ++it) {
149 (*it)->cleanup();
150 }
151 }
152
153 ScriptPromise FetchManager::fetch(ScriptState* scriptState, PassOwnPtr<ResourceR equest> request)
154 {
155 RefPtr<ScriptPromiseResolverWithContext> resolver = ScriptPromiseResolverWit hContext::create(scriptState);
156 ScriptPromise promise = resolver->promise();
157
158 OwnPtr<Loader> loader(adoptPtr(new Loader(m_executionContext, this, resolver .release(), request)));
159 (*m_loaders.add(loader.release()).storedValue)->start();
160 return promise;
161 }
162
163 void FetchManager::onLoaderFinished(Loader* loader)
164 {
165 m_loaders.remove(loader);
166 }
167
168 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698