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

Side by Side Diff: third_party/WebKit/Source/modules/serviceworkers/FetchRespondWithObserver.cpp

Issue 2703343002: ServiceWorker: Use mojo's data pipe for respondWith(stream) (Closed)
Patch Set: Addressed comments from kinuko and haraken Created 3 years, 8 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/serviceworkers/FetchRespondWithObserver.h" 5 #include "modules/serviceworkers/FetchRespondWithObserver.h"
6 6
7 #include <v8.h> 7 #include <v8.h>
8 #include "bindings/core/v8/ScriptValue.h" 8 #include "bindings/core/v8/ScriptValue.h"
9 #include "bindings/core/v8/V8Binding.h" 9 #include "bindings/core/v8/V8Binding.h"
10 #include "bindings/modules/v8/V8Response.h" 10 #include "bindings/modules/v8/V8Response.h"
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 return frame_type != WebURLRequest::kFrameTypeNone; 106 return frame_type != WebURLRequest::kFrameTypeNone;
107 } 107 }
108 108
109 bool IsClientRequest(WebURLRequest::FrameType frame_type, 109 bool IsClientRequest(WebURLRequest::FrameType frame_type,
110 WebURLRequest::RequestContext request_context) { 110 WebURLRequest::RequestContext request_context) {
111 return IsNavigationRequest(frame_type) || 111 return IsNavigationRequest(frame_type) ||
112 request_context == WebURLRequest::kRequestContextSharedWorker || 112 request_context == WebURLRequest::kRequestContextSharedWorker ||
113 request_context == WebURLRequest::kRequestContextWorker; 113 request_context == WebURLRequest::kRequestContextWorker;
114 } 114 }
115 115
116 class NoopLoaderClient final 116 // Notifies the result of FetchDataLoader to |handle_|. |handle_| pass through
117 : public GarbageCollectedFinalized<NoopLoaderClient>, 117 // the result to its observer which is outside of blink.
118 class FetchLoaderClient final
119 : public GarbageCollectedFinalized<FetchLoaderClient>,
118 public FetchDataLoader::Client { 120 public FetchDataLoader::Client {
119 WTF_MAKE_NONCOPYABLE(NoopLoaderClient); 121 WTF_MAKE_NONCOPYABLE(FetchLoaderClient);
120 USING_GARBAGE_COLLECTED_MIXIN(NoopLoaderClient); 122 USING_GARBAGE_COLLECTED_MIXIN(FetchLoaderClient);
121 123
122 public: 124 public:
123 NoopLoaderClient() = default; 125 explicit FetchLoaderClient(
124 void DidFetchDataLoadedStream() override {} 126 std::unique_ptr<WebServiceWorkerStreamHandle> handle)
125 void DidFetchDataLoadFailed() override {} 127 : handle_(std::move(handle)) {}
128
129 void DidFetchDataLoadedDataPipe() override { handle_->Completed(); }
130 void DidFetchDataLoadFailed() override { handle_->Aborted(); }
131
126 DEFINE_INLINE_TRACE() { FetchDataLoader::Client::Trace(visitor); } 132 DEFINE_INLINE_TRACE() { FetchDataLoader::Client::Trace(visitor); }
133
134 private:
135 std::unique_ptr<WebServiceWorkerStreamHandle> handle_;
127 }; 136 };
128 137
129 } // namespace 138 } // namespace
130 139
131 FetchRespondWithObserver* FetchRespondWithObserver::Create( 140 FetchRespondWithObserver* FetchRespondWithObserver::Create(
132 ExecutionContext* context, 141 ExecutionContext* context,
133 int fetch_event_id, 142 int fetch_event_id,
134 const KURL& request_url, 143 const KURL& request_url,
135 WebURLRequest::FetchRequestMode request_mode, 144 WebURLRequest::FetchRequestMode request_mode,
136 WebURLRequest::FetchRedirectMode redirect_mode, 145 WebURLRequest::FetchRedirectMode redirect_mode,
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 return; 224 return;
216 } 225 }
217 226
218 WebServiceWorkerResponse web_response; 227 WebServiceWorkerResponse web_response;
219 response->PopulateWebServiceWorkerResponse(web_response); 228 response->PopulateWebServiceWorkerResponse(web_response);
220 BodyStreamBuffer* buffer = response->InternalBodyBuffer(); 229 BodyStreamBuffer* buffer = response->InternalBodyBuffer();
221 if (buffer) { 230 if (buffer) {
222 RefPtr<BlobDataHandle> blob_data_handle = buffer->DrainAsBlobDataHandle( 231 RefPtr<BlobDataHandle> blob_data_handle = buffer->DrainAsBlobDataHandle(
223 BytesConsumer::BlobSizePolicy::kAllowBlobWithInvalidSize); 232 BytesConsumer::BlobSizePolicy::kAllowBlobWithInvalidSize);
224 if (blob_data_handle) { 233 if (blob_data_handle) {
234 // Handle the blob response.
225 web_response.SetBlobDataHandle(blob_data_handle); 235 web_response.SetBlobDataHandle(blob_data_handle);
226 } else { 236 ServiceWorkerGlobalScopeClient::From(GetExecutionContext())
227 Stream* out_stream = Stream::Create(GetExecutionContext(), ""); 237 ->RespondToFetchEvent(event_id_, web_response, event_dispatch_time_);
228 web_response.SetStreamURL(out_stream->Url()); 238 return;
229 buffer->StartLoading(FetchDataLoader::CreateLoaderAsStream(out_stream),
230 new NoopLoaderClient);
231 } 239 }
240 // Handle the stream response.
241 mojo::DataPipe data_pipe;
242 DCHECK(data_pipe.producer_handle.is_valid());
243 DCHECK(data_pipe.consumer_handle.is_valid());
244
245 std::unique_ptr<WebServiceWorkerStreamHandle> body_stream_handle =
246 WTF::MakeUnique<WebServiceWorkerStreamHandle>(
247 std::move(data_pipe.consumer_handle));
248 ServiceWorkerGlobalScopeClient::From(GetExecutionContext())
249 ->RespondToFetchEventWithResponseStream(event_id_, web_response,
250 body_stream_handle.get(),
251 event_dispatch_time_);
252
253 buffer->StartLoading(FetchDataLoader::CreateLoaderAsDataPipe(
254 std::move(data_pipe.producer_handle)),
255 new FetchLoaderClient(std::move(body_stream_handle)));
256 return;
232 } 257 }
233 ServiceWorkerGlobalScopeClient::From(GetExecutionContext()) 258 ServiceWorkerGlobalScopeClient::From(GetExecutionContext())
234 ->RespondToFetchEvent(event_id_, web_response, event_dispatch_time_); 259 ->RespondToFetchEvent(event_id_, web_response, event_dispatch_time_);
235 } 260 }
236 261
237 void FetchRespondWithObserver::OnNoResponse() { 262 void FetchRespondWithObserver::OnNoResponse() {
238 ServiceWorkerGlobalScopeClient::From(GetExecutionContext()) 263 ServiceWorkerGlobalScopeClient::From(GetExecutionContext())
239 ->RespondToFetchEvent(event_id_, event_dispatch_time_); 264 ->RespondToFetchEventWithNoResponse(event_id_, event_dispatch_time_);
240 } 265 }
241 266
242 FetchRespondWithObserver::FetchRespondWithObserver( 267 FetchRespondWithObserver::FetchRespondWithObserver(
243 ExecutionContext* context, 268 ExecutionContext* context,
244 int fetch_event_id, 269 int fetch_event_id,
245 const KURL& request_url, 270 const KURL& request_url,
246 WebURLRequest::FetchRequestMode request_mode, 271 WebURLRequest::FetchRequestMode request_mode,
247 WebURLRequest::FetchRedirectMode redirect_mode, 272 WebURLRequest::FetchRedirectMode redirect_mode,
248 WebURLRequest::FrameType frame_type, 273 WebURLRequest::FrameType frame_type,
249 WebURLRequest::RequestContext request_context, 274 WebURLRequest::RequestContext request_context,
250 WaitUntilObserver* observer) 275 WaitUntilObserver* observer)
251 : RespondWithObserver(context, fetch_event_id, observer), 276 : RespondWithObserver(context, fetch_event_id, observer),
252 request_url_(request_url), 277 request_url_(request_url),
253 request_mode_(request_mode), 278 request_mode_(request_mode),
254 redirect_mode_(redirect_mode), 279 redirect_mode_(redirect_mode),
255 frame_type_(frame_type), 280 frame_type_(frame_type),
256 request_context_(request_context) {} 281 request_context_(request_context) {}
257 282
258 DEFINE_TRACE(FetchRespondWithObserver) { 283 DEFINE_TRACE(FetchRespondWithObserver) {
259 RespondWithObserver::Trace(visitor); 284 RespondWithObserver::Trace(visitor);
260 } 285 }
261 286
262 } // namespace blink 287 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698