[email protected] | 72daaa9 | 2012-01-18 13:39:02 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 2 | // 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] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 7 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
Lukasz Anforowicz | af2f3357 | 2018-01-17 14:05:08 | [diff] [blame] | 10 | #include <string> |
| 11 | |
[email protected] | 14c1c23 | 2013-06-11 17:52:44 | [diff] [blame] | 12 | #include "base/containers/hash_tables.h" |
nick | a9f3ad7 | 2016-04-07 03:07:49 | [diff] [blame] | 13 | #include "base/gtest_prod_util.h" |
[email protected] | c085967 | 2011-11-08 07:48:23 | [diff] [blame] | 14 | #include "base/lazy_instance.h" |
[email protected] | d5072a8 | 2014-05-15 05:50:18 | [diff] [blame] | 15 | #include "base/logging.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
[email protected] | 8d128d6 | 2011-09-13 22:11:57 | [diff] [blame] | 18 | #include "content/common/content_export.h" |
[email protected] | 67f92bc3 | 2012-01-26 01:56:19 | [diff] [blame] | 19 | #include "content/public/browser/browser_context.h" |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 20 | |
| 21 | class GURL; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 22 | |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 23 | namespace content { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 24 | class SiteInstanceImpl; |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 25 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 26 | /////////////////////////////////////////////////////////////////////////////// |
| 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] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 47 | // 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] | 72daaa9 | 2012-01-18 13:39:02 | [diff] [blame] | 52 | // 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] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 57 | // site_instance_unittest.cc. |
| 58 | // |
Lukasz Anforowicz | af2f3357 | 2018-01-17 14:05:08 | [diff] [blame] | 59 | // 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] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 63 | /////////////////////////////////////////////////////////////////////////////// |
nick | a9f3ad7 | 2016-04-07 03:07:49 | [diff] [blame] | 64 | class CONTENT_EXPORT BrowsingInstance final |
[email protected] | 8d128d6 | 2011-09-13 22:11:57 | [diff] [blame] | 65 | : public base::RefCounted<BrowsingInstance> { |
nick | a9f3ad7 | 2016-04-07 03:07:49 | [diff] [blame] | 66 | 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] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 73 | // Create a new BrowsingInstance. |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 74 | explicit BrowsingInstance(BrowserContext* context); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 75 | |
nick | a9f3ad7 | 2016-04-07 03:07:49 | [diff] [blame] | 76 | ~BrowsingInstance(); |
| 77 | |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 78 | // Get the browser context to which this BrowsingInstance belongs. |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 79 | BrowserContext* browser_context() const { return browser_context_; } |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 80 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 81 | // 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. |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 88 | scoped_refptr<SiteInstanceImpl> GetSiteInstanceForURL(const GURL& url); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 89 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 90 | // Adds the given SiteInstance to our map, to ensure that we do not create |
| 91 | // another SiteInstance for the same site. |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 92 | void RegisterSiteInstance(SiteInstanceImpl* site_instance); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 93 | |
| 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. |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 98 | void UnregisterSiteInstance(SiteInstanceImpl* site_instance); |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 99 | |
[email protected] | d5072a8 | 2014-05-15 05:50:18 | [diff] [blame] | 100 | // 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] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 108 | // Map of site to SiteInstance, to ensure we only have one SiteInstance per |
[email protected] | d5072a8 | 2014-05-15 05:50:18 | [diff] [blame] | 109 | // site. |
dcheng | bccd6b8 | 2016-03-30 16:24:19 | [diff] [blame] | 110 | typedef base::hash_map<std::string, SiteInstanceImpl*> SiteInstanceMap; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 111 | |
[email protected] | 3d7474ff | 2011-07-27 17:47:37 | [diff] [blame] | 112 | // Common browser context to which all SiteInstances in this BrowsingInstance |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 113 | // must belong. |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 114 | BrowserContext* const browser_context_; |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 115 | |
| 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] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 118 | // obtained with SiteInstanceImpl::GetSiteForURL. Note that this map may not |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 119 | // 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] | d5072a8 | 2014-05-15 05:50:18 | [diff] [blame] | 121 | // It also does not contain SiteInstances which have not yet been assigned a |
| 122 | // site, such as about:blank. See NavigatorImpl::ShouldAssignSiteForURL. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 123 | SiteInstanceMap site_instance_map_; |
| 124 | |
[email protected] | d5072a8 | 2014-05-15 05:50:18 | [diff] [blame] | 125 | // Number of WebContentses currently using this BrowsingInstance. |
| 126 | size_t active_contents_count_; |
| 127 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 128 | DISALLOW_COPY_AND_ASSIGN(BrowsingInstance); |
| 129 | }; |
| 130 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 131 | } // namespace content |
| 132 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 133 | #endif // CONTENT_BROWSER_BROWSING_INSTANCE_H_ |