Beispiel #1
0
void ServiceWorkerGlobalScopeProxy::dispatchForeignFetchEvent(
    int fetchEventID,
    const WebServiceWorkerRequest& webRequest) {
  if (!OriginTrials::foreignFetchEnabled(workerGlobalScope())) {
    // If origin trial tokens have expired, or are otherwise no longer valid
    // no events should be dispatched.
    // TODO(mek): Ideally the browser wouldn't even start the service worker
    // if its tokens have expired.
    ServiceWorkerGlobalScopeClient::from(workerGlobalScope())
        ->respondToFetchEvent(fetchEventID, WTF::currentTime());
    ServiceWorkerGlobalScopeClient::from(workerGlobalScope())
        ->didHandleFetchEvent(fetchEventID,
                              WebServiceWorkerEventResultCompleted,
                              WTF::currentTime());
    return;
  }

  ScriptState::Scope scope(
      workerGlobalScope()->scriptController()->getScriptState());
  RefPtr<SecurityOrigin> origin =
      SecurityOrigin::create(webRequest.referrerUrl());
  WaitUntilObserver* waitUntilObserver = WaitUntilObserver::create(
      workerGlobalScope(), WaitUntilObserver::Fetch, fetchEventID);
  ForeignFetchRespondWithObserver* respondWithObserver =
      ForeignFetchRespondWithObserver::create(
          workerGlobalScope(), fetchEventID, webRequest.url(),
          webRequest.mode(), webRequest.frameType(),
          webRequest.requestContext(), origin, waitUntilObserver);
  Request* request = Request::create(
      workerGlobalScope()->scriptController()->getScriptState(), webRequest);
  request->getHeaders()->setGuard(Headers::ImmutableGuard);
  ForeignFetchEventInit eventInit;
  eventInit.setCancelable(true);
  eventInit.setRequest(request);
  eventInit.setOrigin(origin->toString());
  ForeignFetchEvent* fetchEvent = ForeignFetchEvent::create(
      workerGlobalScope()->scriptController()->getScriptState(),
      EventTypeNames::foreignfetch, eventInit, respondWithObserver,
      waitUntilObserver);
  waitUntilObserver->willDispatchEvent();
  respondWithObserver->willDispatchEvent();
  DispatchEventResult dispatchResult =
      workerGlobalScope()->dispatchEvent(fetchEvent);
  respondWithObserver->didDispatchEvent(dispatchResult);
  // false is okay because waitUntil for foreign fetch event doesn't care
  // about the promise rejection or an uncaught runtime script error.
  waitUntilObserver->didDispatchEvent(false /* errorOccurred */);
}
Beispiel #2
0
void ServiceWorkerGlobalScopeProxy::dispatchFetchEvent(
    int fetchEventID,
    const WebServiceWorkerRequest& webRequest,
    bool navigationPreloadSent) {
  ScriptState::Scope scope(
      workerGlobalScope()->scriptController()->getScriptState());
  WaitUntilObserver* waitUntilObserver = WaitUntilObserver::create(
      workerGlobalScope(), WaitUntilObserver::Fetch, fetchEventID);
  RespondWithObserver* respondWithObserver = RespondWithObserver::create(
      workerGlobalScope(), fetchEventID, webRequest.url(), webRequest.mode(),
      webRequest.frameType(), webRequest.requestContext(), waitUntilObserver);
  Request* request = Request::create(
      workerGlobalScope()->scriptController()->getScriptState(), webRequest);
  request->getHeaders()->setGuard(Headers::ImmutableGuard);
  FetchEventInit eventInit;
  eventInit.setCancelable(true);
  eventInit.setRequest(request);
  eventInit.setClientId(
      webRequest.isMainResourceLoad() ? WebString() : webRequest.clientId());
  eventInit.setIsReload(webRequest.isReload());
  ScriptState* scriptState =
      workerGlobalScope()->scriptController()->getScriptState();
  FetchEvent* fetchEvent = FetchEvent::create(
      scriptState, EventTypeNames::fetch, eventInit, respondWithObserver,
      waitUntilObserver, navigationPreloadSent);
  if (navigationPreloadSent) {
    // Keep |fetchEvent| until onNavigationPreloadResponse() or
    // onNavigationPreloadError() will be called.
    m_pendingPreloadFetchEvents.add(fetchEventID, fetchEvent);
  }
  waitUntilObserver->willDispatchEvent();
  respondWithObserver->willDispatchEvent();
  DispatchEventResult dispatchResult =
      workerGlobalScope()->dispatchEvent(fetchEvent);
  respondWithObserver->didDispatchEvent(dispatchResult);
  // false is okay because waitUntil for fetch event doesn't care about the
  // promise rejection or an uncaught runtime script error.
  waitUntilObserver->didDispatchEvent(false /* errorOccurred */);
}