[email protected] | 72daaa9 | 2012-01-18 13:39:02 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 5 | #include "content/browser/browsing_instance.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
7 | #include "base/command_line.h" | ||||
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 9 | #include "content/browser/site_instance_impl.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 10 | #include "content/public/browser/browser_context.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 11 | #include "content/public/browser/content_browser_client.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 12 | #include "content/public/common/content_switches.h" |
[email protected] | a1d2916 | 2011-10-14 17:14:03 | [diff] [blame] | 13 | #include "content/public/common/url_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 15 | namespace content { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 17 | BrowsingInstance::BrowsingInstance(BrowserContext* browser_context) |
[email protected] | d5072a8 | 2014-05-15 05:50:18 | [diff] [blame] | 18 | : browser_context_(browser_context), |
19 | active_contents_count_(0u) { | ||||
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 20 | } |
21 | |||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | bool BrowsingInstance::HasSiteInstance(const GURL& url) { |
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 23 | std::string site = |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 24 | SiteInstanceImpl::GetSiteForURL(browser_context_, url) |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 25 | .possibly_invalid_spec(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 27 | return site_instance_map_.find(site) != site_instance_map_.end(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | } |
29 | |||||
30 | SiteInstance* BrowsingInstance::GetSiteInstanceForURL(const GURL& url) { | ||||
[email protected] | 3a8eecb | 2010-04-22 23:56:30 | [diff] [blame] | 31 | std::string site = |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 32 | SiteInstanceImpl::GetSiteForURL(browser_context_, url) |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 33 | .possibly_invalid_spec(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 35 | SiteInstanceMap::iterator i = site_instance_map_.find(site); |
36 | if (i != site_instance_map_.end()) | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | return i->second; |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 38 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | |
40 | // No current SiteInstance for this site, so let's create one. | ||||
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 41 | SiteInstanceImpl* instance = new SiteInstanceImpl(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | |
43 | // Set the site of this new SiteInstance, which will register it with us. | ||||
44 | instance->SetSite(url); | ||||
45 | return instance; | ||||
46 | } | ||||
47 | |||||
48 | void BrowsingInstance::RegisterSiteInstance(SiteInstance* site_instance) { | ||||
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 49 | DCHECK(static_cast<SiteInstanceImpl*>(site_instance) |
50 | ->browsing_instance_.get() == | ||||
51 | this); | ||||
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 52 | DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); |
[email protected] | 77ab1731 | 2012-09-28 15:34:59 | [diff] [blame] | 53 | std::string site = site_instance->GetSiteURL().possibly_invalid_spec(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | |
55 | // Only register if we don't have a SiteInstance for this site already. | ||||
56 | // It's possible to have two SiteInstances point to the same site if two | ||||
57 | // tabs are navigated there at the same time. (We don't call SetSite or | ||||
58 | // register them until DidNavigate.) If there is a previously existing | ||||
59 | // SiteInstance for this site, we just won't register the new one. | ||||
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 60 | SiteInstanceMap::iterator i = site_instance_map_.find(site); |
61 | if (i == site_instance_map_.end()) { | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | // Not previously registered, so register it. |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 63 | site_instance_map_[site] = site_instance; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | } |
65 | } | ||||
66 | |||||
67 | void BrowsingInstance::UnregisterSiteInstance(SiteInstance* site_instance) { | ||||
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 68 | DCHECK(static_cast<SiteInstanceImpl*>(site_instance) |
69 | ->browsing_instance_.get() == | ||||
70 | this); | ||||
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 71 | DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite()); |
[email protected] | 77ab1731 | 2012-09-28 15:34:59 | [diff] [blame] | 72 | std::string site = site_instance->GetSiteURL().possibly_invalid_spec(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | |
74 | // Only unregister the SiteInstance if it is the same one that is registered | ||||
75 | // for the site. (It might have been an unregistered SiteInstance. See the | ||||
76 | // comments in RegisterSiteInstance.) | ||||
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 77 | SiteInstanceMap::iterator i = site_instance_map_.find(site); |
78 | if (i != site_instance_map_.end() && i->second == site_instance) { | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | // Matches, so erase it. |
[email protected] | 41fb79a5 | 2012-06-29 16:34:33 | [diff] [blame] | 80 | site_instance_map_.erase(i); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | } |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 83 | |
84 | BrowsingInstance::~BrowsingInstance() { | ||||
85 | // We should only be deleted when all of the SiteInstances that refer to | ||||
86 | // us are gone. | ||||
87 | DCHECK(site_instance_map_.empty()); | ||||
[email protected] | d5072a8 | 2014-05-15 05:50:18 | [diff] [blame] | 88 | DCHECK_EQ(0u, active_contents_count_); |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 89 | } |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 90 | |
91 | } // namespace content |