OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 | 11 |
12 class GURL; | 12 class GURL; |
13 class Profile; | |
14 class SiteInstance; | 13 class SiteInstance; |
15 | 14 |
15 namespace content { | |
16 class BrowserContext; | |
17 } | |
18 | |
16 /////////////////////////////////////////////////////////////////////////////// | 19 /////////////////////////////////////////////////////////////////////////////// |
17 // | 20 // |
18 // BrowsingInstance class | 21 // BrowsingInstance class |
19 // | 22 // |
20 // A browsing instance corresponds to the notion of a "unit of related browsing | 23 // A browsing instance corresponds to the notion of a "unit of related browsing |
21 // contexts" in the HTML 5 spec. Intuitively, it represents a collection of | 24 // contexts" in the HTML 5 spec. Intuitively, it represents a collection of |
22 // tabs and frames that can have script connections to each other. In that | 25 // tabs and frames that can have script connections to each other. In that |
23 // sense, it reflects the user interface, and not the contents of the tabs and | 26 // sense, it reflects the user interface, and not the contents of the tabs and |
24 // frames. | 27 // frames. |
25 // | 28 // |
26 // We further subdivide a BrowsingInstance into SiteInstances, which represent | 29 // We further subdivide a BrowsingInstance into SiteInstances, which represent |
27 // the documents within each BrowsingInstance that are from the same site and | 30 // the documents within each BrowsingInstance that are from the same site and |
28 // thus can have script access to each other. Different SiteInstances can | 31 // thus can have script access to each other. Different SiteInstances can |
29 // safely run in different processes, because their documents cannot access | 32 // safely run in different processes, because their documents cannot access |
30 // each other's contents (due to the same origin policy). | 33 // each other's contents (due to the same origin policy). |
31 // | 34 // |
32 // It is important to only have one SiteInstance per site within a given | 35 // It is important to only have one SiteInstance per site within a given |
33 // BrowsingInstance. This is because any two documents from the same site | 36 // BrowsingInstance. This is because any two documents from the same site |
34 // might be able to script each other if they are in the same BrowsingInstance. | 37 // might be able to script each other if they are in the same BrowsingInstance. |
35 // Thus, they must be rendered in the same process. | 38 // Thus, they must be rendered in the same process. |
36 // | 39 // |
37 // If the process-per-site model is in use, then we ensure that there is only | 40 // If the process-per-site model is in use, then we ensure that there is only |
38 // one SiteInstance per site for the entire profile, not just for each | 41 // one SiteInstance per site for the entire context, not just for each |
39 // BrowsingInstance. This reduces the number of renderer processes we create. | 42 // BrowsingInstance. This reduces the number of renderer processes we create. |
40 // (This is currently only true if --process-per-site is specified at the | 43 // (This is currently only true if --process-per-site is specified at the |
41 // command line.) | 44 // command line.) |
42 // | 45 // |
43 // A BrowsingInstance is live as long as any SiteInstance has a reference to | 46 // A BrowsingInstance is live as long as any SiteInstance has a reference to |
44 // it. A SiteInstance is live as long as any NavigationEntry or RenderViewHost | 47 // it. A SiteInstance is live as long as any NavigationEntry or RenderViewHost |
45 // have references to it. Because both classes are RefCounted, they do not | 48 // have references to it. Because both classes are RefCounted, they do not |
46 // need to be manually deleted. | 49 // need to be manually deleted. |
47 // | 50 // |
48 // Currently, the BrowsingInstance class is not visible outside of the | 51 // Currently, the BrowsingInstance class is not visible outside of the |
49 // SiteInstance class. To get a new SiteInstance that is part of the same | 52 // SiteInstance class. To get a new SiteInstance that is part of the same |
50 // BrowsingInstance, use SiteInstance::GetRelatedSiteInstance. Because of | 53 // BrowsingInstance, use SiteInstance::GetRelatedSiteInstance. Because of |
51 // this, BrowsingInstances and SiteInstances are tested together in | 54 // this, BrowsingInstances and SiteInstances are tested together in |
52 // site_instance_unittest.cc. | 55 // site_instance_unittest.cc. |
53 // | 56 // |
54 /////////////////////////////////////////////////////////////////////////////// | 57 /////////////////////////////////////////////////////////////////////////////// |
55 class BrowsingInstance : public base::RefCounted<BrowsingInstance> { | 58 class BrowsingInstance : public base::RefCounted<BrowsingInstance> { |
56 public: | 59 public: |
57 // Create a new BrowsingInstance. | 60 // Create a new BrowsingInstance. |
58 explicit BrowsingInstance(Profile* profile); | 61 explicit BrowsingInstance(content::BrowserContext* context); |
59 | 62 |
60 // Returns whether the process-per-site model is in use (globally or just for | 63 // Returns whether the process-per-site model is in use (globally or just for |
61 // the given url), in which case we should ensure there is only one | 64 // the given url), in which case we should ensure there is only one |
62 // SiteInstance per site for the entire profile, not just for this | 65 // SiteInstance per site for the entire context, not just for this |
63 // BrowsingInstance. | 66 // BrowsingInstance. |
64 virtual bool ShouldUseProcessPerSite(const GURL& url); | 67 virtual bool ShouldUseProcessPerSite(const GURL& url); |
65 | 68 |
66 // Get the profile to which this BrowsingInstance belongs. | 69 // Get the context to which this BrowsingInstance belongs. |
67 Profile* profile() { return profile_; } | 70 content::BrowserContext* context() { return context_; } |
jam
2011/07/22 16:58:38
nit: browser_context in all this file
| |
68 | 71 |
69 // Returns whether this BrowsingInstance has registered a SiteInstance for | 72 // Returns whether this BrowsingInstance has registered a SiteInstance for |
70 // the site of the given URL. | 73 // the site of the given URL. |
71 bool HasSiteInstance(const GURL& url); | 74 bool HasSiteInstance(const GURL& url); |
72 | 75 |
73 // Get the SiteInstance responsible for rendering the given URL. Should | 76 // Get the SiteInstance responsible for rendering the given URL. Should |
74 // create a new one if necessary, but should not create more than one | 77 // create a new one if necessary, but should not create more than one |
75 // SiteInstance per site. | 78 // SiteInstance per site. |
76 SiteInstance* GetSiteInstanceForURL(const GURL& url); | 79 SiteInstance* GetSiteInstanceForURL(const GURL& url); |
77 | 80 |
(...skipping 10 matching lines...) Expand all Loading... | |
88 protected: | 91 protected: |
89 friend class base::RefCounted<BrowsingInstance>; | 92 friend class base::RefCounted<BrowsingInstance>; |
90 | 93 |
91 // Virtual to allow tests to extend it. | 94 // Virtual to allow tests to extend it. |
92 virtual ~BrowsingInstance(); | 95 virtual ~BrowsingInstance(); |
93 | 96 |
94 private: | 97 private: |
95 // Map of site to SiteInstance, to ensure we only have one SiteInstance per | 98 // Map of site to SiteInstance, to ensure we only have one SiteInstance per |
96 typedef base::hash_map<std::string, SiteInstance*> SiteInstanceMap; | 99 typedef base::hash_map<std::string, SiteInstance*> SiteInstanceMap; |
97 | 100 |
98 // Map of Profile to SiteInstanceMap, for use in the process-per-site model. | 101 // Map of BrowserContext to SiteInstanceMap, for use in the process-per-site |
99 // process-per-site model. | 102 // model. |
100 typedef base::hash_map<Profile*, SiteInstanceMap> ProfileSiteInstanceMap; | 103 typedef base::hash_map<content::BrowserContext*, SiteInstanceMap> |
104 ContextSiteInstanceMap; | |
101 | 105 |
102 // Returns a pointer to the relevant SiteInstanceMap for this object. If the | 106 // Returns a pointer to the relevant SiteInstanceMap for this object. If the |
103 // process-per-site model is in use, or if process-per-site-instance is in | 107 // process-per-site model is in use, or if process-per-site-instance is in |
104 // use and |url| matches a site for which we always use one process (e.g., | 108 // use and |url| matches a site for which we always use one process (e.g., |
105 // the new tab page), then this returns the SiteInstanceMap for the entire | 109 // the new tab page), then this returns the SiteInstanceMap for the entire |
106 // profile. If not, this returns the BrowsingInstance's own private | 110 // context. If not, this returns the BrowsingInstance's own private |
107 // SiteInstanceMap. | 111 // SiteInstanceMap. |
108 SiteInstanceMap* GetSiteInstanceMap(Profile* profile, const GURL& url); | 112 SiteInstanceMap* GetSiteInstanceMap(content::BrowserContext* context, |
113 const GURL& url); | |
109 | 114 |
110 // Utility routine which removes the passed SiteInstance from the passed | 115 // Utility routine which removes the passed SiteInstance from the passed |
111 // SiteInstanceMap. | 116 // SiteInstanceMap. |
112 bool RemoveSiteInstanceFromMap(SiteInstanceMap* map, const std::string& site, | 117 bool RemoveSiteInstanceFromMap(SiteInstanceMap* map, const std::string& site, |
113 SiteInstance* site_instance); | 118 SiteInstance* site_instance); |
114 | 119 |
115 // Common profile to which all SiteInstances in this BrowsingInstance | 120 // Common context to which all SiteInstances in this BrowsingInstance |
116 // must belong. | 121 // must belong. |
117 Profile* const profile_; | 122 content::BrowserContext* const context_; |
118 | 123 |
119 // Map of site to SiteInstance, to ensure we only have one SiteInstance per | 124 // Map of site to SiteInstance, to ensure we only have one SiteInstance per |
120 // site. The site string should be the possibly_invalid_spec() of a GURL | 125 // site. The site string should be the possibly_invalid_spec() of a GURL |
121 // obtained with SiteInstance::GetSiteForURL. Note that this map may not | 126 // obtained with SiteInstance::GetSiteForURL. Note that this map may not |
122 // contain every active SiteInstance, because a race exists where two | 127 // contain every active SiteInstance, because a race exists where two |
123 // SiteInstances can be assigned to the same site. This is ok in rare cases. | 128 // SiteInstances can be assigned to the same site. This is ok in rare cases. |
124 // This field is only used if we are not using process-per-site. | 129 // This field is only used if we are not using process-per-site. |
125 SiteInstanceMap site_instance_map_; | 130 SiteInstanceMap site_instance_map_; |
126 | 131 |
127 // Global map of Profile to SiteInstanceMap, for process-per-site. | 132 // Global map of BrowserContext to SiteInstanceMap, for process-per-site. |
128 static ProfileSiteInstanceMap profile_site_instance_map_; | 133 static ContextSiteInstanceMap context_site_instance_map_; |
129 | 134 |
130 DISALLOW_COPY_AND_ASSIGN(BrowsingInstance); | 135 DISALLOW_COPY_AND_ASSIGN(BrowsingInstance); |
131 }; | 136 }; |
132 | 137 |
133 #endif // CONTENT_BROWSER_BROWSING_INSTANCE_H_ | 138 #endif // CONTENT_BROWSER_BROWSING_INSTANCE_H_ |
OLD | NEW |