OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CONTENT_BROWSER_BROWSING_INSTANCE_H_ | 5 #ifndef CONTENT_BROWSER_BROWSING_INSTANCE_H_ |
6 #define CONTENT_BROWSER_BROWSING_INSTANCE_H_ | 6 #define CONTENT_BROWSER_BROWSING_INSTANCE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/hash_tables.h" | 9 #include "base/hash_tables.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 // the documents within each BrowsingInstance that are from the same site and | 33 // the documents within each BrowsingInstance that are from the same site and |
34 // thus can have script access to each other. Different SiteInstances can | 34 // thus can have script access to each other. Different SiteInstances can |
35 // safely run in different processes, because their documents cannot access | 35 // safely run in different processes, because their documents cannot access |
36 // each other's contents (due to the same origin policy). | 36 // each other's contents (due to the same origin policy). |
37 // | 37 // |
38 // It is important to only have one SiteInstance per site within a given | 38 // It is important to only have one SiteInstance per site within a given |
39 // BrowsingInstance. This is because any two documents from the same site | 39 // BrowsingInstance. This is because any two documents from the same site |
40 // might be able to script each other if they are in the same BrowsingInstance. | 40 // might be able to script each other if they are in the same BrowsingInstance. |
41 // Thus, they must be rendered in the same process. | 41 // Thus, they must be rendered in the same process. |
42 // | 42 // |
43 // If the process-per-site model is in use, then we ensure that there is only | |
44 // one SiteInstance per site for the entire browser context, not just for each | |
45 // BrowsingInstance. This reduces the number of renderer processes we create. | |
46 // (This is currently only true if --process-per-site is specified at the | |
47 // command line.) | |
48 // | |
49 // A BrowsingInstance is live as long as any SiteInstance has a reference to | 43 // A BrowsingInstance is live as long as any SiteInstance has a reference to |
50 // it. A SiteInstance is live as long as any NavigationEntry or RenderViewHost | 44 // it. A SiteInstance is live as long as any NavigationEntry or RenderViewHost |
51 // have references to it. Because both classes are RefCounted, they do not | 45 // have references to it. Because both classes are RefCounted, they do not |
52 // need to be manually deleted. | 46 // need to be manually deleted. |
53 // | 47 // |
54 // BrowsingInstance has no public members, as it is designed to be | 48 // BrowsingInstance has no public members, as it is designed to be |
55 // visible only from the SiteInstance class. To get a new | 49 // visible only from the SiteInstance class. To get a new |
56 // SiteInstance that is part of the same BrowsingInstance, use | 50 // SiteInstance that is part of the same BrowsingInstance, use |
57 // SiteInstance::GetRelatedSiteInstance. Because of this, | 51 // SiteInstance::GetRelatedSiteInstance. Because of this, |
58 // BrowsingInstances and SiteInstances are tested together in | 52 // BrowsingInstances and SiteInstances are tested together in |
59 // site_instance_unittest.cc. | 53 // site_instance_unittest.cc. |
60 // | 54 // |
61 /////////////////////////////////////////////////////////////////////////////// | 55 /////////////////////////////////////////////////////////////////////////////// |
62 class CONTENT_EXPORT BrowsingInstance | 56 class CONTENT_EXPORT BrowsingInstance |
63 : public base::RefCounted<BrowsingInstance> { | 57 : public base::RefCounted<BrowsingInstance> { |
64 protected: | 58 protected: |
65 // Create a new BrowsingInstance. | 59 // Create a new BrowsingInstance. |
66 explicit BrowsingInstance(content::BrowserContext* context); | 60 explicit BrowsingInstance(content::BrowserContext* context); |
67 | 61 |
68 // Returns whether the process-per-site model is in use (globally or just for | |
69 // the given url), in which case we should ensure there is only one | |
70 // SiteInstance per site for the entire browser context, not just for this | |
71 // BrowsingInstance. | |
72 virtual bool ShouldUseProcessPerSite(const GURL& url); | |
73 | |
74 // Get the browser context to which this BrowsingInstance belongs. | 62 // Get the browser context to which this BrowsingInstance belongs. |
75 content::BrowserContext* browser_context() const { return browser_context_; } | 63 content::BrowserContext* browser_context() const { return browser_context_; } |
76 | 64 |
77 // Returns whether this BrowsingInstance has registered a SiteInstance for | 65 // Returns whether this BrowsingInstance has registered a SiteInstance for |
78 // the site of the given URL. | 66 // the site of the given URL. |
79 bool HasSiteInstance(const GURL& url); | 67 bool HasSiteInstance(const GURL& url); |
80 | 68 |
81 // Get the SiteInstance responsible for rendering the given URL. Should | 69 // Get the SiteInstance responsible for rendering the given URL. Should |
82 // create a new one if necessary, but should not create more than one | 70 // create a new one if necessary, but should not create more than one |
83 // SiteInstance per site. | 71 // SiteInstance per site. |
(...skipping 14 matching lines...) Expand all Loading... |
98 | 86 |
99 friend class base::RefCounted<BrowsingInstance>; | 87 friend class base::RefCounted<BrowsingInstance>; |
100 | 88 |
101 // Virtual to allow tests to extend it. | 89 // Virtual to allow tests to extend it. |
102 virtual ~BrowsingInstance(); | 90 virtual ~BrowsingInstance(); |
103 | 91 |
104 private: | 92 private: |
105 // Map of site to SiteInstance, to ensure we only have one SiteInstance per | 93 // Map of site to SiteInstance, to ensure we only have one SiteInstance per |
106 typedef base::hash_map<std::string, content::SiteInstance*> SiteInstanceMap; | 94 typedef base::hash_map<std::string, content::SiteInstance*> SiteInstanceMap; |
107 | 95 |
108 // Map of BrowserContext to SiteInstanceMap, for use in the process-per-site | |
109 // model. | |
110 typedef base::hash_map<content::BrowserContext*, SiteInstanceMap> | |
111 ContextSiteInstanceMap; | |
112 | |
113 // Returns a pointer to the relevant SiteInstanceMap for this object. If the | |
114 // process-per-site model is in use, or if process-per-site-instance is in | |
115 // use and |url| matches a site for which we always use one process (e.g., | |
116 // the new tab page), then this returns the SiteInstanceMap for the entire | |
117 // browser context. If not, this returns the BrowsingInstance's own private | |
118 // SiteInstanceMap. | |
119 SiteInstanceMap* GetSiteInstanceMap(content::BrowserContext* browser_context, | |
120 const GURL& url); | |
121 | |
122 // Utility routine which removes the passed SiteInstance from the passed | |
123 // SiteInstanceMap. | |
124 bool RemoveSiteInstanceFromMap(SiteInstanceMap* map, const std::string& site, | |
125 content::SiteInstance* site_instance); | |
126 | |
127 // Common browser context to which all SiteInstances in this BrowsingInstance | 96 // Common browser context to which all SiteInstances in this BrowsingInstance |
128 // must belong. | 97 // must belong. |
129 content::BrowserContext* const browser_context_; | 98 content::BrowserContext* const browser_context_; |
130 | 99 |
131 // Map of site to SiteInstance, to ensure we only have one SiteInstance per | 100 // Map of site to SiteInstance, to ensure we only have one SiteInstance per |
132 // site. The site string should be the possibly_invalid_spec() of a GURL | 101 // site. The site string should be the possibly_invalid_spec() of a GURL |
133 // obtained with SiteInstanceImpl::GetSiteForURL. Note that this map may not | 102 // obtained with SiteInstanceImpl::GetSiteForURL. Note that this map may not |
134 // contain every active SiteInstance, because a race exists where two | 103 // contain every active SiteInstance, because a race exists where two |
135 // SiteInstances can be assigned to the same site. This is ok in rare cases. | 104 // SiteInstances can be assigned to the same site. This is ok in rare cases. |
136 // This field is only used if we are not using process-per-site. | |
137 SiteInstanceMap site_instance_map_; | 105 SiteInstanceMap site_instance_map_; |
138 | 106 |
139 // Global map of BrowserContext to SiteInstanceMap, for process-per-site. | |
140 static base::LazyInstance<ContextSiteInstanceMap>::Leaky | |
141 context_site_instance_map_; | |
142 | |
143 DISALLOW_COPY_AND_ASSIGN(BrowsingInstance); | 107 DISALLOW_COPY_AND_ASSIGN(BrowsingInstance); |
144 }; | 108 }; |
145 | 109 |
146 #endif // CONTENT_BROWSER_BROWSING_INSTANCE_H_ | 110 #endif // CONTENT_BROWSER_BROWSING_INSTANCE_H_ |
OLD | NEW |