extension_throttle_manager.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2012 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 EXTENSIONS_RENDERER_EXTENSION_THROTTLE_MANAGER_H_
  5. #define EXTENSIONS_RENDERER_EXTENSION_THROTTLE_MANAGER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "base/memory/ref_counted.h"
  10. #include "base/synchronization/lock.h"
  11. #include "extensions/renderer/extension_throttle_entry.h"
  12. #include "services/network/public/mojom/url_response_head.mojom-forward.h"
  13. #include "url/gurl.h"
  14. namespace blink {
  15. class URLLoaderThrottle;
  16. class WebURLRequest;
  17. } // namespace blink
  18. namespace net {
  19. struct RedirectInfo;
  20. } // namespace net
  21. namespace extensions {
  22. // This is a thread safe class that registers URL request throttler entries for
  23. // URLs being accessed in order to supervise traffic. URL requests for HTTP
  24. // contents should register their URLs in this manager on each request.
  25. //
  26. // ExtensionThrottleManager maintains a map of URL IDs to URL request
  27. // throttler entries. It creates URL request throttler entries when new URLs
  28. // are registered, and does garbage collection from time to time in order to
  29. // clean out outdated entries. URL ID consists of lowercased scheme, host, port
  30. // and path. All URLs converted to the same ID will share the same entry.
  31. class ExtensionThrottleManager {
  32. public:
  33. ExtensionThrottleManager();
  34. ExtensionThrottleManager(const ExtensionThrottleManager&) = delete;
  35. ExtensionThrottleManager& operator=(const ExtensionThrottleManager&) = delete;
  36. virtual ~ExtensionThrottleManager();
  37. // Creates a throttle which uses this class to prevent extensions from
  38. // requesting a URL too often, if such a throttle is needed.
  39. std::unique_ptr<blink::URLLoaderThrottle> MaybeCreateURLLoaderThrottle(
  40. const blink::WebURLRequest& request);
  41. // Determine if a request to |request_url| should be rejected.
  42. bool ShouldRejectRequest(const GURL& request_url);
  43. // Determine if a redirect from the original |request_url| should be allowed
  44. // to be redirected as specified by |redirect_info|.
  45. bool ShouldRejectRedirect(const GURL& request_url,
  46. const net::RedirectInfo& redirect_info);
  47. // Must be called when the |response_head| for a request has been received.
  48. void WillProcessResponse(
  49. const GURL& response_url,
  50. const network::mojom::URLResponseHead& response_head);
  51. // Set the network status online state as specified in |is_online|.
  52. void SetOnline(bool is_online);
  53. void SetBackoffPolicyForTests(
  54. std::unique_ptr<net::BackoffEntry::Policy> policy);
  55. // Registers a new entry in this service and overrides the existing entry (if
  56. // any) for the URL. The service will hold a reference to the entry.
  57. // It is only used by unit tests.
  58. void OverrideEntryForTests(const GURL& url,
  59. std::unique_ptr<ExtensionThrottleEntry> entry);
  60. int GetNumberOfEntriesForTests() const { return url_entries_.size(); }
  61. protected:
  62. // Method that allows us to transform a URL into an ID that can be used in our
  63. // map. Resulting IDs will be lowercase and consist of the scheme, host, port
  64. // and path (without query string, fragment, etc.).
  65. // If the URL is invalid, the invalid spec will be returned, without any
  66. // transformation.
  67. std::string GetIdFromUrl(const GURL& url) const;
  68. // Must be called for every request, returns the URL request throttler entry
  69. // associated with the URL. The caller must inform this entry of some events.
  70. // Please refer to extension_throttle_entry.h for further information.
  71. ExtensionThrottleEntry* RegisterRequestUrl(const GURL& url);
  72. // Method that does the actual work of garbage collecting.
  73. void GarbageCollectEntries();
  74. private:
  75. // Explicitly erases an entry.
  76. // This is useful to remove those entries which have got infinite lifetime and
  77. // thus won't be garbage collected.
  78. // It is only used by unit tests.
  79. void EraseEntryForTests(const GURL& url);
  80. // Whether throttling is enabled or not.
  81. void set_enforce_throttling(bool enforce);
  82. bool enforce_throttling();
  83. // Method that ensures the map gets cleaned from time to time. The period at
  84. // which garbage collecting happens is adjustable with the
  85. // kRequestBetweenCollecting constant.
  86. void GarbageCollectEntriesIfNecessary();
  87. // Maximum number of entries that we are willing to collect in our map.
  88. static const unsigned int kMaximumNumberOfEntries;
  89. // Number of requests that will be made between garbage collection.
  90. static const unsigned int kRequestsBetweenCollecting;
  91. // Map that contains a list of URL ID (composed of the scheme, host, port and
  92. // path) and their matching ExtensionThrottleEntry.
  93. std::map<std::string, std::unique_ptr<ExtensionThrottleEntry>> url_entries_;
  94. // This keeps track of how many requests have been made. Used with
  95. // GarbageCollectEntries.
  96. unsigned int requests_since_last_gc_;
  97. // Valid after construction.
  98. GURL::Replacements url_id_replacements_;
  99. // This is null when it is not set for tests.
  100. std::unique_ptr<net::BackoffEntry::Policy> backoff_policy_for_tests_;
  101. // Used to synchronize all public methods.
  102. base::Lock lock_;
  103. };
  104. } // namespace extensions
  105. #endif // EXTENSIONS_RENDERER_EXTENSION_THROTTLE_MANAGER_H_