blob: 34c1f073bbd852c260aecf8eade45656d6d10005 [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"
nicka9f3ad72016-04-07 03:07:4911#include "base/gtest_prod_util.h"
[email protected]c0859672011-11-08 07:48:2312#include "base/lazy_instance.h"
[email protected]d5072a82014-05-15 05:50:1813#include "base/logging.h"
avib7348942015-12-25 20:57:1014#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1515#include "base/memory/ref_counted.h"
[email protected]8d128d62011-09-13 22:11:5716#include "content/common/content_export.h"
[email protected]67f92bc32012-01-26 01:56:1917#include "content/public/browser/browser_context.h"
[email protected]df8e899b2011-02-22 22:58:2218
19class GURL;
[email protected]df8e899b2011-02-22 22:58:2220
[email protected]3d7474ff2011-07-27 17:47:3721namespace content {
[email protected]46488322012-10-30 03:22:2022class SiteInstanceImpl;
[email protected]3d7474ff2011-07-27 17:47:3723
[email protected]df8e899b2011-02-22 22:58:2224///////////////////////////////////////////////////////////////////////////////
25//
26// BrowsingInstance class
27//
28// A browsing instance corresponds to the notion of a "unit of related browsing
29// contexts" in the HTML 5 spec. Intuitively, it represents a collection of
30// tabs and frames that can have script connections to each other. In that
31// sense, it reflects the user interface, and not the contents of the tabs and
32// frames.
33//
34// We further subdivide a BrowsingInstance into SiteInstances, which represent
35// the documents within each BrowsingInstance that are from the same site and
36// thus can have script access to each other. Different SiteInstances can
37// safely run in different processes, because their documents cannot access
38// each other's contents (due to the same origin policy).
39//
40// It is important to only have one SiteInstance per site within a given
41// BrowsingInstance. This is because any two documents from the same site
42// might be able to script each other if they are in the same BrowsingInstance.
43// Thus, they must be rendered in the same process.
44//
[email protected]df8e899b2011-02-22 22:58:2245// A BrowsingInstance is live as long as any SiteInstance has a reference to
46// it. A SiteInstance is live as long as any NavigationEntry or RenderViewHost
47// have references to it. Because both classes are RefCounted, they do not
48// need to be manually deleted.
49//
[email protected]72daaa92012-01-18 13:39:0250// BrowsingInstance has no public members, as it is designed to be
51// visible only from the SiteInstance class. To get a new
52// SiteInstance that is part of the same BrowsingInstance, use
53// SiteInstance::GetRelatedSiteInstance. Because of this,
54// BrowsingInstances and SiteInstances are tested together in
[email protected]df8e899b2011-02-22 22:58:2255// site_instance_unittest.cc.
56//
[email protected]df8e899b2011-02-22 22:58:2257///////////////////////////////////////////////////////////////////////////////
nicka9f3ad72016-04-07 03:07:4958class CONTENT_EXPORT BrowsingInstance final
[email protected]8d128d62011-09-13 22:11:5759 : public base::RefCounted<BrowsingInstance> {
nicka9f3ad72016-04-07 03:07:4960 private:
61 friend class base::RefCounted<BrowsingInstance>;
62 friend class SiteInstanceImpl;
63 FRIEND_TEST_ALL_PREFIXES(SiteInstanceTest, OneSiteInstancePerSite);
64 FRIEND_TEST_ALL_PREFIXES(SiteInstanceTest,
65 OneSiteInstancePerSiteInBrowserContext);
66
[email protected]df8e899b2011-02-22 22:58:2267 // Create a new BrowsingInstance.
[email protected]46488322012-10-30 03:22:2068 explicit BrowsingInstance(BrowserContext* context);
[email protected]df8e899b2011-02-22 22:58:2269
nicka9f3ad72016-04-07 03:07:4970 ~BrowsingInstance();
71
[email protected]3d7474ff2011-07-27 17:47:3772 // Get the browser context to which this BrowsingInstance belongs.
[email protected]46488322012-10-30 03:22:2073 BrowserContext* browser_context() const { return browser_context_; }
[email protected]3d7474ff2011-07-27 17:47:3774
[email protected]df8e899b2011-02-22 22:58:2275 // Returns whether this BrowsingInstance has registered a SiteInstance for
76 // the site of the given URL.
77 bool HasSiteInstance(const GURL& url);
78
79 // Get the SiteInstance responsible for rendering the given URL. Should
80 // create a new one if necessary, but should not create more than one
81 // SiteInstance per site.
dchengbccd6b82016-03-30 16:24:1982 scoped_refptr<SiteInstanceImpl> GetSiteInstanceForURL(const GURL& url);
[email protected]df8e899b2011-02-22 22:58:2283
nickd5bbd0b2016-03-31 19:52:4484 // Returns a SiteInstance that should be used for subframes when an oopif is
85 // required, but a dedicated process is not. This SiteInstance will be created
86 // if it doesn't already exist. There is at most one of these per
87 // BrowsingInstance.
88 scoped_refptr<SiteInstanceImpl> GetDefaultSubframeSiteInstance();
89
[email protected]df8e899b2011-02-22 22:58:2290 // Adds the given SiteInstance to our map, to ensure that we do not create
91 // another SiteInstance for the same site.
dchengbccd6b82016-03-30 16:24:1992 void RegisterSiteInstance(SiteInstanceImpl* site_instance);
[email protected]df8e899b2011-02-22 22:58:2293
94 // Removes the given SiteInstance from our map, after all references to it
95 // have been deleted. This means it is safe to create a new SiteInstance
96 // if the user later visits a page from this site, within this
97 // BrowsingInstance.
dchengbccd6b82016-03-30 16:24:1998 void UnregisterSiteInstance(SiteInstanceImpl* site_instance);
[email protected]df8e899b2011-02-22 22:58:2299
[email protected]d5072a82014-05-15 05:50:18100 // Tracks the number of WebContents currently in this BrowsingInstance.
101 size_t active_contents_count() const { return active_contents_count_; }
102 void increment_active_contents_count() { active_contents_count_++; }
103 void decrement_active_contents_count() {
104 DCHECK_LT(0u, active_contents_count_);
105 active_contents_count_--;
106 }
107
[email protected]df8e899b2011-02-22 22:58:22108 // Map of site to SiteInstance, to ensure we only have one SiteInstance per
[email protected]d5072a82014-05-15 05:50:18109 // site.
dchengbccd6b82016-03-30 16:24:19110 typedef base::hash_map<std::string, SiteInstanceImpl*> SiteInstanceMap;
[email protected]df8e899b2011-02-22 22:58:22111
[email protected]3d7474ff2011-07-27 17:47:37112 // Common browser context to which all SiteInstances in this BrowsingInstance
[email protected]df8e899b2011-02-22 22:58:22113 // must belong.
[email protected]46488322012-10-30 03:22:20114 BrowserContext* const browser_context_;
[email protected]df8e899b2011-02-22 22:58:22115
116 // Map of site to SiteInstance, to ensure we only have one SiteInstance per
117 // site. The site string should be the possibly_invalid_spec() of a GURL
[email protected]b6583592012-01-25 19:52:33118 // obtained with SiteInstanceImpl::GetSiteForURL. Note that this map may not
[email protected]df8e899b2011-02-22 22:58:22119 // contain every active SiteInstance, because a race exists where two
120 // SiteInstances can be assigned to the same site. This is ok in rare cases.
[email protected]d5072a82014-05-15 05:50:18121 // It also does not contain SiteInstances which have not yet been assigned a
122 // site, such as about:blank. See NavigatorImpl::ShouldAssignSiteForURL.
[email protected]df8e899b2011-02-22 22:58:22123 SiteInstanceMap site_instance_map_;
124
[email protected]d5072a82014-05-15 05:50:18125 // Number of WebContentses currently using this BrowsingInstance.
126 size_t active_contents_count_;
127
nickd5bbd0b2016-03-31 19:52:44128 SiteInstanceImpl* default_subframe_site_instance_ = nullptr;
129
[email protected]df8e899b2011-02-22 22:58:22130 DISALLOW_COPY_AND_ASSIGN(BrowsingInstance);
131};
132
[email protected]46488322012-10-30 03:22:20133} // namespace content
134
[email protected]df8e899b2011-02-22 22:58:22135#endif // CONTENT_BROWSER_BROWSING_INSTANCE_H_