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/page_zoom.h" | 9 #include "components/ui/zoom/page_zoom.h" |
10 #include "components/ui/zoom/zoom_controller.h" | 10 #include "components/ui/zoom/zoom_controller.h" |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 private: | 139 private: |
140 GuestViewBase* guest_; | 140 GuestViewBase* guest_; |
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::FromBrowserContextIfAvailable(browser_context_)-> | 149 GuestViewManager::FromBrowserContext(browser_context_)-> |
150 GetNextInstanceID()), | 150 GetNextInstanceID()), |
151 view_instance_id_(guestview::kInstanceIDNone), | 151 view_instance_id_(guestview::kInstanceIDNone), |
152 element_instance_id_(guestview::kInstanceIDNone), | 152 element_instance_id_(guestview::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_)-> |
| 161 IsOwnedByExtension(this) ? |
| 162 owner_web_contents->GetLastCommittedURL().host() : std::string(); |
160 } | 163 } |
161 | 164 |
162 void GuestViewBase::Init(const base::DictionaryValue& create_params, | 165 void GuestViewBase::Init(const base::DictionaryValue& create_params, |
163 const WebContentsCreatedCallback& callback) { | 166 const WebContentsCreatedCallback& callback) { |
164 if (initialized_) | 167 if (initialized_) |
165 return; | 168 return; |
166 initialized_ = true; | 169 initialized_ = true; |
167 | 170 |
168 if (!GuestViewManager::FromBrowserContextIfAvailable(browser_context_)-> | 171 if (!GuestViewManager::FromBrowserContext(browser_context_)-> |
169 IsGuestAvailableToContext(this, &owner_extension_id_)) { | 172 IsGuestAvailableToContext(this)) { |
170 // The derived class did not create a WebContents so this class serves no | 173 // The derived class did not create a WebContents so this class serves no |
171 // purpose. Let's self-destruct. | 174 // purpose. Let's self-destruct. |
172 delete this; | 175 delete this; |
173 callback.Run(nullptr); | 176 callback.Run(nullptr); |
174 return; | 177 return; |
175 } | 178 } |
176 | 179 |
177 scoped_ptr<base::DictionaryValue> params(create_params.DeepCopy()); | 180 scoped_ptr<base::DictionaryValue> params(create_params.DeepCopy()); |
178 CreateWebContents(create_params, | 181 CreateWebContents(create_params, |
179 base::Bind(&GuestViewBase::CompleteInit, | 182 base::Bind(&GuestViewBase::CompleteInit, |
(...skipping 21 matching lines...) Expand all Loading... |
201 // At this point, we have just created the guest WebContents, we need to add | 204 // At this point, we have just created the guest WebContents, we need to add |
202 // an observer to the owner WebContents. This observer will be responsible | 205 // an observer to the owner WebContents. This observer will be responsible |
203 // for destroying the guest WebContents if the owner goes away. | 206 // for destroying the guest WebContents if the owner goes away. |
204 owner_contents_observer_.reset( | 207 owner_contents_observer_.reset( |
205 new OwnerContentsObserver(this, owner_web_contents_)); | 208 new OwnerContentsObserver(this, owner_web_contents_)); |
206 | 209 |
207 WebContentsObserver::Observe(guest_web_contents); | 210 WebContentsObserver::Observe(guest_web_contents); |
208 guest_web_contents->SetDelegate(this); | 211 guest_web_contents->SetDelegate(this); |
209 webcontents_guestview_map.Get().insert( | 212 webcontents_guestview_map.Get().insert( |
210 std::make_pair(guest_web_contents, this)); | 213 std::make_pair(guest_web_contents, this)); |
211 GuestViewManager::FromBrowserContextIfAvailable(browser_context_)-> | 214 GuestViewManager::FromBrowserContext(browser_context_)-> |
212 AddGuest(guest_instance_id_, guest_web_contents); | 215 AddGuest(guest_instance_id_, guest_web_contents); |
213 | 216 |
214 // Populate the view instance ID if we have it on creation. | 217 // Populate the view instance ID if we have it on creation. |
215 create_params.GetInteger(guestview::kParameterInstanceId, | 218 create_params.GetInteger(guestview::kParameterInstanceId, |
216 &view_instance_id_); | 219 &view_instance_id_); |
217 | 220 |
218 if (CanRunInDetachedState()) | 221 if (CanRunInDetachedState()) |
219 SetUpSizing(create_params); | 222 SetUpSizing(create_params); |
220 | 223 |
221 // Observe guest zoom changes. | 224 // Observe guest zoom changes. |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 } | 327 } |
325 | 328 |
326 // static | 329 // static |
327 GuestViewBase* GuestViewBase::From(int owner_process_id, | 330 GuestViewBase* GuestViewBase::From(int owner_process_id, |
328 int guest_instance_id) { | 331 int guest_instance_id) { |
329 auto host = content::RenderProcessHost::FromID(owner_process_id); | 332 auto host = content::RenderProcessHost::FromID(owner_process_id); |
330 if (!host) | 333 if (!host) |
331 return nullptr; | 334 return nullptr; |
332 | 335 |
333 content::WebContents* guest_web_contents = | 336 content::WebContents* guest_web_contents = |
334 GuestViewManager::FromBrowserContextIfAvailable( | 337 GuestViewManager::FromBrowserContext( |
335 host->GetBrowserContext())-> | 338 host->GetBrowserContext())-> |
336 GetGuestByInstanceIDSafely(guest_instance_id, owner_process_id); | 339 GetGuestByInstanceIDSafely(guest_instance_id, owner_process_id); |
337 if (!guest_web_contents) | 340 if (!guest_web_contents) |
338 return nullptr; | 341 return nullptr; |
339 | 342 |
340 return GuestViewBase::FromWebContents(guest_web_contents); | 343 return GuestViewBase::FromWebContents(guest_web_contents); |
341 } | 344 } |
342 | 345 |
343 // static | 346 // static |
344 WebContents* GuestViewBase::GetTopLevelWebContents(WebContents* web_contents) { | 347 WebContents* GuestViewBase::GetTopLevelWebContents(WebContents* web_contents) { |
(...skipping 18 matching lines...) Expand all Loading... |
363 bool GuestViewBase::IsDragAndDropEnabled() const { | 366 bool GuestViewBase::IsDragAndDropEnabled() const { |
364 return false; | 367 return false; |
365 } | 368 } |
366 | 369 |
367 bool GuestViewBase::ZoomPropagatesFromEmbedderToGuest() const { | 370 bool GuestViewBase::ZoomPropagatesFromEmbedderToGuest() const { |
368 return true; | 371 return true; |
369 } | 372 } |
370 | 373 |
371 content::WebContents* GuestViewBase::CreateNewGuestWindow( | 374 content::WebContents* GuestViewBase::CreateNewGuestWindow( |
372 const content::WebContents::CreateParams& create_params) { | 375 const content::WebContents::CreateParams& create_params) { |
373 auto guest_manager = | 376 auto guest_manager = GuestViewManager::FromBrowserContext(browser_context()); |
374 GuestViewManager::FromBrowserContextIfAvailable(browser_context()); | |
375 return guest_manager->CreateGuestWithWebContentsParams( | 377 return guest_manager->CreateGuestWithWebContentsParams( |
376 GetViewType(), | 378 GetViewType(), |
377 owner_web_contents(), | 379 owner_web_contents(), |
378 create_params); | 380 create_params); |
379 } | 381 } |
380 | 382 |
381 void GuestViewBase::DidAttach(int guest_proxy_routing_id) { | 383 void GuestViewBase::DidAttach(int guest_proxy_routing_id) { |
382 DCHECK(guest_proxy_routing_id_ == MSG_ROUTING_NONE || | 384 DCHECK(guest_proxy_routing_id_ == MSG_ROUTING_NONE || |
383 guest_proxy_routing_id == guest_proxy_routing_id_); | 385 guest_proxy_routing_id == guest_proxy_routing_id_); |
384 guest_proxy_routing_id_ = guest_proxy_routing_id; | 386 guest_proxy_routing_id_ = guest_proxy_routing_id; |
385 | 387 |
386 opener_lifetime_observer_.reset(); | 388 opener_lifetime_observer_.reset(); |
387 | 389 |
388 SetUpSizing(*attach_params()); | 390 SetUpSizing(*attach_params()); |
389 | 391 |
390 // Give the derived class an opportunity to perform some actions. | 392 // Give the derived class an opportunity to perform some actions. |
391 DidAttachToEmbedder(); | 393 DidAttachToEmbedder(); |
392 | 394 |
393 // Inform the associated GuestViewContainer that the contentWindow is ready. | 395 // Inform the associated GuestViewContainer that the contentWindow is ready. |
394 embedder_web_contents()->Send(new GuestViewMsg_GuestAttached( | 396 embedder_web_contents()->Send(new GuestViewMsg_GuestAttached( |
395 element_instance_id_, | 397 element_instance_id_, |
396 guest_proxy_routing_id)); | 398 guest_proxy_routing_id)); |
397 | 399 |
398 SendQueuedEvents(); | 400 SendQueuedEvents(); |
399 } | 401 } |
400 | 402 |
401 void GuestViewBase::DidDetach() { | 403 void GuestViewBase::DidDetach() { |
402 GuestViewManager::FromBrowserContextIfAvailable(browser_context_)-> | 404 GuestViewManager::FromBrowserContext(browser_context_)->DetachGuest(this); |
403 DetachGuest(this); | |
404 StopTrackingEmbedderZoomLevel(); | 405 StopTrackingEmbedderZoomLevel(); |
405 owner_web_contents()->Send(new GuestViewMsg_GuestDetached( | 406 owner_web_contents()->Send(new GuestViewMsg_GuestDetached( |
406 element_instance_id_)); | 407 element_instance_id_)); |
407 element_instance_id_ = guestview::kInstanceIDNone; | 408 element_instance_id_ = guestview::kInstanceIDNone; |
408 } | 409 } |
409 | 410 |
410 WebContents* GuestViewBase::GetOwnerWebContents() const { | 411 WebContents* GuestViewBase::GetOwnerWebContents() const { |
411 return owner_web_contents_; | 412 return owner_web_contents_; |
412 } | 413 } |
413 | 414 |
(...skipping 29 matching lines...) Expand all Loading... |
443 // Invalidate weak pointers now so that bound callbacks cannot be called late | 444 // Invalidate weak pointers now so that bound callbacks cannot be called late |
444 // into destruction. We must call this after WillDestroy because derived types | 445 // into destruction. We must call this after WillDestroy because derived types |
445 // may wish to access their openers. | 446 // may wish to access their openers. |
446 weak_ptr_factory_.InvalidateWeakPtrs(); | 447 weak_ptr_factory_.InvalidateWeakPtrs(); |
447 | 448 |
448 // Give the content module an opportunity to perform some cleanup. | 449 // Give the content module an opportunity to perform some cleanup. |
449 guest_host_->WillDestroy(); | 450 guest_host_->WillDestroy(); |
450 guest_host_ = nullptr; | 451 guest_host_ = nullptr; |
451 | 452 |
452 webcontents_guestview_map.Get().erase(web_contents()); | 453 webcontents_guestview_map.Get().erase(web_contents()); |
453 GuestViewManager::FromBrowserContextIfAvailable(browser_context_)-> | 454 GuestViewManager::FromBrowserContext(browser_context_)-> |
454 RemoveGuest(guest_instance_id_); | 455 RemoveGuest(guest_instance_id_); |
455 pending_events_.clear(); | 456 pending_events_.clear(); |
456 | 457 |
457 delete web_contents(); | 458 delete web_contents(); |
458 } | 459 } |
459 | 460 |
460 void GuestViewBase::SetAttachParams(const base::DictionaryValue& params) { | 461 void GuestViewBase::SetAttachParams(const base::DictionaryValue& params) { |
461 attach_params_.reset(params.DeepCopy()); | 462 attach_params_.reset(params.DeepCopy()); |
462 attach_params_->GetInteger(guestview::kParameterInstanceId, | 463 attach_params_->GetInteger(guestview::kParameterInstanceId, |
463 &view_instance_id_); | 464 &view_instance_id_); |
(...skipping 17 matching lines...) Expand all Loading... |
481 void GuestViewBase::WillAttach(content::WebContents* embedder_web_contents, | 482 void GuestViewBase::WillAttach(content::WebContents* embedder_web_contents, |
482 int element_instance_id, | 483 int element_instance_id, |
483 bool is_full_page_plugin) { | 484 bool is_full_page_plugin) { |
484 if (owner_web_contents_ != embedder_web_contents) { | 485 if (owner_web_contents_ != embedder_web_contents) { |
485 DCHECK_EQ(owner_contents_observer_->web_contents(), owner_web_contents_); | 486 DCHECK_EQ(owner_contents_observer_->web_contents(), owner_web_contents_); |
486 // Stop tracking the old embedder's zoom level. | 487 // Stop tracking the old embedder's zoom level. |
487 StopTrackingEmbedderZoomLevel(); | 488 StopTrackingEmbedderZoomLevel(); |
488 owner_web_contents_ = embedder_web_contents; | 489 owner_web_contents_ = embedder_web_contents; |
489 owner_contents_observer_.reset( | 490 owner_contents_observer_.reset( |
490 new OwnerContentsObserver(this, embedder_web_contents)); | 491 new OwnerContentsObserver(this, embedder_web_contents)); |
| 492 owner_host_ = GuestViewManager::FromBrowserContext(browser_context_)-> |
| 493 IsOwnedByExtension(this) ? |
| 494 owner_web_contents()->GetLastCommittedURL().host() : std::string(); |
491 } | 495 } |
492 | 496 |
493 // Start tracking the new embedder's zoom level. | 497 // Start tracking the new embedder's zoom level. |
494 StartTrackingEmbedderZoomLevel(); | 498 StartTrackingEmbedderZoomLevel(); |
495 element_instance_id_ = element_instance_id; | 499 element_instance_id_ = element_instance_id; |
496 is_full_page_plugin_ = is_full_page_plugin; | 500 is_full_page_plugin_ = is_full_page_plugin; |
497 | 501 |
498 WillAttachToEmbedder(); | 502 WillAttachToEmbedder(); |
499 } | 503 } |
500 | 504 |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
807 | 811 |
808 auto embedder_zoom_controller = | 812 auto embedder_zoom_controller = |
809 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); | 813 ui_zoom::ZoomController::FromWebContents(owner_web_contents()); |
810 // Chrome Apps do not have a ZoomController. | 814 // Chrome Apps do not have a ZoomController. |
811 if (!embedder_zoom_controller) | 815 if (!embedder_zoom_controller) |
812 return; | 816 return; |
813 embedder_zoom_controller->RemoveObserver(this); | 817 embedder_zoom_controller->RemoveObserver(this); |
814 } | 818 } |
815 | 819 |
816 } // namespace extensions | 820 } // namespace extensions |
OLD | NEW |