| 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_RENDERER_HOST_SITE_INSTANCE_H_ | 5 #ifndef CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ | 6 #define CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "content/browser/renderer_host/render_process_host_impl.h" | 9 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 10 #include "content/common/content_export.h" | 10 #include "content/common/content_export.h" |
| 11 #include "content/public/browser/notification_observer.h" | 11 #include "content/public/browser/notification_observer.h" |
| 12 #include "content/public/browser/notification_registrar.h" | 12 #include "content/public/browser/notification_registrar.h" |
| 13 #include "content/public/browser/site_instance.h" |
| 13 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 14 | 15 |
| 15 class BrowsingInstance; | |
| 16 | |
| 17 namespace content { | 16 namespace content { |
| 18 class BrowserContext; | |
| 19 class RenderProcessHostFactory; | 17 class RenderProcessHostFactory; |
| 20 } | 18 } |
| 21 | 19 |
| 22 /////////////////////////////////////////////////////////////////////////////// | 20 class CONTENT_EXPORT SiteInstanceImpl : public content::SiteInstance, |
| 23 // | 21 public content::NotificationObserver { |
| 24 // SiteInstance class | |
| 25 // | |
| 26 // A SiteInstance is a data structure that is associated with all pages in a | |
| 27 // given instance of a web site. Here, a web site is identified by its | |
| 28 // registered domain name and scheme. An instance includes all pages | |
| 29 // that are connected (i.e., either a user or a script navigated from one | |
| 30 // to the other). We represent instances using the BrowsingInstance class. | |
| 31 // | |
| 32 // In --process-per-tab, one SiteInstance is created for each tab (i.e., in the | |
| 33 // TabContents constructor), unless the tab is created by script (i.e., in | |
| 34 // TabContents::CreateNewView). This corresponds to one process per | |
| 35 // BrowsingInstance. | |
| 36 // | |
| 37 // In process-per-site-instance (the current default process model), | |
| 38 // SiteInstances are created (1) when the user manually creates a new tab | |
| 39 // (which also creates a new BrowsingInstance), and (2) when the user navigates | |
| 40 // across site boundaries (which uses the same BrowsingInstance). If the user | |
| 41 // navigates within a site, or opens links in new tabs within a site, the same | |
| 42 // SiteInstance is used. | |
| 43 // | |
| 44 // In --process-per-site, we consolidate all SiteInstances for a given site, | |
| 45 // throughout the entire browser context. This ensures that only one process | |
| 46 // will be dedicated to each site. | |
| 47 // | |
| 48 // Each NavigationEntry for a TabContents points to the SiteInstance that | |
| 49 // rendered it. Each RenderViewHost also points to the SiteInstance that it is | |
| 50 // associated with. A SiteInstance keeps track of the number of these | |
| 51 // references and deletes itself when the count goes to zero. This means that | |
| 52 // a SiteInstance is only live as long as it is accessible, either from new | |
| 53 // tabs with no NavigationEntries or in NavigationEntries in the history. | |
| 54 // | |
| 55 /////////////////////////////////////////////////////////////////////////////// | |
| 56 class CONTENT_EXPORT SiteInstance : public base::RefCounted<SiteInstance>, | |
| 57 public content::NotificationObserver { | |
| 58 public: | 22 public: |
| 59 // Returns a unique ID for this SiteInstance. | 23 // content::SiteInstance interface overrides. |
| 60 int32 id() { return id_; } | 24 virtual int32 GetId() OVERRIDE; |
| 25 virtual bool HasProcess() const OVERRIDE; |
| 26 virtual content::RenderProcessHost* GetProcess() OVERRIDE; |
| 27 virtual const GURL& GetSite() const OVERRIDE; |
| 28 virtual SiteInstance* GetRelatedSiteInstance(const GURL& url) OVERRIDE; |
| 29 virtual content::BrowserContext* GetBrowserContext() const OVERRIDE; |
| 30 |
| 31 // Set the web site that this SiteInstance is rendering pages for. |
| 32 // This includes the scheme and registered domain, but not the port. If the |
| 33 // URL does not have a valid registered domain, then the full hostname is |
| 34 // stored. |
| 35 void SetSite(const GURL& url); |
| 36 bool HasSite() const; |
| 37 |
| 38 // Returns whether there is currently a related SiteInstance (registered with |
| 39 // BrowsingInstance) for the site of the given url. If so, we should try to |
| 40 // avoid dedicating an unused SiteInstance to it (e.g., in a new tab). |
| 41 bool HasRelatedSiteInstance(const GURL& url); |
| 42 |
| 43 // Returns whether this SiteInstance has a process that is the wrong type for |
| 44 // the given URL. If so, the browser should force a process swap when |
| 45 // navigating to the URL. |
| 46 bool HasWrongProcessForURL(const GURL& url) const; |
| 61 | 47 |
| 62 // Sets the factory used to create new RenderProcessHosts. This will also be | 48 // Sets the factory used to create new RenderProcessHosts. This will also be |
| 63 // passed on to SiteInstances spawned by this one. | 49 // passed on to SiteInstances spawned by this one. |
| 64 // | |
| 65 // The factory must outlive the SiteInstance; ownership is not transferred. It | 50 // The factory must outlive the SiteInstance; ownership is not transferred. It |
| 66 // may be NULL, in which case the default BrowserRenderProcessHost will be | 51 // may be NULL, in which case the default BrowserRenderProcessHost will be |
| 67 // created (this is the behavior if you don't call this function). | 52 // created (this is the behavior if you don't call this function). |
| 68 void set_render_process_host_factory( | 53 void set_render_process_host_factory( |
| 69 content::RenderProcessHostFactory* rph_factory) { | 54 content::RenderProcessHostFactory* rph_factory) { |
| 70 render_process_host_factory_ = rph_factory; | 55 render_process_host_factory_ = rph_factory; |
| 71 } | 56 } |
| 72 | 57 |
| 73 // Whether this SiteInstance has a running process associated with it. | |
| 74 bool HasProcess() const; | |
| 75 | |
| 76 // Returns the current process being used to render pages in this | |
| 77 // SiteInstance. If the process has crashed or otherwise gone away, then | |
| 78 // this method will create a new process and update our host ID accordingly. | |
| 79 content::RenderProcessHost* GetProcess(); | |
| 80 | |
| 81 // Set / Get the web site that this SiteInstance is rendering pages for. | |
| 82 // This includes the scheme and registered domain, but not the port. If the | |
| 83 // URL does not have a valid registered domain, then the full hostname is | |
| 84 // stored. | |
| 85 void SetSite(const GURL& url); | |
| 86 const GURL& site() const { return site_; } | |
| 87 bool has_site() const { return has_site_; } | |
| 88 | |
| 89 // Returns whether there is currently a related SiteInstance (registered with | |
| 90 // BrowsingInstance) for the site of the given url. If so, we should try to | |
| 91 // avoid dedicating an unused SiteInstance to it (e.g., in a new tab). | |
| 92 bool HasRelatedSiteInstance(const GURL& url); | |
| 93 | |
| 94 // Gets a SiteInstance for the given URL that shares the current | |
| 95 // BrowsingInstance, creating a new SiteInstance if necessary. This ensures | |
| 96 // that a BrowsingInstance only has one SiteInstance per site, so that pages | |
| 97 // in a BrowsingInstance have the ability to script each other. Callers | |
| 98 // should ensure that this SiteInstance becomes ref counted, by storing it in | |
| 99 // a scoped_refptr. (By having this method, we can hide the BrowsingInstance | |
| 100 // class from the rest of the codebase.) | |
| 101 // TODO(creis): This may be an argument to build a pass_refptr<T> class, as | |
| 102 // Darin suggests. | |
| 103 SiteInstance* GetRelatedSiteInstance(const GURL& url); | |
| 104 | |
| 105 // Returns whether this SiteInstance has a process that is the wrong type for | |
| 106 // the given URL. If so, the browser should force a process swap when | |
| 107 // navigating to the URL. | |
| 108 bool HasWrongProcessForURL(const GURL& url) const; | |
| 109 | |
| 110 // Browser context to which this SiteInstance (and all related | |
| 111 // SiteInstances) belongs. | |
| 112 content::BrowserContext* GetBrowserContext() const; | |
| 113 | |
| 114 // Factory method to create a new SiteInstance. This will create a new | |
| 115 // new BrowsingInstance, so it should only be used when creating a new tab | |
| 116 // from scratch (or similar circumstances). Callers should ensure that | |
| 117 // this SiteInstance becomes ref counted, by storing it in a scoped_refptr. | |
| 118 // | |
| 119 // The render process host factory may be NULL. See SiteInstance constructor. | |
| 120 // | |
| 121 // TODO(creis): This may be an argument to build a pass_refptr<T> class, as | |
| 122 // Darin suggests. | |
| 123 static SiteInstance* CreateSiteInstance( | |
| 124 content::BrowserContext* browser_context); | |
| 125 | |
| 126 // Factory method to get the appropriate SiteInstance for the given URL, in | |
| 127 // a new BrowsingInstance. Use this instead of CreateSiteInstance when you | |
| 128 // know the URL, since it allows special site grouping rules to be applied | |
| 129 // (for example, to group chrome-ui pages into the same instance). | |
| 130 static SiteInstance* CreateSiteInstanceForURL( | |
| 131 content::BrowserContext* browser_context, const GURL& url); | |
| 132 | |
| 133 // Returns the site for the given URL, which includes only the scheme and | 58 // Returns the site for the given URL, which includes only the scheme and |
| 134 // registered domain. Returns an empty GURL if the URL has no host. | 59 // registered domain. Returns an empty GURL if the URL has no host. |
| 135 static GURL GetSiteForURL(content::BrowserContext* context, const GURL& url); | 60 static GURL GetSiteForURL(content::BrowserContext* context, const GURL& url); |
| 136 | 61 |
| 137 // Return whether both URLs are part of the same web site, for the purpose of | |
| 138 // assigning them to processes accordingly. The decision is currently based | |
| 139 // on the registered domain of the URLs (google.com, bbc.co.uk), as well as | |
| 140 // the scheme (https, http). This ensures that two pages will be in | |
| 141 // the same process if they can communicate with other via JavaScript. | |
| 142 // (e.g., docs.google.com and mail.google.com have DOM access to each other | |
| 143 // if they both set their document.domain properties to google.com.) | |
| 144 static bool IsSameWebSite(content::BrowserContext* browser_context, | |
| 145 const GURL& url1, const GURL& url2); | |
| 146 | |
| 147 protected: | 62 protected: |
| 148 friend class base::RefCounted<SiteInstance>; | |
| 149 friend class BrowsingInstance; | 63 friend class BrowsingInstance; |
| 64 friend class content::SiteInstance; |
| 150 | 65 |
| 151 // Virtual to allow tests to extend it. | 66 // Virtual to allow tests to extend it. |
| 152 virtual ~SiteInstance(); | 67 virtual ~SiteInstanceImpl(); |
| 153 | 68 |
| 154 // Create a new SiteInstance. Protected to give access to BrowsingInstance | 69 // Create a new SiteInstance. Protected to give access to BrowsingInstance |
| 155 // and tests; most callers should use CreateSiteInstance or | 70 // and tests; most callers should use Create or GetRelatedSiteInstance |
| 156 // GetRelatedSiteInstance instead. | 71 // instead. |
| 157 explicit SiteInstance(BrowsingInstance* browsing_instance); | 72 explicit SiteInstanceImpl(BrowsingInstance* browsing_instance); |
| 158 | 73 |
| 74 private: |
| 159 // Get the effective URL for the given actual URL. | 75 // Get the effective URL for the given actual URL. |
| 160 static GURL GetEffectiveURL(content::BrowserContext* browser_context, | 76 static GURL GetEffectiveURL(content::BrowserContext* browser_context, |
| 161 const GURL& url); | 77 const GURL& url); |
| 162 | 78 |
| 163 private: | |
| 164 // content::NotificationObserver implementation. | 79 // content::NotificationObserver implementation. |
| 165 virtual void Observe(int type, | 80 virtual void Observe(int type, |
| 166 const content::NotificationSource& source, | 81 const content::NotificationSource& source, |
| 167 const content::NotificationDetails& details) OVERRIDE; | 82 const content::NotificationDetails& details) OVERRIDE; |
| 168 | 83 |
| 169 // Used to restrict a process' origin access rights. | 84 // Used to restrict a process' origin access rights. |
| 170 void LockToOrigin(); | 85 void LockToOrigin(); |
| 171 | 86 |
| 172 // The next available SiteInstance ID. | 87 // The next available SiteInstance ID. |
| 173 static int32 next_site_instance_id_; | 88 static int32 next_site_instance_id_; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 191 content::RenderProcessHost* process_; | 106 content::RenderProcessHost* process_; |
| 192 | 107 |
| 193 // The web site that this SiteInstance is rendering pages for. | 108 // The web site that this SiteInstance is rendering pages for. |
| 194 GURL site_; | 109 GURL site_; |
| 195 | 110 |
| 196 // Whether SetSite has been called. | 111 // Whether SetSite has been called. |
| 197 bool has_site_; | 112 bool has_site_; |
| 198 | 113 |
| 199 FRIEND_TEST_ALL_PREFIXES(RenderViewHostManagerTest, NewTabPageProcesses); | 114 FRIEND_TEST_ALL_PREFIXES(RenderViewHostManagerTest, NewTabPageProcesses); |
| 200 | 115 |
| 201 DISALLOW_COPY_AND_ASSIGN(SiteInstance); | 116 DISALLOW_COPY_AND_ASSIGN(SiteInstanceImpl); |
| 202 }; | 117 }; |
| 203 | 118 |
| 204 #endif // CONTENT_BROWSER_RENDERER_HOST_SITE_INSTANCE_H_ | 119 #endif // CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_ |
| OLD | NEW |