| OLD | NEW |
| 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/FetchEvent.h" | 5 #include "modules/serviceworkers/FetchEvent.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptState.h" | 7 #include "bindings/core/v8/ScriptState.h" |
| 8 #include "bindings/core/v8/ToV8ForCore.h" | 8 #include "bindings/core/v8/ToV8ForCore.h" |
| 9 #include "bindings/core/v8/V8PrivateProperty.h" | 9 #include "bindings/core/v8/V8PrivateProperty.h" |
| 10 #include "core/dom/ExecutionContext.h" | 10 #include "core/dom/ExecutionContext.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 } | 60 } |
| 61 | 61 |
| 62 ScriptPromise FetchEvent::preloadResponse(ScriptState* script_state) { | 62 ScriptPromise FetchEvent::preloadResponse(ScriptState* script_state) { |
| 63 return preload_response_property_->Promise(script_state->World()); | 63 return preload_response_property_->Promise(script_state->World()); |
| 64 } | 64 } |
| 65 | 65 |
| 66 const AtomicString& FetchEvent::InterfaceName() const { | 66 const AtomicString& FetchEvent::InterfaceName() const { |
| 67 return EventNames::FetchEvent; | 67 return EventNames::FetchEvent; |
| 68 } | 68 } |
| 69 | 69 |
| 70 bool FetchEvent::HasPendingActivity() const { |
| 71 // Prevent V8 from garbage collecting the wrapper object while waiting for the |
| 72 // preload response. This is in order to keep the resolver of preloadResponse |
| 73 // Promise alive. |
| 74 return preload_response_property_->GetState() == |
| 75 PreloadResponseProperty::kPending && |
| 76 GetExecutionContext(); |
| 77 } |
| 78 |
| 70 FetchEvent::FetchEvent(ScriptState* script_state, | 79 FetchEvent::FetchEvent(ScriptState* script_state, |
| 71 const AtomicString& type, | 80 const AtomicString& type, |
| 72 const FetchEventInit& initializer, | 81 const FetchEventInit& initializer, |
| 73 FetchRespondWithObserver* respond_with_observer, | 82 FetchRespondWithObserver* respond_with_observer, |
| 74 WaitUntilObserver* wait_until_observer, | 83 WaitUntilObserver* wait_until_observer, |
| 75 bool navigation_preload_sent) | 84 bool navigation_preload_sent) |
| 76 : ExtendableEvent(type, initializer, wait_until_observer), | 85 : ExtendableEvent(type, initializer, wait_until_observer), |
| 86 ContextClient(ExecutionContext::From(script_state)), |
| 77 observer_(respond_with_observer), | 87 observer_(respond_with_observer), |
| 78 preload_response_property_(new PreloadResponseProperty( | 88 preload_response_property_(new PreloadResponseProperty( |
| 79 ExecutionContext::From(script_state), | 89 ExecutionContext::From(script_state), |
| 80 this, | 90 this, |
| 81 PreloadResponseProperty::kPreloadResponse)) { | 91 PreloadResponseProperty::kPreloadResponse)) { |
| 82 if (!navigation_preload_sent) | 92 if (!navigation_preload_sent) |
| 83 preload_response_property_->ResolveWithUndefined(); | 93 preload_response_property_->ResolveWithUndefined(); |
| 84 | 94 |
| 85 client_id_ = initializer.clientId(); | 95 client_id_ = initializer.clientId(); |
| 86 is_reload_ = initializer.isReload(); | 96 is_reload_ = initializer.isReload(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 100 // Sets a hidden value in order to teach V8 the dependency from | 110 // Sets a hidden value in order to teach V8 the dependency from |
| 101 // the event to the request. | 111 // the event to the request. |
| 102 V8PrivateProperty::GetFetchEventRequest(script_state->GetIsolate()) | 112 V8PrivateProperty::GetFetchEventRequest(script_state->GetIsolate()) |
| 103 .Set(event.As<v8::Object>(), request); | 113 .Set(event.As<v8::Object>(), request); |
| 104 // From the same reason as above, setHiddenValue can return false. | 114 // From the same reason as above, setHiddenValue can return false. |
| 105 // TODO(yhirano): Add an assertion that it returns true once the | 115 // TODO(yhirano): Add an assertion that it returns true once the |
| 106 // graceful shutdown mechanism is introduced. | 116 // graceful shutdown mechanism is introduced. |
| 107 } | 117 } |
| 108 } | 118 } |
| 109 | 119 |
| 120 FetchEvent::~FetchEvent() {} |
| 121 |
| 110 void FetchEvent::OnNavigationPreloadResponse( | 122 void FetchEvent::OnNavigationPreloadResponse( |
| 111 ScriptState* script_state, | 123 ScriptState* script_state, |
| 112 std::unique_ptr<WebURLResponse> response, | 124 std::unique_ptr<WebURLResponse> response, |
| 113 std::unique_ptr<WebDataConsumerHandle> data_consume_handle) { | 125 std::unique_ptr<WebDataConsumerHandle> data_consume_handle) { |
| 114 if (!script_state->ContextIsValid()) | 126 if (!script_state->ContextIsValid()) |
| 115 return; | 127 return; |
| 116 DCHECK(preload_response_property_); | 128 DCHECK(preload_response_property_); |
| 117 ScriptState::Scope scope(script_state); | 129 ScriptState::Scope scope(script_state); |
| 118 FetchResponseData* response_data = | 130 FetchResponseData* response_data = |
| 119 data_consume_handle | 131 data_consume_handle |
| (...skipping 29 matching lines...) Expand all Loading... |
| 149 DCHECK(preload_response_property_); | 161 DCHECK(preload_response_property_); |
| 150 preload_response_property_->Reject( | 162 preload_response_property_->Reject( |
| 151 ServiceWorkerError::Take(nullptr, *error.get())); | 163 ServiceWorkerError::Take(nullptr, *error.get())); |
| 152 } | 164 } |
| 153 | 165 |
| 154 DEFINE_TRACE(FetchEvent) { | 166 DEFINE_TRACE(FetchEvent) { |
| 155 visitor->Trace(observer_); | 167 visitor->Trace(observer_); |
| 156 visitor->Trace(request_); | 168 visitor->Trace(request_); |
| 157 visitor->Trace(preload_response_property_); | 169 visitor->Trace(preload_response_property_); |
| 158 ExtendableEvent::Trace(visitor); | 170 ExtendableEvent::Trace(visitor); |
| 171 ContextClient::Trace(visitor); |
| 159 } | 172 } |
| 160 | 173 |
| 161 } // namespace blink | 174 } // namespace blink |
| OLD | NEW |