chromium/content/browser/service_worker/service_worker_updated_script_loader.h

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_UPDATED_SCRIPT_LOADER_H_
#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_UPDATED_SCRIPT_LOADER_H_

#include "base/time/time.h"
#include "content/browser/service_worker/service_worker_cache_writer.h"
#include "content/browser/service_worker/url_loader_client_checker.h"
#include "content/common/content_export.h"
#include "content/public/browser/browser_thread.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/net_adapters.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/mojom/url_loader.mojom.h"
#include "third_party/blink/public/mojom/loader/resource_load_info.mojom-shared.h"
#include "url/gurl.h"

namespace blink {
class ThrottlingURLLoader;
}  // namespace blink

namespace content {

class ServiceWorkerVersion;

// Used only for ServiceWorkerImportedScriptUpdateCheck.
//
// This is the URLLoader used for loading scripts for a new (installing) service
// worker. This is used for a script which has an update on the script during
// update checking in the browser process. Also used when the request was a
// network error during the update checking so that the same network error can
// be observed at the initial script evaluation in the renderer.
//
// This loader works as follows:
//   1. The ServiceWorkerCacheWriter used in the update check is resumed. After
//      that, the writer starts to make a new resource by copying the header and
//      body which has already been provided by the update checker. This loader
//      observes the copy (WillWriteInfo()/WillWriteData()), and sends the data
//      to |client_|.
//   2. After the copy has done, it resumes the network load. The rest of the
//      script body is responded to the |client_| and also written to service
//      worker storage.
//   3. When OnComplete() is called and the Mojo data pipe for the script body
//      is closed, it calls CommitCompleted() and closes the connections with
//      the network service and the renderer process.
//
// NOTE: To perform the network request, this class uses |loader_factory_| which
// may internally use a non-NetworkService factory if URL has a non-http(s)
// scheme, e.g., a chrome-extension:// URL. Regardless, that is still called a
// "network" request in comments and naming. "network" is meant to distinguish
// from the load this URLLoader does for its client:
//     "network" <------> SWUpdatedScriptLoader <------> client
class CONTENT_EXPORT ServiceWorkerUpdatedScriptLoader final
    : public network::mojom::URLLoader,
      public network::mojom::URLLoaderClient,
      public ServiceWorkerCacheWriter::WriteObserver {};

}  // namespace content

#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_UPDATED_SCRIPT_LOADER_H_