| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/browser/tab_contents/site_instance.h" | 5 #include "chrome/browser/tab_contents/site_instance.h" |
| 6 | 6 |
| 7 #include "chrome/browser/renderer_host/browser_render_process_host.h" | 7 #include "chrome/browser/renderer_host/browser_render_process_host.h" |
| 8 #include "chrome/common/url_constants.h" | 8 #include "chrome/common/url_constants.h" |
| 9 #include "chrome/common/notification_service.h" |
| 9 #include "net/base/registry_controlled_domain.h" | 10 #include "net/base/registry_controlled_domain.h" |
| 10 | 11 |
| 12 SiteInstance::SiteInstance(BrowsingInstance* browsing_instance) |
| 13 : browsing_instance_(browsing_instance), |
| 14 render_process_host_factory_(NULL), |
| 15 process_(NULL), |
| 16 max_page_id_(-1), |
| 17 has_site_(false) { |
| 18 DCHECK(browsing_instance); |
| 19 |
| 20 NotificationService::current()->AddObserver(this, |
| 21 NotificationType::RENDERER_PROCESS_TERMINATED, |
| 22 NotificationService::AllSources()); |
| 23 } |
| 24 |
| 11 SiteInstance::~SiteInstance() { | 25 SiteInstance::~SiteInstance() { |
| 12 // Now that no one is referencing us, we can safely remove ourselves from | 26 // Now that no one is referencing us, we can safely remove ourselves from |
| 13 // the BrowsingInstance. Any future visits to a page from this site | 27 // the BrowsingInstance. Any future visits to a page from this site |
| 14 // (within the same BrowsingInstance) can safely create a new SiteInstance. | 28 // (within the same BrowsingInstance) can safely create a new SiteInstance. |
| 15 if (has_site_) | 29 if (has_site_) |
| 16 browsing_instance_->UnregisterSiteInstance(this); | 30 browsing_instance_->UnregisterSiteInstance(this); |
| 31 |
| 32 NotificationService::current()->RemoveObserver(this, |
| 33 NotificationType::RENDERER_PROCESS_TERMINATED, |
| 34 NotificationService::AllSources()); |
| 17 } | 35 } |
| 18 | 36 |
| 19 RenderProcessHost* SiteInstance::GetProcess() { | 37 RenderProcessHost* SiteInstance::GetProcess() { |
| 20 RenderProcessHost* process = NULL; | |
| 21 if (process_host_id_ != -1) | |
| 22 process = RenderProcessHost::FromID(process_host_id_); | |
| 23 | |
| 24 // Create a new process if ours went away or was reused. | 38 // Create a new process if ours went away or was reused. |
| 25 if (!process) { | 39 if (!process_) { |
| 26 // See if we should reuse an old process | 40 // See if we should reuse an old process |
| 27 if (RenderProcessHost::ShouldTryToUseExistingProcessHost()) | 41 if (RenderProcessHost::ShouldTryToUseExistingProcessHost()) |
| 28 process = RenderProcessHost::GetExistingProcessHost( | 42 process_ = RenderProcessHost::GetExistingProcessHost( |
| 29 browsing_instance_->profile()); | 43 browsing_instance_->profile()); |
| 30 | 44 |
| 31 // Otherwise (or if that fails), create a new one. | 45 // Otherwise (or if that fails), create a new one. |
| 32 if (!process) { | 46 if (!process_) { |
| 33 if (render_process_host_factory_) { | 47 if (render_process_host_factory_) { |
| 34 process = render_process_host_factory_->CreateRenderProcessHost( | 48 process_ = render_process_host_factory_->CreateRenderProcessHost( |
| 35 browsing_instance_->profile()); | 49 browsing_instance_->profile()); |
| 36 } else { | 50 } else { |
| 37 process = new BrowserRenderProcessHost(browsing_instance_->profile()); | 51 process_ = new BrowserRenderProcessHost(browsing_instance_->profile()); |
| 38 } | 52 } |
| 39 } | 53 } |
| 40 | 54 |
| 41 // Update our host ID, so all pages in this SiteInstance will use | 55 // Make sure the process starts at the right max_page_id |
| 42 // the correct process. | 56 process_->UpdateMaxPageID(max_page_id_); |
| 43 process_host_id_ = process->host_id(); | 57 } |
| 58 DCHECK(process_); |
| 44 | 59 |
| 45 // Make sure the process starts at the right max_page_id | 60 return process_; |
| 46 process->UpdateMaxPageID(max_page_id_); | |
| 47 } | |
| 48 DCHECK(process); | |
| 49 | |
| 50 return process; | |
| 51 } | 61 } |
| 52 | 62 |
| 53 void SiteInstance::SetSite(const GURL& url) { | 63 void SiteInstance::SetSite(const GURL& url) { |
| 54 // A SiteInstance's site should not change. | 64 // A SiteInstance's site should not change. |
| 55 // TODO(creis): When following links or script navigations, we can currently | 65 // TODO(creis): When following links or script navigations, we can currently |
| 56 // render pages from other sites in this SiteInstance. This will eventually | 66 // render pages from other sites in this SiteInstance. This will eventually |
| 57 // be fixed, but until then, we should still not set the site of a | 67 // be fixed, but until then, we should still not set the site of a |
| 58 // SiteInstance more than once. | 68 // SiteInstance more than once. |
| 59 DCHECK(!has_site_); | 69 DCHECK(!has_site_); |
| 60 | 70 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 return false; | 154 return false; |
| 145 } | 155 } |
| 146 | 156 |
| 147 // If the schemes differ, they aren't part of the same site. | 157 // If the schemes differ, they aren't part of the same site. |
| 148 if (url1.scheme() != url2.scheme()) { | 158 if (url1.scheme() != url2.scheme()) { |
| 149 return false; | 159 return false; |
| 150 } | 160 } |
| 151 | 161 |
| 152 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2); | 162 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2); |
| 153 } | 163 } |
| 164 |
| 165 void SiteInstance::Observe(NotificationType type, |
| 166 const NotificationSource& source, |
| 167 const NotificationDetails& details) { |
| 168 DCHECK(type == NotificationType::RENDERER_PROCESS_TERMINATED); |
| 169 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); |
| 170 if (rph == process_) |
| 171 process_ = NULL; |
| 172 } |
| OLD | NEW |