| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/guest_view/guest_view_base.h" | 5 #include "components/guest_view/browser/guest_view_base.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "components/guest_view/browser/guest_view_event.h" |
| 10 #include "components/guest_view/browser/guest_view_manager.h" |
| 11 #include "components/guest_view/common/guest_view_constants.h" |
| 12 #include "components/guest_view/common/guest_view_messages.h" |
| 9 #include "components/ui/zoom/page_zoom.h" | 13 #include "components/ui/zoom/page_zoom.h" |
| 10 #include "components/ui/zoom/zoom_controller.h" | 14 #include "components/ui/zoom/zoom_controller.h" |
| 11 #include "content/public/browser/navigation_details.h" | 15 #include "content/public/browser/navigation_details.h" |
| 12 #include "content/public/browser/render_frame_host.h" | 16 #include "content/public/browser/render_frame_host.h" |
| 13 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
| 14 #include "content/public/browser/render_view_host.h" | 18 #include "content/public/browser/render_view_host.h" |
| 15 #include "content/public/browser/render_widget_host_view.h" | 19 #include "content/public/browser/render_widget_host_view.h" |
| 16 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/common/page_zoom.h" | 21 #include "content/public/common/page_zoom.h" |
| 18 #include "content/public/common/url_constants.h" | 22 #include "content/public/common/url_constants.h" |
| 19 #include "extensions/browser/guest_view/guest_view_event.h" | |
| 20 #include "extensions/browser/guest_view/guest_view_manager.h" | |
| 21 #include "extensions/common/guest_view/guest_view_constants.h" | |
| 22 #include "extensions/common/guest_view/guest_view_messages.h" | |
| 23 #include "third_party/WebKit/public/web/WebInputEvent.h" | 23 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 24 | 24 |
| 25 using content::WebContents; | 25 using content::WebContents; |
| 26 | 26 |
| 27 namespace content { | 27 namespace content { |
| 28 struct FrameNavigateParams; | 28 struct FrameNavigateParams; |
| 29 } | 29 } |
| 30 | 30 |
| 31 namespace extensions { | 31 namespace guest_view { |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 using WebContentsGuestViewMap = std::map<const WebContents*, GuestViewBase*>; | 35 using WebContentsGuestViewMap = std::map<const WebContents*, GuestViewBase*>; |
| 36 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = | 36 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = |
| 37 LAZY_INSTANCE_INITIALIZER; | 37 LAZY_INSTANCE_INITIALIZER; |
| 38 | 38 |
| 39 } // namespace | 39 } // namespace |
| 40 | 40 |
| 41 SetSizeParams::SetSizeParams() { | 41 SetSizeParams::SetSizeParams() { |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 | 141 |
| 142 DISALLOW_COPY_AND_ASSIGN(OpenerLifetimeObserver); | 142 DISALLOW_COPY_AND_ASSIGN(OpenerLifetimeObserver); |
| 143 }; | 143 }; |
| 144 | 144 |
| 145 GuestViewBase::GuestViewBase(content::WebContents* owner_web_contents) | 145 GuestViewBase::GuestViewBase(content::WebContents* owner_web_contents) |
| 146 : owner_web_contents_(owner_web_contents), | 146 : owner_web_contents_(owner_web_contents), |
| 147 browser_context_(owner_web_contents->GetBrowserContext()), | 147 browser_context_(owner_web_contents->GetBrowserContext()), |
| 148 guest_instance_id_( | 148 guest_instance_id_( |
| 149 GuestViewManager::FromBrowserContext(browser_context_)-> | 149 GuestViewManager::FromBrowserContext(browser_context_)-> |
| 150 GetNextInstanceID()), | 150 GetNextInstanceID()), |
| 151 view_instance_id_(guestview::kInstanceIDNone), | 151 view_instance_id_(guest_view::kInstanceIDNone), |
| 152 element_instance_id_(guestview::kInstanceIDNone), | 152 element_instance_id_(guest_view::kInstanceIDNone), |
| 153 initialized_(false), | 153 initialized_(false), |
| 154 is_being_destroyed_(false), | 154 is_being_destroyed_(false), |
| 155 guest_host_(nullptr), | 155 guest_host_(nullptr), |
| 156 auto_size_enabled_(false), | 156 auto_size_enabled_(false), |
| 157 is_full_page_plugin_(false), | 157 is_full_page_plugin_(false), |
| 158 guest_proxy_routing_id_(MSG_ROUTING_NONE), | 158 guest_proxy_routing_id_(MSG_ROUTING_NONE), |
| 159 weak_ptr_factory_(this) { | 159 weak_ptr_factory_(this) { |
| 160 owner_host_ = GuestViewManager::FromBrowserContext(browser_context_)-> | 160 owner_host_ = GuestViewManager::FromBrowserContext(browser_context_)-> |
| 161 IsOwnedByExtension(this) ? | 161 IsOwnedByExtension(this) ? |
| 162 owner_web_contents->GetLastCommittedURL().host() : std::string(); | 162 owner_web_contents->GetLastCommittedURL().host() : std::string(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 new OwnerContentsObserver(this, owner_web_contents_)); | 208 new OwnerContentsObserver(this, owner_web_contents_)); |
| 209 | 209 |
| 210 WebContentsObserver::Observe(guest_web_contents); | 210 WebContentsObserver::Observe(guest_web_contents); |
| 211 guest_web_contents->SetDelegate(this); | 211 guest_web_contents->SetDelegate(this); |
| 212 webcontents_guestview_map.Get().insert( | 212 webcontents_guestview_map.Get().insert( |
| 213 std::make_pair(guest_web_contents, this)); | 213 std::make_pair(guest_web_contents, this)); |
| 214 GuestViewManager::FromBrowserContext(browser_context_)-> | 214 GuestViewManager::FromBrowserContext(browser_context_)-> |
| 215 AddGuest(guest_instance_id_, guest_web_contents); | 215 AddGuest(guest_instance_id_, guest_web_contents); |
| 216 | 216 |
| 217 // Populate the view instance ID if we have it on creation. | 217 // Populate the view instance ID if we have it on creation. |
| 218 create_params.GetInteger(guestview::kParameterInstanceId, | 218 create_params.GetInteger(guest_view::kParameterInstanceId, |
| 219 &view_instance_id_); | 219 &view_instance_id_); |
| 220 | 220 |
| 221 if (CanRunInDetachedState()) | 221 if (CanRunInDetachedState()) |
| 222 SetUpSizing(create_params); | 222 SetUpSizing(create_params); |
| 223 | 223 |
| 224 // Observe guest zoom changes. | 224 // Observe guest zoom changes. |
| 225 auto zoom_controller = | 225 auto zoom_controller = |
| 226 ui_zoom::ZoomController::FromWebContents(web_contents()); | 226 ui_zoom::ZoomController::FromWebContents(web_contents()); |
| 227 zoom_controller->AddObserver(this); | 227 zoom_controller->AddObserver(this); |
| 228 | 228 |
| 229 // Give the derived class an opportunity to perform additional initialization. | 229 // Give the derived class an opportunity to perform additional initialization. |
| 230 DidInitialize(create_params); | 230 DidInitialize(create_params); |
| 231 } | 231 } |
| 232 | 232 |
| 233 void GuestViewBase::LoadURLWithParams( | 233 void GuestViewBase::LoadURLWithParams( |
| 234 const content::NavigationController::LoadURLParams& load_params) { | 234 const content::NavigationController::LoadURLParams& load_params) { |
| 235 int guest_proxy_routing_id = host()->LoadURLWithParams(load_params); | 235 int guest_proxy_routing_id = host()->LoadURLWithParams(load_params); |
| 236 DCHECK(guest_proxy_routing_id_ == MSG_ROUTING_NONE || | 236 DCHECK(guest_proxy_routing_id_ == MSG_ROUTING_NONE || |
| 237 guest_proxy_routing_id == guest_proxy_routing_id_); | 237 guest_proxy_routing_id == guest_proxy_routing_id_); |
| 238 guest_proxy_routing_id_ = guest_proxy_routing_id; | 238 guest_proxy_routing_id_ = guest_proxy_routing_id; |
| 239 } | 239 } |
| 240 | 240 |
| 241 void GuestViewBase::DispatchOnResizeEvent(const gfx::Size& old_size, | 241 void GuestViewBase::DispatchOnResizeEvent(const gfx::Size& old_size, |
| 242 const gfx::Size& new_size) { | 242 const gfx::Size& new_size) { |
| 243 if (new_size == old_size) | 243 if (new_size == old_size) |
| 244 return; | 244 return; |
| 245 | 245 |
| 246 // Dispatch the onResize event. | 246 // Dispatch the onResize event. |
| 247 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 247 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
| 248 args->SetInteger(guestview::kOldWidth, old_size.width()); | 248 args->SetInteger(guest_view::kOldWidth, old_size.width()); |
| 249 args->SetInteger(guestview::kOldHeight, old_size.height()); | 249 args->SetInteger(guest_view::kOldHeight, old_size.height()); |
| 250 args->SetInteger(guestview::kNewWidth, new_size.width()); | 250 args->SetInteger(guest_view::kNewWidth, new_size.width()); |
| 251 args->SetInteger(guestview::kNewHeight, new_size.height()); | 251 args->SetInteger(guest_view::kNewHeight, new_size.height()); |
| 252 DispatchEventToGuestProxy( | 252 DispatchEventToGuestProxy( |
| 253 new GuestViewEvent(guestview::kEventResize, args.Pass())); | 253 new GuestViewEvent(guest_view::kEventResize, args.Pass())); |
| 254 } | 254 } |
| 255 | 255 |
| 256 gfx::Size GuestViewBase::GetDefaultSize() const { | 256 gfx::Size GuestViewBase::GetDefaultSize() const { |
| 257 if (is_full_page_plugin()) { | 257 if (is_full_page_plugin()) { |
| 258 // Full page plugins default to the size of the owner's viewport. | 258 // Full page plugins default to the size of the owner's viewport. |
| 259 return owner_web_contents() | 259 return owner_web_contents() |
| 260 ->GetRenderWidgetHostView() | 260 ->GetRenderWidgetHostView() |
| 261 ->GetVisibleViewportSize(); | 261 ->GetVisibleViewportSize(); |
| 262 } else { | 262 } else { |
| 263 return gfx::Size(guestview::kDefaultWidth, guestview::kDefaultHeight); | 263 return gfx::Size(guest_view::kDefaultWidth, guest_view::kDefaultHeight); |
| 264 } | 264 } |
| 265 } | 265 } |
| 266 | 266 |
| 267 void GuestViewBase::SetSize(const SetSizeParams& params) { | 267 void GuestViewBase::SetSize(const SetSizeParams& params) { |
| 268 bool enable_auto_size = | 268 bool enable_auto_size = |
| 269 params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_; | 269 params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_; |
| 270 gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_; | 270 gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_; |
| 271 gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_; | 271 gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_; |
| 272 | 272 |
| 273 if (params.normal_size) | 273 if (params.normal_size) |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 guest_proxy_routing_id)); | 398 guest_proxy_routing_id)); |
| 399 | 399 |
| 400 SendQueuedEvents(); | 400 SendQueuedEvents(); |
| 401 } | 401 } |
| 402 | 402 |
| 403 void GuestViewBase::DidDetach() { | 403 void GuestViewBase::DidDetach() { |
| 404 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest(this); | 404 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest(this); |
| 405 StopTrackingEmbedderZoomLevel(); | 405 StopTrackingEmbedderZoomLevel(); |
| 406 owner_web_contents()->Send(new GuestViewMsg_GuestDetached( | 406 owner_web_contents()->Send(new GuestViewMsg_GuestDetached( |
| 407 element_instance_id_)); | 407 element_instance_id_)); |
| 408 element_instance_id_ = guestview::kInstanceIDNone; | 408 element_instance_id_ = guest_view::kInstanceIDNone; |
| 409 } | 409 } |
| 410 | 410 |
| 411 WebContents* GuestViewBase::GetOwnerWebContents() const { | 411 WebContents* GuestViewBase::GetOwnerWebContents() const { |
| 412 return owner_web_contents_; | 412 return owner_web_contents_; |
| 413 } | 413 } |
| 414 | 414 |
| 415 void GuestViewBase::GuestSizeChanged(const gfx::Size& new_size) { | 415 void GuestViewBase::GuestSizeChanged(const gfx::Size& new_size) { |
| 416 if (!auto_size_enabled_) | 416 if (!auto_size_enabled_) |
| 417 return; | 417 return; |
| 418 GuestSizeChangedDueToAutoSize(guest_size_, new_size); | 418 GuestSizeChangedDueToAutoSize(guest_size_, new_size); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 webcontents_guestview_map.Get().erase(web_contents()); | 453 webcontents_guestview_map.Get().erase(web_contents()); |
| 454 GuestViewManager::FromBrowserContext(browser_context_)-> | 454 GuestViewManager::FromBrowserContext(browser_context_)-> |
| 455 RemoveGuest(guest_instance_id_); | 455 RemoveGuest(guest_instance_id_); |
| 456 pending_events_.clear(); | 456 pending_events_.clear(); |
| 457 | 457 |
| 458 delete web_contents(); | 458 delete web_contents(); |
| 459 } | 459 } |
| 460 | 460 |
| 461 void GuestViewBase::SetAttachParams(const base::DictionaryValue& params) { | 461 void GuestViewBase::SetAttachParams(const base::DictionaryValue& params) { |
| 462 attach_params_.reset(params.DeepCopy()); | 462 attach_params_.reset(params.DeepCopy()); |
| 463 attach_params_->GetInteger(guestview::kParameterInstanceId, | 463 attach_params_->GetInteger(guest_view::kParameterInstanceId, |
| 464 &view_instance_id_); | 464 &view_instance_id_); |
| 465 } | 465 } |
| 466 | 466 |
| 467 void GuestViewBase::SetOpener(GuestViewBase* guest) { | 467 void GuestViewBase::SetOpener(GuestViewBase* guest) { |
| 468 if (guest && guest->IsViewType(GetViewType())) { | 468 if (guest && guest->IsViewType(GetViewType())) { |
| 469 opener_ = guest->weak_ptr_factory_.GetWeakPtr(); | 469 opener_ = guest->weak_ptr_factory_.GetWeakPtr(); |
| 470 if (!attached()) | 470 if (!attached()) |
| 471 opener_lifetime_observer_.reset(new OpenerLifetimeObserver(this)); | 471 opener_lifetime_observer_.reset(new OpenerLifetimeObserver(this)); |
| 472 return; | 472 return; |
| 473 } | 473 } |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 731 return 1.0; | 731 return 1.0; |
| 732 | 732 |
| 733 return content::ZoomLevelToZoomFactor( | 733 return content::ZoomLevelToZoomFactor( |
| 734 ui_zoom::ZoomController::GetZoomLevelForWebContents( | 734 ui_zoom::ZoomController::GetZoomLevelForWebContents( |
| 735 embedder_web_contents())); | 735 embedder_web_contents())); |
| 736 } | 736 } |
| 737 | 737 |
| 738 void GuestViewBase::SetUpSizing(const base::DictionaryValue& params) { | 738 void GuestViewBase::SetUpSizing(const base::DictionaryValue& params) { |
| 739 // Read the autosize parameters passed in from the embedder. | 739 // Read the autosize parameters passed in from the embedder. |
| 740 bool auto_size_enabled = auto_size_enabled_; | 740 bool auto_size_enabled = auto_size_enabled_; |
| 741 params.GetBoolean(guestview::kAttributeAutoSize, &auto_size_enabled); | 741 params.GetBoolean(guest_view::kAttributeAutoSize, &auto_size_enabled); |
| 742 | 742 |
| 743 int max_height = max_auto_size_.height(); | 743 int max_height = max_auto_size_.height(); |
| 744 int max_width = max_auto_size_.width(); | 744 int max_width = max_auto_size_.width(); |
| 745 params.GetInteger(guestview::kAttributeMaxHeight, &max_height); | 745 params.GetInteger(guest_view::kAttributeMaxHeight, &max_height); |
| 746 params.GetInteger(guestview::kAttributeMaxWidth, &max_width); | 746 params.GetInteger(guest_view::kAttributeMaxWidth, &max_width); |
| 747 | 747 |
| 748 int min_height = min_auto_size_.height(); | 748 int min_height = min_auto_size_.height(); |
| 749 int min_width = min_auto_size_.width(); | 749 int min_width = min_auto_size_.width(); |
| 750 params.GetInteger(guestview::kAttributeMinHeight, &min_height); | 750 params.GetInteger(guest_view::kAttributeMinHeight, &min_height); |
| 751 params.GetInteger(guestview::kAttributeMinWidth, &min_width); | 751 params.GetInteger(guest_view::kAttributeMinWidth, &min_width); |
| 752 | 752 |
| 753 double element_height = 0.0; | 753 double element_height = 0.0; |
| 754 double element_width = 0.0; | 754 double element_width = 0.0; |
| 755 params.GetDouble(guestview::kElementHeight, &element_height); | 755 params.GetDouble(guest_view::kElementHeight, &element_height); |
| 756 params.GetDouble(guestview::kElementWidth, &element_width); | 756 params.GetDouble(guest_view::kElementWidth, &element_width); |
| 757 | 757 |
| 758 // Set the normal size to the element size so that the guestview will fit | 758 // Set the normal size to the element size so that the guestview will fit |
| 759 // the element initially if autosize is disabled. | 759 // the element initially if autosize is disabled. |
| 760 int normal_height = normal_size_.height(); | 760 int normal_height = normal_size_.height(); |
| 761 int normal_width = normal_size_.width(); | 761 int normal_width = normal_size_.width(); |
| 762 // If the element size was provided in logical units (versus physical), then | 762 // If the element size was provided in logical units (versus physical), then |
| 763 // it will be converted to physical units. | 763 // it will be converted to physical units. |
| 764 bool element_size_is_logical = false; | 764 bool element_size_is_logical = false; |
| 765 params.GetBoolean(guestview::kElementSizeIsLogical, &element_size_is_logical); | 765 params.GetBoolean(guest_view::kElementSizeIsLogical, |
| 766 &element_size_is_logical); |
| 766 if (element_size_is_logical) { | 767 if (element_size_is_logical) { |
| 767 // Convert the element size from logical pixels to physical pixels. | 768 // Convert the element size from logical pixels to physical pixels. |
| 768 normal_height = LogicalPixelsToPhysicalPixels(element_height); | 769 normal_height = LogicalPixelsToPhysicalPixels(element_height); |
| 769 normal_width = LogicalPixelsToPhysicalPixels(element_width); | 770 normal_width = LogicalPixelsToPhysicalPixels(element_width); |
| 770 } else { | 771 } else { |
| 771 normal_height = lround(element_height); | 772 normal_height = lround(element_height); |
| 772 normal_width = lround(element_width); | 773 normal_width = lround(element_width); |
| 773 } | 774 } |
| 774 | 775 |
| 775 SetSizeParams set_size_params; | 776 SetSizeParams set_size_params; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 return; | 815 return; |
| 815 | 816 |
| 816 auto embedder_zoom_controller = | 817 auto embedder_zoom_controller = |
| 817 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); | 818 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); |
| 818 // Chrome Apps do not have a ZoomController. | 819 // Chrome Apps do not have a ZoomController. |
| 819 if (!embedder_zoom_controller) | 820 if (!embedder_zoom_controller) |
| 820 return; | 821 return; |
| 821 embedder_zoom_controller->RemoveObserver(this); | 822 embedder_zoom_controller->RemoveObserver(this); |
| 822 } | 823 } |
| 823 | 824 |
| 824 } // namespace extensions | 825 } // namespace guest_view |
| OLD | NEW |