feature_manager_on_associated_interface.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2021 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 CHROMECAST_RENDERER_FEATURE_MANAGER_ON_ASSOCIATED_INTERFACE_H_
  5. #define CHROMECAST_RENDERER_FEATURE_MANAGER_ON_ASSOCIATED_INTERFACE_H_
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "chromecast/common/mojom/feature_manager.mojom.h"
  10. #include "content/public/renderer/render_frame_observer.h"
  11. #include "mojo/public/cpp/bindings/associated_receiver_set.h"
  12. #include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
  13. namespace chromecast {
  14. // Similar to the internal FeatureManager, but it's attached to associated
  15. // interface to ensure the actions are taken before the webpage is loaded.
  16. // TODO(b/187758538): Refactor this class with the internal FeatureManager. The
  17. // most likely result is we upstream and merge the internal FeatureManager to
  18. // this class and rename this class to FeatureManager.
  19. class FeatureManagerOnAssociatedInterface
  20. : public content::RenderFrameObserver,
  21. public shell::mojom::FeatureManager {
  22. public:
  23. explicit FeatureManagerOnAssociatedInterface(
  24. content::RenderFrame* render_frame);
  25. FeatureManagerOnAssociatedInterface(
  26. const FeatureManagerOnAssociatedInterface&) = delete;
  27. FeatureManagerOnAssociatedInterface& operator=(
  28. const FeatureManagerOnAssociatedInterface&) = delete;
  29. ~FeatureManagerOnAssociatedInterface() override;
  30. bool configured() const { return configured_; }
  31. bool FeatureEnabled(const std::string& feature) const;
  32. const chromecast::shell::mojom::FeaturePtr& GetFeature(
  33. const std::string& feature) const;
  34. private:
  35. // content::RenderFrameObserver implementation:
  36. bool OnAssociatedInterfaceRequestForFrame(
  37. const std::string& interface_name,
  38. mojo::ScopedInterfaceEndpointHandle* handle) override;
  39. void OnDestruct() override;
  40. // shell::mojom::FeatureManager implementation
  41. void ConfigureFeatures(
  42. std::vector<chromecast::shell::mojom::FeaturePtr> features) override;
  43. // Bind the incoming request with this implementation
  44. void OnFeatureManagerAssociatedRequest(
  45. mojo::PendingAssociatedReceiver<shell::mojom::FeatureManager>
  46. pending_receiver);
  47. // Flag for when the configuration message is received from the browser.
  48. bool configured_;
  49. // Map for storing enabled features, name -> FeaturePtr.
  50. using FeaturesMap =
  51. std::map<std::string, chromecast::shell::mojom::FeaturePtr>;
  52. FeaturesMap features_map_;
  53. blink::AssociatedInterfaceRegistry registry_;
  54. mojo::AssociatedReceiverSet<shell::mojom::FeatureManager> receivers_;
  55. };
  56. } // namespace chromecast
  57. #endif // CHROMECAST_RENDERER_FEATURE_MANAGER_ON_ASSOCIATED_INTERFACE_H_