| 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 "extensions/browser/guest_view/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/ui/zoom/zoom_controller.h" | 9 #include "components/ui/zoom/zoom_controller.h" |
| 10 #include "content/public/browser/navigation_details.h" | 10 #include "content/public/browser/navigation_details.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 GuestViewCreationMap; | 45 GuestViewCreationMap; |
| 46 static base::LazyInstance<GuestViewCreationMap> guest_view_registry = | 46 static base::LazyInstance<GuestViewCreationMap> guest_view_registry = |
| 47 LAZY_INSTANCE_INITIALIZER; | 47 LAZY_INSTANCE_INITIALIZER; |
| 48 | 48 |
| 49 typedef std::map<WebContents*, GuestViewBase*> WebContentsGuestViewMap; | 49 typedef std::map<WebContents*, GuestViewBase*> WebContentsGuestViewMap; |
| 50 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = | 50 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = |
| 51 LAZY_INSTANCE_INITIALIZER; | 51 LAZY_INSTANCE_INITIALIZER; |
| 52 | 52 |
| 53 } // namespace | 53 } // namespace |
| 54 | 54 |
| 55 SetSizeParams::SetSizeParams() {} |
| 56 SetSizeParams::~SetSizeParams() {} |
| 57 |
| 55 GuestViewBase::Event::Event(const std::string& name, | 58 GuestViewBase::Event::Event(const std::string& name, |
| 56 scoped_ptr<base::DictionaryValue> args) | 59 scoped_ptr<base::DictionaryValue> args) |
| 57 : name_(name), args_(args.Pass()) { | 60 : name_(name), args_(args.Pass()) { |
| 58 } | 61 } |
| 59 | 62 |
| 60 GuestViewBase::Event::~Event() { | 63 GuestViewBase::Event::~Event() { |
| 61 } | 64 } |
| 62 | 65 |
| 63 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() { | 66 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() { |
| 64 return args_.Pass(); | 67 return args_.Pass(); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 AddGuest(guest_instance_id_, guest_web_contents); | 215 AddGuest(guest_instance_id_, guest_web_contents); |
| 213 | 216 |
| 214 // Create a ZoomController to allow the guest's contents to be zoomed. | 217 // Create a ZoomController to allow the guest's contents to be zoomed. |
| 215 ui_zoom::ZoomController::CreateForWebContents(guest_web_contents); | 218 ui_zoom::ZoomController::CreateForWebContents(guest_web_contents); |
| 216 | 219 |
| 217 // Populate the view instance ID if we have it on creation. | 220 // Populate the view instance ID if we have it on creation. |
| 218 create_params.GetInteger(guestview::kParameterInstanceId, | 221 create_params.GetInteger(guestview::kParameterInstanceId, |
| 219 &view_instance_id_); | 222 &view_instance_id_); |
| 220 | 223 |
| 221 if (CanRunInDetachedState()) | 224 if (CanRunInDetachedState()) |
| 222 SetUpAutoSize(create_params); | 225 SetUpSizing(create_params); |
| 223 | 226 |
| 224 // Give the derived class an opportunity to perform additional initialization. | 227 // Give the derived class an opportunity to perform additional initialization. |
| 225 DidInitialize(create_params); | 228 DidInitialize(create_params); |
| 226 } | 229 } |
| 227 | 230 |
| 228 void GuestViewBase::SetAutoSize(bool enabled, | 231 void GuestViewBase::SetSize(const SetSizeParams& params) { |
| 229 const gfx::Size& min_size, | 232 bool enable_auto_size = |
| 230 const gfx::Size& max_size) { | 233 params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_; |
| 234 gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_; |
| 235 gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_; |
| 236 |
| 237 if (params.normal_size) |
| 238 normal_size_ = *params.normal_size; |
| 239 |
| 231 min_auto_size_ = min_size; | 240 min_auto_size_ = min_size; |
| 232 min_auto_size_.SetToMin(max_size); | 241 min_auto_size_.SetToMin(max_size); |
| 233 max_auto_size_ = max_size; | 242 max_auto_size_ = max_size; |
| 234 max_auto_size_.SetToMax(min_size); | 243 max_auto_size_.SetToMax(min_size); |
| 235 | 244 |
| 236 enabled &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty() && | 245 enable_auto_size &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty() && |
| 237 IsAutoSizeSupported(); | 246 IsAutoSizeSupported(); |
| 238 if (!enabled && !auto_size_enabled_) | |
| 239 return; | |
| 240 | |
| 241 auto_size_enabled_ = enabled; | |
| 242 | |
| 243 if (!attached() && !CanRunInDetachedState()) | |
| 244 return; | |
| 245 | 247 |
| 246 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | 248 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
| 247 if (auto_size_enabled_) { | 249 if (enable_auto_size) { |
| 250 // Autosize is being enabled. |
| 248 rvh->EnableAutoResize(min_auto_size_, max_auto_size_); | 251 rvh->EnableAutoResize(min_auto_size_, max_auto_size_); |
| 252 normal_size_.SetSize(0, 0); |
| 249 } else { | 253 } else { |
| 250 rvh->DisableAutoResize(element_size_); | 254 // Autosize is being disabled. |
| 251 guest_size_ = element_size_; | 255 // Use default width/height if missing from partially defined normal size. |
| 252 GuestSizeChangedDueToAutoSize(guest_size_, element_size_); | 256 if (normal_size_.width() && !normal_size_.height()) |
| 257 normal_size_.set_height(guestview::kDefaultHeight); |
| 258 if (!normal_size_.width() && normal_size_.height()) |
| 259 normal_size_.set_width(guestview::kDefaultWidth); |
| 260 |
| 261 gfx::Size new_size = !normal_size_.IsEmpty() ? normal_size_ : |
| 262 !guest_size_.IsEmpty() ? guest_size_ : |
| 263 gfx::Size(guestview::kDefaultWidth, guestview::kDefaultHeight); |
| 264 if (auto_size_enabled_) { |
| 265 // Autosize was previously enabled. |
| 266 rvh->DisableAutoResize(new_size); |
| 267 GuestSizeChangedDueToAutoSize(guest_size_, new_size); |
| 268 } else { |
| 269 // Autosize was already disabled. |
| 270 guest_sizer_->SizeContents(new_size); |
| 271 } |
| 272 // TODO (paulmeyer): Launch the onResize event here. |
| 273 guest_size_ = new_size; |
| 253 } | 274 } |
| 275 |
| 276 auto_size_enabled_ = enable_auto_size; |
| 254 } | 277 } |
| 255 | 278 |
| 256 // static | 279 // static |
| 257 void GuestViewBase::RegisterGuestViewType( | 280 void GuestViewBase::RegisterGuestViewType( |
| 258 const std::string& view_type, | 281 const std::string& view_type, |
| 259 const GuestCreationCallback& callback) { | 282 const GuestCreationCallback& callback) { |
| 260 GuestViewCreationMap::iterator it = | 283 GuestViewCreationMap::iterator it = |
| 261 guest_view_registry.Get().find(view_type); | 284 guest_view_registry.Get().find(view_type); |
| 262 DCHECK(it == guest_view_registry.Get().end()); | 285 DCHECK(it == guest_view_registry.Get().end()); |
| 263 guest_view_registry.Get()[view_type] = callback; | 286 guest_view_registry.Get()[view_type] = callback; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 return false; | 345 return false; |
| 323 } | 346 } |
| 324 | 347 |
| 325 bool GuestViewBase::ZoomPropagatesFromEmbedderToGuest() const { | 348 bool GuestViewBase::ZoomPropagatesFromEmbedderToGuest() const { |
| 326 return true; | 349 return true; |
| 327 } | 350 } |
| 328 | 351 |
| 329 void GuestViewBase::DidAttach(int guest_proxy_routing_id) { | 352 void GuestViewBase::DidAttach(int guest_proxy_routing_id) { |
| 330 opener_lifetime_observer_.reset(); | 353 opener_lifetime_observer_.reset(); |
| 331 | 354 |
| 332 SetUpAutoSize(*attach_params()); | 355 SetUpSizing(*attach_params()); |
| 333 | 356 |
| 334 // Give the derived class an opportunity to perform some actions. | 357 // Give the derived class an opportunity to perform some actions. |
| 335 DidAttachToEmbedder(); | 358 DidAttachToEmbedder(); |
| 336 | 359 |
| 337 // Inform the associated GuestViewContainer that the contentWindow is ready. | 360 // Inform the associated GuestViewContainer that the contentWindow is ready. |
| 338 embedder_web_contents()->Send(new ExtensionMsg_GuestAttached( | 361 embedder_web_contents()->Send(new ExtensionMsg_GuestAttached( |
| 339 element_instance_id_, | 362 element_instance_id_, |
| 340 guest_proxy_routing_id)); | 363 guest_proxy_routing_id)); |
| 341 | 364 |
| 342 SendQueuedEvents(); | 365 SendQueuedEvents(); |
| 343 } | 366 } |
| 344 | 367 |
| 345 void GuestViewBase::DidDetach() { | 368 void GuestViewBase::DidDetach() { |
| 346 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest( | 369 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest( |
| 347 this, element_instance_id_); | 370 this, element_instance_id_); |
| 348 StopTrackingEmbedderZoomLevel(); | 371 StopTrackingEmbedderZoomLevel(); |
| 349 owner_web_contents()->Send(new ExtensionMsg_GuestDetached( | 372 owner_web_contents()->Send(new ExtensionMsg_GuestDetached( |
| 350 element_instance_id_)); | 373 element_instance_id_)); |
| 351 element_instance_id_ = guestview::kInstanceIDNone; | 374 element_instance_id_ = guestview::kInstanceIDNone; |
| 352 } | 375 } |
| 353 | 376 |
| 354 void GuestViewBase::ElementSizeChanged(const gfx::Size& size) { | 377 void GuestViewBase::ElementSizeChanged(const gfx::Size& size) { |
| 355 element_size_ = size; | 378 // Only resize if needed. |
| 379 if (size.IsEmpty()) |
| 380 return; |
| 356 | 381 |
| 357 // Only resize if needed. | 382 guest_sizer_->SizeContents(size); |
| 358 if (!size.IsEmpty()) | |
| 359 guest_sizer_->SizeContents(size); | |
| 360 } | 383 } |
| 361 | 384 |
| 362 WebContents* GuestViewBase::GetOwnerWebContents() const { | 385 WebContents* GuestViewBase::GetOwnerWebContents() const { |
| 363 return owner_web_contents_; | 386 return owner_web_contents_; |
| 364 } | 387 } |
| 365 | 388 |
| 366 void GuestViewBase::GuestSizeChanged(const gfx::Size& old_size, | 389 void GuestViewBase::GuestSizeChanged(const gfx::Size& old_size, |
| 367 const gfx::Size& new_size) { | 390 const gfx::Size& new_size) { |
| 368 if (!auto_size_enabled_) | 391 if (!auto_size_enabled_) |
| 369 return; | 392 return; |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 590 // The derived class did not create a WebContents so this class serves no | 613 // The derived class did not create a WebContents so this class serves no |
| 591 // purpose. Let's self-destruct. | 614 // purpose. Let's self-destruct. |
| 592 delete this; | 615 delete this; |
| 593 callback.Run(NULL); | 616 callback.Run(NULL); |
| 594 return; | 617 return; |
| 595 } | 618 } |
| 596 InitWithWebContents(*create_params, guest_web_contents); | 619 InitWithWebContents(*create_params, guest_web_contents); |
| 597 callback.Run(guest_web_contents); | 620 callback.Run(guest_web_contents); |
| 598 } | 621 } |
| 599 | 622 |
| 600 void GuestViewBase::SetUpAutoSize(const base::DictionaryValue& params) { | 623 void GuestViewBase::SetUpSizing(const base::DictionaryValue& params) { |
| 601 // Read the autosize parameters passed in from the embedder. | 624 // Read the autosize parameters passed in from the embedder. |
| 602 bool auto_size_enabled = false; | 625 bool auto_size_enabled = false; |
| 603 params.GetBoolean(guestview::kAttributeAutoSize, &auto_size_enabled); | 626 params.GetBoolean(guestview::kAttributeAutoSize, &auto_size_enabled); |
| 604 | 627 |
| 605 int max_height = 0; | 628 int max_height = 0; |
| 606 int max_width = 0; | 629 int max_width = 0; |
| 607 params.GetInteger(guestview::kAttributeMaxHeight, &max_height); | 630 params.GetInteger(guestview::kAttributeMaxHeight, &max_height); |
| 608 params.GetInteger(guestview::kAttributeMaxWidth, &max_width); | 631 params.GetInteger(guestview::kAttributeMaxWidth, &max_width); |
| 609 | 632 |
| 610 int min_height = 0; | 633 int min_height = 0; |
| 611 int min_width = 0; | 634 int min_width = 0; |
| 612 params.GetInteger(guestview::kAttributeMinHeight, &min_height); | 635 params.GetInteger(guestview::kAttributeMinHeight, &min_height); |
| 613 params.GetInteger(guestview::kAttributeMinWidth, &min_width); | 636 params.GetInteger(guestview::kAttributeMinWidth, &min_width); |
| 614 | 637 |
| 615 // Call SetAutoSize to apply all the appropriate validation and clipping of | 638 // Set the normal size to the element size so that the guestview will fit the |
| 639 // element initially if autosize is disabled. |
| 640 int normal_height = 0; |
| 641 int normal_width = 0; |
| 642 params.GetInteger(guestview::kElementHeight, &normal_height); |
| 643 params.GetInteger(guestview::kElementWidth, &normal_width); |
| 644 |
| 645 SetSizeParams set_size_params; |
| 646 set_size_params.enable_auto_size.reset(new bool(auto_size_enabled)); |
| 647 set_size_params.min_size.reset(new gfx::Size(min_width, min_height)); |
| 648 set_size_params.max_size.reset(new gfx::Size(max_width, max_height)); |
| 649 set_size_params.normal_size.reset(new gfx::Size(normal_width, normal_height)); |
| 650 |
| 651 // Call SetSize to apply all the appropriate validation and clipping of |
| 616 // values. | 652 // values. |
| 617 SetAutoSize(auto_size_enabled, | 653 SetSize(set_size_params); |
| 618 gfx::Size(min_width, min_height), | |
| 619 gfx::Size(max_width, max_height)); | |
| 620 } | 654 } |
| 621 | 655 |
| 622 void GuestViewBase::StartTrackingEmbedderZoomLevel() { | 656 void GuestViewBase::StartTrackingEmbedderZoomLevel() { |
| 623 if (!ZoomPropagatesFromEmbedderToGuest()) | 657 if (!ZoomPropagatesFromEmbedderToGuest()) |
| 624 return; | 658 return; |
| 625 | 659 |
| 626 ui_zoom::ZoomController* zoom_controller = | 660 ui_zoom::ZoomController* zoom_controller = |
| 627 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); | 661 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); |
| 628 // Chrome Apps do not have a ZoomController. | 662 // Chrome Apps do not have a ZoomController. |
| 629 if (!zoom_controller) | 663 if (!zoom_controller) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 649 // static | 683 // static |
| 650 void GuestViewBase::RegisterGuestViewTypes() { | 684 void GuestViewBase::RegisterGuestViewTypes() { |
| 651 AppViewGuest::Register(); | 685 AppViewGuest::Register(); |
| 652 ExtensionOptionsGuest::Register(); | 686 ExtensionOptionsGuest::Register(); |
| 653 MimeHandlerViewGuest::Register(); | 687 MimeHandlerViewGuest::Register(); |
| 654 SurfaceWorkerGuest::Register(); | 688 SurfaceWorkerGuest::Register(); |
| 655 WebViewGuest::Register(); | 689 WebViewGuest::Register(); |
| 656 } | 690 } |
| 657 | 691 |
| 658 } // namespace extensions | 692 } // namespace extensions |
| OLD | NEW |