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

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

Issue 1228233007: Shows error messages in the inspector when .respondWith() is called with wrong responses. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 5 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 "config.h" 5 #include "config.h"
6 #include "modules/serviceworkers/RespondWithObserver.h" 6 #include "modules/serviceworkers/RespondWithObserver.h"
7 7
8 #include "bindings/core/v8/ScriptFunction.h" 8 #include "bindings/core/v8/ScriptFunction.h"
9 #include "bindings/core/v8/ScriptPromise.h" 9 #include "bindings/core/v8/ScriptPromise.h"
10 #include "bindings/core/v8/ScriptValue.h" 10 #include "bindings/core/v8/ScriptValue.h"
11 #include "bindings/core/v8/V8Binding.h" 11 #include "bindings/core/v8/V8Binding.h"
12 #include "bindings/modules/v8/V8Response.h" 12 #include "bindings/modules/v8/V8Response.h"
13 #include "core/dom/ExceptionCode.h" 13 #include "core/dom/ExceptionCode.h"
14 #include "core/dom/ExecutionContext.h" 14 #include "core/dom/ExecutionContext.h"
15 #include "core/inspector/ConsoleMessage.h" 15 #include "core/inspector/ConsoleMessage.h"
16 #include "core/streams/Stream.h" 16 #include "core/streams/Stream.h"
17 #include "modules/fetch/BodyStreamBuffer.h" 17 #include "modules/fetch/BodyStreamBuffer.h"
18 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" 18 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h"
19 #include "platform/RuntimeEnabledFeatures.h" 19 #include "platform/RuntimeEnabledFeatures.h"
20 #include "public/platform/WebServiceWorkerResponse.h" 20 #include "public/platform/WebServiceWorkerResponse.h"
21 #include "wtf/Assertions.h" 21 #include "wtf/Assertions.h"
22 #include "wtf/RefPtr.h" 22 #include "wtf/RefPtr.h"
23 #include <v8.h> 23 #include <v8.h>
24 24
25 namespace blink { 25 namespace blink {
26 namespace {
27
28 // Outputs the error message to let the developer know about the reason of the u nusual failures.
29 void maybeOutputErrorMessage(ExecutionContext* context, WebServiceWorkerResponse Error error, const KURL& requestURL)
30 {
falken 2015/07/10 11:20:44 I suspect a more user-friendly way to present this
horo 2015/07/10 12:13:26 Done.
31 String errorMessage;
32 switch (error) {
33 case WebServiceWorkerResponseErrorUnknown:
34 errorMessage = "An unexpected error occured while handling the FetchEven t (" + requestURL.string() + ").";
falken 2015/07/10 11:20:44 "an unexpected error occurred." (this should proba
horo 2015/07/10 12:13:27 Done.
35 break;
36 case WebServiceWorkerResponseErrorPromiseRejected:
falken 2015/07/10 11:20:44 Can you comment why this doesn't have a message (I
horo 2015/07/10 12:13:27 Humm. It may better to show more detailed message
37 return;
38 break;
39 case WebServiceWorkerResponseErrorDefaultPrevented:
40 errorMessage = "event.preventDefault() for the FetchEvent (" + requestUR L.string() + ") without calling event.respondWith() is treated as a network erro r.";
falken 2015/07/10 11:20:44 "preventDefault() was called without calling respo
horo 2015/07/10 12:13:27 Done.
41 break;
42 case WebServiceWorkerResponseErrorNoV8Instance:
43 errorMessage = "The object with which respond to the request (" + reques tURL.string() + ") must be a Response object.";
falken 2015/07/10 11:20:44 "an object that was not a Response was passed to r
horo 2015/07/10 12:13:27 Done.
44 break;
45 case WebServiceWorkerResponseErrorResponseTypeError:
falken 2015/07/10 11:20:44 ditto, comment
horo 2015/07/10 12:13:26 Added.
46 return;
47 break;
48 case WebServiceWorkerResponseErrorResponseTypeOpaque:
49 errorMessage = "Opaque type response can't respond to the request (" + r equestURL.string() + ") which type isn't no-cors.";
falken 2015/07/10 11:20:44 "an opaque response was used for a request whose t
horo 2015/07/10 12:13:26 Done.
50 break;
51 case WebServiceWorkerResponseErrorResponseTypeNotBasicOrDefault:
52 errorMessage = "The type of the Response object with which respond to th e client request (" + requestURL.string() + ") must be basic or default.";
falken 2015/07/10 11:20:44 "the response for a client request must have type
horo 2015/07/10 12:13:27 Done.
53 break;
54 case WebServiceWorkerResponseErrorBodyUsed:
55 errorMessage = "The body used response can't respond to the request (" + requestURL.string() + ").";
falken 2015/07/10 11:20:44 "a Response whose \"bodyUsed\" is \"true\" cannot
horo 2015/07/10 12:13:26 Done.
56 break;
57 }
58 context->addConsoleMessage(ConsoleMessage::create(JSMessageSource, ErrorMess ageLevel, errorMessage));
falken 2015/07/10 11:20:44 I suspect this should be a Warning or Info, it's c
horo 2015/07/10 12:13:26 Changed to Info.
Mike West 2015/07/10 13:01:09 The "intentionally doing this" worries me a bit; w
falken 2015/07/13 00:51:36 +pfeldman FYI I don't have a very compelling use
falken 2015/07/13 00:58:10 Ah.. just realized respondWith()'s return value wo
59 }
60
61 } // namespace
26 62
27 class RespondWithObserver::ThenFunction final : public ScriptFunction { 63 class RespondWithObserver::ThenFunction final : public ScriptFunction {
28 public: 64 public:
29 enum ResolveType { 65 enum ResolveType {
30 Fulfilled, 66 Fulfilled,
31 Rejected, 67 Rejected,
32 }; 68 };
33 69
34 static v8::Local<v8::Function> createFunction(ScriptState* scriptState, Resp ondWithObserver* observer, ResolveType type) 70 static v8::Local<v8::Function> createFunction(ScriptState* scriptState, Resp ondWithObserver* observer, ResolveType type)
35 { 71 {
(...skipping 26 matching lines...) Expand all
62 m_observer->responseWasFulfilled(value); 98 m_observer->responseWasFulfilled(value);
63 } 99 }
64 m_observer = nullptr; 100 m_observer = nullptr;
65 return value; 101 return value;
66 } 102 }
67 103
68 Member<RespondWithObserver> m_observer; 104 Member<RespondWithObserver> m_observer;
69 ResolveType m_resolveType; 105 ResolveType m_resolveType;
70 }; 106 };
71 107
72 RespondWithObserver* RespondWithObserver::create(ExecutionContext* context, int eventID, WebURLRequest::FetchRequestMode requestMode, WebURLRequest::FrameType f rameType) 108 RespondWithObserver* RespondWithObserver::create(ExecutionContext* context, int eventID, const KURL& requestURL, WebURLRequest::FetchRequestMode requestMode, We bURLRequest::FrameType frameType)
73 { 109 {
74 return new RespondWithObserver(context, eventID, requestMode, frameType); 110 return new RespondWithObserver(context, eventID, requestURL, requestMode, fr ameType);
75 } 111 }
76 112
77 void RespondWithObserver::contextDestroyed() 113 void RespondWithObserver::contextDestroyed()
78 { 114 {
79 ContextLifecycleObserver::contextDestroyed(); 115 ContextLifecycleObserver::contextDestroyed();
80 m_state = Done; 116 m_state = Done;
81 } 117 }
82 118
83 void RespondWithObserver::didDispatchEvent(bool defaultPrevented) 119 void RespondWithObserver::didDispatchEvent(bool defaultPrevented)
84 { 120 {
(...skipping 19 matching lines...) Expand all
104 140
105 m_state = Pending; 141 m_state = Pending;
106 ScriptPromise::cast(scriptState, value).then( 142 ScriptPromise::cast(scriptState, value).then(
107 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled) , 143 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled) ,
108 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected)) ; 144 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected)) ;
109 } 145 }
110 146
111 void RespondWithObserver::responseWasRejected(WebServiceWorkerResponseError erro r) 147 void RespondWithObserver::responseWasRejected(WebServiceWorkerResponseError erro r)
112 { 148 {
113 ASSERT(executionContext()); 149 ASSERT(executionContext());
150 maybeOutputErrorMessage(executionContext(), error, m_requestURL);
114 // The default value of WebServiceWorkerResponse's status is 0, which maps 151 // The default value of WebServiceWorkerResponse's status is 0, which maps
115 // to a network error. 152 // to a network error.
116 WebServiceWorkerResponse webResponse; 153 WebServiceWorkerResponse webResponse;
117 webResponse.setError(error); 154 webResponse.setError(error);
118 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven t(m_eventID, webResponse); 155 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven t(m_eventID, webResponse);
119 m_state = Done; 156 m_state = Done;
120 } 157 }
121 158
122 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) 159 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value)
123 { 160 {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 buffer->startLoading(loader, nullptr); 204 buffer->startLoading(loader, nullptr);
168 m_state = Done; 205 m_state = Done;
169 return; 206 return;
170 } 207 }
171 WebServiceWorkerResponse webResponse; 208 WebServiceWorkerResponse webResponse;
172 response->populateWebServiceWorkerResponse(webResponse); 209 response->populateWebServiceWorkerResponse(webResponse);
173 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven t(m_eventID, webResponse); 210 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven t(m_eventID, webResponse);
174 m_state = Done; 211 m_state = Done;
175 } 212 }
176 213
177 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID, WebURLRequest::FetchRequestMode requestMode, WebURLRequest::FrameType frameType ) 214 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID, const KURL& requestURL, WebURLRequest::FetchRequestMode requestMode, WebURLRequ est::FrameType frameType)
178 : ContextLifecycleObserver(context) 215 : ContextLifecycleObserver(context)
179 , m_eventID(eventID) 216 , m_eventID(eventID)
217 , m_requestURL(requestURL)
180 , m_requestMode(requestMode) 218 , m_requestMode(requestMode)
181 , m_frameType(frameType) 219 , m_frameType(frameType)
182 , m_state(Initial) 220 , m_state(Initial)
183 { 221 {
184 } 222 }
185 223
186 DEFINE_TRACE(RespondWithObserver) 224 DEFINE_TRACE(RespondWithObserver)
187 { 225 {
188 ContextLifecycleObserver::trace(visitor); 226 ContextLifecycleObserver::trace(visitor);
189 } 227 }
190 228
191 } // namespace blink 229 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698