url_loader_throttle.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 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_URL_REWRITE_COMMON_URL_LOADER_THROTTLE_H_
  5. #define COMPONENTS_URL_REWRITE_COMMON_URL_LOADER_THROTTLE_H_
  6. #include "components/url_rewrite/common/url_request_rewrite_rules.h"
  7. #include "third_party/blink/public/common/loader/url_loader_throttle.h"
  8. namespace network {
  9. struct ResourceRequest;
  10. }
  11. namespace url_rewrite {
  12. // Implements a URLLoaderThrottle that applies network request rewrites provided
  13. // through the |UrlRequestRewriteRules| rules.
  14. class URLLoaderThrottle : public blink::URLLoaderThrottle {
  15. public:
  16. // A callback that checks if provided header is CORS exempt. The
  17. // implementation must be case-insensitive.
  18. using IsHeaderCorsExemptCallback =
  19. base::RepeatingCallback<bool(base::StringPiece)>;
  20. URLLoaderThrottle(scoped_refptr<UrlRequestRewriteRules> rules,
  21. IsHeaderCorsExemptCallback is_header_cors_exempt_callback);
  22. ~URLLoaderThrottle() override;
  23. URLLoaderThrottle(const URLLoaderThrottle&) = delete;
  24. URLLoaderThrottle& operator=(const URLLoaderThrottle&) = delete;
  25. // blink::URLLoaderThrottle implementation.
  26. void DetachFromCurrentSequence() override;
  27. void WillStartRequest(network::ResourceRequest* request,
  28. bool* defer) override;
  29. bool makes_unsafe_redirect() override;
  30. private:
  31. // Applies transformations specified by |rule| to |request|, conditional on
  32. // the matching criteria of |rule|.
  33. void ApplyRule(network::ResourceRequest* request,
  34. const mojom::UrlRequestRulePtr& rule);
  35. // Applies |rewrite| transformations to |request|.
  36. void ApplyRewrite(network::ResourceRequest* request,
  37. const mojom::UrlRequestActionPtr& rewrite);
  38. // Adds HTTP headers from |add_headers| to |request|.
  39. void ApplyAddHeaders(
  40. network::ResourceRequest* request,
  41. const mojom::UrlRequestRewriteAddHeadersPtr& add_headers);
  42. scoped_refptr<UrlRequestRewriteRules> rules_;
  43. IsHeaderCorsExemptCallback is_header_cors_exempt_callback_;
  44. };
  45. } // namespace url_rewrite
  46. #endif // COMPONENTS_URL_REWRITE_COMMON_URL_LOADER_THROTTLE_H_