Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / site_instance_impl.h
blob2c55688bc323424f5c5227157d9ceacf440ba551
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"
12 #include "url/gurl.h"
14 namespace content {
15 class BrowsingInstance;
16 class RenderProcessHostFactory;
18 class CONTENT_EXPORT SiteInstanceImpl : public SiteInstance,
19 public RenderProcessHostObserver {
20 public:
21 // SiteInstance interface overrides.
22 int32 GetId() override;
23 bool HasProcess() const override;
24 RenderProcessHost* GetProcess() override;
25 BrowserContext* GetBrowserContext() const override;
26 const GURL& GetSiteURL() const override;
27 SiteInstance* GetRelatedSiteInstance(const GURL& url) override;
28 bool IsRelatedSiteInstance(const SiteInstance* instance) override;
29 size_t GetRelatedActiveContentsCount() override;
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;
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);
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);
48 // Increase the number of active frames in this SiteInstance. This is
49 // increased when a frame is created, or a currently swapped out frame
50 // is swapped in.
51 void increment_active_frame_count() { active_frame_count_++; }
53 // Decrease the number of active frames in this SiteInstance. This is
54 // decreased when a frame is destroyed, or a currently active frame is
55 // swapped out.
56 void decrement_active_frame_count() { active_frame_count_--; }
58 // Get the number of active frames which belong to this SiteInstance. If
59 // there are no active frames left, all frames in this SiteInstance can be
60 // safely discarded.
61 size_t active_frame_count() { return active_frame_count_; }
63 // Increase the number of active WebContentses using this SiteInstance. Note
64 // that, unlike active_frame_count, this does not count pending RFHs.
65 void IncrementRelatedActiveContentsCount();
67 // Decrease the number of active WebContentses using this SiteInstance. Note
68 // that, unlike active_frame_count, this does not count pending RFHs.
69 void DecrementRelatedActiveContentsCount();
71 // Sets the global factory used to create new RenderProcessHosts. It may be
72 // NULL, in which case the default BrowserRenderProcessHost will be created
73 // (this is the behavior if you don't call this function). The factory must
74 // be set back to NULL before it's destroyed; ownership is not transferred.
75 static void set_render_process_host_factory(
76 const RenderProcessHostFactory* rph_factory);
78 // Get the effective URL for the given actual URL. This allows the
79 // ContentBrowserClient to override the SiteInstance's site for certain URLs.
80 // For example, Chrome uses this to replace hosted app URLs with extension
81 // hosts.
82 // Only public so that we can make a consistent process swap decision in
83 // RenderFrameHostManager.
84 static GURL GetEffectiveURL(BrowserContext* browser_context,
85 const GURL& url);
87 protected:
88 friend class BrowsingInstance;
89 friend class SiteInstance;
91 // Virtual to allow tests to extend it.
92 ~SiteInstanceImpl() override;
94 // Create a new SiteInstance. Protected to give access to BrowsingInstance
95 // and tests; most callers should use Create or GetRelatedSiteInstance
96 // instead.
97 explicit SiteInstanceImpl(BrowsingInstance* browsing_instance);
99 private:
100 // RenderProcessHostObserver implementation.
101 void RenderProcessHostDestroyed(RenderProcessHost* host) override;
103 // Used to restrict a process' origin access rights.
104 void LockToOrigin();
106 // An object used to construct RenderProcessHosts.
107 static const RenderProcessHostFactory* g_render_process_host_factory_;
109 // The next available SiteInstance ID.
110 static int32 next_site_instance_id_;
112 // A unique ID for this SiteInstance.
113 int32 id_;
115 // The number of active frames in this SiteInstance.
116 size_t active_frame_count_;
118 // BrowsingInstance to which this SiteInstance belongs.
119 scoped_refptr<BrowsingInstance> browsing_instance_;
121 // Current RenderProcessHost that is rendering pages for this SiteInstance.
122 // This pointer will only change once the RenderProcessHost is destructed. It
123 // will still remain the same even if the process crashes, since in that
124 // scenario the RenderProcessHost remains the same.
125 RenderProcessHost* process_;
127 // The web site that this SiteInstance is rendering pages for.
128 GURL site_;
130 // Whether SetSite has been called.
131 bool has_site_;
133 DISALLOW_COPY_AND_ASSIGN(SiteInstanceImpl);
136 } // namespace content
138 #endif // CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_