blob: 4beb487b9a01a846785609102490334c10ee95a8 [file] [log] [blame]
[email protected]72daaa92012-01-18 13:39:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]df8e899b2011-02-22 22:58:222// 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_BROWSING_INSTANCE_H_
6#define CONTENT_BROWSER_BROWSING_INSTANCE_H_
[email protected]df8e899b2011-02-22 22:58:227
avib7348942015-12-25 20:57:108#include <stddef.h>
9
[email protected]14c1c232013-06-11 17:52:4410#include "base/containers/hash_tables.h"
[email protected]c0859672011-11-08 07:48:2311#include "base/lazy_instance.h"
[email protected]d5072a82014-05-15 05:50:1812#include "base/logging.h"
avib7348942015-12-25 20:57:1013#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
[email protected]8d128d62011-09-13 22:11:5715#include "content/common/content_export.h"
[email protected]67f92bc32012-01-26 01:56:1916#include "content/public/browser/browser_context.h"
[email protected]df8e899b2011-02-22 22:58:2217
18class GURL;
[email protected]df8e899b2011-02-22 22:58:2219
[email protected]3d7474ff2011-07-27 17:47:3720namespace content {
[email protected]46488322012-10-30 03:22:2021class SiteInstanceImpl;
[email protected]3d7474ff2011-07-27 17:47:3722
[email protected]df8e899b2011-02-22 22:58:2223///////////////////////////////////////////////////////////////////////////////
24//
25// BrowsingInstance class
26//
27// A browsing instance corresponds to the notion of a "unit of related browsing
28// contexts" in the HTML 5 spec. Intuitively, it represents a collection of
29// tabs and frames that can have script connections to each other. In that
30// sense, it reflects the user interface, and not the contents of the tabs and
31// frames.
32//
33// We further subdivide a BrowsingInstance into SiteInstances, which represent
34// the documents within each BrowsingInstance that are from the same site and
35// thus can have script access to each other. Different SiteInstances can
36// safely run in different processes, because their documents cannot access
37// each other's contents (due to the same origin policy).
38//
39// It is important to only have one SiteInstance per site within a given
40// BrowsingInstance. This is because any two documents from the same site
41// might be able to script each other if they are in the same BrowsingInstance.
42// Thus, they must be rendered in the same process.
43//
[email protected]df8e899b2011-02-22 22:58:2244// A BrowsingInstance is live as long as any SiteInstance has a reference to
45// it. A SiteInstance is live as long as any NavigationEntry or RenderViewHost
46// have references to it. Because both classes are RefCounted, they do not
47// need to be manually deleted.
48//
[email protected]72daaa92012-01-18 13:39:0249// BrowsingInstance has no public members, as it is designed to be
50// visible only from the SiteInstance class. To get a new
51// SiteInstance that is part of the same BrowsingInstance, use
52// SiteInstance::GetRelatedSiteInstance. Because of this,
53// BrowsingInstances and SiteInstances are tested together in
[email protected]df8e899b2011-02-22 22:58:2254// site_instance_unittest.cc.
55//
56///////////////////////////////////////////////////////////////////////////////
[email protected]8d128d62011-09-13 22:11:5757class CONTENT_EXPORT BrowsingInstance
58 : public base::RefCounted<BrowsingInstance> {
[email protected]72daaa92012-01-18 13:39:0259 protected:
[email protected]df8e899b2011-02-22 22:58:2260 // Create a new BrowsingInstance.
[email protected]46488322012-10-30 03:22:2061 explicit BrowsingInstance(BrowserContext* context);
[email protected]df8e899b2011-02-22 22:58:2262
[email protected]3d7474ff2011-07-27 17:47:3763 // Get the browser context to which this BrowsingInstance belongs.
[email protected]46488322012-10-30 03:22:2064 BrowserContext* browser_context() const { return browser_context_; }
[email protected]3d7474ff2011-07-27 17:47:3765
[email protected]df8e899b2011-02-22 22:58:2266 // Returns whether this BrowsingInstance has registered a SiteInstance for
67 // the site of the given URL.
68 bool HasSiteInstance(const GURL& url);
69
70 // Get the SiteInstance responsible for rendering the given URL. Should
71 // create a new one if necessary, but should not create more than one
72 // SiteInstance per site.
dchengbccd6b82016-03-30 16:24:1973 scoped_refptr<SiteInstanceImpl> GetSiteInstanceForURL(const GURL& url);
[email protected]df8e899b2011-02-22 22:58:2274
nickd5bbd0b2016-03-31 19:52:4475 // Returns a SiteInstance that should be used for subframes when an oopif is
76 // required, but a dedicated process is not. This SiteInstance will be created
77 // if it doesn't already exist. There is at most one of these per
78 // BrowsingInstance.
79 scoped_refptr<SiteInstanceImpl> GetDefaultSubframeSiteInstance();
80
[email protected]df8e899b2011-02-22 22:58:2281 // Adds the given SiteInstance to our map, to ensure that we do not create
82 // another SiteInstance for the same site.
dchengbccd6b82016-03-30 16:24:1983 void RegisterSiteInstance(SiteInstanceImpl* site_instance);
[email protected]df8e899b2011-02-22 22:58:2284
85 // Removes the given SiteInstance from our map, after all references to it
86 // have been deleted. This means it is safe to create a new SiteInstance
87 // if the user later visits a page from this site, within this
88 // BrowsingInstance.
dchengbccd6b82016-03-30 16:24:1989 void UnregisterSiteInstance(SiteInstanceImpl* site_instance);
[email protected]df8e899b2011-02-22 22:58:2290
[email protected]d5072a82014-05-15 05:50:1891 // Tracks the number of WebContents currently in this BrowsingInstance.
92 size_t active_contents_count() const { return active_contents_count_; }
93 void increment_active_contents_count() { active_contents_count_++; }
94 void decrement_active_contents_count() {
95 DCHECK_LT(0u, active_contents_count_);
96 active_contents_count_--;
97 }
98
[email protected]b6583592012-01-25 19:52:3399 friend class SiteInstanceImpl;
[email protected]72daaa92012-01-18 13:39:02100
[email protected]df8e899b2011-02-22 22:58:22101 friend class base::RefCounted<BrowsingInstance>;
102
103 // Virtual to allow tests to extend it.
104 virtual ~BrowsingInstance();
105
106 private:
107 // Map of site to SiteInstance, to ensure we only have one SiteInstance per
[email protected]d5072a82014-05-15 05:50:18108 // site.
dchengbccd6b82016-03-30 16:24:19109 typedef base::hash_map<std::string, SiteInstanceImpl*> SiteInstanceMap;
[email protected]df8e899b2011-02-22 22:58:22110
[email protected]3d7474ff2011-07-27 17:47:37111 // Common browser context to which all SiteInstances in this BrowsingInstance
[email protected]df8e899b2011-02-22 22:58:22112 // must belong.
[email protected]46488322012-10-30 03:22:20113 BrowserContext* const browser_context_;
[email protected]df8e899b2011-02-22 22:58:22114
115 // Map of site to SiteInstance, to ensure we only have one SiteInstance per
116 // site. The site string should be the possibly_invalid_spec() of a GURL
[email protected]b6583592012-01-25 19:52:33117 // obtained with SiteInstanceImpl::GetSiteForURL. Note that this map may not
[email protected]df8e899b2011-02-22 22:58:22118 // contain every active SiteInstance, because a race exists where two
119 // SiteInstances can be assigned to the same site. This is ok in rare cases.
[email protected]d5072a82014-05-15 05:50:18120 // It also does not contain SiteInstances which have not yet been assigned a
121 // site, such as about:blank. See NavigatorImpl::ShouldAssignSiteForURL.
[email protected]df8e899b2011-02-22 22:58:22122 SiteInstanceMap site_instance_map_;
123
[email protected]d5072a82014-05-15 05:50:18124 // Number of WebContentses currently using this BrowsingInstance.
125 size_t active_contents_count_;
126
nickd5bbd0b2016-03-31 19:52:44127 SiteInstanceImpl* default_subframe_site_instance_ = nullptr;
128
[email protected]df8e899b2011-02-22 22:58:22129 DISALLOW_COPY_AND_ASSIGN(BrowsingInstance);
130};
131
[email protected]46488322012-10-30 03:22:20132} // namespace content
133
[email protected]df8e899b2011-02-22 22:58:22134#endif // CONTENT_BROWSER_BROWSING_INSTANCE_H_