blob: 4b7ee6ca5222cccd368fb5e9fe526b55e89ae088 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/browsing_instance.h"
6
7#include "base/command_line.h"
[email protected]f3ec7742009-01-15 00:59:168#include "chrome/browser/tab_contents/site_instance.h"
initial.commit09911bf2008-07-26 23:55:299#include "chrome/common/chrome_switches.h"
[email protected]dcf7d352009-02-26 01:56:0210#include "chrome/common/url_constants.h"
initial.commit09911bf2008-07-26 23:55:2911
12/*static*/
13BrowsingInstance::ProfileSiteInstanceMap
14 BrowsingInstance::profile_site_instance_map_;
15
16bool BrowsingInstance::ShouldUseProcessPerSite(const GURL& url) {
17 // Returns true if we should use the process-per-site model. This will be
18 // the case if the --process-per-site switch is specified, or in
19 // process-per-site-instance for particular sites (e.g., the new tab page).
20
[email protected]bb975362009-01-21 01:00:2221 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
22 if (command_line.HasSwitch(switches::kProcessPerSite))
initial.commit09911bf2008-07-26 23:55:2923 return true;
24
[email protected]bb975362009-01-21 01:00:2225 if (!command_line.HasSwitch(switches::kProcessPerTab)) {
initial.commit09911bf2008-07-26 23:55:2926 // We are not in process-per-site or process-per-tab, so we must be in the
27 // default (process-per-site-instance). Only use the process-per-site
28 // logic for particular sites that we want to consolidate.
29 // Note that --single-process may have been specified, but that affects the
30 // process creation logic in RenderProcessHost, so we do not need to worry
31 // about it here.
[email protected]dcf7d352009-02-26 01:56:0232 if (url.SchemeIs(chrome::kChromeUIScheme))
initial.commit09911bf2008-07-26 23:55:2933 // Always consolidate instances of the new tab page (and instances of any
34 // other internal resource urls).
35 return true;
36
37 // TODO(creis): List any other special cases that we want to limit to a
38 // single process for all instances.
39 }
40
41 // In all other cases, don't use process-per-site logic.
42 return false;
43}
44
45BrowsingInstance::SiteInstanceMap* BrowsingInstance::GetSiteInstanceMap(
46 Profile* profile, const GURL& url) {
47 if (!ShouldUseProcessPerSite(url)) {
48 // Not using process-per-site, so use a map specific to this instance.
49 return &site_instance_map_;
50 }
51
52 // Otherwise, process-per-site is in use, at least for this URL. Look up the
53 // global map for this profile, creating an entry if necessary.
54 return &profile_site_instance_map_[profile];
55}
56
57bool BrowsingInstance::HasSiteInstance(const GURL& url) {
58 std::string site = SiteInstance::GetSiteForURL(url).possibly_invalid_spec();
59
60 SiteInstanceMap* map = GetSiteInstanceMap(profile_, url);
61 SiteInstanceMap::iterator i = map->find(site);
62 return (i != map->end());
63}
64
65SiteInstance* BrowsingInstance::GetSiteInstanceForURL(const GURL& url) {
66 std::string site = SiteInstance::GetSiteForURL(url).possibly_invalid_spec();
67
68 SiteInstanceMap* map = GetSiteInstanceMap(profile_, url);
69 SiteInstanceMap::iterator i = map->find(site);
70 if (i != map->end()) {
71 return i->second;
72 }
73
74 // No current SiteInstance for this site, so let's create one.
75 SiteInstance* instance = new SiteInstance(this);
76
77 // Set the site of this new SiteInstance, which will register it with us.
78 instance->SetSite(url);
79 return instance;
80}
81
82void BrowsingInstance::RegisterSiteInstance(SiteInstance* site_instance) {
83 DCHECK(site_instance->browsing_instance() == this);
84 DCHECK(site_instance->has_site());
85 std::string site = site_instance->site().possibly_invalid_spec();
86
87 // Only register if we don't have a SiteInstance for this site already.
88 // It's possible to have two SiteInstances point to the same site if two
89 // tabs are navigated there at the same time. (We don't call SetSite or
90 // register them until DidNavigate.) If there is a previously existing
91 // SiteInstance for this site, we just won't register the new one.
92 SiteInstanceMap* map = GetSiteInstanceMap(profile_, site_instance->site());
93 SiteInstanceMap::iterator i = map->find(site);
94 if (i == map->end()) {
95 // Not previously registered, so register it.
96 (*map)[site] = site_instance;
97 }
98}
99
100void BrowsingInstance::UnregisterSiteInstance(SiteInstance* site_instance) {
101 DCHECK(site_instance->browsing_instance() == this);
102 DCHECK(site_instance->has_site());
103 std::string site = site_instance->site().possibly_invalid_spec();
104
105 // Only unregister the SiteInstance if it is the same one that is registered
106 // for the site. (It might have been an unregistered SiteInstance. See the
107 // comments in RegisterSiteInstance.)
108 SiteInstanceMap* map = GetSiteInstanceMap(profile_, site_instance->site());
109 SiteInstanceMap::iterator i = map->find(site);
110 if (i != map->end() && i->second == site_instance) {
111 // Matches, so erase it.
112 map->erase(i);
113 }
114}
license.botbf09a502008-08-24 00:55:55115