Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Sharon Yang | 0a9475e | 2021-10-25 21:58:33 | [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 | #include "content/browser/site_instance_group.h" |
| 6 | |
David Sanders | 6f0eeb0 | 2025-01-30 06:48:50 | [diff] [blame] | 7 | #include "base/auto_reset.h" |
David Sanders | d4bf5eb | 2022-03-17 07:12:05 | [diff] [blame] | 8 | #include "base/observer_list.h" |
Sharon Yang | 2f3304a | 2022-05-13 02:24:25 | [diff] [blame] | 9 | #include "content/browser/renderer_host/render_process_host_impl.h" |
| 10 | #include "content/browser/site_instance_impl.h" |
David Sanders | 2ee1f350 | 2022-03-16 07:21:19 | [diff] [blame] | 11 | #include "content/public/browser/render_process_host.h" |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 12 | |
Sharon Yang | 0a9475e | 2021-10-25 21:58:33 | [diff] [blame] | 13 | namespace content { |
| 14 | |
| 15 | namespace { |
| 16 | SiteInstanceGroupId::Generator site_instance_group_id_generator; |
| 17 | } // namespace |
| 18 | |
Sharon Yang | 73883d3 | 2023-03-14 00:16:45 | [diff] [blame] | 19 | SiteInstanceGroup::SiteInstanceGroup(BrowsingInstance* browsing_instance, |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 20 | RenderProcessHost* process) |
| 21 | : id_(site_instance_group_id_generator.GenerateNextId()), |
Sharon Yang | 73883d3 | 2023-03-14 00:16:45 | [diff] [blame] | 22 | browsing_instance_(browsing_instance), |
Sharon Yang | 2f3304a | 2022-05-13 02:24:25 | [diff] [blame] | 23 | process_(process->GetSafeRef()), |
| 24 | agent_scheduling_group_( |
| 25 | AgentSchedulingGroupHost::GetOrCreate(*this, *process) |
| 26 | ->GetSafeRef()) { |
| 27 | process->AddObserver(this); |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 28 | } |
Sharon Yang | 0a9475e | 2021-10-25 21:58:33 | [diff] [blame] | 29 | |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 30 | SiteInstanceGroup::~SiteInstanceGroup() { |
Sharon Yang | 485c958 | 2023-07-25 21:39:46 | [diff] [blame] | 31 | // Make sure `this` is not getting destructed while observers are still being |
| 32 | // notified. |
| 33 | CHECK(!is_notifying_observers_); |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 34 | process_->RemoveObserver(this); |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 35 | } |
Sharon Yang | 0a9475e | 2021-10-25 21:58:33 | [diff] [blame] | 36 | |
Sharon Yang | 80d37bb | 2022-03-10 02:32:11 | [diff] [blame] | 37 | SiteInstanceGroupId SiteInstanceGroup::GetId() const { |
Sharon Yang | 0a9475e | 2021-10-25 21:58:33 | [diff] [blame] | 38 | return id_; |
| 39 | } |
| 40 | |
Dave Tapuska | 80fb7252 | 2022-03-18 21:34:23 | [diff] [blame] | 41 | base::SafeRef<SiteInstanceGroup> SiteInstanceGroup::GetSafeRef() { |
| 42 | return weak_ptr_factory_.GetSafeRef(); |
| 43 | } |
| 44 | |
Sharon Yang | 1a7a6370 | 2025-05-23 01:14:55 | [diff] [blame] | 45 | base::WeakPtr<SiteInstanceGroup> SiteInstanceGroup::GetWeakPtr() { |
| 46 | return weak_ptr_factory_.GetWeakPtr(); |
| 47 | } |
| 48 | |
Arthur Sonzogni | 734cfaf6 | 2023-03-20 15:06:19 | [diff] [blame] | 49 | base::WeakPtr<SiteInstanceGroup> |
| 50 | SiteInstanceGroup::GetWeakPtrToAllowDangling() { |
| 51 | return weak_ptr_factory_.GetWeakPtr(); |
| 52 | } |
| 53 | |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 54 | void SiteInstanceGroup::AddObserver(Observer* observer) { |
| 55 | observers_.AddObserver(observer); |
| 56 | } |
| 57 | |
| 58 | void SiteInstanceGroup::RemoveObserver(Observer* observer) { |
| 59 | observers_.RemoveObserver(observer); |
| 60 | } |
| 61 | |
Sharon Yang | 2f3304a | 2022-05-13 02:24:25 | [diff] [blame] | 62 | void SiteInstanceGroup::AddSiteInstance(SiteInstanceImpl* site_instance) { |
Sharon Yang | d265b5f | 2023-07-12 18:10:16 | [diff] [blame] | 63 | CHECK(site_instance); |
| 64 | CHECK(!site_instances_.contains(site_instance)); |
Sharon Yang | 73883d3 | 2023-03-14 00:16:45 | [diff] [blame] | 65 | CHECK_EQ(browsing_instance_id(), site_instance->GetBrowsingInstanceId()); |
Sharon Yang | 2f3304a | 2022-05-13 02:24:25 | [diff] [blame] | 66 | site_instances_.insert(site_instance); |
| 67 | } |
| 68 | |
| 69 | void SiteInstanceGroup::RemoveSiteInstance(SiteInstanceImpl* site_instance) { |
| 70 | site_instances_.erase(site_instance); |
| 71 | if (site_instances_.empty()) |
| 72 | process_->Cleanup(); |
| 73 | } |
| 74 | |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 75 | void SiteInstanceGroup::IncrementActiveFrameCount() { |
| 76 | active_frame_count_++; |
| 77 | } |
| 78 | |
| 79 | void SiteInstanceGroup::DecrementActiveFrameCount() { |
| 80 | if (--active_frame_count_ == 0) { |
Sharon Yang | 485c958 | 2023-07-25 21:39:46 | [diff] [blame] | 81 | base::AutoReset<bool> scope(&is_notifying_observers_, true); |
Sharon Yang | 417a5df | 2024-04-23 17:57:15 | [diff] [blame] | 82 | for (auto& observer : observers_) { |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 83 | observer.ActiveFrameCountIsZero(this); |
Sharon Yang | 417a5df | 2024-04-23 17:57:15 | [diff] [blame] | 84 | } |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Sharon Yang | 417a5df | 2024-04-23 17:57:15 | [diff] [blame] | 88 | void SiteInstanceGroup::IncrementKeepAliveCount() { |
| 89 | keep_alive_count_++; |
Alex Moshchuk | b7f0821 | 2024-09-11 22:57:35 | [diff] [blame] | 90 | auto* rphi = static_cast<RenderProcessHostImpl*>(process()); |
| 91 | if (!rphi->AreRefCountsDisabled()) { |
| 92 | rphi->IncrementNavigationStateKeepAliveCount(); |
| 93 | } |
Sharon Yang | 417a5df | 2024-04-23 17:57:15 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void SiteInstanceGroup::DecrementKeepAliveCount() { |
| 97 | if (--keep_alive_count_ == 0) { |
| 98 | base::AutoReset<bool> scope(&is_notifying_observers_, true); |
| 99 | for (auto& observer : observers_) { |
| 100 | observer.KeepAliveCountIsZero(this); |
| 101 | } |
| 102 | } |
Alex Moshchuk | b7f0821 | 2024-09-11 22:57:35 | [diff] [blame] | 103 | auto* rphi = static_cast<RenderProcessHostImpl*>(process()); |
| 104 | if (!rphi->AreRefCountsDisabled()) { |
| 105 | rphi->DecrementNavigationStateKeepAliveCount(); |
| 106 | } |
Sharon Yang | 417a5df | 2024-04-23 17:57:15 | [diff] [blame] | 107 | } |
| 108 | |
Sharon Yang | f67d1d7 | 2023-02-01 23:39:48 | [diff] [blame] | 109 | bool SiteInstanceGroup::IsRelatedSiteInstanceGroup(SiteInstanceGroup* group) { |
| 110 | return browsing_instance_id() == group->browsing_instance_id(); |
| 111 | } |
| 112 | |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 113 | void SiteInstanceGroup::RenderProcessHostDestroyed(RenderProcessHost* host) { |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 114 | DCHECK_EQ(process_->GetDeprecatedID(), host->GetDeprecatedID()); |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 115 | process_->RemoveObserver(this); |
Sharon Yang | 2f3304a | 2022-05-13 02:24:25 | [diff] [blame] | 116 | |
| 117 | // Remove references to `this` from all SiteInstances in this group. That will |
| 118 | // cause `this` to be destructed, to enforce the invariant that a |
| 119 | // SiteInstanceGroup must have a RenderProcessHost. |
Sharon Yang | e3efbf2 | 2023-07-11 17:06:47 | [diff] [blame] | 120 | for (auto instance : site_instances_) { |
Sharon Yang | 2f3304a | 2022-05-13 02:24:25 | [diff] [blame] | 121 | instance->ResetSiteInstanceGroup(); |
Sharon Yang | e3efbf2 | 2023-07-11 17:06:47 | [diff] [blame] | 122 | } |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void SiteInstanceGroup::RenderProcessExited( |
| 126 | RenderProcessHost* host, |
| 127 | const ChildProcessTerminationInfo& info) { |
Sharon Yang | 0618f75 | 2023-08-11 00:04:52 | [diff] [blame] | 128 | // Increment the refcount of `this` to keep it alive while iterating over the |
| 129 | // observer list. That will prevent `this` from getting deleted during |
| 130 | // iteration. |
| 131 | scoped_refptr<SiteInstanceGroup> self_refcount = base::WrapRefCounted(this); |
| 132 | base::AutoReset<bool> scope(&is_notifying_observers_, true); |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 133 | for (auto& observer : observers_) |
| 134 | observer.RenderProcessGone(this, info); |
| 135 | } |
| 136 | |
Sharon Yang | 1f99c1f | 2023-11-28 00:20:37 | [diff] [blame] | 137 | const StoragePartitionConfig& SiteInstanceGroup::GetStoragePartitionConfig() |
| 138 | const { |
| 139 | return process()->GetStoragePartition()->GetConfig(); |
| 140 | } |
| 141 | |
Sharon Yang | 73883d3 | 2023-03-14 00:16:45 | [diff] [blame] | 142 | // static |
| 143 | SiteInstanceGroup* SiteInstanceGroup::CreateForTesting( |
| 144 | BrowserContext* browser_context, |
| 145 | RenderProcessHost* process) { |
| 146 | return new SiteInstanceGroup( |
| 147 | new BrowsingInstance(browser_context, |
| 148 | WebExposedIsolationInfo::CreateNonIsolated(), |
| 149 | /*is_guest=*/false, |
Jason Lin | 21873705 | 2023-11-21 08:11:39 | [diff] [blame] | 150 | /*is_fenced=*/false, |
Camille Lamy | c935192 | 2025-05-01 02:57:44 | [diff] [blame] | 151 | /*is_fixed_storage_partition=*/false), |
Sharon Yang | 73883d3 | 2023-03-14 00:16:45 | [diff] [blame] | 152 | process); |
| 153 | } |
| 154 | |
| 155 | // static |
| 156 | SiteInstanceGroup* SiteInstanceGroup::CreateForTesting( |
| 157 | SiteInstanceGroup* group, |
| 158 | RenderProcessHost* process) { |
| 159 | return new SiteInstanceGroup( |
| 160 | group->browsing_instance_for_testing(), // IN-TEST |
| 161 | process); |
| 162 | } |
| 163 | |
Sharon Yang | 80d37bb | 2022-03-10 02:32:11 | [diff] [blame] | 164 | void SiteInstanceGroup::WriteIntoTrace( |
Alexander Timin | 074cd18 | 2022-03-23 18:11:22 | [diff] [blame] | 165 | perfetto::TracedProto<TraceProto> proto) const { |
Sharon Yang | 80d37bb | 2022-03-10 02:32:11 | [diff] [blame] | 166 | proto->set_site_instance_group_id(GetId().value()); |
| 167 | proto->set_active_frame_count(active_frame_count()); |
Alexander Timin | 074cd18 | 2022-03-23 18:11:22 | [diff] [blame] | 168 | proto.Set(TraceProto::kProcess, process()); |
Sharon Yang | efe5263 | 2022-03-08 23:06:06 | [diff] [blame] | 169 | } |
| 170 | |
Sharon Yang | 0a9475e | 2021-10-25 21:58:33 | [diff] [blame] | 171 | } // namespace content |