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::DispatchOnResizeEvent(const gfx::Size& old_size, | 231 void GuestViewBase::DispatchOnResizeEvent(const gfx::Size& old_size, |
229 const gfx::Size& new_size) { | 232 const gfx::Size& new_size) { |
230 if (new_size == old_size) | 233 if (new_size == old_size) |
231 return; | 234 return; |
232 | 235 |
233 // Dispatch the onResize event. | 236 // Dispatch the onResize event. |
234 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | 237 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); |
235 args->SetInteger(guestview::kOldWidth, old_size.width()); | 238 args->SetInteger(guestview::kOldWidth, old_size.width()); |
236 args->SetInteger(guestview::kOldHeight, old_size.height()); | 239 args->SetInteger(guestview::kOldHeight, old_size.height()); |
237 args->SetInteger(guestview::kNewWidth, new_size.width()); | 240 args->SetInteger(guestview::kNewWidth, new_size.width()); |
238 args->SetInteger(guestview::kNewHeight, new_size.height()); | 241 args->SetInteger(guestview::kNewHeight, new_size.height()); |
239 DispatchEventToGuestProxy(new Event(guestview::kEventResize, args.Pass())); | 242 DispatchEventToGuestProxy(new Event(guestview::kEventResize, args.Pass())); |
240 } | 243 } |
241 | 244 |
242 void GuestViewBase::SetAutoSize(bool enabled, | 245 void GuestViewBase::SetSize(const SetSizeParams& params) { |
243 const gfx::Size& min_size, | 246 bool enable_auto_size = |
244 const gfx::Size& max_size) { | 247 params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_; |
248 gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_; | |
249 gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_; | |
250 | |
251 if (params.normal_size) | |
252 normal_size_ = *params.normal_size; | |
253 | |
245 min_auto_size_ = min_size; | 254 min_auto_size_ = min_size; |
246 min_auto_size_.SetToMin(max_size); | 255 min_auto_size_.SetToMin(max_size); |
247 max_auto_size_ = max_size; | 256 max_auto_size_ = max_size; |
248 max_auto_size_.SetToMax(min_size); | 257 max_auto_size_.SetToMax(min_size); |
249 | 258 |
250 enabled &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty() && | 259 enable_auto_size &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty() && |
251 IsAutoSizeSupported(); | 260 IsAutoSizeSupported(); |
252 if (!enabled && !auto_size_enabled_) | |
253 return; | |
254 | |
255 auto_size_enabled_ = enabled; | |
256 | |
257 if (!attached() && !CanRunInDetachedState()) | |
258 return; | |
259 | 261 |
260 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | 262 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
261 if (auto_size_enabled_) { | 263 if (enable_auto_size) { |
264 // Autosize is being enabled. | |
262 rvh->EnableAutoResize(min_auto_size_, max_auto_size_); | 265 rvh->EnableAutoResize(min_auto_size_, max_auto_size_); |
266 normal_size_.SetSize(0, 0); | |
263 } else { | 267 } else { |
264 rvh->DisableAutoResize(element_size_); | 268 // Autosize is being disabled. |
265 DispatchOnResizeEvent(guest_size_, element_size_); | 269 // Use default width/height if missing from partially defined normal size. |
266 GuestSizeChangedDueToAutoSize(guest_size_, element_size_); | 270 if (normal_size_.width() && !normal_size_.height()) |
267 guest_size_ = element_size_; | 271 normal_size_.set_height(guestview::kDefaultHeight); |
272 if (!normal_size_.width() && normal_size_.height()) | |
273 normal_size_.set_width(guestview::kDefaultWidth); | |
274 | |
275 gfx::Size new_size = !normal_size_.IsEmpty() ? normal_size_ : | |
Fady Samuel
2015/01/20 01:13:19
nit: Can we please break this up a bit? It's a bit
| |
276 !guest_size_.IsEmpty() ? guest_size_ : | |
277 gfx::Size(guestview::kDefaultWidth, guestview::kDefaultHeight); | |
278 if (auto_size_enabled_) { | |
279 // Autosize was previously enabled. | |
280 rvh->DisableAutoResize(new_size); | |
281 GuestSizeChangedDueToAutoSize(guest_size_, new_size); | |
282 } else { | |
283 // Autosize was already disabled. | |
284 guest_sizer_->SizeContents(new_size); | |
285 } | |
286 | |
287 guest_size_ = new_size; | |
268 } | 288 } |
289 | |
290 auto_size_enabled_ = enable_auto_size; | |
269 } | 291 } |
270 | 292 |
271 // static | 293 // static |
272 void GuestViewBase::RegisterGuestViewType( | 294 void GuestViewBase::RegisterGuestViewType( |
273 const std::string& view_type, | 295 const std::string& view_type, |
274 const GuestCreationCallback& callback) { | 296 const GuestCreationCallback& callback) { |
275 GuestViewCreationMap::iterator it = | 297 GuestViewCreationMap::iterator it = |
276 guest_view_registry.Get().find(view_type); | 298 guest_view_registry.Get().find(view_type); |
277 DCHECK(it == guest_view_registry.Get().end()); | 299 DCHECK(it == guest_view_registry.Get().end()); |
278 guest_view_registry.Get()[view_type] = callback; | 300 guest_view_registry.Get()[view_type] = callback; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
347 GuestViewManager::FromBrowserContext(browser_context()); | 369 GuestViewManager::FromBrowserContext(browser_context()); |
348 return guest_manager->CreateGuestWithWebContentsParams( | 370 return guest_manager->CreateGuestWithWebContentsParams( |
349 GetViewType(), | 371 GetViewType(), |
350 owner_web_contents(), | 372 owner_web_contents(), |
351 create_params); | 373 create_params); |
352 } | 374 } |
353 | 375 |
354 void GuestViewBase::DidAttach(int guest_proxy_routing_id) { | 376 void GuestViewBase::DidAttach(int guest_proxy_routing_id) { |
355 opener_lifetime_observer_.reset(); | 377 opener_lifetime_observer_.reset(); |
356 | 378 |
357 SetUpAutoSize(*attach_params()); | 379 SetUpSizing(*attach_params()); |
358 | 380 |
359 // Give the derived class an opportunity to perform some actions. | 381 // Give the derived class an opportunity to perform some actions. |
360 DidAttachToEmbedder(); | 382 DidAttachToEmbedder(); |
361 | 383 |
362 // Inform the associated GuestViewContainer that the contentWindow is ready. | 384 // Inform the associated GuestViewContainer that the contentWindow is ready. |
363 embedder_web_contents()->Send(new ExtensionMsg_GuestAttached( | 385 embedder_web_contents()->Send(new ExtensionMsg_GuestAttached( |
364 element_instance_id_, | 386 element_instance_id_, |
365 guest_proxy_routing_id)); | 387 guest_proxy_routing_id)); |
366 | 388 |
367 SendQueuedEvents(); | 389 SendQueuedEvents(); |
368 } | 390 } |
369 | 391 |
370 void GuestViewBase::DidDetach() { | 392 void GuestViewBase::DidDetach() { |
371 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest( | 393 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest( |
372 this, element_instance_id_); | 394 this, element_instance_id_); |
373 StopTrackingEmbedderZoomLevel(); | 395 StopTrackingEmbedderZoomLevel(); |
374 owner_web_contents()->Send(new ExtensionMsg_GuestDetached( | 396 owner_web_contents()->Send(new ExtensionMsg_GuestDetached( |
375 element_instance_id_)); | 397 element_instance_id_)); |
376 element_instance_id_ = guestview::kInstanceIDNone; | 398 element_instance_id_ = guestview::kInstanceIDNone; |
377 } | 399 } |
378 | 400 |
379 void GuestViewBase::ElementSizeChanged(const gfx::Size& size) { | 401 void GuestViewBase::ElementSizeChanged(const gfx::Size& size) { |
380 element_size_ = size; | 402 if (size.IsEmpty()) |
403 return; | |
381 | 404 |
382 // Only resize if needed. | 405 // Only resize if needed. |
383 if (!size.IsEmpty()) { | 406 guest_sizer_->SizeContents(size); |
384 guest_sizer_->SizeContents(size); | 407 guest_size_ = size; |
385 guest_size_ = size; | |
386 } | |
387 } | 408 } |
388 | 409 |
389 WebContents* GuestViewBase::GetOwnerWebContents() const { | 410 WebContents* GuestViewBase::GetOwnerWebContents() const { |
390 return owner_web_contents_; | 411 return owner_web_contents_; |
391 } | 412 } |
392 | 413 |
393 void GuestViewBase::GuestSizeChanged(const gfx::Size& old_size, | 414 void GuestViewBase::GuestSizeChanged(const gfx::Size& old_size, |
394 const gfx::Size& new_size) { | 415 const gfx::Size& new_size) { |
395 DispatchOnResizeEvent(old_size, new_size); | 416 DispatchOnResizeEvent(old_size, new_size); |
417 | |
396 if (!auto_size_enabled_) | 418 if (!auto_size_enabled_) |
397 return; | 419 return; |
398 guest_size_ = new_size; | 420 guest_size_ = new_size; |
399 GuestSizeChangedDueToAutoSize(old_size, new_size); | 421 GuestSizeChangedDueToAutoSize(old_size, new_size); |
400 } | 422 } |
401 | 423 |
402 const GURL& GuestViewBase::GetOwnerSiteURL() const { | 424 const GURL& GuestViewBase::GetOwnerSiteURL() const { |
403 return owner_web_contents()->GetLastCommittedURL(); | 425 return owner_web_contents()->GetLastCommittedURL(); |
404 } | 426 } |
405 | 427 |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
626 // The derived class did not create a WebContents so this class serves no | 648 // The derived class did not create a WebContents so this class serves no |
627 // purpose. Let's self-destruct. | 649 // purpose. Let's self-destruct. |
628 delete this; | 650 delete this; |
629 callback.Run(NULL); | 651 callback.Run(NULL); |
630 return; | 652 return; |
631 } | 653 } |
632 InitWithWebContents(*create_params, guest_web_contents); | 654 InitWithWebContents(*create_params, guest_web_contents); |
633 callback.Run(guest_web_contents); | 655 callback.Run(guest_web_contents); |
634 } | 656 } |
635 | 657 |
636 void GuestViewBase::SetUpAutoSize(const base::DictionaryValue& params) { | 658 void GuestViewBase::SetUpSizing(const base::DictionaryValue& params) { |
637 // Read the autosize parameters passed in from the embedder. | 659 // Read the autosize parameters passed in from the embedder. |
638 bool auto_size_enabled = false; | 660 bool auto_size_enabled = false; |
639 params.GetBoolean(guestview::kAttributeAutoSize, &auto_size_enabled); | 661 params.GetBoolean(guestview::kAttributeAutoSize, &auto_size_enabled); |
640 | 662 |
641 int max_height = 0; | 663 int max_height = 0; |
642 int max_width = 0; | 664 int max_width = 0; |
643 params.GetInteger(guestview::kAttributeMaxHeight, &max_height); | 665 params.GetInteger(guestview::kAttributeMaxHeight, &max_height); |
644 params.GetInteger(guestview::kAttributeMaxWidth, &max_width); | 666 params.GetInteger(guestview::kAttributeMaxWidth, &max_width); |
645 | 667 |
646 int min_height = 0; | 668 int min_height = 0; |
647 int min_width = 0; | 669 int min_width = 0; |
648 params.GetInteger(guestview::kAttributeMinHeight, &min_height); | 670 params.GetInteger(guestview::kAttributeMinHeight, &min_height); |
649 params.GetInteger(guestview::kAttributeMinWidth, &min_width); | 671 params.GetInteger(guestview::kAttributeMinWidth, &min_width); |
650 | 672 |
651 // Call SetAutoSize to apply all the appropriate validation and clipping of | 673 // Set the normal size to the element size so that the guestview will fit the |
674 // element initially if autosize is disabled. | |
675 int normal_height = 0; | |
676 int normal_width = 0; | |
677 params.GetInteger(guestview::kElementHeight, &normal_height); | |
678 params.GetInteger(guestview::kElementWidth, &normal_width); | |
679 | |
680 SetSizeParams set_size_params; | |
681 set_size_params.enable_auto_size.reset(new bool(auto_size_enabled)); | |
682 set_size_params.min_size.reset(new gfx::Size(min_width, min_height)); | |
683 set_size_params.max_size.reset(new gfx::Size(max_width, max_height)); | |
684 set_size_params.normal_size.reset(new gfx::Size(normal_width, normal_height)); | |
685 | |
686 // Call SetSize to apply all the appropriate validation and clipping of | |
652 // values. | 687 // values. |
653 SetAutoSize(auto_size_enabled, | 688 SetSize(set_size_params); |
654 gfx::Size(min_width, min_height), | |
655 gfx::Size(max_width, max_height)); | |
656 } | 689 } |
657 | 690 |
658 void GuestViewBase::StartTrackingEmbedderZoomLevel() { | 691 void GuestViewBase::StartTrackingEmbedderZoomLevel() { |
659 if (!ZoomPropagatesFromEmbedderToGuest()) | 692 if (!ZoomPropagatesFromEmbedderToGuest()) |
660 return; | 693 return; |
661 | 694 |
662 ui_zoom::ZoomController* zoom_controller = | 695 ui_zoom::ZoomController* zoom_controller = |
663 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); | 696 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); |
664 // Chrome Apps do not have a ZoomController. | 697 // Chrome Apps do not have a ZoomController. |
665 if (!zoom_controller) | 698 if (!zoom_controller) |
(...skipping 19 matching lines...) Expand all Loading... | |
685 // static | 718 // static |
686 void GuestViewBase::RegisterGuestViewTypes() { | 719 void GuestViewBase::RegisterGuestViewTypes() { |
687 AppViewGuest::Register(); | 720 AppViewGuest::Register(); |
688 ExtensionOptionsGuest::Register(); | 721 ExtensionOptionsGuest::Register(); |
689 MimeHandlerViewGuest::Register(); | 722 MimeHandlerViewGuest::Register(); |
690 SurfaceWorkerGuest::Register(); | 723 SurfaceWorkerGuest::Register(); |
691 WebViewGuest::Register(); | 724 WebViewGuest::Register(); |
692 } | 725 } |
693 | 726 |
694 } // namespace extensions | 727 } // namespace extensions |
OLD | NEW |