url_request_rewrite_rules_manager.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_BROWSER_URL_REQUEST_REWRITE_RULES_MANAGER_H_
  5. #define COMPONENTS_URL_REWRITE_BROWSER_URL_REQUEST_REWRITE_RULES_MANAGER_H_
  6. #include "base/memory/scoped_refptr.h"
  7. #include "base/sequence_checker.h"
  8. #include "components/url_rewrite/common/url_request_rewrite_rules.h"
  9. #include "content/public/browser/global_routing_id.h"
  10. #include "content/public/browser/web_contents_observer.h"
  11. #include "mojo/public/cpp/bindings/associated_remote.h"
  12. namespace content {
  13. class RenderFrameHost;
  14. class WebContents;
  15. } // namespace content
  16. namespace url_rewrite {
  17. // Adapts the UrlRequestRewrite FIDL API to be sent to the renderers over the
  18. // over the UrlRequestRewrite Mojo API.
  19. class UrlRequestRewriteRulesManager {
  20. public:
  21. UrlRequestRewriteRulesManager();
  22. UrlRequestRewriteRulesManager(const UrlRequestRewriteRulesManager&) = delete;
  23. UrlRequestRewriteRulesManager& operator=(
  24. const UrlRequestRewriteRulesManager&) = delete;
  25. ~UrlRequestRewriteRulesManager();
  26. // Observes the frames created in the |web_contents| and updates their URL
  27. // request rewrite rules. Returns true on success, false otherwise.
  28. bool AddWebContents(content::WebContents* web_contents);
  29. // Removes the |web_contents| from the observer list. Returns true on success,
  30. // false otherwise.
  31. bool RemoveWebContents(content::WebContents* web_contents);
  32. // Signals |rules| have been updated. Returns true if
  33. // rules have been successfully validated and updated, false otherwise.
  34. bool OnRulesUpdated(mojom::UrlRequestRewriteRulesPtr rules);
  35. const scoped_refptr<UrlRequestRewriteRules>& GetCachedRules() const;
  36. // Used for testing.
  37. size_t GetUpdatersSizeForTesting() const;
  38. private:
  39. // Observes render frame creation and destruction for a certain WebContents
  40. // and updates the URL request rewrite rules for each frame.
  41. class Updater final : public content::WebContentsObserver {
  42. public:
  43. Updater(content::WebContents* web_contents,
  44. const scoped_refptr<UrlRequestRewriteRules>& cached_rules);
  45. Updater(const Updater&) = delete;
  46. Updater& operator=(const Updater&) = delete;
  47. ~Updater() override;
  48. // Notifies UrlRequestRulesReceivers that URL rewrite rules are updated.
  49. void OnRulesUpdated(
  50. const scoped_refptr<UrlRequestRewriteRules>& cached_rules);
  51. private:
  52. // Callback used to iterate over the initial set of RenderFrameHosts in the
  53. // WebContents.
  54. void MaybeRegisterExistingRenderFrame(
  55. content::RenderFrameHost* render_frame_host);
  56. // content::WebContentsObserver implementation.
  57. void RenderFrameCreated(
  58. content::RenderFrameHost* render_frame_host) override;
  59. void RenderFrameDeleted(
  60. content::RenderFrameHost* render_frame_host) override;
  61. scoped_refptr<UrlRequestRewriteRules> cached_rules_;
  62. // Map of GlobalRoutingID to their current associated remote.
  63. std::map<content::GlobalRenderFrameHostId,
  64. mojo::AssociatedRemote<mojom::UrlRequestRulesReceiver>>
  65. active_remotes_;
  66. SEQUENCE_CHECKER(sequence_checker_);
  67. };
  68. scoped_refptr<UrlRequestRewriteRules> cached_rules_;
  69. // Map of WebContent to their URL request rewrite rules updater.
  70. std::map<content::WebContents*, std::unique_ptr<Updater>> updaters_;
  71. SEQUENCE_CHECKER(sequence_checker_);
  72. };
  73. } // namespace url_rewrite
  74. #endif // COMPONENTS_URL_REWRITE_BROWSER_URL_REQUEST_REWRITE_RULES_MANAGER_H_