Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Side by Side Diff: extensions/browser/guest_view/guest_view_base.cc

Issue 847893002: Implemented explicit resizing from guestview. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 GuestViewManager::FromBrowserContext(browser_context_)-> 211 GuestViewManager::FromBrowserContext(browser_context_)->
209 AddGuest(guest_instance_id_, guest_web_contents); 212 AddGuest(guest_instance_id_, guest_web_contents);
210 213
211 // 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.
212 ui_zoom::ZoomController::CreateForWebContents(guest_web_contents); 215 ui_zoom::ZoomController::CreateForWebContents(guest_web_contents);
213 216
214 // Give the derived class an opportunity to perform additional initialization. 217 // Give the derived class an opportunity to perform additional initialization.
215 DidInitialize(); 218 DidInitialize();
216 } 219 }
217 220
218 void GuestViewBase::SetAutoSize(bool enabled, 221 void GuestViewBase::SetSize(const SetSizeParams& params) {
219 const gfx::Size& min_size, 222 bool enable_auto_size =
220 const gfx::Size& max_size) { 223 params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_;
224 gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_;
225 gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_;
226
227 if (params.normal_size)
228 normal_size_ = *params.normal_size;
229
221 min_auto_size_ = min_size; 230 min_auto_size_ = min_size;
222 min_auto_size_.SetToMin(max_size); 231 min_auto_size_.SetToMin(max_size);
223 max_auto_size_ = max_size; 232 max_auto_size_ = max_size;
224 max_auto_size_.SetToMax(min_size); 233 max_auto_size_.SetToMax(min_size);
225 234
226 enabled &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty() && 235 enable_auto_size &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty() &&
227 IsAutoSizeSupported(); 236 IsAutoSizeSupported();
228 if (!enabled && !auto_size_enabled_)
229 return;
230
231 auto_size_enabled_ = enabled;
232
233 if (!attached())
234 return;
235 237
236 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); 238 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
237 if (auto_size_enabled_) { 239 if (enable_auto_size) {
240 // Autosize is being enabled.
238 rvh->EnableAutoResize(min_auto_size_, max_auto_size_); 241 rvh->EnableAutoResize(min_auto_size_, max_auto_size_);
242 normal_size_.SetSize(0, 0);
239 } else { 243 } else {
240 rvh->DisableAutoResize(element_size_); 244 // Autosize is being disabled.
241 guest_size_ = element_size_; 245 // Use default width/height if missing from partially defined normal size.
242 GuestSizeChangedDueToAutoSize(guest_size_, element_size_); 246 if (normal_size_.width() && !normal_size_.height())
247 normal_size_.set_height(guestview::kDefaultHeight);
248 if (!normal_size_.width() && normal_size_.height())
249 normal_size_.set_width(guestview::kDefaultWidth);
250
251 gfx::Size new_size = !normal_size_.IsEmpty() ? normal_size_ :
252 !guest_size_.IsEmpty() ? guest_size_ :
253 gfx::Size(guestview::kDefaultWidth, guestview::kDefaultHeight);
254 if (auto_size_enabled_) {
255 // Autosize was previously enabled.
256 rvh->DisableAutoResize(new_size);
257 GuestSizeChangedDueToAutoSize(guest_size_, new_size);
258 } else {
259 // Autosize was already disabled.
260 guest_sizer_->SizeContents(new_size);
261 }
262 // TODO (paulmeyer): Launch the onResize event here.
263 guest_size_ = new_size;
243 } 264 }
265
266 auto_size_enabled_ = enable_auto_size;
244 } 267 }
245 268
246 // static 269 // static
247 void GuestViewBase::RegisterGuestViewType( 270 void GuestViewBase::RegisterGuestViewType(
248 const std::string& view_type, 271 const std::string& view_type,
249 const GuestCreationCallback& callback) { 272 const GuestCreationCallback& callback) {
250 GuestViewCreationMap::iterator it = 273 GuestViewCreationMap::iterator it =
251 guest_view_registry.Get().find(view_type); 274 guest_view_registry.Get().find(view_type);
252 DCHECK(it == guest_view_registry.Get().end()); 275 DCHECK(it == guest_view_registry.Get().end());
253 guest_view_registry.Get()[view_type] = callback; 276 guest_view_registry.Get()[view_type] = callback;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 return false; 335 return false;
313 } 336 }
314 337
315 bool GuestViewBase::ZoomPropagatesFromEmbedderToGuest() const { 338 bool GuestViewBase::ZoomPropagatesFromEmbedderToGuest() const {
316 return true; 339 return true;
317 } 340 }
318 341
319 void GuestViewBase::DidAttach(int guest_proxy_routing_id) { 342 void GuestViewBase::DidAttach(int guest_proxy_routing_id) {
320 opener_lifetime_observer_.reset(); 343 opener_lifetime_observer_.reset();
321 344
322 SetUpAutoSize(); 345 SetUpSizing();
323 346
324 // Give the derived class an opportunity to perform some actions. 347 // Give the derived class an opportunity to perform some actions.
325 DidAttachToEmbedder(); 348 DidAttachToEmbedder();
326 349
327 // Inform the associated GuestViewContainer that the contentWindow is ready. 350 // Inform the associated GuestViewContainer that the contentWindow is ready.
328 embedder_web_contents()->Send(new ExtensionMsg_GuestAttached( 351 embedder_web_contents()->Send(new ExtensionMsg_GuestAttached(
329 element_instance_id_, 352 element_instance_id_,
330 guest_proxy_routing_id)); 353 guest_proxy_routing_id));
331 354
332 SendQueuedEvents(); 355 SendQueuedEvents();
333 } 356 }
334 357
335 void GuestViewBase::DidDetach() { 358 void GuestViewBase::DidDetach() {
336 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest( 359 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest(
337 this, element_instance_id_); 360 this, element_instance_id_);
338 StopTrackingEmbedderZoomLevel(); 361 StopTrackingEmbedderZoomLevel();
339 owner_web_contents()->Send(new ExtensionMsg_GuestDetached( 362 owner_web_contents()->Send(new ExtensionMsg_GuestDetached(
340 element_instance_id_)); 363 element_instance_id_));
341 element_instance_id_ = guestview::kInstanceIDNone; 364 element_instance_id_ = guestview::kInstanceIDNone;
342 } 365 }
343 366
344 void GuestViewBase::ElementSizeChanged(const gfx::Size& size) { 367 void GuestViewBase::ElementSizeChanged(const gfx::Size& size) {
345 element_size_ = size; 368 // Only resize if needed.
369 if (size.IsEmpty())
370 return;
346 371
347 // Only resize if needed. 372 guest_sizer_->SizeContents(size);
348 if (!size.IsEmpty())
349 guest_sizer_->SizeContents(size);
350 } 373 }
351 374
352 WebContents* GuestViewBase::GetOwnerWebContents() const { 375 WebContents* GuestViewBase::GetOwnerWebContents() const {
353 return owner_web_contents_; 376 return owner_web_contents_;
354 } 377 }
355 378
356 void GuestViewBase::GuestSizeChanged(const gfx::Size& old_size, 379 void GuestViewBase::GuestSizeChanged(const gfx::Size& old_size,
357 const gfx::Size& new_size) { 380 const gfx::Size& new_size) {
358 if (!auto_size_enabled_) 381 if (!auto_size_enabled_)
359 return; 382 return;
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 // The derived class did not create a WebContents so this class serves no 600 // The derived class did not create a WebContents so this class serves no
578 // purpose. Let's self-destruct. 601 // purpose. Let's self-destruct.
579 delete this; 602 delete this;
580 callback.Run(NULL); 603 callback.Run(NULL);
581 return; 604 return;
582 } 605 }
583 InitWithWebContents(guest_web_contents); 606 InitWithWebContents(guest_web_contents);
584 callback.Run(guest_web_contents); 607 callback.Run(guest_web_contents);
585 } 608 }
586 609
587 void GuestViewBase::SetUpAutoSize() { 610 void GuestViewBase::SetUpSizing() {
588 // Read the autosize parameters passed in from the embedder. 611 // Read the autosize parameters passed in from the embedder.
589 bool auto_size_enabled = false; 612 bool auto_size_enabled = false;
590 attach_params()->GetBoolean(guestview::kAttributeAutoSize, 613 attach_params()->GetBoolean(guestview::kAttributeAutoSize,
591 &auto_size_enabled); 614 &auto_size_enabled);
592 615
593 int max_height = 0; 616 int max_height = 0;
594 int max_width = 0; 617 int max_width = 0;
595 attach_params()->GetInteger(guestview::kAttributeMaxHeight, &max_height); 618 attach_params()->GetInteger(guestview::kAttributeMaxHeight, &max_height);
596 attach_params()->GetInteger(guestview::kAttributeMaxWidth, &max_width); 619 attach_params()->GetInteger(guestview::kAttributeMaxWidth, &max_width);
597 620
598 int min_height = 0; 621 int min_height = 0;
599 int min_width = 0; 622 int min_width = 0;
600 attach_params()->GetInteger(guestview::kAttributeMinHeight, &min_height); 623 attach_params()->GetInteger(guestview::kAttributeMinHeight, &min_height);
601 attach_params()->GetInteger(guestview::kAttributeMinWidth, &min_width); 624 attach_params()->GetInteger(guestview::kAttributeMinWidth, &min_width);
602 625
603 // Call SetAutoSize to apply all the appropriate validation and clipping of 626 // Set the normal size to the element size so that the guestview will fit the
627 // element initially if autosize is disabled.
628 int normal_height = 0;
629 int normal_width = 0;
630 attach_params()->GetInteger(guestview::kElementHeight, &normal_height);
631 attach_params()->GetInteger(guestview::kElementWidth, &normal_width);
632
633 SetSizeParams set_size_params;
634 set_size_params.enable_auto_size.reset(new bool(auto_size_enabled));
635 set_size_params.min_size.reset(new gfx::Size(min_width, min_height));
636 set_size_params.max_size.reset(new gfx::Size(max_width, max_height));
637 set_size_params.normal_size.reset(new gfx::Size(normal_width, normal_height));
638
639 // Call SetSize to apply all the appropriate validation and clipping of
604 // values. 640 // values.
605 SetAutoSize(auto_size_enabled, gfx::Size(min_width, min_height), 641 SetSize(set_size_params);
606 gfx::Size(max_width, max_height));
607 } 642 }
608 643
609 void GuestViewBase::StartTrackingEmbedderZoomLevel() { 644 void GuestViewBase::StartTrackingEmbedderZoomLevel() {
610 if (!ZoomPropagatesFromEmbedderToGuest()) 645 if (!ZoomPropagatesFromEmbedderToGuest())
611 return; 646 return;
612 647
613 ui_zoom::ZoomController* zoom_controller = 648 ui_zoom::ZoomController* zoom_controller =
614 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); 649 ui_zoom::ZoomController::FromWebContents(owner_web_contents());
615 // Chrome Apps do not have a ZoomController. 650 // Chrome Apps do not have a ZoomController.
616 if (!zoom_controller) 651 if (!zoom_controller)
(...skipping 19 matching lines...) Expand all
636 // static 671 // static
637 void GuestViewBase::RegisterGuestViewTypes() { 672 void GuestViewBase::RegisterGuestViewTypes() {
638 AppViewGuest::Register(); 673 AppViewGuest::Register();
639 ExtensionOptionsGuest::Register(); 674 ExtensionOptionsGuest::Register();
640 MimeHandlerViewGuest::Register(); 675 MimeHandlerViewGuest::Register();
641 SurfaceWorkerGuest::Register(); 676 SurfaceWorkerGuest::Register();
642 WebViewGuest::Register(); 677 WebViewGuest::Register();
643 } 678 }
644 679
645 } // namespace extensions 680 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698