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

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

Issue 641083002: Propagate error/exception information for rejected Promises in Service workers. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 2 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/core/v8/V8ErrorEvent.h"
12 #include "bindings/modules/v8/V8Response.h" 13 #include "bindings/modules/v8/V8Response.h"
13 #include "core/dom/ExecutionContext.h" 14 #include "core/dom/ExecutionContext.h"
15 #include "core/events/ErrorEvent.h"
14 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" 16 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h"
15 #include "platform/RuntimeEnabledFeatures.h" 17 #include "platform/RuntimeEnabledFeatures.h"
16 #include "public/platform/WebServiceWorkerResponse.h" 18 #include "public/platform/WebServiceWorkerResponse.h"
17 #include "wtf/Assertions.h" 19 #include "wtf/Assertions.h"
18 #include "wtf/RefPtr.h" 20 #include "wtf/RefPtr.h"
21 #include "core/workers/WorkerGlobalScope.h"
22 #include "core/workers/WorkerThread.h"
23 #include "core/workers/WorkerReportingProxy.h"
24 #include "core/dom/ExecutionContext.h"
19 #include <v8.h> 25 #include <v8.h>
20 26
27 #include "base/logging.h"
28
21 namespace blink { 29 namespace blink {
22 30
23 class RespondWithObserver::ThenFunction FINAL : public ScriptFunction { 31 class RespondWithObserver::ThenFunction FINAL : public ScriptFunction {
24 public: 32 public:
25 enum ResolveType { 33 enum ResolveType {
26 Fulfilled, 34 Fulfilled,
27 Rejected, 35 Rejected,
28 }; 36 };
29 37
30 static v8::Handle<v8::Function> createFunction(ScriptState* scriptState, Res pondWithObserver* observer, ResolveType type) 38 static v8::Handle<v8::Function> createFunction(ScriptState* scriptState, Res pondWithObserver* observer, ResolveType type)
(...skipping 14 matching lines...) Expand all
45 , m_observer(observer) 53 , m_observer(observer)
46 , m_resolveType(type) 54 , m_resolveType(type)
47 { 55 {
48 } 56 }
49 57
50 virtual ScriptValue call(ScriptValue value) OVERRIDE 58 virtual ScriptValue call(ScriptValue value) OVERRIDE
51 { 59 {
52 ASSERT(m_observer); 60 ASSERT(m_observer);
53 ASSERT(m_resolveType == Fulfilled || m_resolveType == Rejected); 61 ASSERT(m_resolveType == Fulfilled || m_resolveType == Rejected);
54 if (m_resolveType == Rejected) 62 if (m_resolveType == Rejected)
55 m_observer->responseWasRejected(); 63 m_observer->responseWasRejected(value);
56 else 64 else
57 m_observer->responseWasFulfilled(value); 65 m_observer->responseWasFulfilled(value);
58 m_observer = nullptr; 66 m_observer = nullptr;
59 return value; 67 return value;
60 } 68 }
61 69
62 Member<RespondWithObserver> m_observer; 70 Member<RespondWithObserver> m_observer;
63 ResolveType m_resolveType; 71 ResolveType m_resolveType;
64 }; 72 };
65 73
(...skipping 24 matching lines...) Expand all
90 exceptionState.throwDOMException(InvalidStateError, "respondWith is alre ady called."); 98 exceptionState.throwDOMException(InvalidStateError, "respondWith is alre ady called.");
91 return; 99 return;
92 } 100 }
93 101
94 m_state = Pending; 102 m_state = Pending;
95 ScriptPromise::cast(scriptState, value).then( 103 ScriptPromise::cast(scriptState, value).then(
96 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled) , 104 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled) ,
97 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected)) ; 105 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected)) ;
98 } 106 }
99 107
100 void RespondWithObserver::responseWasRejected() 108 void RespondWithObserver::responseWasRejected(const ScriptValue& value)
101 { 109 {
110 VLOG(1) <<__FUNCTION__;
102 ASSERT(executionContext()); 111 ASSERT(executionContext());
112 if(V8ErrorEvent::hasInstance(value.v8Value(), toIsolate(executionContext())) ) {
113 ErrorEvent* errEvent = V8ErrorEvent::toImplWithTypeCheck(toIsolate(execu tionContext()), value.v8Value());
114 toWorkerGlobalScope(executionContext())->thread()->workerReportingProxy( )
115 .reportException(errEvent->message(),
116 errEvent->lineno(),
117 errEvent->colno(),
118 errEvent->filename());
119
120 VLOG(1) << errEvent->message().ascii().data();
121 VLOG(1) << errEvent->filename().ascii().data()<< " : "<< errEvent->linen o() << " : " << errEvent->colno();
122 }
123
103 // The default value of WebServiceWorkerResponse's status is 0, which maps 124 // The default value of WebServiceWorkerResponse's status is 0, which maps
104 // to a network error. 125 // to a network error.
105 WebServiceWorkerResponse webResponse; 126 WebServiceWorkerResponse webResponse;
106 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven t(m_eventID, webResponse); 127 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven t(m_eventID, webResponse);
107 m_state = Done; 128 m_state = Done;
108 } 129 }
109 130
110 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) 131 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value)
111 { 132 {
112 ASSERT(executionContext()); 133 ASSERT(executionContext());
113 if (!V8Response::hasInstance(value.v8Value(), toIsolate(executionContext())) ) { 134 if (!V8Response::hasInstance(value.v8Value(), toIsolate(executionContext())) ) {
114 responseWasRejected(); 135 responseWasRejected(value);
115 return; 136 return;
116 } 137 }
117 Response* response = V8Response::toImplWithTypeCheck(toIsolate(executionCont ext()), value.v8Value()); 138 Response* response = V8Response::toImplWithTypeCheck(toIsolate(executionCont ext()), value.v8Value());
118 // "If either |response|'s type is |opaque| and |request|'s mode is not 139 // "If either |response|'s type is |opaque| and |request|'s mode is not
119 // |no CORS| or |response|'s type is |error|, return a network error." 140 // |no CORS| or |response|'s type is |error|, return a network error."
120 const FetchResponseData::Type responseType = response->response()->type(); 141 const FetchResponseData::Type responseType = response->response()->type();
121 if ((responseType == FetchResponseData::OpaqueType && m_requestMode != WebUR LRequest::FetchRequestModeNoCORS) || responseType == FetchResponseData::ErrorTyp e) { 142 if ((responseType == FetchResponseData::OpaqueType && m_requestMode != WebUR LRequest::FetchRequestModeNoCORS) || responseType == FetchResponseData::ErrorTyp e) {
122 responseWasRejected(); 143 responseWasRejected(value);
123 return; 144 return;
124 } 145 }
125 // Treat the opaque response as a network error for frame loading. 146 // Treat the opaque response as a network error for frame loading.
126 if (responseType == FetchResponseData::OpaqueType && m_frameType != WebURLRe quest::FrameTypeNone) { 147 if (responseType == FetchResponseData::OpaqueType && m_frameType != WebURLRe quest::FrameTypeNone) {
127 responseWasRejected(); 148 responseWasRejected(value);
128 return; 149 return;
129 } 150 }
130 WebServiceWorkerResponse webResponse; 151 WebServiceWorkerResponse webResponse;
131 response->populateWebServiceWorkerResponse(webResponse); 152 response->populateWebServiceWorkerResponse(webResponse);
132 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven t(m_eventID, webResponse); 153 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven t(m_eventID, webResponse);
133 m_state = Done; 154 m_state = Done;
134 } 155 }
135 156
136 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID, WebURLRequest::FetchRequestMode requestMode, WebURLRequest::FrameType frameType ) 157 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID, WebURLRequest::FetchRequestMode requestMode, WebURLRequest::FrameType frameType )
137 : ContextLifecycleObserver(context) 158 : ContextLifecycleObserver(context)
138 , m_eventID(eventID) 159 , m_eventID(eventID)
139 , m_requestMode(requestMode) 160 , m_requestMode(requestMode)
140 , m_frameType(frameType) 161 , m_frameType(frameType)
141 , m_state(Initial) 162 , m_state(Initial)
142 { 163 {
143 } 164 }
144 165
145 } // namespace blink 166 } // namespace blink
OLDNEW
« no previous file with comments | « Source/modules/serviceworkers/RespondWithObserver.h ('k') | Source/modules/serviceworkers/WaitUntilObserver.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698