Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / content / browser / site_instance_impl.h
blob3bbaf337544a187e71b16a8f932d9fb0f800d3eb
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;
30 bool RequiresDedicatedProcess() override;
32 // Set the web site that this SiteInstance is rendering pages for.
33 // This includes the scheme and registered domain, but not the port. If the
34 // URL does not have a valid registered domain, then the full hostname is
35 // stored.
36 void SetSite(const GURL& url);
37 bool HasSite() const;
39 // Returns whether there is currently a related SiteInstance (registered with
40 // BrowsingInstance) for the site of the given url. If so, we should try to
41 // avoid dedicating an unused SiteInstance to it (e.g., in a new tab).
42 bool HasRelatedSiteInstance(const GURL& url);
44 // Returns whether this SiteInstance has a process that is the wrong type for
45 // the given URL. If so, the browser should force a process swap when
46 // navigating to the URL.
47 bool HasWrongProcessForURL(const GURL& url);
49 // Increase the number of active frames in this SiteInstance. This is
50 // increased when a frame is created, or a currently swapped out frame
51 // is swapped in.
52 void increment_active_frame_count() { active_frame_count_++; }
54 // Decrease the number of active frames in this SiteInstance. This is
55 // decreased when a frame is destroyed, or a currently active frame is
56 // swapped out.
57 void decrement_active_frame_count() { active_frame_count_--; }
59 // Get the number of active frames which belong to this SiteInstance. If
60 // there are no active frames left, all frames in this SiteInstance can be
61 // safely discarded.
62 size_t active_frame_count() { return active_frame_count_; }
64 // Increase the number of active WebContentses using this SiteInstance. Note
65 // that, unlike active_frame_count, this does not count pending RFHs.
66 void IncrementRelatedActiveContentsCount();
68 // Decrease the number of active WebContentses using this SiteInstance. Note
69 // that, unlike active_frame_count, this does not count pending RFHs.
70 void DecrementRelatedActiveContentsCount();
72 // Sets the global factory used to create new RenderProcessHosts. It may be
73 // NULL, in which case the default BrowserRenderProcessHost will be created
74 // (this is the behavior if you don't call this function). The factory must
75 // be set back to NULL before it's destroyed; ownership is not transferred.
76 static void set_render_process_host_factory(
77 const RenderProcessHostFactory* rph_factory);
79 // Get the effective URL for the given actual URL. This allows the
80 // ContentBrowserClient to override the SiteInstance's site for certain URLs.
81 // For example, Chrome uses this to replace hosted app URLs with extension
82 // hosts.
83 // Only public so that we can make a consistent process swap decision in
84 // RenderFrameHostManager.
85 static GURL GetEffectiveURL(BrowserContext* browser_context,
86 const GURL& url);
88 protected:
89 friend class BrowsingInstance;
90 friend class SiteInstance;
92 // Virtual to allow tests to extend it.
93 ~SiteInstanceImpl() override;
95 // Create a new SiteInstance. Protected to give access to BrowsingInstance
96 // and tests; most callers should use Create or GetRelatedSiteInstance
97 // instead.
98 explicit SiteInstanceImpl(BrowsingInstance* browsing_instance);
100 private:
101 // RenderProcessHostObserver implementation.
102 void RenderProcessHostDestroyed(RenderProcessHost* host) override;
104 // Used to restrict a process' origin access rights.
105 void LockToOrigin();
107 // An object used to construct RenderProcessHosts.
108 static const RenderProcessHostFactory* g_render_process_host_factory_;
110 // The next available SiteInstance ID.
111 static int32 next_site_instance_id_;
113 // A unique ID for this SiteInstance.
114 int32 id_;
116 // The number of active frames in this SiteInstance.
117 size_t active_frame_count_;
119 // BrowsingInstance to which this SiteInstance belongs.
120 scoped_refptr<BrowsingInstance> browsing_instance_;
122 // Current RenderProcessHost that is rendering pages for this SiteInstance.
123 // This pointer will only change once the RenderProcessHost is destructed. It
124 // will still remain the same even if the process crashes, since in that
125 // scenario the RenderProcessHost remains the same.
126 RenderProcessHost* process_;
128 // The web site that this SiteInstance is rendering pages for.
129 GURL site_;
131 // Whether SetSite has been called.
132 bool has_site_;
134 DISALLOW_COPY_AND_ASSIGN(SiteInstanceImpl);
137 } // namespace content
139 #endif // CONTENT_BROWSER_SITE_INSTANCE_IMPL_H_