cast_runner.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_CAST_CAST_RUNNER_H_
  5. #define FUCHSIA_WEB_RUNNERS_CAST_CAST_RUNNER_H_
  6. #include <fuchsia/camera3/cpp/fidl.h>
  7. #include <fuchsia/media/cpp/fidl.h>
  8. #include <fuchsia/sys/cpp/fidl.h>
  9. #include <fuchsia/web/cpp/fidl.h>
  10. #include <memory>
  11. #include <set>
  12. #include <vector>
  13. #include "base/callback.h"
  14. #include "base/containers/flat_set.h"
  15. #include "base/containers/unique_ptr_adapters.h"
  16. #include "base/fuchsia/startup_context.h"
  17. #include "fuchsia_web/runners/cast/cast_component.h"
  18. #include "fuchsia_web/runners/cast/fidl/fidl/chromium/cast/cpp/fidl.h"
  19. #include "fuchsia_web/runners/cast/pending_cast_component.h"
  20. #include "fuchsia_web/runners/common/web_content_runner.h"
  21. #include "third_party/abseil-cpp/absl/types/optional.h"
  22. namespace base {
  23. class FilteredServiceDirectory;
  24. }
  25. namespace fuchsia::legacymetrics {
  26. class MetricsRecorder;
  27. }
  28. class WebInstanceHost;
  29. // sys::Runner which instantiates Cast activities specified via cast/casts URIs.
  30. class CastRunner final : public fuchsia::sys::Runner,
  31. public chromium::cast::DataReset,
  32. public PendingCastComponent::Delegate {
  33. public:
  34. static constexpr uint16_t kRemoteDebuggingPort = 9222;
  35. // Creates the Runner for Cast components.
  36. // |web_instance_host|: Used to create an isolated web_instance
  37. // Component in which to host the fuchsia.web.Context.
  38. // |is_headless|: True if this instance should create Contexts with the
  39. // HEADLESS feature set.
  40. CastRunner(WebInstanceHost* web_instance_host, bool is_headless);
  41. ~CastRunner() override;
  42. CastRunner(const CastRunner&) = delete;
  43. CastRunner& operator=(const CastRunner&) = delete;
  44. // fuchsia::sys::Runner implementation.
  45. void StartComponent(fuchsia::sys::Package package,
  46. fuchsia::sys::StartupInfo startup_info,
  47. fidl::InterfaceRequest<fuchsia::sys::ComponentController>
  48. controller_request) override;
  49. // chromium::cast::DataReset implementation.
  50. void DeletePersistentData(DeletePersistentDataCallback callback) override;
  51. // Enables the special component that provides the fuchsia.web.FrameHost API,
  52. // hosted using the same WebEngine instance as the main web.Context.
  53. void set_enable_frame_host_component() {
  54. enable_frame_host_component_ = true;
  55. }
  56. // Disables use of the VULKAN feature when creating Contexts. Must be set
  57. // before calling StartComponent().
  58. void set_disable_vulkan_for_test() { disable_vulkan_for_test_ = true; }
  59. private:
  60. // PendingCastComponent::Delegate implementation.
  61. void LaunchPendingComponent(PendingCastComponent* pending_component,
  62. CastComponent::Params params) override;
  63. void CancelPendingComponent(PendingCastComponent* pending_component) override;
  64. // Handles component destruction.
  65. void OnComponentDestroyed(CastComponent* component);
  66. // Handlers used to provide parameters for main & isolated Contexts.
  67. WebContentRunner::WebInstanceConfig GetCommonWebInstanceConfig();
  68. WebContentRunner::WebInstanceConfig GetMainWebInstanceConfig();
  69. WebContentRunner::WebInstanceConfig
  70. GetIsolatedWebInstanceConfigWithFuchsiaDirs(
  71. std::vector<fuchsia::web::ContentDirectoryProvider> content_directories);
  72. // TODO(crbug.com/1082821): Remove this once the CastStreamingReceiver
  73. // Component has been implemented.
  74. WebContentRunner::WebInstanceConfig
  75. GetIsolatedWebInstanceConfigForCastStreaming();
  76. // Returns CreateContextParams for |app_config|. Returns nullopt if there is
  77. // no need to create an isolated context.
  78. absl::optional<WebContentRunner::WebInstanceConfig>
  79. GetWebInstanceConfigForAppConfig(
  80. chromium::cast::ApplicationConfig* app_config);
  81. // Launches an isolated Context with the given `config` and returns the newly
  82. // created WebContentRunner.
  83. WebContentRunner* CreateIsolatedRunner(
  84. WebContentRunner::WebInstanceConfig config);
  85. // Called when an isolated component terminates, to allow the Context hosting
  86. // it to be torn down.
  87. void OnIsolatedContextEmpty(WebContentRunner* context);
  88. // Connection handlers for redirected services.
  89. void OnAudioServiceRequest(
  90. fidl::InterfaceRequest<fuchsia::media::Audio> request);
  91. void OnCameraServiceRequest(
  92. fidl::InterfaceRequest<fuchsia::camera3::DeviceWatcher> request);
  93. void OnMetricsRecorderServiceRequest(
  94. fidl::InterfaceRequest<fuchsia::legacymetrics::MetricsRecorder> request);
  95. // Internal implementation of StartComponent(), called after validating the
  96. // component URL and ensuring that CORS-exempt headers have been fetched.
  97. void StartComponentInternal(
  98. const GURL& url,
  99. std::unique_ptr<base::StartupContext> startup_context,
  100. fidl::InterfaceRequest<fuchsia::sys::ComponentController>
  101. controller_request);
  102. // Moves all data persisted by the main Context to a staging directory,
  103. // which will be deleted the next time the Runner starts up, and configures
  104. // the Runner to reject new component-launch requests until it is shutdown.
  105. // Returns false if tha data directory cannot be cleaned-up.
  106. bool DeletePersistentDataInternal();
  107. // TODO(crbug.com/1188780): Used to detect when the persisted cache directory
  108. // was erased. The sentinel file is created at the top-level of the cache
  109. // directory, so cannot be deleted by the Context, only by the cache being
  110. // erased.
  111. void CreatePersistedCacheSentinel();
  112. bool WasPersistedCacheErased();
  113. // Passed to WebContentRunners to use to create web_instance Components.
  114. WebInstanceHost* const web_instance_host_;
  115. // True if this Runner uses Context(s) with the HEADLESS feature set.
  116. const bool is_headless_;
  117. // Holds the main fuchsia.web.Context used to host CastComponents.
  118. // Note that although |main_context_| is actually a WebContentRunner, that is
  119. // only being used to maintain the Context for the hosted components.
  120. const std::unique_ptr<base::FilteredServiceDirectory> main_services_;
  121. const std::unique_ptr<WebContentRunner> main_context_;
  122. const std::unique_ptr<base::FilteredServiceDirectory> isolated_services_;
  123. // Holds fuchsia.web.Contexts used to host isolated components.
  124. base::flat_set<std::unique_ptr<WebContentRunner>, base::UniquePtrComparator>
  125. isolated_contexts_;
  126. // Temporarily holds a PendingCastComponent instance, responsible for fetching
  127. // the parameters required to launch the component, for each call to
  128. // StartComponent().
  129. base::flat_set<std::unique_ptr<PendingCastComponent>,
  130. base::UniquePtrComparator>
  131. pending_components_;
  132. // True if this Runner should offer the fuchsia.web.FrameHost component.
  133. bool enable_frame_host_component_ = false;
  134. // Used to fetch & cache the list of CORS exempt HTTP headers to configure
  135. // each web.Context with.
  136. absl::optional<std::vector<std::vector<uint8_t>>> cors_exempt_headers_;
  137. chromium::cast::CorsExemptHeaderProviderPtr cors_exempt_headers_provider_;
  138. std::vector<base::OnceClosure> on_have_cors_exempt_headers_;
  139. // Reference to the service directory of the most recent FrameHost component.
  140. // Used to route MetricsRecorder requests from the web.Context, if there are
  141. // no CastComponents available through which to do so.
  142. base::WeakPtr<const sys::ServiceDirectory>
  143. frame_host_component_incoming_services_;
  144. // List of components created with permission to access MICROPHONE.
  145. base::flat_set<CastComponent*> audio_capturer_components_;
  146. // List of components created with permission to access CAMERA.
  147. base::flat_set<CastComponent*> video_capturer_components_;
  148. // The URL of the agent first using the respective capturer component.
  149. std::string first_audio_capturer_agent_url_;
  150. std::string first_video_capturer_agent_url_;
  151. // True if Contexts should be created without VULKAN set.
  152. bool disable_vulkan_for_test_ = false;
  153. // True if cast runner entered data reset mode. Prevents new components
  154. // in the main context from being launched. This is set to true once data
  155. // reset starts and does not switch back to false upon completion.
  156. bool data_reset_in_progress_ = false;
  157. // True if the cache sentinel file should exist.
  158. // TODO(crbug.com/1188780): Remove once an explicit cache flush signal exists.
  159. bool was_cache_sentinel_created_ = false;
  160. };
  161. #endif // FUCHSIA_WEB_RUNNERS_CAST_CAST_RUNNER_H_