chromium/chromecast/browser/cast_network_contexts.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 CHROMECAST_BROWSER_CAST_NETWORK_CONTEXTS_H_
#define CHROMECAST_BROWSER_CAST_NETWORK_CONTEXTS_H_

#include <memory>
#include <string>
#include <vector>

#include "base/memory/scoped_refptr.h"
#include "content/public/browser/browser_thread.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/remote_set.h"
#include "net/proxy_resolution/proxy_config_service.h"
#include "services/cert_verifier/public/mojom/cert_verifier_service_factory.mojom-forward.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "services/network/public/mojom/proxy_config.mojom.h"
#include "services/network/public/mojom/proxy_config_with_annotation.mojom.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"

class PrefProxyConfigTracker;

namespace base {
class FilePath;
}

namespace content {
class BrowserContext;
}

namespace network {
namespace mojom {
class NetworkContext;
class URLLoaderFactory;
}  // namespace mojom
class SharedURLLoaderFactory;
}  // namespace network

namespace chromecast {
namespace shell {

// This class owns the NetworkContext used for the system and for configuring it
// along with the BrowserContext's NetworkContext.
// It will create and configure its own NetworkContext for the system, and
// create the BrowserContext's main StoragePartition's NetworkContext.
// It lives on the UI thread.
class CastNetworkContexts : public net::ProxyConfigService::Observer,
                            public network::mojom::ProxyConfigPollerClient {
 public:
  explicit CastNetworkContexts(
      std::vector<std::string> cors_exempt_headers_list);

  CastNetworkContexts(const CastNetworkContexts&) = delete;
  CastNetworkContexts& operator=(const CastNetworkContexts&) = delete;

  ~CastNetworkContexts() override;

  // Returns the System NetworkContext. Does any initialization of the
  // NetworkService that may be needed when first called.
  network::mojom::NetworkContext* GetSystemContext();

  // Returns a URLLoaderFactory owned by the CastNetworkContexts that is
  // backed by the system NetworkContext. Allows sharing of the
  // URLLoaderFactory. Prefer this to creating a new one.  Call Clone() on the
  // value returned by this method to get a URLLoaderFactory that can be used on
  // other threads.
  network::mojom::URLLoaderFactory* GetSystemURLLoaderFactory();

  // Returns a SharedURLLoaderFactory that is backed by the system
  // NetworkContext.
  scoped_refptr<network::SharedURLLoaderFactory>
  GetSystemSharedURLLoaderFactory();

  // Sets a list of domains which will be allowed to persist cookies.
  void SetAllowedDomainsForPersistentCookies(
      std::vector<std::string> allowed_domains_list);

  // Called when content creates a NetworkService. Creates the
  // system NetworkContext, if the network service is enabled.
  void OnNetworkServiceCreated(network::mojom::NetworkService* network_service);

  void ConfigureNetworkContextParams(
      content::BrowserContext* context,
      bool in_memory,
      const base::FilePath& relative_partition_path,
      network::mojom::NetworkContextParams* network_context_params,
      cert_verifier::mojom::CertVerifierCreationParams*
          cert_verifier_creation_params);

  // Called when the locale has changed.
  void OnLocaleUpdate();

  // Called on shutdown of the PrefService.
  void OnPrefServiceShutdown();

 private:
  class URLLoaderFactoryForSystem;

  // Fills in |network_context_params| with the default set of parameters for
  // configuring the network service.
  void ConfigureDefaultNetworkContextParams(
      network::mojom::NetworkContextParams* network_context_params);

  // Creates parameters for the system NetworkContext. May only be called once,
  // since it initializes some class members.
  network::mojom::NetworkContextParamsPtr CreateSystemNetworkContextParams();

  // Creates parameters for CookieManager of all NetworkContexts.
  network::mojom::CookieManagerParamsPtr CreateCookieManagerParams();

  // Populates proxy-related fields of |network_context_params|. Updated
  // ProxyConfigs will be sent to a NetworkContext created with those params
  // whenever the configuration changes. Can be called more than once to inform
  // multiple NetworkContexts of proxy changes.
  void AddProxyToNetworkContextParams(
      network::mojom::NetworkContextParams* network_context_params);

  // net::ProxyConfigService::Observer implementation:
  void OnProxyConfigChanged(
      const net::ProxyConfigWithAnnotation& config,
      net::ProxyConfigService::ConfigAvailability availability) override;

  // network::mojom::ProxyConfigPollerClient implementation:
  void OnLazyProxyConfigPoll() override;

  const std::vector<std::string> cors_exempt_headers_list_;
  std::vector<std::string> allowed_domains_for_persistent_cookies_;

  // The system NetworkContext.
  mojo::Remote<network::mojom::NetworkContext> system_network_context_;

  // URLLoaderFactory backed by the NetworkContext returned by
  // GetSystemContext(), so consumers don't all need to create their own
  // factory.
  scoped_refptr<URLLoaderFactoryForSystem> system_shared_url_loader_factory_;
  mojo::Remote<network::mojom::URLLoaderFactory> system_url_loader_factory_;

  std::unique_ptr<net::ProxyConfigService> proxy_config_service_;
  // Monitors prefs related to proxy configuration.
  std::unique_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_impl_;

  mojo::ReceiverSet<network::mojom::ProxyConfigPollerClient>
      poller_receiver_set_;
  mojo::RemoteSet<network::mojom::ProxyConfigClient> proxy_config_client_set_;
};

}  // namespace shell
}  // namespace chromecast

#endif  // CHROMECAST_BROWSER_CAST_NETWORK_CONTEXTS_H_