feature_manager.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_H_
  5. #define CHROMECAST_RENDERER_FEATURE_MANAGER_H_
  6. #include <iosfwd>
  7. #include <map>
  8. #include <string>
  9. #include <vector>
  10. #include "base/containers/flat_set.h"
  11. #include "base/values.h"
  12. #include "chromecast/common/mojom/feature_manager.mojom.h"
  13. #include "chromecast/renderer/native_bindings_helper.h"
  14. #include "content/public/renderer/render_frame_observer.h"
  15. #include "mojo/public/cpp/bindings/pending_receiver.h"
  16. #include "mojo/public/cpp/bindings/receiver.h"
  17. #include "mojo/public/cpp/bindings/receiver_set.h"
  18. #include "services/service_manager/public/cpp/binder_registry.h"
  19. #include "url/gurl.h"
  20. namespace chromecast {
  21. // Receives messages from the browser process to enable/disable Cast
  22. // application-facing features. Features usually have an associated V8 binding
  23. // which exposes a platform capability to the app.
  24. class FeatureManager : public content::RenderFrameObserver,
  25. public shell::mojom::FeatureManager {
  26. public:
  27. explicit FeatureManager(content::RenderFrame* render_frame);
  28. FeatureManager(const FeatureManager&) = delete;
  29. FeatureManager& operator=(const FeatureManager&) = delete;
  30. ~FeatureManager() override;
  31. const GURL& dev_origin() const { return dev_origin_; }
  32. bool configured() const { return configured_; }
  33. bool FeatureEnabled(const std::string& feature) const;
  34. const chromecast::shell::mojom::FeaturePtr& GetFeature(
  35. const std::string& feature) const;
  36. friend std::ostream& operator<<(std::ostream& os,
  37. const FeatureManager& features);
  38. protected:
  39. // Allows a derived class to add its own features at the end of
  40. // mojom::FeatureManager::ConfigureFeatures().
  41. virtual void ConfigureFeaturesInternal();
  42. // Map for storing enabled features, name -> FeaturePtr.
  43. using FeaturesMap =
  44. std::map<std::string, chromecast::shell::mojom::FeaturePtr>;
  45. FeaturesMap features_map_;
  46. base::flat_set<CastBinding*> v8_bindings_;
  47. private:
  48. // content::RenderFrameObserver implementation:
  49. void OnInterfaceRequestForFrame(
  50. const std::string& interface_name,
  51. mojo::ScopedMessagePipeHandle* interface_pipe) override;
  52. void DidClearWindowObject() override;
  53. void OnDestruct() override;
  54. // shell::mojom::FeatureManager implementation
  55. void ConfigureFeatures(
  56. std::vector<chromecast::shell::mojom::FeaturePtr> features) override;
  57. // Bind the incoming request with this implementation
  58. void OnFeatureManagerRequest(
  59. mojo::PendingReceiver<shell::mojom::FeatureManager> request);
  60. void EnableBindings();
  61. void SetupAdditionalSecureOrigin();
  62. // Flag for when the configuration message is received from the browser.
  63. bool configured_;
  64. bool can_install_bindings_;
  65. // Origin enabled for development
  66. GURL dev_origin_;
  67. bool secure_origin_set_;
  68. service_manager::BinderRegistry registry_;
  69. mojo::ReceiverSet<shell::mojom::FeatureManager> bindings_;
  70. };
  71. } // namespace chromecast
  72. #endif // CHROMECAST_RENDERER_FEATURE_MANAGER_H_