1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93

content / browser / origin_trials / critical_origin_trials_throttle.h [blame]

// Copyright 2022 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_ORIGIN_TRIALS_CRITICAL_ORIGIN_TRIALS_THROTTLE_H_
#define CONTENT_BROWSER_ORIGIN_TRIALS_CRITICAL_ORIGIN_TRIALS_THROTTLE_H_

#include <optional>

#include "base/containers/flat_set.h"
#include "base/memory/raw_ref.h"
#include "content/common/content_export.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "third_party/blink/public/common/loader/url_loader_throttle.h"
#include "url/gurl.h"

namespace content {

class OriginTrialsControllerDelegate;

class CONTENT_EXPORT CriticalOriginTrialsThrottle
    : public blink::URLLoaderThrottle {
 public:
  // Create a throttle using the passed |origin_trials_delegate| for token
  // validation and the |top_frame_origin| as the partition origin. An empty
  // optional should be passed for |top_frame_origin| if the request is a main
  // frame navigation request.
  // TODO(crbug.com/40254225): Switch |top_frame_origin| to use Cookie
  // partitioning.
  CriticalOriginTrialsThrottle(
      OriginTrialsControllerDelegate& origin_trials_delegate,
      std::optional<url::Origin> top_frame_origin,
      std::optional<ukm::SourceId> source_id);

  ~CriticalOriginTrialsThrottle() override;

  // blink::URLLoaderThrottle
  void WillStartRequest(network::ResourceRequest* request,
                        bool* defer) override;
  void BeforeWillProcessResponse(
      const GURL& response_url,
      const network::mojom::URLResponseHead& response_head,
      RestartWithURLReset* restart_with_url_reset) override;
  void BeforeWillRedirectRequest(
      net::RedirectInfo* redirect_info,
      const network::mojom::URLResponseHead& response_head,
      RestartWithURLReset* restart_with_url_reset,
      std::vector<std::string>* to_be_removed_request_headers,
      net::HttpRequestHeaders* modified_request_headers,
      net::HttpRequestHeaders* modified_cors_exempt_request_headers) override;

  // This throttle only handles navigation requests. Use this method to
  // determine if the throttle will handle the passed-in |request| before
  // constructing a throttle.
  static bool IsNavigationRequest(const network::ResourceRequest& request);

 private:
  // The delegate is owned by the BrowserContext, and is expected to outlive
  // this throttle.
  raw_ref<OriginTrialsControllerDelegate, DanglingUntriaged>
      origin_trials_delegate_;

  std::optional<url::Origin> top_frame_origin_;

  bool is_navigation_request_ = false;

  // Ensure that there's only one restart per origin.
  base::flat_set<url::Origin> restarted_origins_;

  // Url of the last request made.
  GURL request_url_;

  // UKM source ID for the last request made.
  std::optional<ukm::SourceId> source_id_;

  // Trials that were persisted for the origin at the beginning of the request.
  base::flat_set<std::string> original_persisted_trials_;

  // Determine if critical origin trials have been enabled by the server
  // response and a restart is required.
  void MaybeRestartWithTrials(
      const network::mojom::URLResponseHead& response_head,
      RestartWithURLReset* restart_with_url_reset);

  // Stores the pre-request information, so it can be compared with the received
  // response headers.
  void SetPreRequestFields(const GURL& request_url);
};

}  // namespace content

#endif  // CONTENT_BROWSER_ORIGIN_TRIALS_CRITICAL_ORIGIN_TRIALS_THROTTLE_H_