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