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

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

Issue 1102173002: Move GuestView layer in browser to components (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Jochen's comment + GuestViewBase cleanup Created 5 years, 7 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 "components/guest_view/browser/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/guest_view/browser/guest_view_event.h"
10 #include "components/guest_view/browser/guest_view_manager.h"
11 #include "components/guest_view/common/guest_view_constants.h"
12 #include "components/guest_view/common/guest_view_messages.h"
9 #include "components/ui/zoom/page_zoom.h" 13 #include "components/ui/zoom/page_zoom.h"
10 #include "components/ui/zoom/zoom_controller.h" 14 #include "components/ui/zoom/zoom_controller.h"
11 #include "content/public/browser/navigation_details.h" 15 #include "content/public/browser/navigation_details.h"
12 #include "content/public/browser/render_frame_host.h" 16 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/render_process_host.h" 17 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h" 18 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/render_widget_host_view.h" 19 #include "content/public/browser/render_widget_host_view.h"
16 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/page_zoom.h" 21 #include "content/public/common/page_zoom.h"
18 #include "content/public/common/url_constants.h" 22 #include "content/public/common/url_constants.h"
19 #include "extensions/browser/guest_view/guest_view_event.h"
20 #include "extensions/browser/guest_view/guest_view_manager.h"
21 #include "extensions/common/guest_view/guest_view_constants.h"
22 #include "extensions/common/guest_view/guest_view_messages.h"
23 #include "third_party/WebKit/public/web/WebInputEvent.h" 23 #include "third_party/WebKit/public/web/WebInputEvent.h"
24 24
25 using content::WebContents; 25 using content::WebContents;
26 26
27 namespace content { 27 namespace content {
28 struct FrameNavigateParams; 28 struct FrameNavigateParams;
29 } 29 }
30 30
31 namespace extensions { 31 namespace guest_view {
32 32
33 namespace { 33 namespace {
34 34
35 using WebContentsGuestViewMap = std::map<const WebContents*, GuestViewBase*>; 35 using WebContentsGuestViewMap = std::map<const WebContents*, GuestViewBase*>;
36 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map = 36 static base::LazyInstance<WebContentsGuestViewMap> webcontents_guestview_map =
37 LAZY_INSTANCE_INITIALIZER; 37 LAZY_INSTANCE_INITIALIZER;
38 38
39 } // namespace 39 } // namespace
40 40
41 SetSizeParams::SetSizeParams() { 41 SetSizeParams::SetSizeParams() {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 141
142 DISALLOW_COPY_AND_ASSIGN(OpenerLifetimeObserver); 142 DISALLOW_COPY_AND_ASSIGN(OpenerLifetimeObserver);
143 }; 143 };
144 144
145 GuestViewBase::GuestViewBase(content::WebContents* owner_web_contents) 145 GuestViewBase::GuestViewBase(content::WebContents* owner_web_contents)
146 : owner_web_contents_(owner_web_contents), 146 : owner_web_contents_(owner_web_contents),
147 browser_context_(owner_web_contents->GetBrowserContext()), 147 browser_context_(owner_web_contents->GetBrowserContext()),
148 guest_instance_id_( 148 guest_instance_id_(
149 GuestViewManager::FromBrowserContext(browser_context_)-> 149 GuestViewManager::FromBrowserContext(browser_context_)->
150 GetNextInstanceID()), 150 GetNextInstanceID()),
151 view_instance_id_(guestview::kInstanceIDNone), 151 view_instance_id_(kInstanceIDNone),
152 element_instance_id_(guestview::kInstanceIDNone), 152 element_instance_id_(kInstanceIDNone),
153 initialized_(false), 153 initialized_(false),
154 is_being_destroyed_(false), 154 is_being_destroyed_(false),
155 guest_host_(nullptr), 155 guest_host_(nullptr),
156 auto_size_enabled_(false), 156 auto_size_enabled_(false),
157 is_full_page_plugin_(false), 157 is_full_page_plugin_(false),
158 guest_proxy_routing_id_(MSG_ROUTING_NONE), 158 guest_proxy_routing_id_(MSG_ROUTING_NONE),
159 weak_ptr_factory_(this) { 159 weak_ptr_factory_(this) {
160 owner_host_ = GuestViewManager::FromBrowserContext(browser_context_)-> 160 owner_host_ = GuestViewManager::FromBrowserContext(browser_context_)->
161 IsOwnedByExtension(this) ? 161 IsOwnedByExtension(this) ?
162 owner_web_contents->GetLastCommittedURL().host() : std::string(); 162 owner_web_contents->GetLastCommittedURL().host() : std::string();
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 new OwnerContentsObserver(this, owner_web_contents_)); 208 new OwnerContentsObserver(this, owner_web_contents_));
209 209
210 WebContentsObserver::Observe(guest_web_contents); 210 WebContentsObserver::Observe(guest_web_contents);
211 guest_web_contents->SetDelegate(this); 211 guest_web_contents->SetDelegate(this);
212 webcontents_guestview_map.Get().insert( 212 webcontents_guestview_map.Get().insert(
213 std::make_pair(guest_web_contents, this)); 213 std::make_pair(guest_web_contents, this));
214 GuestViewManager::FromBrowserContext(browser_context_)-> 214 GuestViewManager::FromBrowserContext(browser_context_)->
215 AddGuest(guest_instance_id_, guest_web_contents); 215 AddGuest(guest_instance_id_, guest_web_contents);
216 216
217 // Populate the view instance ID if we have it on creation. 217 // Populate the view instance ID if we have it on creation.
218 create_params.GetInteger(guestview::kParameterInstanceId, 218 create_params.GetInteger(kParameterInstanceId, &view_instance_id_);
219 &view_instance_id_);
220 219
221 if (CanRunInDetachedState()) 220 if (CanRunInDetachedState())
222 SetUpSizing(create_params); 221 SetUpSizing(create_params);
223 222
224 // Observe guest zoom changes. 223 // Observe guest zoom changes.
225 auto zoom_controller = 224 auto zoom_controller =
226 ui_zoom::ZoomController::FromWebContents(web_contents()); 225 ui_zoom::ZoomController::FromWebContents(web_contents());
227 zoom_controller->AddObserver(this); 226 zoom_controller->AddObserver(this);
228 227
229 // Give the derived class an opportunity to perform additional initialization. 228 // Give the derived class an opportunity to perform additional initialization.
230 DidInitialize(create_params); 229 DidInitialize(create_params);
231 } 230 }
232 231
233 void GuestViewBase::LoadURLWithParams( 232 void GuestViewBase::LoadURLWithParams(
234 const content::NavigationController::LoadURLParams& load_params) { 233 const content::NavigationController::LoadURLParams& load_params) {
235 int guest_proxy_routing_id = host()->LoadURLWithParams(load_params); 234 int guest_proxy_routing_id = host()->LoadURLWithParams(load_params);
236 DCHECK(guest_proxy_routing_id_ == MSG_ROUTING_NONE || 235 DCHECK(guest_proxy_routing_id_ == MSG_ROUTING_NONE ||
237 guest_proxy_routing_id == guest_proxy_routing_id_); 236 guest_proxy_routing_id == guest_proxy_routing_id_);
238 guest_proxy_routing_id_ = guest_proxy_routing_id; 237 guest_proxy_routing_id_ = guest_proxy_routing_id;
239 } 238 }
240 239
241 void GuestViewBase::DispatchOnResizeEvent(const gfx::Size& old_size, 240 void GuestViewBase::DispatchOnResizeEvent(const gfx::Size& old_size,
242 const gfx::Size& new_size) { 241 const gfx::Size& new_size) {
243 if (new_size == old_size) 242 if (new_size == old_size)
244 return; 243 return;
245 244
246 // Dispatch the onResize event. 245 // Dispatch the onResize event.
247 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); 246 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
248 args->SetInteger(guestview::kOldWidth, old_size.width()); 247 args->SetInteger(kOldWidth, old_size.width());
249 args->SetInteger(guestview::kOldHeight, old_size.height()); 248 args->SetInteger(kOldHeight, old_size.height());
250 args->SetInteger(guestview::kNewWidth, new_size.width()); 249 args->SetInteger(kNewWidth, new_size.width());
251 args->SetInteger(guestview::kNewHeight, new_size.height()); 250 args->SetInteger(kNewHeight, new_size.height());
252 DispatchEventToGuestProxy( 251 DispatchEventToGuestProxy(new GuestViewEvent(kEventResize, args.Pass()));
253 new GuestViewEvent(guestview::kEventResize, args.Pass()));
254 } 252 }
255 253
256 gfx::Size GuestViewBase::GetDefaultSize() const { 254 gfx::Size GuestViewBase::GetDefaultSize() const {
257 if (is_full_page_plugin()) { 255 if (is_full_page_plugin()) {
258 // Full page plugins default to the size of the owner's viewport. 256 // Full page plugins default to the size of the owner's viewport.
259 return owner_web_contents() 257 return owner_web_contents()
260 ->GetRenderWidgetHostView() 258 ->GetRenderWidgetHostView()
261 ->GetVisibleViewportSize(); 259 ->GetVisibleViewportSize();
262 } else { 260 } else {
263 return gfx::Size(guestview::kDefaultWidth, guestview::kDefaultHeight); 261 return gfx::Size(kDefaultWidth, kDefaultHeight);
264 } 262 }
265 } 263 }
266 264
267 void GuestViewBase::SetSize(const SetSizeParams& params) { 265 void GuestViewBase::SetSize(const SetSizeParams& params) {
268 bool enable_auto_size = 266 bool enable_auto_size =
269 params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_; 267 params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_;
270 gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_; 268 gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_;
271 gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_; 269 gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_;
272 270
273 if (params.normal_size) 271 if (params.normal_size)
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 guest_proxy_routing_id)); 396 guest_proxy_routing_id));
399 397
400 SendQueuedEvents(); 398 SendQueuedEvents();
401 } 399 }
402 400
403 void GuestViewBase::DidDetach() { 401 void GuestViewBase::DidDetach() {
404 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest(this); 402 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest(this);
405 StopTrackingEmbedderZoomLevel(); 403 StopTrackingEmbedderZoomLevel();
406 owner_web_contents()->Send(new GuestViewMsg_GuestDetached( 404 owner_web_contents()->Send(new GuestViewMsg_GuestDetached(
407 element_instance_id_)); 405 element_instance_id_));
408 element_instance_id_ = guestview::kInstanceIDNone; 406 element_instance_id_ = kInstanceIDNone;
409 } 407 }
410 408
411 WebContents* GuestViewBase::GetOwnerWebContents() const { 409 WebContents* GuestViewBase::GetOwnerWebContents() const {
412 return owner_web_contents_; 410 return owner_web_contents_;
413 } 411 }
414 412
415 void GuestViewBase::GuestSizeChanged(const gfx::Size& new_size) { 413 void GuestViewBase::GuestSizeChanged(const gfx::Size& new_size) {
416 if (!auto_size_enabled_) 414 if (!auto_size_enabled_)
417 return; 415 return;
418 GuestSizeChangedDueToAutoSize(guest_size_, new_size); 416 GuestSizeChangedDueToAutoSize(guest_size_, new_size);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 webcontents_guestview_map.Get().erase(web_contents()); 451 webcontents_guestview_map.Get().erase(web_contents());
454 GuestViewManager::FromBrowserContext(browser_context_)-> 452 GuestViewManager::FromBrowserContext(browser_context_)->
455 RemoveGuest(guest_instance_id_); 453 RemoveGuest(guest_instance_id_);
456 pending_events_.clear(); 454 pending_events_.clear();
457 455
458 delete web_contents(); 456 delete web_contents();
459 } 457 }
460 458
461 void GuestViewBase::SetAttachParams(const base::DictionaryValue& params) { 459 void GuestViewBase::SetAttachParams(const base::DictionaryValue& params) {
462 attach_params_.reset(params.DeepCopy()); 460 attach_params_.reset(params.DeepCopy());
463 attach_params_->GetInteger(guestview::kParameterInstanceId, 461 attach_params_->GetInteger(kParameterInstanceId, &view_instance_id_);
464 &view_instance_id_);
465 } 462 }
466 463
467 void GuestViewBase::SetOpener(GuestViewBase* guest) { 464 void GuestViewBase::SetOpener(GuestViewBase* guest) {
468 if (guest && guest->IsViewType(GetViewType())) { 465 if (guest && guest->IsViewType(GetViewType())) {
469 opener_ = guest->weak_ptr_factory_.GetWeakPtr(); 466 opener_ = guest->weak_ptr_factory_.GetWeakPtr();
470 if (!attached()) 467 if (!attached())
471 opener_lifetime_observer_.reset(new OpenerLifetimeObserver(this)); 468 opener_lifetime_observer_.reset(new OpenerLifetimeObserver(this));
472 return; 469 return;
473 } 470 }
474 opener_ = base::WeakPtr<GuestViewBase>(); 471 opener_ = base::WeakPtr<GuestViewBase>();
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 return 1.0; 728 return 1.0;
732 729
733 return content::ZoomLevelToZoomFactor( 730 return content::ZoomLevelToZoomFactor(
734 ui_zoom::ZoomController::GetZoomLevelForWebContents( 731 ui_zoom::ZoomController::GetZoomLevelForWebContents(
735 embedder_web_contents())); 732 embedder_web_contents()));
736 } 733 }
737 734
738 void GuestViewBase::SetUpSizing(const base::DictionaryValue& params) { 735 void GuestViewBase::SetUpSizing(const base::DictionaryValue& params) {
739 // Read the autosize parameters passed in from the embedder. 736 // Read the autosize parameters passed in from the embedder.
740 bool auto_size_enabled = auto_size_enabled_; 737 bool auto_size_enabled = auto_size_enabled_;
741 params.GetBoolean(guestview::kAttributeAutoSize, &auto_size_enabled); 738 params.GetBoolean(kAttributeAutoSize, &auto_size_enabled);
742 739
743 int max_height = max_auto_size_.height(); 740 int max_height = max_auto_size_.height();
744 int max_width = max_auto_size_.width(); 741 int max_width = max_auto_size_.width();
745 params.GetInteger(guestview::kAttributeMaxHeight, &max_height); 742 params.GetInteger(kAttributeMaxHeight, &max_height);
746 params.GetInteger(guestview::kAttributeMaxWidth, &max_width); 743 params.GetInteger(kAttributeMaxWidth, &max_width);
747 744
748 int min_height = min_auto_size_.height(); 745 int min_height = min_auto_size_.height();
749 int min_width = min_auto_size_.width(); 746 int min_width = min_auto_size_.width();
750 params.GetInteger(guestview::kAttributeMinHeight, &min_height); 747 params.GetInteger(kAttributeMinHeight, &min_height);
751 params.GetInteger(guestview::kAttributeMinWidth, &min_width); 748 params.GetInteger(kAttributeMinWidth, &min_width);
752 749
753 double element_height = 0.0; 750 double element_height = 0.0;
754 double element_width = 0.0; 751 double element_width = 0.0;
755 params.GetDouble(guestview::kElementHeight, &element_height); 752 params.GetDouble(kElementHeight, &element_height);
756 params.GetDouble(guestview::kElementWidth, &element_width); 753 params.GetDouble(kElementWidth, &element_width);
757 754
758 // Set the normal size to the element size so that the guestview will fit 755 // Set the normal size to the element size so that the guestview will fit
759 // the element initially if autosize is disabled. 756 // the element initially if autosize is disabled.
760 int normal_height = normal_size_.height(); 757 int normal_height = normal_size_.height();
761 int normal_width = normal_size_.width(); 758 int normal_width = normal_size_.width();
762 // If the element size was provided in logical units (versus physical), then 759 // If the element size was provided in logical units (versus physical), then
763 // it will be converted to physical units. 760 // it will be converted to physical units.
764 bool element_size_is_logical = false; 761 bool element_size_is_logical = false;
765 params.GetBoolean(guestview::kElementSizeIsLogical, &element_size_is_logical); 762 params.GetBoolean(kElementSizeIsLogical, &element_size_is_logical);
766 if (element_size_is_logical) { 763 if (element_size_is_logical) {
767 // Convert the element size from logical pixels to physical pixels. 764 // Convert the element size from logical pixels to physical pixels.
768 normal_height = LogicalPixelsToPhysicalPixels(element_height); 765 normal_height = LogicalPixelsToPhysicalPixels(element_height);
769 normal_width = LogicalPixelsToPhysicalPixels(element_width); 766 normal_width = LogicalPixelsToPhysicalPixels(element_width);
770 } else { 767 } else {
771 normal_height = lround(element_height); 768 normal_height = lround(element_height);
772 normal_width = lround(element_width); 769 normal_width = lround(element_width);
773 } 770 }
774 771
775 SetSizeParams set_size_params; 772 SetSizeParams set_size_params;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
814 return; 811 return;
815 812
816 auto embedder_zoom_controller = 813 auto embedder_zoom_controller =
817 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); 814 ui_zoom::ZoomController::FromWebContents(owner_web_contents());
818 // Chrome Apps do not have a ZoomController. 815 // Chrome Apps do not have a ZoomController.
819 if (!embedder_zoom_controller) 816 if (!embedder_zoom_controller)
820 return; 817 return;
821 embedder_zoom_controller->RemoveObserver(this); 818 embedder_zoom_controller->RemoveObserver(this);
822 } 819 }
823 820
824 } // namespace extensions 821 } // namespace guest_view
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698