web_view_guest_delegate.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/web_view_guest_delegate.h"
  5. #include "atom/browser/api/atom_api_web_contents.h"
  6. #include "atom/common/native_mate_converters/gurl_converter.h"
  7. #include "content/browser/web_contents/web_contents_impl.h"
  8. #include "content/public/browser/guest_host.h"
  9. #include "content/public/browser/navigation_handle.h"
  10. #include "content/public/browser/render_frame_host.h"
  11. #include "content/public/browser/render_view_host.h"
  12. #include "content/public/browser/render_widget_host.h"
  13. #include "content/public/browser/render_widget_host_view.h"
  14. namespace atom {
  15. namespace {
  16. const int kDefaultWidth = 300;
  17. const int kDefaultHeight = 300;
  18. } // namespace
  19. SetSizeParams::SetSizeParams() = default;
  20. SetSizeParams::~SetSizeParams() = default;
  21. WebViewGuestDelegate::WebViewGuestDelegate() {}
  22. WebViewGuestDelegate::~WebViewGuestDelegate() {}
  23. void WebViewGuestDelegate::Initialize(api::WebContents* api_web_contents) {
  24. api_web_contents_ = api_web_contents;
  25. Observe(api_web_contents->GetWebContents());
  26. }
  27. void WebViewGuestDelegate::Destroy() {
  28. // Give the content module an opportunity to perform some cleanup.
  29. ResetZoomController();
  30. guest_host_->WillDestroy();
  31. guest_host_ = nullptr;
  32. }
  33. void WebViewGuestDelegate::SetSize(const SetSizeParams& params) {
  34. bool enable_auto_size =
  35. params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_;
  36. gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_;
  37. gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_;
  38. if (params.normal_size)
  39. normal_size_ = *params.normal_size;
  40. min_auto_size_ = min_size;
  41. min_auto_size_.SetToMin(max_size);
  42. max_auto_size_ = max_size;
  43. max_auto_size_.SetToMax(min_size);
  44. enable_auto_size &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty();
  45. auto* rvh = web_contents()->GetRenderViewHost();
  46. if (enable_auto_size) {
  47. // Autosize is being enabled.
  48. rvh->EnableAutoResize(min_auto_size_, max_auto_size_);
  49. normal_size_.SetSize(0, 0);
  50. } else {
  51. // Autosize is being disabled.
  52. // Use default width/height if missing from partially defined normal size.
  53. if (normal_size_.width() && !normal_size_.height())
  54. normal_size_.set_height(GetDefaultSize().height());
  55. if (!normal_size_.width() && normal_size_.height())
  56. normal_size_.set_width(GetDefaultSize().width());
  57. gfx::Size new_size;
  58. if (!normal_size_.IsEmpty()) {
  59. new_size = normal_size_;
  60. } else if (!guest_size_.IsEmpty()) {
  61. new_size = guest_size_;
  62. } else {
  63. new_size = GetDefaultSize();
  64. }
  65. if (auto_size_enabled_) {
  66. // Autosize was previously enabled.
  67. rvh->DisableAutoResize(new_size);
  68. GuestSizeChangedDueToAutoSize(guest_size_, new_size);
  69. } else {
  70. // Autosize was already disabled.
  71. guest_host_->SizeContents(new_size);
  72. }
  73. guest_size_ = new_size;
  74. }
  75. auto_size_enabled_ = enable_auto_size;
  76. }
  77. void WebViewGuestDelegate::DidFinishNavigation(
  78. content::NavigationHandle* navigation_handle) {
  79. if (navigation_handle->HasCommitted() && !navigation_handle->IsErrorPage()) {
  80. auto is_main_frame = navigation_handle->IsInMainFrame();
  81. auto url = navigation_handle->GetURL();
  82. api_web_contents_->Emit("load-commit", url, is_main_frame);
  83. }
  84. }
  85. void WebViewGuestDelegate::DidDetach() {
  86. attached_ = false;
  87. ResetZoomController();
  88. }
  89. void WebViewGuestDelegate::DidAttach(int guest_proxy_routing_id) {
  90. attached_ = true;
  91. api_web_contents_->Emit("did-attach");
  92. ResetZoomController();
  93. embedder_zoom_controller_ =
  94. WebContentsZoomController::FromWebContents(embedder_web_contents_);
  95. auto* zoom_controller = api_web_contents_->GetZoomController();
  96. embedder_zoom_controller_->AddObserver(this);
  97. zoom_controller->SetEmbedderZoomController(embedder_zoom_controller_);
  98. }
  99. content::WebContents* WebViewGuestDelegate::GetOwnerWebContents() const {
  100. return embedder_web_contents_;
  101. }
  102. void WebViewGuestDelegate::GuestSizeChanged(const gfx::Size& new_size) {
  103. if (!auto_size_enabled_)
  104. return;
  105. GuestSizeChangedDueToAutoSize(guest_size_, new_size);
  106. guest_size_ = new_size;
  107. }
  108. void WebViewGuestDelegate::SetGuestHost(content::GuestHost* guest_host) {
  109. guest_host_ = guest_host;
  110. }
  111. void WebViewGuestDelegate::WillAttach(
  112. content::WebContents* embedder_web_contents,
  113. int element_instance_id,
  114. bool is_full_page_plugin,
  115. const base::Closure& completion_callback) {
  116. embedder_web_contents_ = embedder_web_contents;
  117. is_full_page_plugin_ = is_full_page_plugin;
  118. completion_callback.Run();
  119. }
  120. void WebViewGuestDelegate::OnZoomLevelChanged(
  121. content::WebContents* web_contents,
  122. double level,
  123. bool is_temporary) {
  124. if (web_contents == GetOwnerWebContents()) {
  125. if (is_temporary) {
  126. api_web_contents_->GetZoomController()->SetTemporaryZoomLevel(level);
  127. } else {
  128. api_web_contents_->GetZoomController()->SetZoomLevel(level);
  129. }
  130. // Change the default zoom factor to match the embedders' new zoom level.
  131. double zoom_factor = content::ZoomLevelToZoomFactor(level);
  132. api_web_contents_->GetZoomController()->SetDefaultZoomFactor(zoom_factor);
  133. }
  134. }
  135. void WebViewGuestDelegate::GuestSizeChangedDueToAutoSize(
  136. const gfx::Size& old_size,
  137. const gfx::Size& new_size) {
  138. api_web_contents_->Emit("size-changed", old_size.width(), old_size.height(),
  139. new_size.width(), new_size.height());
  140. }
  141. gfx::Size WebViewGuestDelegate::GetDefaultSize() const {
  142. if (is_full_page_plugin_) {
  143. // Full page plugins default to the size of the owner's viewport.
  144. return embedder_web_contents_->GetRenderWidgetHostView()
  145. ->GetVisibleViewportSize();
  146. } else {
  147. return gfx::Size(kDefaultWidth, kDefaultHeight);
  148. }
  149. }
  150. void WebViewGuestDelegate::ResetZoomController() {
  151. if (embedder_zoom_controller_) {
  152. embedder_zoom_controller_->RemoveObserver(this);
  153. embedder_zoom_controller_ = nullptr;
  154. }
  155. }
  156. bool WebViewGuestDelegate::CanBeEmbeddedInsideCrossProcessFrames() {
  157. return true;
  158. }
  159. content::RenderWidgetHost* WebViewGuestDelegate::GetOwnerRenderWidgetHost() {
  160. return embedder_web_contents_->GetRenderViewHost()->GetWidget();
  161. }
  162. content::SiteInstance* WebViewGuestDelegate::GetOwnerSiteInstance() {
  163. return embedder_web_contents_->GetSiteInstance();
  164. }
  165. content::WebContents* WebViewGuestDelegate::CreateNewGuestWindow(
  166. const content::WebContents::CreateParams& create_params) {
  167. // Code below mirrors what content::WebContentsImpl::CreateNewWindow
  168. // does for non-guest sources
  169. content::WebContents::CreateParams guest_params(create_params);
  170. guest_params.initial_size =
  171. embedder_web_contents_->GetContainerBounds().size();
  172. guest_params.context = embedder_web_contents_->GetNativeView();
  173. auto* guest_contents = content::WebContents::Create(guest_params);
  174. auto* guest_contents_impl =
  175. static_cast<content::WebContentsImpl*>(guest_contents);
  176. guest_contents_impl->GetView()->CreateViewForWidget(
  177. guest_contents->GetRenderViewHost()->GetWidget(), false);
  178. return guest_contents;
  179. }
  180. } // namespace atom