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/page_zoom.h" | 9 #include "components/ui/zoom/page_zoom.h" |
10 #include "components/ui/zoom/zoom_controller.h" | 10 #include "components/ui/zoom/zoom_controller.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 std::map<std::string, GuestViewBase::GuestCreationCallback>; | 46 std::map<std::string, GuestViewBase::GuestCreationCallback>; |
47 static base::LazyInstance<GuestViewCreationMap> guest_view_registry = | 47 static base::LazyInstance<GuestViewCreationMap> guest_view_registry = |
48 LAZY_INSTANCE_INITIALIZER; | 48 LAZY_INSTANCE_INITIALIZER; |
49 | 49 |
50 using WebContentsGuestViewMap = std::map<WebContents*, GuestViewBase*>; | 50 using WebContentsGuestViewMap = std::map<WebContents*, GuestViewBase*>; |
51 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = | 51 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = |
52 LAZY_INSTANCE_INITIALIZER; | 52 LAZY_INSTANCE_INITIALIZER; |
53 | 53 |
54 } // namespace | 54 } // namespace |
55 | 55 |
| 56 SetSizeParams::SetSizeParams() { |
| 57 } |
| 58 SetSizeParams::~SetSizeParams() { |
| 59 } |
| 60 |
56 GuestViewBase::Event::Event(const std::string& name, | 61 GuestViewBase::Event::Event(const std::string& name, |
57 scoped_ptr<base::DictionaryValue> args) | 62 scoped_ptr<base::DictionaryValue> args) |
58 : name_(name), args_(args.Pass()) { | 63 : name_(name), args_(args.Pass()) { |
59 } | 64 } |
60 | 65 |
61 GuestViewBase::Event::~Event() { | 66 GuestViewBase::Event::~Event() { |
62 } | 67 } |
63 | 68 |
64 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() { | 69 scoped_ptr<base::DictionaryValue> GuestViewBase::Event::GetArguments() { |
65 return args_.Pass(); | 70 return args_.Pass(); |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 AddGuest(guest_instance_id_, guest_web_contents); | 217 AddGuest(guest_instance_id_, guest_web_contents); |
213 | 218 |
214 // Create a ZoomController to allow the guest's contents to be zoomed. | 219 // Create a ZoomController to allow the guest's contents to be zoomed. |
215 ui_zoom::ZoomController::CreateForWebContents(guest_web_contents); | 220 ui_zoom::ZoomController::CreateForWebContents(guest_web_contents); |
216 | 221 |
217 // Populate the view instance ID if we have it on creation. | 222 // Populate the view instance ID if we have it on creation. |
218 create_params.GetInteger(guestview::kParameterInstanceId, | 223 create_params.GetInteger(guestview::kParameterInstanceId, |
219 &view_instance_id_); | 224 &view_instance_id_); |
220 | 225 |
221 if (CanRunInDetachedState()) | 226 if (CanRunInDetachedState()) |
222 SetUpAutoSize(create_params); | 227 SetUpSizing(create_params); |
223 | 228 |
224 // Give the derived class an opportunity to perform additional initialization. | 229 // Give the derived class an opportunity to perform additional initialization. |
225 DidInitialize(create_params); | 230 DidInitialize(create_params); |
226 } | 231 } |
227 | 232 |
228 void GuestViewBase::DispatchOnResizeEvent(const gfx::Size& old_size, | 233 void GuestViewBase::DispatchOnResizeEvent(const gfx::Size& old_size, |
229 const gfx::Size& new_size) { | 234 const gfx::Size& new_size) { |
230 if (new_size == old_size) | 235 if (new_size == old_size) |
231 return; | 236 return; |
232 | 237 |
233 // Dispatch the onResize event. | 238 // Dispatch the onResize event. |
234 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 239 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
235 args->SetInteger(guestview::kOldWidth, old_size.width()); | 240 args->SetInteger(guestview::kOldWidth, old_size.width()); |
236 args->SetInteger(guestview::kOldHeight, old_size.height()); | 241 args->SetInteger(guestview::kOldHeight, old_size.height()); |
237 args->SetInteger(guestview::kNewWidth, new_size.width()); | 242 args->SetInteger(guestview::kNewWidth, new_size.width()); |
238 args->SetInteger(guestview::kNewHeight, new_size.height()); | 243 args->SetInteger(guestview::kNewHeight, new_size.height()); |
239 DispatchEventToGuestProxy(new Event(guestview::kEventResize, args.Pass())); | 244 DispatchEventToGuestProxy(new Event(guestview::kEventResize, args.Pass())); |
240 } | 245 } |
241 | 246 |
242 void GuestViewBase::SetAutoSize(bool enabled, | 247 void GuestViewBase::SetSize(const SetSizeParams& params) { |
243 const gfx::Size& min_size, | 248 bool enable_auto_size = |
244 const gfx::Size& max_size) { | 249 params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_; |
| 250 gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_; |
| 251 gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_; |
| 252 |
| 253 if (params.normal_size) |
| 254 normal_size_ = *params.normal_size; |
| 255 |
245 min_auto_size_ = min_size; | 256 min_auto_size_ = min_size; |
246 min_auto_size_.SetToMin(max_size); | 257 min_auto_size_.SetToMin(max_size); |
247 max_auto_size_ = max_size; | 258 max_auto_size_ = max_size; |
248 max_auto_size_.SetToMax(min_size); | 259 max_auto_size_.SetToMax(min_size); |
249 | 260 |
250 enabled &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty() && | 261 enable_auto_size &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty() && |
251 IsAutoSizeSupported(); | 262 IsAutoSizeSupported(); |
252 if (!enabled && !auto_size_enabled_) | |
253 return; | |
254 | |
255 auto_size_enabled_ = enabled; | |
256 | |
257 if (!attached() && !CanRunInDetachedState()) | |
258 return; | |
259 | 263 |
260 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | 264 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
261 if (auto_size_enabled_) { | 265 if (enable_auto_size) { |
| 266 // Autosize is being enabled. |
262 rvh->EnableAutoResize(min_auto_size_, max_auto_size_); | 267 rvh->EnableAutoResize(min_auto_size_, max_auto_size_); |
| 268 normal_size_.SetSize(0, 0); |
263 } else { | 269 } else { |
264 rvh->DisableAutoResize(element_size_); | 270 // Autosize is being disabled. |
265 DispatchOnResizeEvent(guest_size_, element_size_); | 271 // Use default width/height if missing from partially defined normal size. |
266 GuestSizeChangedDueToAutoSize(guest_size_, element_size_); | 272 if (normal_size_.width() && !normal_size_.height()) |
267 guest_size_ = element_size_; | 273 normal_size_.set_height(guestview::kDefaultHeight); |
| 274 if (!normal_size_.width() && normal_size_.height()) |
| 275 normal_size_.set_width(guestview::kDefaultWidth); |
| 276 |
| 277 gfx::Size new_size; |
| 278 if (!normal_size_.IsEmpty()) { |
| 279 new_size = normal_size_; |
| 280 } else if (!guest_size_.IsEmpty()) { |
| 281 new_size = guest_size_; |
| 282 } else { |
| 283 new_size = gfx::Size(guestview::kDefaultWidth, guestview::kDefaultHeight); |
| 284 } |
| 285 |
| 286 if (auto_size_enabled_) { |
| 287 // Autosize was previously enabled. |
| 288 rvh->DisableAutoResize(new_size); |
| 289 GuestSizeChangedDueToAutoSize(guest_size_, new_size); |
| 290 } else { |
| 291 // Autosize was already disabled. |
| 292 guest_sizer_->SizeContents(new_size); |
| 293 } |
| 294 |
| 295 guest_size_ = new_size; |
268 } | 296 } |
| 297 |
| 298 auto_size_enabled_ = enable_auto_size; |
269 } | 299 } |
270 | 300 |
271 // static | 301 // static |
272 void GuestViewBase::RegisterGuestViewType( | 302 void GuestViewBase::RegisterGuestViewType( |
273 const std::string& view_type, | 303 const std::string& view_type, |
274 const GuestCreationCallback& callback) { | 304 const GuestCreationCallback& callback) { |
275 auto it = guest_view_registry.Get().find(view_type); | 305 auto it = guest_view_registry.Get().find(view_type); |
276 DCHECK(it == guest_view_registry.Get().end()); | 306 DCHECK(it == guest_view_registry.Get().end()); |
277 guest_view_registry.Get()[view_type] = callback; | 307 guest_view_registry.Get()[view_type] = callback; |
278 } | 308 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 auto guest_manager = GuestViewManager::FromBrowserContext(browser_context()); | 372 auto guest_manager = GuestViewManager::FromBrowserContext(browser_context()); |
343 return guest_manager->CreateGuestWithWebContentsParams( | 373 return guest_manager->CreateGuestWithWebContentsParams( |
344 GetViewType(), | 374 GetViewType(), |
345 owner_web_contents(), | 375 owner_web_contents(), |
346 create_params); | 376 create_params); |
347 } | 377 } |
348 | 378 |
349 void GuestViewBase::DidAttach(int guest_proxy_routing_id) { | 379 void GuestViewBase::DidAttach(int guest_proxy_routing_id) { |
350 opener_lifetime_observer_.reset(); | 380 opener_lifetime_observer_.reset(); |
351 | 381 |
352 SetUpAutoSize(*attach_params()); | 382 SetUpSizing(*attach_params()); |
353 | 383 |
354 // Give the derived class an opportunity to perform some actions. | 384 // Give the derived class an opportunity to perform some actions. |
355 DidAttachToEmbedder(); | 385 DidAttachToEmbedder(); |
356 | 386 |
357 // Inform the associated GuestViewContainer that the contentWindow is ready. | 387 // Inform the associated GuestViewContainer that the contentWindow is ready. |
358 embedder_web_contents()->Send(new ExtensionMsg_GuestAttached( | 388 embedder_web_contents()->Send(new ExtensionMsg_GuestAttached( |
359 element_instance_id_, | 389 element_instance_id_, |
360 guest_proxy_routing_id)); | 390 guest_proxy_routing_id)); |
361 | 391 |
362 SendQueuedEvents(); | 392 SendQueuedEvents(); |
363 } | 393 } |
364 | 394 |
365 void GuestViewBase::DidDetach() { | 395 void GuestViewBase::DidDetach() { |
366 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest( | 396 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest( |
367 this, element_instance_id_); | 397 this, element_instance_id_); |
368 StopTrackingEmbedderZoomLevel(); | 398 StopTrackingEmbedderZoomLevel(); |
369 owner_web_contents()->Send(new ExtensionMsg_GuestDetached( | 399 owner_web_contents()->Send(new ExtensionMsg_GuestDetached( |
370 element_instance_id_)); | 400 element_instance_id_)); |
371 element_instance_id_ = guestview::kInstanceIDNone; | 401 element_instance_id_ = guestview::kInstanceIDNone; |
372 } | 402 } |
373 | 403 |
374 void GuestViewBase::ElementSizeChanged(const gfx::Size& size) { | 404 void GuestViewBase::ElementSizeChanged(const gfx::Size& size) { |
375 element_size_ = size; | 405 if (size.IsEmpty()) |
| 406 return; |
376 | 407 |
377 // Only resize if needed. | 408 // Only resize if needed. |
378 if (!size.IsEmpty()) { | 409 guest_sizer_->SizeContents(size); |
379 guest_sizer_->SizeContents(size); | 410 guest_size_ = size; |
380 guest_size_ = size; | |
381 } | |
382 } | 411 } |
383 | 412 |
384 WebContents* GuestViewBase::GetOwnerWebContents() const { | 413 WebContents* GuestViewBase::GetOwnerWebContents() const { |
385 return owner_web_contents_; | 414 return owner_web_contents_; |
386 } | 415 } |
387 | 416 |
388 void GuestViewBase::GuestSizeChanged(const gfx::Size& old_size, | 417 void GuestViewBase::GuestSizeChanged(const gfx::Size& old_size, |
389 const gfx::Size& new_size) { | 418 const gfx::Size& new_size) { |
390 DispatchOnResizeEvent(old_size, new_size); | 419 DispatchOnResizeEvent(old_size, new_size); |
| 420 |
391 if (!auto_size_enabled_) | 421 if (!auto_size_enabled_) |
392 return; | 422 return; |
393 guest_size_ = new_size; | 423 guest_size_ = new_size; |
394 GuestSizeChangedDueToAutoSize(old_size, new_size); | 424 GuestSizeChangedDueToAutoSize(old_size, new_size); |
395 } | 425 } |
396 | 426 |
397 const GURL& GuestViewBase::GetOwnerSiteURL() const { | 427 const GURL& GuestViewBase::GetOwnerSiteURL() const { |
398 return owner_web_contents()->GetLastCommittedURL(); | 428 return owner_web_contents()->GetLastCommittedURL(); |
399 } | 429 } |
400 | 430 |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 // The derived class did not create a WebContents so this class serves no | 671 // The derived class did not create a WebContents so this class serves no |
642 // purpose. Let's self-destruct. | 672 // purpose. Let's self-destruct. |
643 delete this; | 673 delete this; |
644 callback.Run(nullptr); | 674 callback.Run(nullptr); |
645 return; | 675 return; |
646 } | 676 } |
647 InitWithWebContents(*create_params, guest_web_contents); | 677 InitWithWebContents(*create_params, guest_web_contents); |
648 callback.Run(guest_web_contents); | 678 callback.Run(guest_web_contents); |
649 } | 679 } |
650 | 680 |
651 void GuestViewBase::SetUpAutoSize(const base::DictionaryValue& params) { | 681 void GuestViewBase::SetUpSizing(const base::DictionaryValue& params) { |
652 // Read the autosize parameters passed in from the embedder. | 682 // Read the autosize parameters passed in from the embedder. |
653 bool auto_size_enabled = false; | 683 bool auto_size_enabled = false; |
654 params.GetBoolean(guestview::kAttributeAutoSize, &auto_size_enabled); | 684 params.GetBoolean(guestview::kAttributeAutoSize, &auto_size_enabled); |
655 | 685 |
656 int max_height = 0; | 686 int max_height = 0; |
657 int max_width = 0; | 687 int max_width = 0; |
658 params.GetInteger(guestview::kAttributeMaxHeight, &max_height); | 688 params.GetInteger(guestview::kAttributeMaxHeight, &max_height); |
659 params.GetInteger(guestview::kAttributeMaxWidth, &max_width); | 689 params.GetInteger(guestview::kAttributeMaxWidth, &max_width); |
660 | 690 |
661 int min_height = 0; | 691 int min_height = 0; |
662 int min_width = 0; | 692 int min_width = 0; |
663 params.GetInteger(guestview::kAttributeMinHeight, &min_height); | 693 params.GetInteger(guestview::kAttributeMinHeight, &min_height); |
664 params.GetInteger(guestview::kAttributeMinWidth, &min_width); | 694 params.GetInteger(guestview::kAttributeMinWidth, &min_width); |
665 | 695 |
666 // Call SetAutoSize to apply all the appropriate validation and clipping of | 696 // Set the normal size to the element size so that the guestview will fit the |
| 697 // element initially if autosize is disabled. |
| 698 int normal_height = 0; |
| 699 int normal_width = 0; |
| 700 params.GetInteger(guestview::kElementHeight, &normal_height); |
| 701 params.GetInteger(guestview::kElementWidth, &normal_width); |
| 702 |
| 703 SetSizeParams set_size_params; |
| 704 set_size_params.enable_auto_size.reset(new bool(auto_size_enabled)); |
| 705 set_size_params.min_size.reset(new gfx::Size(min_width, min_height)); |
| 706 set_size_params.max_size.reset(new gfx::Size(max_width, max_height)); |
| 707 set_size_params.normal_size.reset(new gfx::Size(normal_width, normal_height)); |
| 708 |
| 709 // Call SetSize to apply all the appropriate validation and clipping of |
667 // values. | 710 // values. |
668 SetAutoSize(auto_size_enabled, | 711 SetSize(set_size_params); |
669 gfx::Size(min_width, min_height), | |
670 gfx::Size(max_width, max_height)); | |
671 } | 712 } |
672 | 713 |
673 void GuestViewBase::StartTrackingEmbedderZoomLevel() { | 714 void GuestViewBase::StartTrackingEmbedderZoomLevel() { |
674 if (!ZoomPropagatesFromEmbedderToGuest()) | 715 if (!ZoomPropagatesFromEmbedderToGuest()) |
675 return; | 716 return; |
676 | 717 |
677 auto embedder_zoom_controller = | 718 auto embedder_zoom_controller = |
678 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); | 719 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); |
679 // Chrome Apps do not have a ZoomController. | 720 // Chrome Apps do not have a ZoomController. |
680 if (!embedder_zoom_controller) | 721 if (!embedder_zoom_controller) |
(...skipping 20 matching lines...) Expand all Loading... |
701 // static | 742 // static |
702 void GuestViewBase::RegisterGuestViewTypes() { | 743 void GuestViewBase::RegisterGuestViewTypes() { |
703 AppViewGuest::Register(); | 744 AppViewGuest::Register(); |
704 ExtensionOptionsGuest::Register(); | 745 ExtensionOptionsGuest::Register(); |
705 MimeHandlerViewGuest::Register(); | 746 MimeHandlerViewGuest::Register(); |
706 SurfaceWorkerGuest::Register(); | 747 SurfaceWorkerGuest::Register(); |
707 WebViewGuest::Register(); | 748 WebViewGuest::Register(); |
708 } | 749 } |
709 | 750 |
710 } // namespace extensions | 751 } // namespace extensions |
OLD | NEW |