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 "content/public/browser/render_frame_host.h" | 9 #include "content/public/browser/render_frame_host.h" |
10 #include "content/public/browser/render_process_host.h" | 10 #include "content/public/browser/render_process_host.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 : WebContentsObserver(guest->embedder_web_contents()), | 63 : WebContentsObserver(guest->embedder_web_contents()), |
64 destroyed_(false), | 64 destroyed_(false), |
65 guest_(guest) { | 65 guest_(guest) { |
66 } | 66 } |
67 | 67 |
68 virtual ~EmbedderWebContentsObserver() { | 68 virtual ~EmbedderWebContentsObserver() { |
69 } | 69 } |
70 | 70 |
71 // WebContentsObserver implementation. | 71 // WebContentsObserver implementation. |
72 virtual void WebContentsDestroyed() OVERRIDE { | 72 virtual void WebContentsDestroyed() OVERRIDE { |
| 73 // If the embedder is destroyed then destroy the guest. |
73 Destroy(); | 74 Destroy(); |
74 } | 75 } |
75 | 76 |
76 virtual void RenderViewHostChanged( | 77 virtual void AboutToNavigateRenderView( |
77 content::RenderViewHost* old_host, | 78 content::RenderViewHost* render_view_host) OVERRIDE { |
78 content::RenderViewHost* new_host) OVERRIDE { | 79 // If the embedder navigates then destroy the guest. |
79 Destroy(); | 80 Destroy(); |
80 } | 81 } |
81 | 82 |
82 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE { | 83 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE { |
| 84 // If the embedder crashes, then destroy the guest. |
83 Destroy(); | 85 Destroy(); |
84 } | 86 } |
85 | 87 |
86 private: | 88 private: |
87 bool destroyed_; | 89 bool destroyed_; |
88 GuestViewBase* guest_; | 90 GuestViewBase* guest_; |
89 | 91 |
90 void Destroy() { | 92 void Destroy() { |
91 if (destroyed_) | 93 if (destroyed_) |
92 return; | 94 return; |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 // WebContents. | 292 // WebContents. |
291 CHECK(!attached()); | 293 CHECK(!attached()); |
292 CHECK_EQ(host->GetID(), embedder_render_process_id()); | 294 CHECK_EQ(host->GetID(), embedder_render_process_id()); |
293 | 295 |
294 // This code path may be reached if the embedder WebContents is killed for | 296 // This code path may be reached if the embedder WebContents is killed for |
295 // whatever reason immediately after a called to GuestViewInternal.createGuest | 297 // whatever reason immediately after a called to GuestViewInternal.createGuest |
296 // and before attaching the new guest to a frame. | 298 // and before attaching the new guest to a frame. |
297 Destroy(); | 299 Destroy(); |
298 } | 300 } |
299 | 301 |
300 void GuestViewBase::Destroy() { | |
301 DCHECK(web_contents()); | |
302 content::RenderProcessHost* host = | |
303 content::RenderProcessHost::FromID(embedder_render_process_id()); | |
304 if (host) | |
305 host->RemoveObserver(this); | |
306 WillDestroy(); | |
307 if (!destruction_callback_.is_null()) | |
308 destruction_callback_.Run(); | |
309 | |
310 webcontents_guestview_map.Get().erase(web_contents()); | |
311 GuestViewManager::FromBrowserContext(browser_context_)-> | |
312 RemoveGuest(guest_instance_id_); | |
313 pending_events_.clear(); | |
314 | |
315 delete web_contents(); | |
316 } | |
317 | |
318 void GuestViewBase::DidAttach(int guest_proxy_routing_id) { | 302 void GuestViewBase::DidAttach(int guest_proxy_routing_id) { |
319 // Give the derived class an opportunity to perform some actions. | 303 // Give the derived class an opportunity to perform some actions. |
320 DidAttachToEmbedder(); | 304 DidAttachToEmbedder(); |
321 | 305 |
322 // Inform the associated GuestViewContainer that the contentWindow is ready. | 306 // Inform the associated GuestViewContainer that the contentWindow is ready. |
323 embedder_web_contents()->Send(new ExtensionMsg_GuestAttached( | 307 embedder_web_contents()->Send(new ExtensionMsg_GuestAttached( |
324 embedder_web_contents()->GetMainFrame()->GetRoutingID(), | 308 embedder_web_contents()->GetMainFrame()->GetRoutingID(), |
325 element_instance_id_, | 309 element_instance_id_, |
326 guest_proxy_routing_id)); | 310 guest_proxy_routing_id)); |
327 | 311 |
328 SendQueuedEvents(); | 312 SendQueuedEvents(); |
329 } | 313 } |
330 | 314 |
331 void GuestViewBase::ElementSizeChanged(const gfx::Size& old_size, | 315 void GuestViewBase::ElementSizeChanged(const gfx::Size& old_size, |
332 const gfx::Size& new_size) { | 316 const gfx::Size& new_size) { |
333 element_size_ = new_size; | 317 element_size_ = new_size; |
334 } | 318 } |
335 | 319 |
336 void GuestViewBase::GuestSizeChanged(const gfx::Size& old_size, | 320 void GuestViewBase::GuestSizeChanged(const gfx::Size& old_size, |
337 const gfx::Size& new_size) { | 321 const gfx::Size& new_size) { |
338 if (!auto_size_enabled_) | 322 if (!auto_size_enabled_) |
339 return; | 323 return; |
340 guest_size_ = new_size; | 324 guest_size_ = new_size; |
341 GuestSizeChangedDueToAutoSize(old_size, new_size); | 325 GuestSizeChangedDueToAutoSize(old_size, new_size); |
342 } | 326 } |
343 | 327 |
| 328 void GuestViewBase::Destroy() { |
| 329 DCHECK(web_contents()); |
| 330 content::RenderProcessHost* host = |
| 331 content::RenderProcessHost::FromID(embedder_render_process_id()); |
| 332 if (host) |
| 333 host->RemoveObserver(this); |
| 334 |
| 335 // Give the derived class an opportunity to perform some cleanup. |
| 336 WillDestroy(); |
| 337 |
| 338 // Give the content module an opportunity to perform some cleanup. |
| 339 if (!destruction_callback_.is_null()) |
| 340 destruction_callback_.Run(); |
| 341 |
| 342 webcontents_guestview_map.Get().erase(web_contents()); |
| 343 GuestViewManager::FromBrowserContext(browser_context_)-> |
| 344 RemoveGuest(guest_instance_id_); |
| 345 pending_events_.clear(); |
| 346 |
| 347 delete web_contents(); |
| 348 } |
| 349 |
344 void GuestViewBase::SetAttachParams(const base::DictionaryValue& params) { | 350 void GuestViewBase::SetAttachParams(const base::DictionaryValue& params) { |
345 attach_params_.reset(params.DeepCopy()); | 351 attach_params_.reset(params.DeepCopy()); |
346 attach_params_->GetInteger(guestview::kParameterInstanceId, | 352 attach_params_->GetInteger(guestview::kParameterInstanceId, |
347 &view_instance_id_); | 353 &view_instance_id_); |
348 } | 354 } |
349 | 355 |
350 void GuestViewBase::SetOpener(GuestViewBase* guest) { | 356 void GuestViewBase::SetOpener(GuestViewBase* guest) { |
351 if (guest && guest->IsViewType(GetViewType())) { | 357 if (guest && guest->IsViewType(GetViewType())) { |
352 opener_ = guest->AsWeakPtr(); | 358 opener_ = guest->AsWeakPtr(); |
353 return; | 359 return; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 GuestReady(); | 395 GuestReady(); |
390 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); | 396 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); |
391 if (auto_size_enabled_) { | 397 if (auto_size_enabled_) { |
392 rvh->EnableAutoResize(min_auto_size_, max_auto_size_); | 398 rvh->EnableAutoResize(min_auto_size_, max_auto_size_); |
393 } else { | 399 } else { |
394 rvh->DisableAutoResize(element_size_); | 400 rvh->DisableAutoResize(element_size_); |
395 } | 401 } |
396 } | 402 } |
397 | 403 |
398 void GuestViewBase::WebContentsDestroyed() { | 404 void GuestViewBase::WebContentsDestroyed() { |
| 405 // Let the derived class know that its WebContents is in the process of |
| 406 // being destroyed. web_contents() is still valid at this point. |
| 407 // TODO(fsamuel): This allows for reentrant code into WebContents during |
| 408 // destruction. This could potentially lead to bugs. Perhaps we should get rid |
| 409 // of this? |
399 GuestDestroyed(); | 410 GuestDestroyed(); |
| 411 |
| 412 // Self-destruct. |
400 delete this; | 413 delete this; |
401 } | 414 } |
402 | 415 |
403 void GuestViewBase::ActivateContents(WebContents* web_contents) { | 416 void GuestViewBase::ActivateContents(WebContents* web_contents) { |
404 if (!attached() || !embedder_web_contents()->GetDelegate()) | 417 if (!attached() || !embedder_web_contents()->GetDelegate()) |
405 return; | 418 return; |
406 | 419 |
407 embedder_web_contents()->GetDelegate()->ActivateContents( | 420 embedder_web_contents()->GetDelegate()->ActivateContents( |
408 embedder_web_contents()); | 421 embedder_web_contents()); |
409 } | 422 } |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 | 504 |
492 // static | 505 // static |
493 void GuestViewBase::RegisterGuestViewTypes() { | 506 void GuestViewBase::RegisterGuestViewTypes() { |
494 AppViewGuest::Register(); | 507 AppViewGuest::Register(); |
495 ExtensionOptionsGuest::Register(); | 508 ExtensionOptionsGuest::Register(); |
496 MimeHandlerViewGuest::Register(); | 509 MimeHandlerViewGuest::Register(); |
497 WebViewGuest::Register(); | 510 WebViewGuest::Register(); |
498 } | 511 } |
499 | 512 |
500 } // namespace extensions | 513 } // namespace extensions |
OLD | NEW |