web_content_runner.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2018 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 FUCHSIA_WEB_RUNNERS_COMMON_WEB_CONTENT_RUNNER_H_
  5. #define FUCHSIA_WEB_RUNNERS_COMMON_WEB_CONTENT_RUNNER_H_
  6. #include <fuchsia/io/cpp/fidl.h>
  7. #include <fuchsia/sys/cpp/fidl.h>
  8. #include <fuchsia/web/cpp/fidl.h>
  9. #include <memory>
  10. #include <set>
  11. #include <vector>
  12. #include "base/callback.h"
  13. #include "base/command_line.h"
  14. #include "base/containers/unique_ptr_adapters.h"
  15. class WebComponent;
  16. class WebInstanceHost;
  17. // sys::Runner that instantiates components hosting standard web content.
  18. class WebContentRunner : public fuchsia::sys::Runner {
  19. public:
  20. struct WebInstanceConfig {
  21. WebInstanceConfig();
  22. ~WebInstanceConfig();
  23. WebInstanceConfig(WebInstanceConfig&&);
  24. WebInstanceConfig& operator=(WebInstanceConfig&&);
  25. fuchsia::web::CreateContextParams params;
  26. base::CommandLine extra_args;
  27. };
  28. using GetWebInstanceConfigCallback =
  29. base::RepeatingCallback<WebInstanceConfig()>;
  30. // Creates a Runner which will (re-)create the Context, if not already
  31. // running, when StartComponent() is called.
  32. // |web_instance_host|: Used to create a web_instance Component in which to
  33. // host the fuchsia.web.Context.
  34. // |get_web_instance_config_callback|: Returns parameters for the Runner's
  35. // fuchsia.web.Context.
  36. WebContentRunner(
  37. WebInstanceHost* web_instance_host,
  38. GetWebInstanceConfigCallback get_web_instance_config_callback);
  39. // Creates a Runner using a Context configured with `web_instance_config`.
  40. // The Runner becomes non-functional if the Context terminates.
  41. WebContentRunner(WebInstanceHost* web_instance_host,
  42. WebInstanceConfig web_instance_config);
  43. ~WebContentRunner() override;
  44. WebContentRunner(const WebContentRunner&) = delete;
  45. WebContentRunner& operator=(const WebContentRunner&) = delete;
  46. // Returns a request handler for fuchsia.web.FrameHost protocol requests.
  47. // FrameHost instances will be run in the same web_instance as the Context
  48. // used to host WebComponent's Frames.
  49. // If no web_instance is currently running then
  50. // |get_web_instance_config_callback_| will be used to create one with the
  51. // specified parameters.
  52. fidl::InterfaceRequestHandler<fuchsia::web::FrameHost>
  53. GetFrameHostRequestHandler();
  54. // Used by WebComponent to create a Frame in this Runner's web_instance.
  55. // If no web_instance is active then |get_web_instance_config_callback_| will
  56. // be used to create one, if set (see class constructors).
  57. void CreateFrameWithParams(
  58. fuchsia::web::CreateFrameParams params,
  59. fidl::InterfaceRequest<fuchsia::web::Frame> request);
  60. // Used by WebComponent instances to signal that the ComponentController
  61. // channel was dropped, and therefore the component should be destroyed.
  62. void DestroyComponent(WebComponent* component);
  63. // fuchsia::sys::Runner implementation.
  64. void StartComponent(fuchsia::sys::Package package,
  65. fuchsia::sys::StartupInfo startup_info,
  66. fidl::InterfaceRequest<fuchsia::sys::ComponentController>
  67. controller_request) override;
  68. // Registers a WebComponent, or specialization, with this Runner.
  69. void RegisterComponent(std::unique_ptr<WebComponent> component);
  70. // Sets a callback to invoke when |components_| next becomes empty.
  71. void SetOnEmptyCallback(base::OnceClosure on_empty);
  72. // Tears down the Context, if any. This will trigger any active WebComponents
  73. // to be asynchronously torn-down.
  74. void DestroyWebContext();
  75. // TODO(https://crbug.com/1065707): Remove this once capability routing for
  76. // the fuchsia.legacymetrics.Provider service is properly set up.
  77. // Returns a pointer to any currently running component, or nullptr if no
  78. // components are currently running.
  79. WebComponent* GetAnyComponent();
  80. private:
  81. // Ensures that there is a web_instance Component running, and connects
  82. // |context_| to it.
  83. void EnsureWebInstanceAndContext();
  84. // Starts the web_instance and connects |context_| to it.
  85. void CreateWebInstanceAndContext(WebInstanceConfig web_instance_config);
  86. WebInstanceHost* const web_instance_host_;
  87. const GetWebInstanceConfigCallback get_web_instance_config_callback_;
  88. // If set, invoked whenever a WebComponent is created.
  89. base::RepeatingCallback<void(WebComponent*)>
  90. web_component_created_callback_for_test_;
  91. fuchsia::web::ContextPtr context_;
  92. fuchsia::io::DirectoryHandle web_instance_services_;
  93. std::set<std::unique_ptr<WebComponent>, base::UniquePtrComparator>
  94. components_;
  95. base::OnceClosure on_empty_callback_;
  96. };
  97. #endif // FUCHSIA_WEB_RUNNERS_COMMON_WEB_CONTENT_RUNNER_H_