blob: 118f1781b30dbd3f4346353dc6ad2370d74c5cd6 [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
Lukasz Anforowiczaf2f33572018-01-17 14:05:0810#include <string>
11
[email protected]14c1c232013-06-11 17:52:4412#include "base/containers/hash_tables.h"
nicka9f3ad72016-04-07 03:07:4913#include "base/gtest_prod_util.h"
[email protected]c0859672011-11-08 07:48:2314#include "base/lazy_instance.h"
[email protected]d5072a82014-05-15 05:50:1815#include "base/logging.h"
avib7348942015-12-25 20:57:1016#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/ref_counted.h"
[email protected]8d128d62011-09-13 22:11:5718#include "content/common/content_export.h"
[email protected]67f92bc32012-01-26 01:56:1919#include "content/public/browser/browser_context.h"
[email protected]df8e899b2011-02-22 22:58:2220
21class GURL;
[email protected]df8e899b2011-02-22 22:58:2222
[email protected]3d7474ff2011-07-27 17:47:3723namespace content {
[email protected]46488322012-10-30 03:22:2024class SiteInstanceImpl;
[email protected]3d7474ff2011-07-27 17:47:3725
[email protected]df8e899b2011-02-22 22:58:2226///////////////////////////////////////////////////////////////////////////////
27//
28// BrowsingInstance class
29//
30// A browsing instance corresponds to the notion of a "unit of related browsing
31// contexts" in the HTML 5 spec. Intuitively, it represents a collection of
32// tabs and frames that can have script connections to each other. In that
33// sense, it reflects the user interface, and not the contents of the tabs and
34// frames.
35//
36// We further subdivide a BrowsingInstance into SiteInstances, which represent
37// the documents within each BrowsingInstance that are from the same site and
38// thus can have script access to each other. Different SiteInstances can
39// safely run in different processes, because their documents cannot access
40// each other's contents (due to the same origin policy).
41//
42// It is important to only have one SiteInstance per site within a given
43// BrowsingInstance. This is because any two documents from the same site
44// might be able to script each other if they are in the same BrowsingInstance.
45// Thus, they must be rendered in the same process.
46//
[email protected]df8e899b2011-02-22 22:58:2247// A BrowsingInstance is live as long as any SiteInstance has a reference to
48// it. A SiteInstance is live as long as any NavigationEntry or RenderViewHost
49// have references to it. Because both classes are RefCounted, they do not
50// need to be manually deleted.
51//
[email protected]72daaa92012-01-18 13:39:0252// BrowsingInstance has no public members, as it is designed to be
53// visible only from the SiteInstance class. To get a new
54// SiteInstance that is part of the same BrowsingInstance, use
55// SiteInstance::GetRelatedSiteInstance. Because of this,
56// BrowsingInstances and SiteInstances are tested together in
[email protected]df8e899b2011-02-22 22:58:2257// site_instance_unittest.cc.
58//
Lukasz Anforowiczaf2f33572018-01-17 14:05:0859// Note that a browsing instance in the browser is independently tracked in
60// the renderer inside blink::Page::RelatedPages() method (in theory the browser
61// and renderer should always stay in sync).
62//
[email protected]df8e899b2011-02-22 22:58:2263///////////////////////////////////////////////////////////////////////////////
nicka9f3ad72016-04-07 03:07:4964class CONTENT_EXPORT BrowsingInstance final
[email protected]8d128d62011-09-13 22:11:5765 : public base::RefCounted<BrowsingInstance> {
nicka9f3ad72016-04-07 03:07:4966 private:
67 friend class base::RefCounted<BrowsingInstance>;
68 friend class SiteInstanceImpl;
69 FRIEND_TEST_ALL_PREFIXES(SiteInstanceTest, OneSiteInstancePerSite);
70 FRIEND_TEST_ALL_PREFIXES(SiteInstanceTest,
71 OneSiteInstancePerSiteInBrowserContext);
72
[email protected]df8e899b2011-02-22 22:58:2273 // Create a new BrowsingInstance.
[email protected]46488322012-10-30 03:22:2074 explicit BrowsingInstance(BrowserContext* context);
[email protected]df8e899b2011-02-22 22:58:2275
nicka9f3ad72016-04-07 03:07:4976 ~BrowsingInstance();
77
[email protected]3d7474ff2011-07-27 17:47:3778 // Get the browser context to which this BrowsingInstance belongs.
[email protected]46488322012-10-30 03:22:2079 BrowserContext* browser_context() const { return browser_context_; }
[email protected]3d7474ff2011-07-27 17:47:3780
[email protected]df8e899b2011-02-22 22:58:2281 // Returns whether this BrowsingInstance has registered a SiteInstance for
82 // the site of the given URL.
83 bool HasSiteInstance(const GURL& url);
84
85 // Get the SiteInstance responsible for rendering the given URL. Should
86 // create a new one if necessary, but should not create more than one
87 // SiteInstance per site.
dchengbccd6b82016-03-30 16:24:1988 scoped_refptr<SiteInstanceImpl> GetSiteInstanceForURL(const GURL& url);
[email protected]df8e899b2011-02-22 22:58:2289
[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
[email protected]df8e899b2011-02-22 22:58:22128 DISALLOW_COPY_AND_ASSIGN(BrowsingInstance);
129};
130
[email protected]46488322012-10-30 03:22:20131} // namespace content
132
[email protected]df8e899b2011-02-22 22:58:22133#endif // CONTENT_BROWSER_BROWSING_INSTANCE_H_