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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_ 6 #define CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_ 7 8 #include "content/browser/renderer_host/render_process_host_impl.h" 9 #include "content/common/content_export.h" 10 #include "content/public/browser/notification_observer.h" 11 #include "content/public/browser/notification_registrar.h" 12 #include "content/public/browser/site_instance.h" 13 #include "url/gurl.h" 14 15 namespace content { 16 class RenderProcessHostFactory; 17 18 class CONTENT_EXPORT SiteInstanceImpl : public SiteInstance, 19 public NotificationObserver { 20 public: 21 // SiteInstance interface overrides. 22 virtual int32 GetId() OVERRIDE; 23 virtual bool HasProcess() const OVERRIDE; 24 virtual RenderProcessHost* GetProcess() OVERRIDE; 25 virtual const GURL& GetSiteURL() const OVERRIDE; 26 virtual SiteInstance* GetRelatedSiteInstance(const GURL& url) OVERRIDE; 27 virtual bool IsRelatedSiteInstance(const SiteInstance* instance) OVERRIDE; 28 virtual BrowserContext* GetBrowserContext() const OVERRIDE; 29 30 // Set the web site that this SiteInstance is rendering pages for. 31 // This includes the scheme and registered domain, but not the port. If the 32 // URL does not have a valid registered domain, then the full hostname is 33 // stored. 34 void SetSite(const GURL& url); 35 bool HasSite() const; 36 37 // Returns whether there is currently a related SiteInstance (registered with 38 // BrowsingInstance) for the site of the given url. If so, we should try to 39 // avoid dedicating an unused SiteInstance to it (e.g., in a new tab). 40 bool HasRelatedSiteInstance(const GURL& url); 41 42 // Returns whether this SiteInstance has a process that is the wrong type for 43 // the given URL. If so, the browser should force a process swap when 44 // navigating to the URL. 45 bool HasWrongProcessForURL(const GURL& url); 46 47 // Increase the number of active views in this SiteInstance. This is 48 // increased when a view is created, or a currently swapped out view 49 // is swapped in. 50 void increment_active_view_count() { active_view_count_++; } 51 52 // Decrease the number of active views in this SiteInstance. This is 53 // decreased when a view is destroyed, or a currently active view is 54 // swapped out. 55 void decrement_active_view_count() { active_view_count_--; } 56 57 // Get the number of active views which belong to this 58 // SiteInstance. If there is no active view left in this 59 // SiteInstance, all view in this SiteInstance can be safely 60 // discarded to save memory. 61 size_t active_view_count() { return active_view_count_; } 62 63 // Sets the global factory used to create new RenderProcessHosts. It may be 64 // NULL, in which case the default BrowserRenderProcessHost will be created 65 // (this is the behavior if you don't call this function). The factory must 66 // be set back to NULL before it's destroyed; ownership is not transferred. 67 static void set_render_process_host_factory( 68 const RenderProcessHostFactory* rph_factory); 69 70 protected: 71 friend class BrowsingInstance; 72 friend class SiteInstance; 73 74 // Virtual to allow tests to extend it. 75 virtual ~SiteInstanceImpl(); 76 77 // Create a new SiteInstance. Protected to give access to BrowsingInstance 78 // and tests; most callers should use Create or GetRelatedSiteInstance 79 // instead. 80 explicit SiteInstanceImpl(BrowsingInstance* browsing_instance); 81 82 private: 83 // Get the effective URL for the given actual URL. 84 static GURL GetEffectiveURL(BrowserContext* browser_context, 85 const GURL& url); 86 87 // NotificationObserver implementation. 88 virtual void Observe(int type, 89 const NotificationSource& source, 90 const NotificationDetails& details) OVERRIDE; 91 92 // Used to restrict a process' origin access rights. 93 void LockToOrigin(); 94 95 // An object used to construct RenderProcessHosts. 96 static const RenderProcessHostFactory* g_render_process_host_factory_; 97 98 // The next available SiteInstance ID. 99 static int32 next_site_instance_id_; 100 101 // A unique ID for this SiteInstance. 102 int32 id_; 103 104 // The number of active views under this SiteInstance. 105 size_t active_view_count_; 106 107 NotificationRegistrar registrar_; 108 109 // BrowsingInstance to which this SiteInstance belongs. 110 scoped_refptr<BrowsingInstance> browsing_instance_; 111 112 // Factory for new RenderProcessHosts, not owned by this class. NULL indiactes 113 // that the default BrowserRenderProcessHost should be created. 114 const RenderProcessHostFactory* render_process_host_factory_; 115 116 // Current RenderProcessHost that is rendering pages for this SiteInstance. 117 // This pointer will only change once the RenderProcessHost is destructed. It 118 // will still remain the same even if the process crashes, since in that 119 // scenario the RenderProcessHost remains the same. 120 RenderProcessHost* process_; 121 122 // The web site that this SiteInstance is rendering pages for. 123 GURL site_; 124 125 // Whether SetSite has been called. 126 bool has_site_; 127 128 DISALLOW_COPY_AND_ASSIGN(SiteInstanceImpl); 129 }; 130 131 } // namespace content 132 133 #endif // CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_ 134