aw_render_view_ext.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) 2012 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. #include "android_webview/renderer/aw_render_view_ext.h"
  5. #include <map>
  6. #include "android_webview/common/mojom/frame.mojom.h"
  7. #include "base/containers/contains.h"
  8. #include "base/no_destructor.h"
  9. #include "content/public/renderer/render_frame.h"
  10. #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
  11. #include "third_party/blink/public/web/web_local_frame.h"
  12. #include "third_party/blink/public/web/web_view.h"
  13. namespace android_webview {
  14. // Registry for blink::WebView => AwRenderViewExt lookups
  15. typedef std::map<blink::WebView*, AwRenderViewExt*> ViewExtMap;
  16. ViewExtMap* GetViewExtMap() {
  17. static base::NoDestructor<ViewExtMap> map;
  18. return map.get();
  19. }
  20. AwRenderViewExt::AwRenderViewExt(blink::WebView* web_view,
  21. bool created_by_renderer)
  22. : blink::WebViewObserver(web_view),
  23. created_by_renderer_(created_by_renderer) {
  24. DCHECK(web_view != nullptr);
  25. DCHECK(!base::Contains(*GetViewExtMap(), web_view));
  26. GetViewExtMap()->emplace(web_view, this);
  27. }
  28. AwRenderViewExt::~AwRenderViewExt() {
  29. // Remove myself from the blink::WebView => AwRenderViewExt register. Ideally,
  30. // we'd just use GetWebView() and erase by key. However, by this time the
  31. // GetWebView has already been cleared so we have to iterate over all
  32. // WebViews in the map and wipe the one(s) that point to this
  33. // AwRenderViewExt
  34. auto* map = GetViewExtMap();
  35. auto it = map->begin();
  36. while (it != map->end()) {
  37. if (it->second == this) {
  38. it = map->erase(it);
  39. } else {
  40. ++it;
  41. }
  42. }
  43. }
  44. // static
  45. void AwRenderViewExt::WebViewCreated(blink::WebView* web_view,
  46. bool created_by_renderer) {
  47. new AwRenderViewExt(web_view,
  48. created_by_renderer); // |web_view| takes ownership.
  49. }
  50. // static
  51. AwRenderViewExt* AwRenderViewExt::FromWebView(blink::WebView* web_view) {
  52. DCHECK(web_view != nullptr);
  53. auto iter = GetViewExtMap()->find(web_view);
  54. DCHECK(GetViewExtMap()->end() != iter)
  55. << "AwRenderViewExt should always exist for a WebView";
  56. AwRenderViewExt* render_view_ext = iter->second;
  57. return render_view_ext;
  58. }
  59. void AwRenderViewExt::DidCommitCompositorFrame() {
  60. UpdateContentsSize();
  61. }
  62. void AwRenderViewExt::DidUpdateMainFrameLayout() {
  63. // The size may have changed.
  64. needs_contents_size_update_ = true;
  65. }
  66. void AwRenderViewExt::OnDestruct() {
  67. delete this;
  68. }
  69. void AwRenderViewExt::UpdateContentsSize() {
  70. blink::WebView* webview = GetWebView();
  71. blink::WebFrame* main_frame = webview->MainFrame();
  72. // Even without out-of-process iframes, we now create RemoteFrames for the
  73. // main frame when you navigate cross-process, to create a placeholder in the
  74. // old process. This is necessary to support things like postMessage across
  75. // windows that have references to each other. The RemoteFrame will
  76. // immediately go away if there aren't any active frames left in the old
  77. // process.
  78. if (!main_frame->IsWebLocalFrame())
  79. return;
  80. if (!needs_contents_size_update_)
  81. return;
  82. needs_contents_size_update_ = false;
  83. gfx::Size contents_size = main_frame->ToWebLocalFrame()->DocumentSize();
  84. // Fall back to contentsPreferredMinimumSize if the mainFrame is reporting a
  85. // 0x0 size (this happens during initial load).
  86. if (contents_size.IsEmpty()) {
  87. contents_size = webview->ContentsPreferredMinimumSize();
  88. }
  89. if (contents_size == last_sent_contents_size_)
  90. return;
  91. last_sent_contents_size_ = contents_size;
  92. mojo::AssociatedRemote<mojom::FrameHost> frame_host_remote;
  93. content::RenderFrame::FromWebFrame(main_frame->ToWebLocalFrame())
  94. ->GetRemoteAssociatedInterfaces()
  95. ->GetInterface(&frame_host_remote);
  96. frame_host_remote->ContentsSizeChanged(contents_size);
  97. }
  98. } // namespace android_webview