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