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 "components/guest_view/browser/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" | 9 #include "components/guest_view/browser/guest_view_event.h" |
10 #include "components/guest_view/browser/guest_view_manager.h" | 10 #include "components/guest_view/browser/guest_view_manager.h" |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 guest_host_(nullptr), | 152 guest_host_(nullptr), |
153 auto_size_enabled_(false), | 153 auto_size_enabled_(false), |
154 is_full_page_plugin_(false), | 154 is_full_page_plugin_(false), |
155 guest_proxy_routing_id_(MSG_ROUTING_NONE), | 155 guest_proxy_routing_id_(MSG_ROUTING_NONE), |
156 weak_ptr_factory_(this) { | 156 weak_ptr_factory_(this) { |
157 owner_host_ = GuestViewManager::FromBrowserContext(browser_context_)-> | 157 owner_host_ = GuestViewManager::FromBrowserContext(browser_context_)-> |
158 IsOwnedByExtension(this) ? | 158 IsOwnedByExtension(this) ? |
159 owner_web_contents->GetLastCommittedURL().host() : std::string(); | 159 owner_web_contents->GetLastCommittedURL().host() : std::string(); |
160 } | 160 } |
161 | 161 |
162 GuestViewBase::~GuestViewBase() {} | |
163 | |
162 void GuestViewBase::Init(const base::DictionaryValue& create_params, | 164 void GuestViewBase::Init(const base::DictionaryValue& create_params, |
163 const WebContentsCreatedCallback& callback) { | 165 const WebContentsCreatedCallback& callback) { |
164 if (initialized_) | 166 if (initialized_) |
165 return; | 167 return; |
166 initialized_ = true; | 168 initialized_ = true; |
167 | 169 |
168 if (!GuestViewManager::FromBrowserContext(browser_context_)-> | 170 if (!GuestViewManager::FromBrowserContext(browser_context_)-> |
169 IsGuestAvailableToContext(this)) { | 171 IsGuestAvailableToContext(this)) { |
170 // The derived class did not create a WebContents so this class serves no | 172 // The derived class did not create a WebContents so this class serves no |
171 // purpose. Let's self-destruct. | 173 // purpose. Let's self-destruct. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 | 221 |
220 // Observe guest zoom changes. | 222 // Observe guest zoom changes. |
221 auto zoom_controller = | 223 auto zoom_controller = |
222 ui_zoom::ZoomController::FromWebContents(web_contents()); | 224 ui_zoom::ZoomController::FromWebContents(web_contents()); |
223 zoom_controller->AddObserver(this); | 225 zoom_controller->AddObserver(this); |
224 | 226 |
225 // Give the derived class an opportunity to perform additional initialization. | 227 // Give the derived class an opportunity to perform additional initialization. |
226 DidInitialize(create_params); | 228 DidInitialize(create_params); |
227 } | 229 } |
228 | 230 |
229 void GuestViewBase::LoadURLWithParams( | |
230 const content::NavigationController::LoadURLParams& load_params) { | |
231 int guest_proxy_routing_id = host()->LoadURLWithParams(load_params); | |
232 DCHECK(guest_proxy_routing_id_ == MSG_ROUTING_NONE || | |
233 guest_proxy_routing_id == guest_proxy_routing_id_); | |
234 guest_proxy_routing_id_ = guest_proxy_routing_id; | |
235 } | |
236 | |
237 void GuestViewBase::DispatchOnResizeEvent(const gfx::Size& old_size, | |
238 const gfx::Size& new_size) { | |
239 if (new_size == old_size) | |
240 return; | |
241 | |
242 // Dispatch the onResize event. | |
243 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
244 args->SetInteger(kOldWidth, old_size.width()); | |
245 args->SetInteger(kOldHeight, old_size.height()); | |
246 args->SetInteger(kNewWidth, new_size.width()); | |
247 args->SetInteger(kNewHeight, new_size.height()); | |
248 DispatchEventToGuestProxy(new GuestViewEvent(kEventResize, args.Pass())); | |
249 } | |
250 | |
251 gfx::Size GuestViewBase::GetDefaultSize() const { | |
252 if (is_full_page_plugin()) { | |
253 // Full page plugins default to the size of the owner's viewport. | |
254 return owner_web_contents() | |
255 ->GetRenderWidgetHostView() | |
256 ->GetVisibleViewportSize(); | |
257 } else { | |
258 return gfx::Size(kDefaultWidth, kDefaultHeight); | |
259 } | |
260 } | |
261 | |
262 void GuestViewBase::SetSize(const SetSizeParams& params) { | |
263 bool enable_auto_size = | |
264 params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_; | |
265 gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_; | |
266 gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_; | |
267 | |
268 if (params.normal_size) | |
269 normal_size_ = *params.normal_size; | |
270 | |
271 min_auto_size_ = min_size; | |
272 min_auto_size_.SetToMin(max_size); | |
273 max_auto_size_ = max_size; | |
274 max_auto_size_.SetToMax(min_size); | |
275 | |
276 enable_auto_size &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty() && | |
277 IsAutoSizeSupported(); | |
278 | |
279 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | |
280 if (enable_auto_size) { | |
281 // Autosize is being enabled. | |
282 rvh->EnableAutoResize(min_auto_size_, max_auto_size_); | |
283 normal_size_.SetSize(0, 0); | |
284 } else { | |
285 // Autosize is being disabled. | |
286 // Use default width/height if missing from partially defined normal size. | |
287 if (normal_size_.width() && !normal_size_.height()) | |
288 normal_size_.set_height(GetDefaultSize().height()); | |
289 if (!normal_size_.width() && normal_size_.height()) | |
290 normal_size_.set_width(GetDefaultSize().width()); | |
291 | |
292 gfx::Size new_size; | |
293 if (!normal_size_.IsEmpty()) { | |
294 new_size = normal_size_; | |
295 } else if (!guest_size_.IsEmpty()) { | |
296 new_size = guest_size_; | |
297 } else { | |
298 new_size = GetDefaultSize(); | |
299 } | |
300 | |
301 bool changed_due_to_auto_resize = false; | |
302 if (auto_size_enabled_) { | |
303 // Autosize was previously enabled. | |
304 rvh->DisableAutoResize(new_size); | |
305 changed_due_to_auto_resize = true; | |
306 } else { | |
307 // Autosize was already disabled. | |
308 guest_host_->SizeContents(new_size); | |
309 } | |
310 | |
311 UpdateGuestSize(new_size, changed_due_to_auto_resize); | |
312 } | |
313 | |
314 auto_size_enabled_ = enable_auto_size; | |
315 } | |
316 | |
317 // static | 231 // static |
318 void GuestViewBase::CleanUp(content::BrowserContext* browser_context, | 232 void GuestViewBase::CleanUp(content::BrowserContext* browser_context, |
319 int embedder_process_id, | 233 int embedder_process_id, |
320 int view_instance_id) { | 234 int view_instance_id) { |
321 // TODO(paulmeyer): Add in any general GuestView cleanup work here. | 235 // TODO(paulmeyer): Add in any general GuestView cleanup work here. |
322 } | 236 } |
323 | 237 |
324 // static | 238 // static |
325 GuestViewBase* GuestViewBase::FromWebContents(const WebContents* web_contents) { | 239 GuestViewBase* GuestViewBase::FromWebContents(const WebContents* web_contents) { |
326 WebContentsGuestViewMap* guest_map = webcontents_guestview_map.Pointer(); | 240 WebContentsGuestViewMap* guest_map = webcontents_guestview_map.Pointer(); |
(...skipping 22 matching lines...) Expand all Loading... | |
349 while (GuestViewBase* guest = FromWebContents(web_contents)) | 263 while (GuestViewBase* guest = FromWebContents(web_contents)) |
350 web_contents = guest->owner_web_contents(); | 264 web_contents = guest->owner_web_contents(); |
351 return web_contents; | 265 return web_contents; |
352 } | 266 } |
353 | 267 |
354 // static | 268 // static |
355 bool GuestViewBase::IsGuest(WebContents* web_contents) { | 269 bool GuestViewBase::IsGuest(WebContents* web_contents) { |
356 return !!GuestViewBase::FromWebContents(web_contents); | 270 return !!GuestViewBase::FromWebContents(web_contents); |
357 } | 271 } |
358 | 272 |
359 bool GuestViewBase::IsAutoSizeSupported() const { | |
360 return false; | |
361 } | |
362 | |
363 bool GuestViewBase::IsPreferredSizeModeEnabled() const { | |
364 return false; | |
365 } | |
366 | |
367 bool GuestViewBase::ZoomPropagatesFromEmbedderToGuest() const { | |
368 return true; | |
369 } | |
370 | |
371 void GuestViewBase::SetContextMenuPosition(const gfx::Point& position) {} | |
372 | |
373 WebContents* GuestViewBase::CreateNewGuestWindow( | |
374 const WebContents::CreateParams& create_params) { | |
375 auto guest_manager = GuestViewManager::FromBrowserContext(browser_context()); | |
376 return guest_manager->CreateGuestWithWebContentsParams( | |
377 GetViewType(), | |
378 owner_web_contents(), | |
379 create_params); | |
380 } | |
381 | |
382 void GuestViewBase::DidAttach(int guest_proxy_routing_id) { | 273 void GuestViewBase::DidAttach(int guest_proxy_routing_id) { |
383 DCHECK(guest_proxy_routing_id_ == MSG_ROUTING_NONE || | 274 DCHECK(guest_proxy_routing_id_ == MSG_ROUTING_NONE || |
384 guest_proxy_routing_id == guest_proxy_routing_id_); | 275 guest_proxy_routing_id == guest_proxy_routing_id_); |
385 guest_proxy_routing_id_ = guest_proxy_routing_id; | 276 guest_proxy_routing_id_ = guest_proxy_routing_id; |
386 | 277 |
387 opener_lifetime_observer_.reset(); | 278 opener_lifetime_observer_.reset(); |
388 | 279 |
389 SetUpSizing(*attach_params()); | 280 SetUpSizing(*attach_params()); |
390 | 281 |
391 // Give the derived class an opportunity to perform some actions. | 282 // Give the derived class an opportunity to perform some actions. |
392 DidAttachToEmbedder(); | 283 DidAttachToEmbedder(); |
393 | 284 |
394 // Inform the associated GuestViewContainer that the contentWindow is ready. | 285 // Inform the associated GuestViewContainer that the contentWindow is ready. |
395 embedder_web_contents()->Send(new GuestViewMsg_GuestAttached( | 286 embedder_web_contents()->Send(new GuestViewMsg_GuestAttached( |
396 element_instance_id_, | 287 element_instance_id_, |
397 guest_proxy_routing_id)); | 288 guest_proxy_routing_id)); |
398 | 289 |
399 SendQueuedEvents(); | 290 SendQueuedEvents(); |
400 } | 291 } |
401 | 292 |
402 void GuestViewBase::DidDetach() { | 293 void GuestViewBase::WillAttach(WebContents* embedder_web_contents, |
403 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest(this); | 294 int element_instance_id, |
404 StopTrackingEmbedderZoomLevel(); | 295 bool is_full_page_plugin, |
405 owner_web_contents()->Send(new GuestViewMsg_GuestDetached( | 296 const base::Closure& callback) { |
406 element_instance_id_)); | 297 // Stop tracking the old embedder's zoom level. |
407 element_instance_id_ = kInstanceIDNone; | 298 if (owner_web_contents()) |
299 StopTrackingEmbedderZoomLevel(); | |
300 | |
301 if (owner_web_contents_ != embedder_web_contents) { | |
302 DCHECK_EQ(owner_contents_observer_->web_contents(), owner_web_contents_); | |
303 owner_web_contents_ = embedder_web_contents; | |
304 owner_contents_observer_.reset( | |
305 new OwnerContentsObserver(this, embedder_web_contents)); | |
306 owner_host_ = GuestViewManager::FromBrowserContext(browser_context_) | |
307 ->IsOwnedByExtension(this) | |
308 ? owner_web_contents()->GetLastCommittedURL().host() | |
309 : std::string(); | |
310 } | |
311 | |
312 // Start tracking the new embedder's zoom level. | |
313 StartTrackingEmbedderZoomLevel(); | |
314 element_instance_id_ = element_instance_id; | |
315 is_full_page_plugin_ = is_full_page_plugin; | |
316 | |
317 WillAttachToEmbedder(); | |
318 | |
319 // Completing attachment will resume suspended resource loads and then send | |
320 // queued events. | |
321 SignalWhenReady(callback); | |
408 } | 322 } |
409 | 323 |
410 bool GuestViewBase::Find(int request_id, | 324 bool GuestViewBase::IsAutoSizeSupported() const { |
411 const base::string16& search_text, | |
412 const blink::WebFindOptions& options) { | |
413 if (ShouldHandleFindRequestsForEmbedder()) { | |
414 web_contents()->Find(request_id, search_text, options); | |
415 return true; | |
416 } | |
417 return false; | 325 return false; |
418 } | 326 } |
419 | 327 |
420 bool GuestViewBase::StopFinding(content::StopFindAction action) { | 328 bool GuestViewBase::IsPreferredSizeModeEnabled() const { |
421 if (ShouldHandleFindRequestsForEmbedder()) { | |
422 web_contents()->StopFinding(action); | |
423 return true; | |
424 } | |
425 return false; | 329 return false; |
426 } | 330 } |
427 | 331 |
428 WebContents* GuestViewBase::GetOwnerWebContents() const { | 332 bool GuestViewBase::ZoomPropagatesFromEmbedderToGuest() const { |
429 return owner_web_contents_; | 333 return true; |
430 } | 334 } |
431 | 335 |
432 void GuestViewBase::GuestSizeChanged(const gfx::Size& new_size) { | 336 void GuestViewBase::DispatchEventToGuestProxy(GuestViewEvent* event) { |
433 UpdateGuestSize(new_size, auto_size_enabled_); | 337 event->Dispatch(this, guest_instance_id_); |
338 } | |
339 | |
340 void GuestViewBase::DispatchEventToView(GuestViewEvent* event) { | |
341 if (!attached() && | |
342 (!CanRunInDetachedState() || !can_owner_receive_events())) { | |
343 pending_events_.push_back(linked_ptr<GuestViewEvent>(event)); | |
344 return; | |
345 } | |
346 | |
347 event->Dispatch(this, view_instance_id_); | |
348 } | |
349 | |
350 void GuestViewBase::SetSize(const SetSizeParams& params) { | |
351 bool enable_auto_size = | |
352 params.enable_auto_size ? *params.enable_auto_size : auto_size_enabled_; | |
353 gfx::Size min_size = params.min_size ? *params.min_size : min_auto_size_; | |
354 gfx::Size max_size = params.max_size ? *params.max_size : max_auto_size_; | |
355 | |
356 if (params.normal_size) | |
357 normal_size_ = *params.normal_size; | |
358 | |
359 min_auto_size_ = min_size; | |
360 min_auto_size_.SetToMin(max_size); | |
361 max_auto_size_ = max_size; | |
362 max_auto_size_.SetToMax(min_size); | |
363 | |
364 enable_auto_size &= !min_auto_size_.IsEmpty() && !max_auto_size_.IsEmpty() && | |
365 IsAutoSizeSupported(); | |
366 | |
367 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | |
368 if (enable_auto_size) { | |
369 // Autosize is being enabled. | |
370 rvh->EnableAutoResize(min_auto_size_, max_auto_size_); | |
371 normal_size_.SetSize(0, 0); | |
372 } else { | |
373 // Autosize is being disabled. | |
374 // Use default width/height if missing from partially defined normal size. | |
375 if (normal_size_.width() && !normal_size_.height()) | |
376 normal_size_.set_height(GetDefaultSize().height()); | |
377 if (!normal_size_.width() && normal_size_.height()) | |
378 normal_size_.set_width(GetDefaultSize().width()); | |
379 | |
380 gfx::Size new_size; | |
381 if (!normal_size_.IsEmpty()) { | |
382 new_size = normal_size_; | |
383 } else if (!guest_size_.IsEmpty()) { | |
384 new_size = guest_size_; | |
385 } else { | |
386 new_size = GetDefaultSize(); | |
387 } | |
388 | |
389 bool changed_due_to_auto_resize = false; | |
390 if (auto_size_enabled_) { | |
391 // Autosize was previously enabled. | |
392 rvh->DisableAutoResize(new_size); | |
393 changed_due_to_auto_resize = true; | |
394 } else { | |
395 // Autosize was already disabled. | |
396 guest_host_->SizeContents(new_size); | |
397 } | |
398 | |
399 UpdateGuestSize(new_size, changed_due_to_auto_resize); | |
400 } | |
401 | |
402 auto_size_enabled_ = enable_auto_size; | |
434 } | 403 } |
435 | 404 |
436 const GURL& GuestViewBase::GetOwnerSiteURL() const { | 405 const GURL& GuestViewBase::GetOwnerSiteURL() const { |
437 return owner_web_contents()->GetLastCommittedURL(); | 406 return owner_web_contents()->GetLastCommittedURL(); |
438 } | 407 } |
439 | 408 |
440 void GuestViewBase::Destroy() { | 409 void GuestViewBase::Destroy() { |
441 if (is_being_destroyed_) | 410 if (is_being_destroyed_) |
442 return; | 411 return; |
443 | 412 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
480 if (guest && guest->IsViewType(GetViewType())) { | 449 if (guest && guest->IsViewType(GetViewType())) { |
481 opener_ = guest->weak_ptr_factory_.GetWeakPtr(); | 450 opener_ = guest->weak_ptr_factory_.GetWeakPtr(); |
482 if (!attached()) | 451 if (!attached()) |
483 opener_lifetime_observer_.reset(new OpenerLifetimeObserver(this)); | 452 opener_lifetime_observer_.reset(new OpenerLifetimeObserver(this)); |
484 return; | 453 return; |
485 } | 454 } |
486 opener_ = base::WeakPtr<GuestViewBase>(); | 455 opener_ = base::WeakPtr<GuestViewBase>(); |
487 opener_lifetime_observer_.reset(); | 456 opener_lifetime_observer_.reset(); |
488 } | 457 } |
489 | 458 |
490 void GuestViewBase::SetGuestHost(content::GuestHost* guest_host) { | 459 void GuestViewBase::SetContextMenuPosition(const gfx::Point& position) {} |
491 guest_host_ = guest_host; | 460 |
461 void GuestViewBase::HandleKeyboardEvent( | |
462 WebContents* source, | |
463 const content::NativeWebKeyboardEvent& event) { | |
464 if (!attached()) | |
465 return; | |
466 | |
467 // Send the keyboard events back to the embedder to reprocess them. | |
468 embedder_web_contents()->GetDelegate()->HandleKeyboardEvent( | |
469 embedder_web_contents(), event); | |
492 } | 470 } |
493 | 471 |
494 void GuestViewBase::WillAttach(WebContents* embedder_web_contents, | 472 bool GuestViewBase::PreHandleGestureEvent(WebContents* source, |
495 int element_instance_id, | 473 const blink::WebGestureEvent& event) { |
496 bool is_full_page_plugin, | 474 return event.type == blink::WebGestureEvent::GesturePinchBegin || |
497 const base::Closure& callback) { | 475 event.type == blink::WebGestureEvent::GesturePinchUpdate || |
498 // Stop tracking the old embedder's zoom level. | 476 event.type == blink::WebGestureEvent::GesturePinchEnd; |
499 if (owner_web_contents()) | 477 } |
500 StopTrackingEmbedderZoomLevel(); | |
501 | 478 |
502 if (owner_web_contents_ != embedder_web_contents) { | 479 void GuestViewBase::FindReply(WebContents* source, |
503 DCHECK_EQ(owner_contents_observer_->web_contents(), owner_web_contents_); | 480 int request_id, |
504 owner_web_contents_ = embedder_web_contents; | 481 int number_of_matches, |
505 owner_contents_observer_.reset( | 482 const gfx::Rect& selection_rect, |
506 new OwnerContentsObserver(this, embedder_web_contents)); | 483 int active_match_ordinal, |
507 owner_host_ = GuestViewManager::FromBrowserContext(browser_context_)-> | 484 bool final_update) { |
508 IsOwnedByExtension(this) ? | 485 if (ShouldHandleFindRequestsForEmbedder() && attached() && |
509 owner_web_contents()->GetLastCommittedURL().host() : std::string(); | 486 embedder_web_contents()->GetDelegate()) { |
487 embedder_web_contents()->GetDelegate()->FindReply( | |
488 embedder_web_contents(), request_id, number_of_matches, selection_rect, | |
489 active_match_ordinal, final_update); | |
510 } | 490 } |
491 } | |
511 | 492 |
512 // Start tracking the new embedder's zoom level. | 493 void GuestViewBase::DidNavigateMainFrame( |
513 StartTrackingEmbedderZoomLevel(); | 494 const content::LoadCommittedDetails& details, |
514 element_instance_id_ = element_instance_id; | 495 const content::FrameNavigateParams& params) { |
515 is_full_page_plugin_ = is_full_page_plugin; | 496 if (attached() && ZoomPropagatesFromEmbedderToGuest()) |
497 SetGuestZoomLevelToMatchEmbedder(); | |
516 | 498 |
517 WillAttachToEmbedder(); | 499 // TODO(lazyboy): This breaks guest visibility in --site-per-process because |
518 | 500 // we do not take the widget's visibility into account. We need to also |
519 // Completing attachment will resume suspended resource loads and then send | 501 // stay hidden during "visibility:none" state. |
520 // queued events. | 502 if (content::BrowserPluginGuestMode::UseCrossProcessFramesForGuests()) { |
521 SignalWhenReady(callback); | 503 web_contents()->WasShown(); |
504 } | |
522 } | 505 } |
523 | 506 |
524 void GuestViewBase::SignalWhenReady(const base::Closure& callback) { | 507 void GuestViewBase::SignalWhenReady(const base::Closure& callback) { |
525 // The default behavior is to call the |callback| immediately. Derived classes | 508 // The default behavior is to call the |callback| immediately. Derived classes |
526 // can implement an alternative signal for readiness. | 509 // can implement an alternative signal for readiness. |
527 callback.Run(); | 510 callback.Run(); |
528 } | 511 } |
529 | 512 |
530 bool GuestViewBase::ShouldHandleFindRequestsForEmbedder() const { | 513 bool GuestViewBase::ShouldHandleFindRequestsForEmbedder() const { |
531 return false; | 514 return false; |
532 } | 515 } |
533 | 516 |
517 void GuestViewBase::LoadURLWithParams( | |
518 const content::NavigationController::LoadURLParams& load_params) { | |
519 int guest_proxy_routing_id = host()->LoadURLWithParams(load_params); | |
520 DCHECK(guest_proxy_routing_id_ == MSG_ROUTING_NONE || | |
521 guest_proxy_routing_id == guest_proxy_routing_id_); | |
522 guest_proxy_routing_id_ = guest_proxy_routing_id; | |
523 } | |
524 | |
534 int GuestViewBase::LogicalPixelsToPhysicalPixels(double logical_pixels) const { | 525 int GuestViewBase::LogicalPixelsToPhysicalPixels(double logical_pixels) const { |
535 DCHECK(logical_pixels >= 0); | 526 DCHECK(logical_pixels >= 0); |
536 double zoom_factor = GetEmbedderZoomFactor(); | 527 double zoom_factor = GetEmbedderZoomFactor(); |
537 return lround(logical_pixels * zoom_factor); | 528 return lround(logical_pixels * zoom_factor); |
538 } | 529 } |
539 | 530 |
540 double GuestViewBase::PhysicalPixelsToLogicalPixels(int physical_pixels) const { | 531 double GuestViewBase::PhysicalPixelsToLogicalPixels(int physical_pixels) const { |
541 DCHECK(physical_pixels >= 0); | 532 DCHECK(physical_pixels >= 0); |
542 double zoom_factor = GetEmbedderZoomFactor(); | 533 double zoom_factor = GetEmbedderZoomFactor(); |
543 return physical_pixels / zoom_factor; | 534 return physical_pixels / zoom_factor; |
544 } | 535 } |
545 | 536 |
546 void GuestViewBase::DidStopLoading() { | 537 void GuestViewBase::SetGuestZoomLevelToMatchEmbedder() { |
547 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | 538 auto embedder_zoom_controller = |
539 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); | |
540 if (!embedder_zoom_controller) | |
541 return; | |
548 | 542 |
549 if (IsPreferredSizeModeEnabled()) | 543 ui_zoom::ZoomController::FromWebContents(web_contents()) |
550 rvh->EnablePreferredSizeMode(); | 544 ->SetZoomLevel(embedder_zoom_controller->GetZoomLevel()); |
551 GuestViewDidStopLoading(); | |
552 } | 545 } |
553 | 546 |
554 void GuestViewBase::RenderViewReady() { | 547 WebContents* GuestViewBase::CreateNewGuestWindow( |
555 GuestReady(); | 548 const WebContents::CreateParams& create_params) { |
549 auto guest_manager = GuestViewManager::FromBrowserContext(browser_context()); | |
550 return guest_manager->CreateGuestWithWebContentsParams( | |
551 GetViewType(), owner_web_contents(), create_params); | |
556 } | 552 } |
557 | 553 |
558 void GuestViewBase::WebContentsDestroyed() { | 554 void GuestViewBase::DidDetach() { |
559 // Let the derived class know that its WebContents is in the process of | 555 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest(this); |
560 // being destroyed. web_contents() is still valid at this point. | 556 StopTrackingEmbedderZoomLevel(); |
561 // TODO(fsamuel): This allows for reentrant code into WebContents during | 557 owner_web_contents()->Send( |
562 // destruction. This could potentially lead to bugs. Perhaps we should get rid | 558 new GuestViewMsg_GuestDetached(element_instance_id_)); |
563 // of this? | 559 element_instance_id_ = kInstanceIDNone; |
564 GuestDestroyed(); | |
565 | |
566 // Self-destruct. | |
567 delete this; | |
568 } | 560 } |
569 | 561 |
570 void GuestViewBase::DidNavigateMainFrame( | 562 bool GuestViewBase::Find(int request_id, |
571 const content::LoadCommittedDetails& details, | 563 const base::string16& search_text, |
572 const content::FrameNavigateParams& params) { | 564 const blink::WebFindOptions& options) { |
573 if (attached() && ZoomPropagatesFromEmbedderToGuest()) | 565 if (ShouldHandleFindRequestsForEmbedder()) { |
574 SetGuestZoomLevelToMatchEmbedder(); | 566 web_contents()->Find(request_id, search_text, options); |
567 return true; | |
568 } | |
569 return false; | |
570 } | |
575 | 571 |
576 // TODO(lazyboy): This breaks guest visibility in --site-per-process because | 572 bool GuestViewBase::StopFinding(content::StopFindAction action) { |
577 // we do not take the widget's visibility into account. We need to also | 573 if (ShouldHandleFindRequestsForEmbedder()) { |
578 // stay hidden during "visibility:none" state. | 574 web_contents()->StopFinding(action); |
579 if (content::BrowserPluginGuestMode::UseCrossProcessFramesForGuests()) { | 575 return true; |
580 web_contents()->WasShown(); | |
581 } | 576 } |
577 return false; | |
578 } | |
579 | |
580 WebContents* GuestViewBase::GetOwnerWebContents() const { | |
581 return owner_web_contents_; | |
582 } | |
583 | |
584 void GuestViewBase::GuestSizeChanged(const gfx::Size& new_size) { | |
585 UpdateGuestSize(new_size, auto_size_enabled_); | |
586 } | |
587 | |
588 void GuestViewBase::SetGuestHost(content::GuestHost* guest_host) { | |
589 guest_host_ = guest_host; | |
582 } | 590 } |
583 | 591 |
584 void GuestViewBase::ActivateContents(WebContents* web_contents) { | 592 void GuestViewBase::ActivateContents(WebContents* web_contents) { |
585 if (!attached() || !embedder_web_contents()->GetDelegate()) | 593 if (!attached() || !embedder_web_contents()->GetDelegate()) |
586 return; | 594 return; |
587 | 595 |
588 embedder_web_contents()->GetDelegate()->ActivateContents( | 596 embedder_web_contents()->GetDelegate()->ActivateContents( |
589 embedder_web_contents()); | 597 embedder_web_contents()); |
590 } | 598 } |
591 | 599 |
(...skipping 14 matching lines...) Expand all Loading... | |
606 embedder_web_contents()->GetDelegate()->ContentsMouseEvent( | 614 embedder_web_contents()->GetDelegate()->ContentsMouseEvent( |
607 embedder_web_contents(), location, motion); | 615 embedder_web_contents(), location, motion); |
608 } | 616 } |
609 | 617 |
610 void GuestViewBase::ContentsZoomChange(bool zoom_in) { | 618 void GuestViewBase::ContentsZoomChange(bool zoom_in) { |
611 ui_zoom::PageZoom::Zoom( | 619 ui_zoom::PageZoom::Zoom( |
612 embedder_web_contents(), | 620 embedder_web_contents(), |
613 zoom_in ? content::PAGE_ZOOM_IN : content::PAGE_ZOOM_OUT); | 621 zoom_in ? content::PAGE_ZOOM_IN : content::PAGE_ZOOM_OUT); |
614 } | 622 } |
615 | 623 |
616 void GuestViewBase::HandleKeyboardEvent( | |
617 WebContents* source, | |
618 const content::NativeWebKeyboardEvent& event) { | |
619 if (!attached()) | |
620 return; | |
621 | |
622 // Send the keyboard events back to the embedder to reprocess them. | |
623 embedder_web_contents()->GetDelegate()-> | |
624 HandleKeyboardEvent(embedder_web_contents(), event); | |
625 } | |
626 | |
627 void GuestViewBase::LoadingStateChanged(WebContents* source, | 624 void GuestViewBase::LoadingStateChanged(WebContents* source, |
628 bool to_different_document) { | 625 bool to_different_document) { |
629 if (!attached() || !embedder_web_contents()->GetDelegate()) | 626 if (!attached() || !embedder_web_contents()->GetDelegate()) |
630 return; | 627 return; |
631 | 628 |
632 embedder_web_contents()->GetDelegate()->LoadingStateChanged( | 629 embedder_web_contents()->GetDelegate()->LoadingStateChanged( |
633 embedder_web_contents(), to_different_document); | 630 embedder_web_contents(), to_different_document); |
634 } | 631 } |
635 | 632 |
636 content::ColorChooser* GuestViewBase::OpenColorChooser( | 633 content::ColorChooser* GuestViewBase::OpenColorChooser( |
(...skipping 18 matching lines...) Expand all Loading... | |
655 return; | 652 return; |
656 | 653 |
657 embedder_web_contents()->GetDelegate()->RunFileChooser(web_contents, params); | 654 embedder_web_contents()->GetDelegate()->RunFileChooser(web_contents, params); |
658 } | 655 } |
659 | 656 |
660 bool GuestViewBase::ShouldFocusPageAfterCrash() { | 657 bool GuestViewBase::ShouldFocusPageAfterCrash() { |
661 // Focus is managed elsewhere. | 658 // Focus is managed elsewhere. |
662 return false; | 659 return false; |
663 } | 660 } |
664 | 661 |
665 bool GuestViewBase::PreHandleGestureEvent(WebContents* source, | |
666 const blink::WebGestureEvent& event) { | |
667 return event.type == blink::WebGestureEvent::GesturePinchBegin || | |
668 event.type == blink::WebGestureEvent::GesturePinchUpdate || | |
669 event.type == blink::WebGestureEvent::GesturePinchEnd; | |
670 } | |
671 | |
672 void GuestViewBase::UpdatePreferredSize(WebContents* target_web_contents, | 662 void GuestViewBase::UpdatePreferredSize(WebContents* target_web_contents, |
673 const gfx::Size& pref_size) { | 663 const gfx::Size& pref_size) { |
674 // In theory it's not necessary to check IsPreferredSizeModeEnabled() because | 664 // In theory it's not necessary to check IsPreferredSizeModeEnabled() because |
675 // there will only be events if it was enabled in the first place. However, | 665 // there will only be events if it was enabled in the first place. However, |
676 // something else may have turned on preferred size mode, so double check. | 666 // something else may have turned on preferred size mode, so double check. |
677 DCHECK_EQ(web_contents(), target_web_contents); | 667 DCHECK_EQ(web_contents(), target_web_contents); |
678 if (IsPreferredSizeModeEnabled()) { | 668 if (IsPreferredSizeModeEnabled()) { |
679 OnPreferredSizeChanged(pref_size); | 669 OnPreferredSizeChanged(pref_size); |
680 } | 670 } |
681 } | 671 } |
682 | 672 |
683 void GuestViewBase::UpdateTargetURL(WebContents* source, const GURL& url) { | 673 void GuestViewBase::UpdateTargetURL(WebContents* source, const GURL& url) { |
684 if (!attached() || !embedder_web_contents()->GetDelegate()) | 674 if (!attached() || !embedder_web_contents()->GetDelegate()) |
685 return; | 675 return; |
686 | 676 |
687 embedder_web_contents()->GetDelegate()->UpdateTargetURL( | 677 embedder_web_contents()->GetDelegate()->UpdateTargetURL( |
688 embedder_web_contents(), url); | 678 embedder_web_contents(), url); |
689 } | 679 } |
690 | 680 |
691 bool GuestViewBase::ShouldResumeRequestsForCreatedWindow() { | 681 bool GuestViewBase::ShouldResumeRequestsForCreatedWindow() { |
692 return false; | 682 return false; |
693 } | 683 } |
694 | 684 |
695 void GuestViewBase::FindReply(WebContents* source, | 685 void GuestViewBase::DidStopLoading() { |
696 int request_id, | 686 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
697 int number_of_matches, | 687 |
698 const gfx::Rect& selection_rect, | 688 if (IsPreferredSizeModeEnabled()) |
699 int active_match_ordinal, | 689 rvh->EnablePreferredSizeMode(); |
700 bool final_update) { | 690 GuestViewDidStopLoading(); |
701 if (ShouldHandleFindRequestsForEmbedder() && | |
702 attached() && embedder_web_contents()->GetDelegate()) { | |
703 embedder_web_contents()->GetDelegate()->FindReply(embedder_web_contents(), | |
704 request_id, | |
705 number_of_matches, | |
706 selection_rect, | |
707 active_match_ordinal, | |
708 final_update); | |
709 } | |
710 } | 691 } |
711 | 692 |
712 GuestViewBase::~GuestViewBase() { | 693 void GuestViewBase::RenderViewReady() { |
694 GuestReady(); | |
695 } | |
696 | |
697 void GuestViewBase::WebContentsDestroyed() { | |
698 // Let the derived class know that its WebContents is in the process of | |
699 // being destroyed. web_contents() is still valid at this point. | |
700 // TODO(fsamuel): This allows for reentrant code into WebContents during | |
701 // destruction. This could potentially lead to bugs. Perhaps we should get rid | |
702 // of this? | |
703 GuestDestroyed(); | |
704 | |
705 // Self-destruct. | |
706 delete this; | |
713 } | 707 } |
714 | 708 |
715 void GuestViewBase::OnZoomChanged( | 709 void GuestViewBase::OnZoomChanged( |
716 const ui_zoom::ZoomController::ZoomChangedEventData& data) { | 710 const ui_zoom::ZoomController::ZoomChangedEventData& data) { |
717 if (data.web_contents == embedder_web_contents()) { | 711 if (data.web_contents == embedder_web_contents()) { |
718 // The embedder's zoom level has changed. | 712 // The embedder's zoom level has changed. |
719 auto guest_zoom_controller = | 713 auto guest_zoom_controller = |
720 ui_zoom::ZoomController::FromWebContents(web_contents()); | 714 ui_zoom::ZoomController::FromWebContents(web_contents()); |
721 if (content::ZoomValuesEqual(data.new_zoom_level, | 715 if (content::ZoomValuesEqual(data.new_zoom_level, |
722 guest_zoom_controller->GetZoomLevel())) { | 716 guest_zoom_controller->GetZoomLevel())) { |
723 return; | 717 return; |
724 } | 718 } |
725 // When the embedder's zoom level doesn't match the guest's, then update the | 719 // When the embedder's zoom level doesn't match the guest's, then update the |
726 // guest's zoom level to match. | 720 // guest's zoom level to match. |
727 guest_zoom_controller->SetZoomLevel(data.new_zoom_level); | 721 guest_zoom_controller->SetZoomLevel(data.new_zoom_level); |
728 | |
729 EmbedderZoomChanged(data.old_zoom_level, data.new_zoom_level); | |
Fady Samuel
2015/10/10 10:58:01
Why was this deleted?
paulmeyer
2015/10/16 21:13:03
Because it does nothing and is never overridden.
Fady Samuel
2015/10/16 21:17:28
Acknowledged.
| |
730 return; | 722 return; |
731 } | 723 } |
732 | 724 |
733 if (data.web_contents == web_contents()) { | 725 if (data.web_contents == web_contents()) { |
734 // The guest's zoom level has changed. | 726 // The guest's zoom level has changed. |
735 GuestZoomChanged(data.old_zoom_level, data.new_zoom_level); | 727 GuestZoomChanged(data.old_zoom_level, data.new_zoom_level); |
736 } | 728 } |
737 } | 729 } |
738 | 730 |
739 void GuestViewBase::DispatchEventToGuestProxy(GuestViewEvent* event) { | |
740 event->Dispatch(this, guest_instance_id_); | |
741 } | |
742 | |
743 void GuestViewBase::DispatchEventToView(GuestViewEvent* event) { | |
744 if (!attached() && | |
745 (!CanRunInDetachedState() || !can_owner_receive_events())) { | |
746 pending_events_.push_back(linked_ptr<GuestViewEvent>(event)); | |
747 return; | |
748 } | |
749 | |
750 event->Dispatch(this, view_instance_id_); | |
751 } | |
752 | |
753 void GuestViewBase::SendQueuedEvents() { | 731 void GuestViewBase::SendQueuedEvents() { |
754 if (!attached()) | 732 if (!attached()) |
755 return; | 733 return; |
756 while (!pending_events_.empty()) { | 734 while (!pending_events_.empty()) { |
757 linked_ptr<GuestViewEvent> event_ptr = pending_events_.front(); | 735 linked_ptr<GuestViewEvent> event_ptr = pending_events_.front(); |
758 pending_events_.pop_front(); | 736 pending_events_.pop_front(); |
759 event_ptr.release()->Dispatch(this, view_instance_id_); | 737 event_ptr.release()->Dispatch(this, view_instance_id_); |
760 } | 738 } |
761 } | 739 } |
762 | 740 |
763 void GuestViewBase::CompleteInit( | 741 void GuestViewBase::CompleteInit( |
764 scoped_ptr<base::DictionaryValue> create_params, | 742 scoped_ptr<base::DictionaryValue> create_params, |
765 const WebContentsCreatedCallback& callback, | 743 const WebContentsCreatedCallback& callback, |
766 WebContents* guest_web_contents) { | 744 WebContents* guest_web_contents) { |
767 if (!guest_web_contents) { | 745 if (!guest_web_contents) { |
768 // The derived class did not create a WebContents so this class serves no | 746 // The derived class did not create a WebContents so this class serves no |
769 // purpose. Let's self-destruct. | 747 // purpose. Let's self-destruct. |
770 delete this; | 748 delete this; |
771 callback.Run(nullptr); | 749 callback.Run(nullptr); |
772 return; | 750 return; |
773 } | 751 } |
774 InitWithWebContents(*create_params, guest_web_contents); | 752 InitWithWebContents(*create_params, guest_web_contents); |
775 callback.Run(guest_web_contents); | 753 callback.Run(guest_web_contents); |
776 } | 754 } |
777 | 755 |
756 void GuestViewBase::DispatchOnResizeEvent(const gfx::Size& old_size, | |
757 const gfx::Size& new_size) { | |
758 if (new_size == old_size) | |
759 return; | |
760 | |
761 // Dispatch the onResize event. | |
762 scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue()); | |
763 args->SetInteger(kOldWidth, old_size.width()); | |
764 args->SetInteger(kOldHeight, old_size.height()); | |
765 args->SetInteger(kNewWidth, new_size.width()); | |
766 args->SetInteger(kNewHeight, new_size.height()); | |
767 DispatchEventToGuestProxy(new GuestViewEvent(kEventResize, args.Pass())); | |
768 } | |
769 | |
770 gfx::Size GuestViewBase::GetDefaultSize() const { | |
771 if (is_full_page_plugin()) { | |
772 // Full page plugins default to the size of the owner's viewport. | |
773 return owner_web_contents() | |
774 ->GetRenderWidgetHostView() | |
775 ->GetVisibleViewportSize(); | |
776 } else { | |
777 return gfx::Size(kDefaultWidth, kDefaultHeight); | |
778 } | |
779 } | |
780 | |
778 double GuestViewBase::GetEmbedderZoomFactor() const { | 781 double GuestViewBase::GetEmbedderZoomFactor() const { |
779 if (!embedder_web_contents()) | 782 if (!embedder_web_contents()) |
780 return 1.0; | 783 return 1.0; |
781 | 784 |
782 return content::ZoomLevelToZoomFactor( | 785 return content::ZoomLevelToZoomFactor( |
783 ui_zoom::ZoomController::GetZoomLevelForWebContents( | 786 ui_zoom::ZoomController::GetZoomLevelForWebContents( |
784 embedder_web_contents())); | 787 embedder_web_contents())); |
785 } | 788 } |
786 | 789 |
787 void GuestViewBase::SetUpSizing(const base::DictionaryValue& params) { | 790 void GuestViewBase::SetUpSizing(const base::DictionaryValue& params) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
825 set_size_params.enable_auto_size.reset(new bool(auto_size_enabled)); | 828 set_size_params.enable_auto_size.reset(new bool(auto_size_enabled)); |
826 set_size_params.min_size.reset(new gfx::Size(min_width, min_height)); | 829 set_size_params.min_size.reset(new gfx::Size(min_width, min_height)); |
827 set_size_params.max_size.reset(new gfx::Size(max_width, max_height)); | 830 set_size_params.max_size.reset(new gfx::Size(max_width, max_height)); |
828 set_size_params.normal_size.reset(new gfx::Size(normal_width, normal_height)); | 831 set_size_params.normal_size.reset(new gfx::Size(normal_width, normal_height)); |
829 | 832 |
830 // Call SetSize to apply all the appropriate validation and clipping of | 833 // Call SetSize to apply all the appropriate validation and clipping of |
831 // values. | 834 // values. |
832 SetSize(set_size_params); | 835 SetSize(set_size_params); |
833 } | 836 } |
834 | 837 |
835 void GuestViewBase::SetGuestZoomLevelToMatchEmbedder() { | |
836 auto embedder_zoom_controller = | |
837 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); | |
838 if (!embedder_zoom_controller) | |
839 return; | |
840 | |
841 ui_zoom::ZoomController::FromWebContents(web_contents()) | |
842 ->SetZoomLevel(embedder_zoom_controller->GetZoomLevel()); | |
843 } | |
844 | |
845 void GuestViewBase::StartTrackingEmbedderZoomLevel() { | 838 void GuestViewBase::StartTrackingEmbedderZoomLevel() { |
846 if (!ZoomPropagatesFromEmbedderToGuest()) | 839 if (!ZoomPropagatesFromEmbedderToGuest()) |
847 return; | 840 return; |
848 | 841 |
849 auto embedder_zoom_controller = | 842 auto embedder_zoom_controller = |
850 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); | 843 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); |
851 // Chrome Apps do not have a ZoomController. | 844 // Chrome Apps do not have a ZoomController. |
852 if (!embedder_zoom_controller) | 845 if (!embedder_zoom_controller) |
853 return; | 846 return; |
854 // Listen to the embedder's zoom changes. | 847 // Listen to the embedder's zoom changes. |
(...skipping 17 matching lines...) Expand all Loading... | |
872 | 865 |
873 void GuestViewBase::UpdateGuestSize(const gfx::Size& new_size, | 866 void GuestViewBase::UpdateGuestSize(const gfx::Size& new_size, |
874 bool due_to_auto_resize) { | 867 bool due_to_auto_resize) { |
875 if (due_to_auto_resize) | 868 if (due_to_auto_resize) |
876 GuestSizeChangedDueToAutoSize(guest_size_, new_size); | 869 GuestSizeChangedDueToAutoSize(guest_size_, new_size); |
877 DispatchOnResizeEvent(guest_size_, new_size); | 870 DispatchOnResizeEvent(guest_size_, new_size); |
878 guest_size_ = new_size; | 871 guest_size_ = new_size; |
879 } | 872 } |
880 | 873 |
881 } // namespace guest_view | 874 } // namespace guest_view |
OLD | NEW |