frame_impl.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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_WEBENGINE_BROWSER_FRAME_IMPL_H_
  5. #define FUCHSIA_WEB_WEBENGINE_BROWSER_FRAME_IMPL_H_
  6. #include <fuchsia/logger/cpp/fidl.h>
  7. #include <fuchsia/mem/cpp/fidl.h>
  8. #include <fuchsia/web/cpp/fidl.h>
  9. #include <lib/fidl/cpp/binding_set.h>
  10. #include <lib/inspect/cpp/vmo/types.h>
  11. #include <lib/syslog/structured_backend/cpp/fuchsia_syslog.h>
  12. #include <lib/ui/scenic/cpp/view_ref_pair.h>
  13. #include <lib/zx/channel.h>
  14. #include <list>
  15. #include <map>
  16. #include <memory>
  17. #include <string>
  18. #include <utility>
  19. #include <vector>
  20. #include "base/fuchsia/scoped_fx_logger.h"
  21. #include "base/gtest_prod_util.h"
  22. #include "base/memory/read_only_shared_memory_region.h"
  23. #include "components/media_control/browser/media_blocker.h"
  24. #include "components/on_load_script_injector/browser/on_load_script_injector_host.h"
  25. #include "components/url_rewrite/browser/url_request_rewrite_rules_manager.h"
  26. #include "content/public/browser/web_contents_delegate.h"
  27. #include "content/public/browser/web_contents_observer.h"
  28. #include "fuchsia_web/webengine/browser/accessibility_bridge.h"
  29. #include "fuchsia_web/webengine/browser/event_filter.h"
  30. #include "fuchsia_web/webengine/browser/frame_permission_controller.h"
  31. #include "fuchsia_web/webengine/browser/navigation_controller_impl.h"
  32. #include "fuchsia_web/webengine/browser/theme_manager.h"
  33. #include "fuchsia_web/webengine/web_engine_export.h"
  34. #include "third_party/abseil-cpp/absl/types/optional.h"
  35. #include "third_party/blink/public/common/web_preferences/web_preferences.h"
  36. #include "ui/accessibility/platform/fuchsia/accessibility_bridge_fuchsia_impl.h"
  37. #include "ui/aura/window_tree_host.h"
  38. #include "ui/wm/core/focus_controller.h"
  39. #include "url/gurl.h"
  40. namespace content {
  41. class FromRenderFrameHost;
  42. } // namespace content
  43. class ReceiverSessionClient;
  44. class ContextImpl;
  45. class FrameWindowTreeHost;
  46. class FrameLayoutManager;
  47. class MediaPlayerImpl;
  48. class NavigationPolicyHandler;
  49. // Implementation of fuchsia.web.Frame based on content::WebContents.
  50. class WEB_ENGINE_EXPORT FrameImpl : public fuchsia::web::Frame,
  51. public content::WebContentsObserver,
  52. public content::WebContentsDelegate {
  53. public:
  54. // Returns FrameImpl that owns the |web_contents| or nullptr if the
  55. // |web_contents| is nullptr. Returns nullptr if there is no FrameImpl that
  56. // owns the |web_contents|, which can happen if FrameImpl has not been
  57. // initialized yet.
  58. static FrameImpl* FromWebContents(content::WebContents* web_contents);
  59. // Returns FrameImpl that owns the |render_frame_host| or nullptr if the
  60. // |render_frame_host| is nullptr. Returns nullptr if there is no FrameImpl
  61. // that owns the |web_contents|, which can happen if FrameImpl has not been
  62. // initialized yet.
  63. static FrameImpl* FromRenderFrameHost(
  64. content::RenderFrameHost* render_frame_host);
  65. // |context| must out-live |this|.
  66. // |params| apply both to this Frame, and also to any popup Frames it creates.
  67. // |inspect_node| will be populated with diagnostic data for this Frame.
  68. // DestroyFrame() is automatically called on |context| if the |frame_request|
  69. // channel disconnects.
  70. FrameImpl(std::unique_ptr<content::WebContents> web_contents,
  71. ContextImpl* context,
  72. fuchsia::web::CreateFrameParams params,
  73. inspect::Node inspect_node,
  74. fidl::InterfaceRequest<fuchsia::web::Frame> frame_request);
  75. ~FrameImpl() override;
  76. FrameImpl(const FrameImpl&) = delete;
  77. FrameImpl& operator=(const FrameImpl&) = delete;
  78. const fuchsia::web::FrameMediaSettings& media_settings() const {
  79. return media_settings_;
  80. }
  81. FramePermissionController* permission_controller() {
  82. return &permission_controller_;
  83. }
  84. url_rewrite::UrlRequestRewriteRulesManager*
  85. url_request_rewrite_rules_manager() {
  86. return &url_request_rewrite_rules_manager_;
  87. }
  88. NavigationPolicyHandler* navigation_policy_handler() {
  89. return navigation_policy_handler_.get();
  90. }
  91. // Enables explicit sites filtering and set the error page. If |error_page| is
  92. // empty, the default error page will be used.
  93. void EnableExplicitSitesFilter(std::string error_page);
  94. const absl::optional<std::string>& explicit_sites_filter_error_page() const {
  95. return explicit_sites_filter_error_page_;
  96. }
  97. // Accessors required by tests.
  98. zx::unowned_channel GetBindingChannelForTest() const;
  99. content::WebContents* web_contents_for_test() const {
  100. return web_contents_.get();
  101. }
  102. bool has_view_for_test() const { return window_tree_host_ != nullptr; }
  103. FrameWindowTreeHost* window_tree_host_for_test() {
  104. return window_tree_host_.get();
  105. }
  106. // Accessibility bridge accessor/setter methods.
  107. // TODO(crbug.com/1291330): Remove the three methods below.
  108. void set_use_v2_accessibility_bridge(bool use_v2_accessibility_bridge) {
  109. use_v2_accessibility_bridge_ = use_v2_accessibility_bridge;
  110. }
  111. AccessibilityBridge* accessibility_bridge_for_test() const {
  112. return accessibility_bridge_.get();
  113. }
  114. void set_semantics_manager_for_test(
  115. fuchsia::accessibility::semantics::SemanticsManager* semantics_manager) {
  116. semantics_manager_for_test_ = semantics_manager;
  117. }
  118. // Override |blink_prefs| with settings defined in |content_settings_|.
  119. //
  120. // This method is called when WebPreferences is first created and when it is
  121. // recomputed.
  122. void OverrideWebPreferences(blink::web_pref::WebPreferences* web_prefs);
  123. void set_window_size_for_test(gfx::Size size) {
  124. window_size_for_test_ = size;
  125. }
  126. void set_device_scale_factor_for_test(float device_scale_factor) {
  127. device_scale_factor_for_test_ = device_scale_factor;
  128. }
  129. private:
  130. FRIEND_TEST_ALL_PREFIXES(FrameImplTest, DelayedNavigationEventAck);
  131. FRIEND_TEST_ALL_PREFIXES(FrameImplTest, NavigationObserverDisconnected);
  132. FRIEND_TEST_ALL_PREFIXES(FrameImplTest, NoNavigationObserverAttached);
  133. FRIEND_TEST_ALL_PREFIXES(FrameImplTest, ReloadFrame);
  134. FRIEND_TEST_ALL_PREFIXES(FrameImplTest, Stop);
  135. FRIEND_TEST_ALL_PREFIXES(FuchsiaFrameAccessibilityTest, HitTest);
  136. // Used for storing awaiting popup frames in |pending_popups_|
  137. struct PendingPopup {
  138. PendingPopup(FrameImpl* frame_ptr,
  139. fidl::InterfaceHandle<fuchsia::web::Frame> handle,
  140. fuchsia::web::PopupFrameCreationInfo creation_info);
  141. PendingPopup(PendingPopup&& other);
  142. ~PendingPopup();
  143. FrameImpl* frame_ptr;
  144. fidl::InterfaceHandle<fuchsia::web::Frame> handle;
  145. fuchsia::web::PopupFrameCreationInfo creation_info;
  146. };
  147. aura::Window* root_window() const;
  148. // Shared implementation for the ExecuteJavaScript[NoResult]() APIs.
  149. void ExecuteJavaScriptInternal(std::vector<std::string> origins,
  150. fuchsia::mem::Buffer script,
  151. ExecuteJavaScriptCallback callback,
  152. bool need_result);
  153. // Sends the next entry in |pending_popups_| to |popup_listener_|.
  154. void MaybeSendPopup();
  155. void OnPopupListenerDisconnected(zx_status_t status);
  156. // Cleans up the MediaPlayerImpl on disconnect.
  157. void OnMediaPlayerDisconnect();
  158. // An error handler for |accessibility_bridge_|.
  159. bool OnAccessibilityError(zx_status_t error);
  160. // Creates and initializes WindowTreeHost for the view with the specified
  161. // |view_token|. |view_token| may be uninitialized in headless mode.
  162. void SetupWindowTreeHost(fuchsia::ui::views::ViewToken view_token,
  163. scenic::ViewRefPair view_ref_pair);
  164. // Creates and initializes WindowTreeHost for the view with the specified
  165. // |view_creation_token|. |view_creation_token| may be uninitialized in
  166. // headless mode.
  167. void SetupWindowTreeHost(
  168. fuchsia::ui::views::ViewCreationToken view_creation_token,
  169. scenic::ViewRefPair view_ref_pair);
  170. // Initializes WindowTreeHost.
  171. void InitWindowTreeHost();
  172. // Destroys the WindowTreeHost along with its view or other associated
  173. // resources.
  174. void DestroyWindowTreeHost();
  175. // Destroys |this| and sends the FIDL |error| to the client.
  176. void CloseAndDestroyFrame(zx_status_t error);
  177. // Determines whether |message| is a Cast Streaming message and if so, handles
  178. // it. Returns whether it handled the message, regardless of whether that was
  179. // successful. If true is returned, |callback| has been called. Returns false
  180. // immediately if Cast Streaming support is not enabled. Called by
  181. // PostMessage().
  182. bool MaybeHandleCastStreamingMessage(std::string* origin,
  183. fuchsia::web::WebMessage* message,
  184. PostMessageCallback* callback);
  185. void MaybeStartCastStreaming(content::NavigationHandle* navigation_handle);
  186. // Updates zoom level for the specified |render_view_host|.
  187. void UpdateRenderViewZoomLevel(content::RenderViewHost* render_view_host);
  188. // Helper method for connecting to AccessibilityBridge on
  189. // |accessibility_bridge_|.
  190. void ConnectToAccessibilityBridge();
  191. // fuchsia::web::Frame implementation.
  192. void CreateView(fuchsia::ui::views::ViewToken view_token) override;
  193. void CreateViewWithViewRef(fuchsia::ui::views::ViewToken view_token,
  194. fuchsia::ui::views::ViewRefControl control_ref,
  195. fuchsia::ui::views::ViewRef view_ref) override;
  196. void CreateView2(fuchsia::web::CreateView2Args view_args) override;
  197. void GetMediaPlayer(fidl::InterfaceRequest<fuchsia::media::sessions2::Player>
  198. player) override;
  199. void GetNavigationController(
  200. fidl::InterfaceRequest<fuchsia::web::NavigationController> controller)
  201. override;
  202. void ExecuteJavaScript(std::vector<std::string> origins,
  203. fuchsia::mem::Buffer script,
  204. ExecuteJavaScriptCallback callback) override;
  205. void ExecuteJavaScriptNoResult(
  206. std::vector<std::string> origins,
  207. fuchsia::mem::Buffer script,
  208. ExecuteJavaScriptNoResultCallback callback) override;
  209. void AddBeforeLoadJavaScript(
  210. uint64_t id,
  211. std::vector<std::string> origins,
  212. fuchsia::mem::Buffer script,
  213. AddBeforeLoadJavaScriptCallback callback) override;
  214. void RemoveBeforeLoadJavaScript(uint64_t id) override;
  215. void PostMessage(std::string origin,
  216. fuchsia::web::WebMessage message,
  217. fuchsia::web::Frame::PostMessageCallback callback) override;
  218. void SetNavigationEventListener(
  219. fidl::InterfaceHandle<fuchsia::web::NavigationEventListener> listener)
  220. override;
  221. void SetNavigationEventListener2(
  222. fidl::InterfaceHandle<fuchsia::web::NavigationEventListener> listener,
  223. fuchsia::web::NavigationEventListenerFlags flags) override;
  224. void SetJavaScriptLogLevel(fuchsia::web::ConsoleLogLevel level) override;
  225. void SetConsoleLogSink(fuchsia::logger::LogSinkHandle sink) override;
  226. void ConfigureInputTypes(fuchsia::web::InputTypes types,
  227. fuchsia::web::AllowInputState allow) override;
  228. void SetPopupFrameCreationListener(
  229. fidl::InterfaceHandle<fuchsia::web::PopupFrameCreationListener> listener)
  230. override;
  231. void SetUrlRequestRewriteRules(
  232. std::vector<fuchsia::web::UrlRequestRewriteRule> rules,
  233. SetUrlRequestRewriteRulesCallback callback) override;
  234. void EnableHeadlessRendering() override;
  235. void DisableHeadlessRendering() override;
  236. void SetMediaSettings(
  237. fuchsia::web::FrameMediaSettings media_settings) override;
  238. void ForceContentDimensions(
  239. std::unique_ptr<fuchsia::ui::gfx::vec2> web_dips) override;
  240. void SetPermissionState(fuchsia::web::PermissionDescriptor permission,
  241. std::string web_origin,
  242. fuchsia::web::PermissionState state) override;
  243. void SetBlockMediaLoading(bool blocked) override;
  244. void MediaStartedPlaying(const MediaPlayerInfo& video_type,
  245. const content::MediaPlayerId& id) override;
  246. void MediaStoppedPlaying(
  247. const MediaPlayerInfo& video_type,
  248. const content::MediaPlayerId& id,
  249. WebContentsObserver::MediaStoppedReason reason) override;
  250. void GetPrivateMemorySize(GetPrivateMemorySizeCallback callback) override;
  251. void SetNavigationPolicyProvider(
  252. fuchsia::web::NavigationPolicyProviderParams params,
  253. fidl::InterfaceHandle<fuchsia::web::NavigationPolicyProvider> provider)
  254. override;
  255. void SetPreferredTheme(fuchsia::settings::ThemeType theme) override;
  256. void SetPageScale(float scale) override;
  257. void SetContentAreaSettings(
  258. fuchsia::web::ContentAreaSettings settings) override;
  259. void ResetContentAreaSettings() override;
  260. void OnThemeManagerError();
  261. // content::WebContentsDelegate implementation.
  262. void CloseContents(content::WebContents* source) override;
  263. bool DidAddMessageToConsole(content::WebContents* source,
  264. blink::mojom::ConsoleMessageLevel log_level,
  265. const std::u16string& message,
  266. int32_t line_no,
  267. const std::u16string& source_id) override;
  268. bool IsWebContentsCreationOverridden(
  269. content::SiteInstance* source_site_instance,
  270. content::mojom::WindowContainerType window_container_type,
  271. const GURL& opener_url,
  272. const std::string& frame_name,
  273. const GURL& target_url) override;
  274. void WebContentsCreated(content::WebContents* source_contents,
  275. int opener_render_process_id,
  276. int opener_render_frame_id,
  277. const std::string& frame_name,
  278. const GURL& target_url,
  279. content::WebContents* new_contents) override;
  280. void AddNewContents(content::WebContents* source,
  281. std::unique_ptr<content::WebContents> new_contents,
  282. const GURL& target_url,
  283. WindowOpenDisposition disposition,
  284. const gfx::Rect& initial_rect,
  285. bool user_gesture,
  286. bool* was_blocked) override;
  287. void RequestMediaAccessPermission(
  288. content::WebContents* web_contents,
  289. const content::MediaStreamRequest& request,
  290. content::MediaResponseCallback callback) override;
  291. bool CheckMediaAccessPermission(content::RenderFrameHost* render_frame_host,
  292. const GURL& security_origin,
  293. blink::mojom::MediaStreamType type) override;
  294. bool CanOverscrollContent() override;
  295. // content::WebContentsObserver implementation.
  296. void ReadyToCommitNavigation(
  297. content::NavigationHandle* navigation_handle) override;
  298. void DidFinishLoad(content::RenderFrameHost* render_frame_host,
  299. const GURL& validated_url) override;
  300. void RenderFrameCreated(content::RenderFrameHost* frame_host) override;
  301. void RenderViewHostChanged(content::RenderViewHost* old_host,
  302. content::RenderViewHost* new_host) override;
  303. void DidFirstVisuallyNonEmptyPaint() override;
  304. void ResourceLoadComplete(
  305. content::RenderFrameHost* render_frame_host,
  306. const content::GlobalRequestID& request_id,
  307. const blink::mojom::ResourceLoadInfo& resource_load_info) override;
  308. void OnPixelScaleUpdate(float pixel_scale);
  309. void SetAccessibilityEnabled(bool enabled);
  310. const std::unique_ptr<content::WebContents> web_contents_;
  311. ContextImpl* const context_;
  312. // Optional tag to apply when emitting web console logs.
  313. const std::string console_log_tag_;
  314. // Logger used for console messages from content, depending on |log_level_|.
  315. base::ScopedFxLogger console_logger_;
  316. FuchsiaLogSeverity log_level_ = FUCHSIA_LOG_NONE;
  317. // Parameters applied to popups created by content running in this Frame.
  318. const fuchsia::web::CreateFrameParams params_for_popups_;
  319. std::unique_ptr<FrameWindowTreeHost> window_tree_host_;
  320. std::unique_ptr<wm::FocusController> focus_controller_;
  321. // Owned via |window_tree_host_|.
  322. FrameLayoutManager* layout_manager_ = nullptr;
  323. // TODO(crbug.com/1291330): Remove acessibility_bridge_ and
  324. // semantics_manager_for_test_.
  325. std::unique_ptr<AccessibilityBridge> accessibility_bridge_;
  326. fuchsia::accessibility::semantics::SemanticsManager*
  327. semantics_manager_for_test_ = nullptr;
  328. std::unique_ptr<ui::AccessibilityBridgeFuchsiaImpl> v2_accessibility_bridge_;
  329. // Test settings.
  330. absl::optional<gfx::Size> window_size_for_test_;
  331. absl::optional<float> device_scale_factor_for_test_;
  332. EventFilter event_filter_;
  333. NavigationControllerImpl navigation_controller_;
  334. url_rewrite::UrlRequestRewriteRulesManager url_request_rewrite_rules_manager_;
  335. FramePermissionController permission_controller_;
  336. std::unique_ptr<NavigationPolicyHandler> navigation_policy_handler_;
  337. fuchsia::web::FrameMediaSettings media_settings_;
  338. // Stored settings for web contents in the current Frame.
  339. fuchsia::web::ContentAreaSettings content_area_settings_;
  340. // Used for receiving and dispatching popup created by this Frame.
  341. fuchsia::web::PopupFrameCreationListenerPtr popup_listener_;
  342. std::list<PendingPopup> pending_popups_;
  343. bool popup_ack_outstanding_ = false;
  344. gfx::Size render_size_override_;
  345. std::unique_ptr<MediaPlayerImpl> media_player_;
  346. std::unique_ptr<ReceiverSessionClient> receiver_session_client_;
  347. on_load_script_injector::OnLoadScriptInjectorHost<uint64_t> script_injector_;
  348. fidl::Binding<fuchsia::web::Frame> binding_;
  349. media_control::MediaBlocker media_blocker_;
  350. ThemeManager theme_manager_;
  351. // The error page to be displayed when a navigation to an explicit site is
  352. // filtered. Explicit sites are filtered if it has a value. If set to the
  353. // empty string, the default error page will be displayed.
  354. absl::optional<std::string> explicit_sites_filter_error_page_;
  355. // Used to publish Frame details to Inspect.
  356. inspect::Node inspect_node_;
  357. const inspect::StringProperty inspect_name_property_;
  358. // TODO(crbug.com/1291330): Remove.
  359. // Used to control which accessibility bridge version is live.
  360. bool use_v2_accessibility_bridge_ = true;
  361. base::WeakPtrFactory<FrameImpl> weak_factory_{this};
  362. };
  363. #endif // FUCHSIA_WEB_WEBENGINE_BROWSER_FRAME_IMPL_H_