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.
5 #ifndef CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_
6 #define CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_
8 #include "content/browser/renderer_host/render_process_host_impl.h"
9 #include "content/common/content_export.h"
10 #include "content/public/browser/render_process_host_observer.h"
11 #include "content/public/browser/site_instance.h"
15 class RenderProcessHostFactory
;
17 class CONTENT_EXPORT SiteInstanceImpl
: public SiteInstance
,
18 public RenderProcessHostObserver
{
20 // SiteInstance interface overrides.
21 virtual int32
GetId() OVERRIDE
;
22 virtual bool HasProcess() const OVERRIDE
;
23 virtual RenderProcessHost
* GetProcess() OVERRIDE
;
24 virtual const GURL
& GetSiteURL() const OVERRIDE
;
25 virtual SiteInstance
* GetRelatedSiteInstance(const GURL
& url
) OVERRIDE
;
26 virtual bool IsRelatedSiteInstance(const SiteInstance
* instance
) OVERRIDE
;
27 virtual BrowserContext
* GetBrowserContext() const OVERRIDE
;
29 // Set the web site that this SiteInstance is rendering pages for.
30 // This includes the scheme and registered domain, but not the port. If the
31 // URL does not have a valid registered domain, then the full hostname is
33 void SetSite(const GURL
& url
);
36 // Returns whether there is currently a related SiteInstance (registered with
37 // BrowsingInstance) for the site of the given url. If so, we should try to
38 // avoid dedicating an unused SiteInstance to it (e.g., in a new tab).
39 bool HasRelatedSiteInstance(const GURL
& url
);
41 // Returns whether this SiteInstance has a process that is the wrong type for
42 // the given URL. If so, the browser should force a process swap when
43 // navigating to the URL.
44 bool HasWrongProcessForURL(const GURL
& url
);
46 // Increase the number of active views in this SiteInstance. This is
47 // increased when a view is created, or a currently swapped out view
49 void increment_active_view_count() { active_view_count_
++; }
51 // Decrease the number of active views in this SiteInstance. This is
52 // decreased when a view is destroyed, or a currently active view is
54 void decrement_active_view_count() { active_view_count_
--; }
56 // Get the number of active views which belong to this
57 // SiteInstance. If there is no active view left in this
58 // SiteInstance, all view in this SiteInstance can be safely
59 // discarded to save memory.
60 size_t active_view_count() { return active_view_count_
; }
62 // Sets the global factory used to create new RenderProcessHosts. It may be
63 // NULL, in which case the default BrowserRenderProcessHost will be created
64 // (this is the behavior if you don't call this function). The factory must
65 // be set back to NULL before it's destroyed; ownership is not transferred.
66 static void set_render_process_host_factory(
67 const RenderProcessHostFactory
* rph_factory
);
69 // Get the effective URL for the given actual URL. This allows the
70 // ContentBrowserClient to override the SiteInstance's site for certain URLs.
71 // For example, Chrome uses this to replace hosted app URLs with extension
73 // Only public so that we can make a consistent process swap decision in
74 // RenderFrameHostManager.
75 static GURL
GetEffectiveURL(BrowserContext
* browser_context
,
79 friend class BrowsingInstance
;
80 friend class SiteInstance
;
82 // Virtual to allow tests to extend it.
83 virtual ~SiteInstanceImpl();
85 // Create a new SiteInstance. Protected to give access to BrowsingInstance
86 // and tests; most callers should use Create or GetRelatedSiteInstance
88 explicit SiteInstanceImpl(BrowsingInstance
* browsing_instance
);
91 // RenderProcessHostObserver implementation.
92 virtual void RenderProcessHostDestroyed(RenderProcessHost
* host
) OVERRIDE
;
94 // Used to restrict a process' origin access rights.
97 // An object used to construct RenderProcessHosts.
98 static const RenderProcessHostFactory
* g_render_process_host_factory_
;
100 // The next available SiteInstance ID.
101 static int32 next_site_instance_id_
;
103 // A unique ID for this SiteInstance.
106 // The number of active views under this SiteInstance.
107 size_t active_view_count_
;
109 // BrowsingInstance to which this SiteInstance belongs.
110 scoped_refptr
<BrowsingInstance
> browsing_instance_
;
112 // Current RenderProcessHost that is rendering pages for this SiteInstance.
113 // This pointer will only change once the RenderProcessHost is destructed. It
114 // will still remain the same even if the process crashes, since in that
115 // scenario the RenderProcessHost remains the same.
116 RenderProcessHost
* process_
;
118 // The web site that this SiteInstance is rendering pages for.
121 // Whether SetSite has been called.
124 DISALLOW_COPY_AND_ASSIGN(SiteInstanceImpl
);
127 } // namespace content
129 #endif // CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_