prerender_url_loader_throttle.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_NO_STATE_PREFETCH_COMMON_PRERENDER_URL_LOADER_THROTTLE_H_
  5. #define COMPONENTS_NO_STATE_PREFETCH_COMMON_PRERENDER_URL_LOADER_THROTTLE_H_
  6. #include "base/callback.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/task/sequenced_task_runner.h"
  9. #include "base/timer/timer.h"
  10. #include "components/no_state_prefetch/common/prerender_canceler.mojom.h"
  11. #include "net/base/request_priority.h"
  12. #include "services/network/public/mojom/fetch_api.mojom-shared.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. #include "third_party/blink/public/common/loader/url_loader_throttle.h"
  15. namespace prerender {
  16. class PrerenderURLLoaderThrottle
  17. : public blink::URLLoaderThrottle,
  18. public base::SupportsWeakPtr<PrerenderURLLoaderThrottle> {
  19. public:
  20. PrerenderURLLoaderThrottle(
  21. const std::string& histogram_prefix,
  22. mojo::PendingRemote<prerender::mojom::PrerenderCanceler> canceler);
  23. ~PrerenderURLLoaderThrottle() override;
  24. // Called when the prerender is used. This will unpaused requests and set the
  25. // priorities to the original value.
  26. void PrerenderUsed();
  27. void set_destruction_closure(base::OnceClosure closure) {
  28. destruction_closure_ = std::move(closure);
  29. }
  30. private:
  31. // blink::URLLoaderThrottle implementation.
  32. void DetachFromCurrentSequence() override;
  33. void WillStartRequest(network::ResourceRequest* request,
  34. bool* defer) override;
  35. const char* NameForLoggingWillStartRequest() override;
  36. void WillRedirectRequest(
  37. net::RedirectInfo* redirect_info,
  38. const network::mojom::URLResponseHead& response_head,
  39. bool* defer,
  40. std::vector<std::string>* to_be_removed_headers,
  41. net::HttpRequestHeaders* modified_headers,
  42. net::HttpRequestHeaders* modified_cors_exempt_headers) override;
  43. void WillProcessResponse(const GURL& response_url,
  44. network::mojom::URLResponseHead* response_head,
  45. bool* defer) override;
  46. void OnTimedOut();
  47. std::string histogram_prefix_;
  48. bool deferred_ = false;
  49. int redirect_count_ = 0;
  50. network::mojom::RequestDestination request_destination_;
  51. mojo::PendingRemote<prerender::mojom::PrerenderCanceler> canceler_;
  52. // The throttle changes most request priorities to IDLE during prerendering.
  53. // The priority is reset back to the original priority when prerendering is
  54. // finished.
  55. absl::optional<net::RequestPriority> original_request_priority_;
  56. base::OnceClosure destruction_closure_;
  57. base::OneShotTimer detached_timer_;
  58. };
  59. } // namespace prerender
  60. #endif // COMPONENTS_NO_STATE_PREFETCH_COMMON_PRERENDER_URL_LOADER_THROTTLE_H_