Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Harkiran Bolaria | 8dec6f9 | 2021-12-07 14:57:12 | [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/renderer_host/browsing_context_state.h" |
| 6 | |
David Sanders | 27d3205 | 2022-04-04 18:08:19 | [diff] [blame] | 7 | #include "base/memory/ptr_util.h" |
Harkiran Bolaria | 2912a6b3 | 2022-02-22 16:43:45 | [diff] [blame] | 8 | #include "content/browser/renderer_host/frame_tree_node.h" |
Harkiran Bolaria | 3f83fba7 | 2022-03-10 17:48:40 | [diff] [blame] | 9 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
Harkiran Bolaria | 2912a6b3 | 2022-02-22 16:43:45 | [diff] [blame] | 10 | #include "content/browser/renderer_host/render_view_host_impl.h" |
Charlie Reis | 37be268 | 2023-01-10 17:04:47 | [diff] [blame] | 11 | #include "content/browser/site_instance_impl.h" |
Harkiran Bolaria | 2912a6b3 | 2022-02-22 16:43:45 | [diff] [blame] | 12 | #include "content/common/content_navigation_policy.h" |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 13 | #include "services/network/public/cpp/web_sandbox_flags.h" |
| 14 | #include "services/network/public/mojom/web_sandbox_flags.mojom.h" |
| 15 | |
Harkiran Bolaria | 8dec6f9 | 2021-12-07 14:57:12 | [diff] [blame] | 16 | namespace features { |
Daniel Cheng | 0abd9f3 | 2022-09-22 04:20:11 | [diff] [blame] | 17 | BASE_FEATURE(kNewBrowsingContextStateOnBrowsingContextGroupSwap, |
| 18 | "NewBrowsingContextStateOnBrowsingContextGroupSwap", |
| 19 | base::FEATURE_DISABLED_BY_DEFAULT); |
Harkiran Bolaria | 8dec6f9 | 2021-12-07 14:57:12 | [diff] [blame] | 20 | |
| 21 | BrowsingContextStateImplementationType GetBrowsingContextMode() { |
| 22 | if (base::FeatureList::IsEnabled( |
| 23 | kNewBrowsingContextStateOnBrowsingContextGroupSwap)) { |
| 24 | return BrowsingContextStateImplementationType:: |
| 25 | kSwapForCrossBrowsingInstanceNavigations; |
| 26 | } |
| 27 | |
| 28 | return BrowsingContextStateImplementationType:: |
| 29 | kLegacyOneToOneWithFrameTreeNode; |
| 30 | } |
| 31 | } // namespace features |
| 32 | |
| 33 | namespace content { |
| 34 | |
Harkiran Bolaria | a834778 | 2022-04-06 09:25:11 | [diff] [blame] | 35 | using perfetto::protos::pbzero::ChromeTrackEvent; |
| 36 | |
Harkiran Bolaria | 4eacb3a | 2021-12-13 20:03:47 | [diff] [blame] | 37 | BrowsingContextState::BrowsingContextState( |
Harkiran Bolaria | 880a763 | 2022-02-28 16:02:50 | [diff] [blame] | 38 | blink::mojom::FrameReplicationStatePtr replication_state, |
Arthur Sonzogni | 91fd79b | 2023-04-19 15:37:14 | [diff] [blame] | 39 | RenderFrameHostImpl* parent, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 40 | std::optional<BrowsingInstanceId> browsing_instance_id, |
| 41 | std::optional<base::UnguessableToken> coop_related_group_token) |
Harkiran Bolaria | 0b3bdef0 | 2022-03-10 13:04:40 | [diff] [blame] | 42 | : replication_state_(std::move(replication_state)), |
| 43 | parent_(parent), |
Arthur Hemery | 08d8288 | 2023-04-27 12:33:14 | [diff] [blame] | 44 | browsing_instance_id_(browsing_instance_id), |
Arthur Hemery | a3e593f | 2023-05-11 17:15:57 | [diff] [blame] | 45 | coop_related_group_token_(coop_related_group_token) { |
Harkiran Bolaria | a834778 | 2022-04-06 09:25:11 | [diff] [blame] | 46 | TRACE_EVENT_BEGIN("navigation", "BrowsingContextState", |
| 47 | perfetto::Track::FromPointer(this), |
| 48 | "browsing_context_state_when_created", this); |
| 49 | } |
Harkiran Bolaria | 8dec6f9 | 2021-12-07 14:57:12 | [diff] [blame] | 50 | |
Harkiran Bolaria | a834778 | 2022-04-06 09:25:11 | [diff] [blame] | 51 | BrowsingContextState::~BrowsingContextState() { |
| 52 | TRACE_EVENT_END("navigation", perfetto::Track::FromPointer(this)); |
W. James MacLean | 0fbe7dc5 | 2023-08-22 20:20:48 | [diff] [blame] | 53 | CHECK(proxy_hosts_.empty()); |
Harkiran Bolaria | a834778 | 2022-04-06 09:25:11 | [diff] [blame] | 54 | } |
Harkiran Bolaria | 4eacb3a | 2021-12-13 20:03:47 | [diff] [blame] | 55 | |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 56 | RenderFrameProxyHost* BrowsingContextState::GetRenderFrameProxyHost( |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 57 | SiteInstanceGroup* site_instance_group, |
| 58 | ProxyAccessMode proxy_access_mode) const { |
Peilin Wang | 9791d80 | 2023-05-09 19:07:31 | [diff] [blame] | 59 | TRACE_EVENT_BEGIN("navigation.debug", |
Harkiran Bolaria | a834778 | 2022-04-06 09:25:11 | [diff] [blame] | 60 | "BrowsingContextState::GetRenderFrameProxyHost", |
| 61 | ChromeTrackEvent::kBrowsingContextState, this, |
| 62 | ChromeTrackEvent::kSiteInstanceGroup, site_instance_group); |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 63 | auto* proxy = |
| 64 | GetRenderFrameProxyHostImpl(site_instance_group, proxy_access_mode); |
Peilin Wang | 9791d80 | 2023-05-09 19:07:31 | [diff] [blame] | 65 | TRACE_EVENT_END("navigation.debug", ChromeTrackEvent::kRenderFrameProxyHost, |
| 66 | proxy); |
Harkiran Bolaria | a834778 | 2022-04-06 09:25:11 | [diff] [blame] | 67 | return proxy; |
| 68 | } |
| 69 | |
| 70 | RenderFrameProxyHost* BrowsingContextState::GetRenderFrameProxyHostImpl( |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 71 | SiteInstanceGroup* site_instance_group, |
| 72 | ProxyAccessMode proxy_access_mode) const { |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 73 | if (features::GetBrowsingContextMode() == |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 74 | features::BrowsingContextStateImplementationType:: |
| 75 | kSwapForCrossBrowsingInstanceNavigations && |
| 76 | proxy_access_mode == ProxyAccessMode::kRegular) { |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 77 | // CHECK to verify that the proxy is being accessed from the correct |
| 78 | // BrowsingContextState. As both BrowsingContextState (in non-legacy mode) |
| 79 | // and RenderFrameProxyHost (via SiteInstance) are tied to a given |
Arthur Hemery | 08d8288 | 2023-04-27 12:33:14 | [diff] [blame] | 80 | // CoopRelatedGroup, the CoopRelatedGroupId of the BrowsingContextState |
| 81 | // (in the non-legacy mode) and of the SiteInstanceGroup should match. If |
| 82 | // they do not, the code calling this method has likely chosen the wrong |
| 83 | // BrowsingContextState (e.g. one from the current RenderFrameHost rather |
| 84 | // than from speculative or vice versa) – as this can lead to various |
| 85 | // unpredictable bugs in proxy management logic, we want to crash the |
| 86 | // browser here when this condition fails. |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 87 | // |
Arthur Hemery | 08d8288 | 2023-04-27 12:33:14 | [diff] [blame] | 88 | // Note: Outer delegates are an exception, and when we're expecting to |
| 89 | // interact with one, we should pass in the proper `proxy_access_mode` to |
| 90 | // not end up in this condition. |
Arthur Hemery | a3e593f | 2023-05-11 17:15:57 | [diff] [blame] | 91 | CHECK_EQ(coop_related_group_token_.value(), |
| 92 | site_instance_group->coop_related_group_token()); |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 93 | } |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 94 | auto it = proxy_hosts_.find(site_instance_group->GetId()); |
Harkiran Bolaria | a834778 | 2022-04-06 09:25:11 | [diff] [blame] | 95 | if (it != proxy_hosts_.end()) { |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 96 | return it->second.get(); |
Harkiran Bolaria | a834778 | 2022-04-06 09:25:11 | [diff] [blame] | 97 | } |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 98 | return nullptr; |
| 99 | } |
| 100 | |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 101 | void BrowsingContextState::DeleteRenderFrameProxyHost( |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 102 | SiteInstanceGroup* site_instance_group, |
| 103 | ProxyAccessMode proxy_access_mode) { |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 104 | if (features::GetBrowsingContextMode() == |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 105 | features::BrowsingContextStateImplementationType:: |
| 106 | kSwapForCrossBrowsingInstanceNavigations && |
| 107 | proxy_access_mode == ProxyAccessMode::kRegular) { |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 108 | // See comments in GetRenderFrameProxyHost for why this check is needed. |
Arthur Hemery | a3e593f | 2023-05-11 17:15:57 | [diff] [blame] | 109 | CHECK_EQ(coop_related_group_token_.value(), |
| 110 | site_instance_group->coop_related_group_token()); |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 111 | } |
Harkiran Bolaria | a834778 | 2022-04-06 09:25:11 | [diff] [blame] | 112 | TRACE_EVENT("navigation", "BrowsingContextState::DeleteRenderFrameProxyHost", |
| 113 | ChromeTrackEvent::kBrowsingContextState, this, |
| 114 | ChromeTrackEvent::kSiteInstanceGroup, site_instance_group); |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 115 | site_instance_group->RemoveObserver(this); |
| 116 | proxy_hosts_.erase(site_instance_group->GetId()); |
| 117 | } |
| 118 | |
| 119 | RenderFrameProxyHost* BrowsingContextState::CreateRenderFrameProxyHost( |
Charlie Reis | 37be268 | 2023-01-10 17:04:47 | [diff] [blame] | 120 | SiteInstanceImpl* site_instance, |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 121 | const scoped_refptr<RenderViewHostImpl>& rvh, |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 122 | FrameTreeNode* frame_tree_node, |
Dave Tapuska | 14457010 | 2022-08-02 19:28:27 | [diff] [blame] | 123 | ProxyAccessMode proxy_access_mode, |
| 124 | const blink::RemoteFrameToken& frame_token) { |
Harkiran Bolaria | a834778 | 2022-04-06 09:25:11 | [diff] [blame] | 125 | TRACE_EVENT_BEGIN( |
| 126 | "navigation", "BrowsingContextState::CreateRenderFrameProxyHost", |
| 127 | ChromeTrackEvent::kBrowsingContextState, this, |
Charlie Reis | 37be268 | 2023-01-10 17:04:47 | [diff] [blame] | 128 | ChromeTrackEvent::kSiteInstanceGroup, site_instance->group(), |
Harkiran Bolaria | a834778 | 2022-04-06 09:25:11 | [diff] [blame] | 129 | ChromeTrackEvent::kRenderViewHost, rvh ? rvh.get() : nullptr, |
| 130 | ChromeTrackEvent::kFrameTreeNodeInfo, frame_tree_node); |
| 131 | |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 132 | if (features::GetBrowsingContextMode() == |
| 133 | features::BrowsingContextStateImplementationType:: |
| 134 | kLegacyOneToOneWithFrameTreeNode) { |
| 135 | DCHECK_EQ(this, |
| 136 | frame_tree_node->current_frame_host()->browsing_context_state()); |
| 137 | } |
| 138 | |
| 139 | if (features::GetBrowsingContextMode() == |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 140 | features::BrowsingContextStateImplementationType:: |
| 141 | kSwapForCrossBrowsingInstanceNavigations && |
| 142 | proxy_access_mode == ProxyAccessMode::kRegular) { |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 143 | // See comments in GetRenderFrameProxyHost for why this check is needed. |
Arthur Hemery | a3e593f | 2023-05-11 17:15:57 | [diff] [blame] | 144 | CHECK_EQ(coop_related_group_token_.value(), |
| 145 | site_instance->coop_related_group_token()); |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 146 | } |
| 147 | |
Charlie Reis | 37be268 | 2023-01-10 17:04:47 | [diff] [blame] | 148 | auto site_instance_group_id = site_instance->group()->GetId(); |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 149 | CHECK(proxy_hosts_.find(site_instance_group_id) == proxy_hosts_.end()) |
| 150 | << "A proxy already existed for this SiteInstanceGroup."; |
Dave Tapuska | 14457010 | 2022-08-02 19:28:27 | [diff] [blame] | 151 | RenderFrameProxyHost* proxy_host = new RenderFrameProxyHost( |
| 152 | site_instance, std::move(rvh), frame_tree_node, frame_token); |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 153 | proxy_hosts_[site_instance_group_id] = base::WrapUnique(proxy_host); |
Charlie Reis | 37be268 | 2023-01-10 17:04:47 | [diff] [blame] | 154 | site_instance->group()->AddObserver(this); |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 155 | |
Harkiran Bolaria | a834778 | 2022-04-06 09:25:11 | [diff] [blame] | 156 | TRACE_EVENT_END("navigation", ChromeTrackEvent::kRenderFrameProxyHost, |
| 157 | proxy_host); |
Harkiran Bolaria | 5c5a9739 | 2022-03-10 14:18:50 | [diff] [blame] | 158 | return proxy_host; |
| 159 | } |
| 160 | |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 161 | RenderFrameProxyHost* BrowsingContextState::CreateOuterDelegateProxy( |
Charlie Reis | 37be268 | 2023-01-10 17:04:47 | [diff] [blame] | 162 | SiteInstanceImpl* outer_contents_site_instance, |
Dave Tapuska | 14457010 | 2022-08-02 19:28:27 | [diff] [blame] | 163 | FrameTreeNode* frame_tree_node, |
| 164 | const blink::RemoteFrameToken& frame_token) { |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 165 | // We only get here when Delegate for this manager is an inner delegate. |
| 166 | return CreateRenderFrameProxyHost(outer_contents_site_instance, |
| 167 | /*rvh=*/nullptr, frame_tree_node, |
Dave Tapuska | 14457010 | 2022-08-02 19:28:27 | [diff] [blame] | 168 | ProxyAccessMode::kAllowOuterDelegate, |
| 169 | frame_token); |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void BrowsingContextState::DeleteOuterDelegateProxy( |
| 173 | SiteInstanceGroup* outer_contents_site_instance_group) { |
| 174 | DeleteRenderFrameProxyHost( |
| 175 | outer_contents_site_instance_group, |
| 176 | BrowsingContextState::ProxyAccessMode::kAllowOuterDelegate); |
| 177 | } |
| 178 | |
Harkiran Bolaria | d22a1dca | 2022-02-22 17:01:12 | [diff] [blame] | 179 | size_t BrowsingContextState::GetProxyCount() { |
| 180 | return proxy_hosts_.size(); |
| 181 | } |
| 182 | |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 183 | bool BrowsingContextState::UpdateFramePolicyHeaders( |
| 184 | network::mojom::WebSandboxFlags sandbox_flags, |
| 185 | const blink::ParsedPermissionsPolicy& parsed_header) { |
| 186 | bool changed = false; |
| 187 | if (replication_state_->permissions_policy_header != parsed_header) { |
| 188 | replication_state_->permissions_policy_header = parsed_header; |
| 189 | changed = true; |
| 190 | } |
| 191 | // TODO(iclelland): Kill the renderer if sandbox flags is not a subset of the |
| 192 | // currently effective sandbox flags from the frame. https://crbug.com/740556 |
| 193 | network::mojom::WebSandboxFlags updated_flags = |
| 194 | sandbox_flags | replication_state_->frame_policy.sandbox_flags; |
| 195 | if (replication_state_->active_sandbox_flags != updated_flags) { |
| 196 | replication_state_->active_sandbox_flags = updated_flags; |
| 197 | changed = true; |
| 198 | } |
| 199 | // Notify any proxies if the policies have been changed. |
| 200 | if (changed) { |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 201 | ExecuteRemoteFramesBroadcastMethod( |
| 202 | base::BindRepeating( |
| 203 | [](blink::mojom::FrameReplicationStatePtr& replication_state, |
| 204 | RenderFrameProxyHost* proxy) { |
| 205 | proxy->GetAssociatedRemoteFrame()->DidSetFramePolicyHeaders( |
| 206 | replication_state->active_sandbox_flags, |
| 207 | replication_state->permissions_policy_header); |
| 208 | }, |
| 209 | std::ref(replication_state_)), |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 210 | /*group_to_skip=*/nullptr, /*outer_delegate_proxy=*/nullptr); |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 211 | } |
| 212 | return changed; |
| 213 | } |
| 214 | |
| 215 | bool BrowsingContextState::CommitFramePolicy( |
| 216 | const blink::FramePolicy& new_frame_policy) { |
| 217 | // Documents create iframes, iframes host new documents. Both are associated |
| 218 | // with sandbox flags. They are required to be stricter or equal to their |
| 219 | // owner when they change, as we go down. |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 220 | // TODO(crbug.com/40202483). Enforce the invariant mentioned above, |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 221 | // once the interactions with fenced frame has been tested and clarified. |
| 222 | |
| 223 | bool did_change_flags = new_frame_policy.sandbox_flags != |
| 224 | replication_state_->frame_policy.sandbox_flags; |
| 225 | bool did_change_container_policy = |
| 226 | new_frame_policy.container_policy != |
| 227 | replication_state_->frame_policy.container_policy; |
| 228 | bool did_change_required_document_policy = |
| 229 | new_frame_policy.required_document_policy != |
| 230 | replication_state_->frame_policy.required_document_policy; |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 231 | |
Harkiran Bolaria | 4eacb3a | 2021-12-13 20:03:47 | [diff] [blame] | 232 | if (did_change_flags) { |
| 233 | replication_state_->frame_policy.sandbox_flags = |
| 234 | new_frame_policy.sandbox_flags; |
| 235 | } |
| 236 | if (did_change_container_policy) { |
| 237 | replication_state_->frame_policy.container_policy = |
| 238 | new_frame_policy.container_policy; |
| 239 | } |
| 240 | if (did_change_required_document_policy) { |
| 241 | replication_state_->frame_policy.required_document_policy = |
| 242 | new_frame_policy.required_document_policy; |
| 243 | } |
Harkiran Bolaria | e352143 | 2021-12-14 11:27:43 | [diff] [blame] | 244 | |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 245 | UpdateFramePolicyHeaders(new_frame_policy.sandbox_flags, |
| 246 | replication_state_->permissions_policy_header); |
| 247 | return did_change_flags || did_change_container_policy || |
| 248 | did_change_required_document_policy; |
Harkiran Bolaria | 7fdb4c64 | 2021-12-20 12:47:00 | [diff] [blame] | 249 | } |
| 250 | |
Harkiran Bolaria | 880a763 | 2022-02-28 16:02:50 | [diff] [blame] | 251 | void BrowsingContextState::SetFrameName(const std::string& name, |
| 252 | const std::string& unique_name) { |
| 253 | if (name == replication_state_->name) { |
| 254 | // |unique_name| shouldn't change unless |name| changes. |
| 255 | DCHECK_EQ(unique_name, replication_state_->unique_name); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | if (parent_) { |
| 260 | // Non-main frames should have a non-empty unique name. |
| 261 | DCHECK(!unique_name.empty()); |
| 262 | } else { |
| 263 | // Unique name of main frames should always stay empty. |
| 264 | DCHECK(unique_name.empty()); |
| 265 | } |
| 266 | |
| 267 | // Note the unique name should only be able to change before the first real |
| 268 | // load is committed, but that's not strongly enforced here. |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 269 | ExecuteRemoteFramesBroadcastMethod( |
| 270 | base::BindRepeating( |
| 271 | [](const std::string& name, const std::string& unique_name, |
| 272 | RenderFrameProxyHost* proxy) { |
| 273 | proxy->GetAssociatedRemoteFrame()->SetReplicatedName(name, |
| 274 | unique_name); |
| 275 | }, |
| 276 | std::ref(name), std::ref(unique_name)), |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 277 | /*group_to_skip=*/nullptr, /*outer_delegate_proxy=*/nullptr); |
Harkiran Bolaria | 880a763 | 2022-02-28 16:02:50 | [diff] [blame] | 278 | replication_state_->unique_name = unique_name; |
| 279 | replication_state_->name = name; |
| 280 | } |
| 281 | |
Harkiran Bolaria | e352143 | 2021-12-14 11:27:43 | [diff] [blame] | 282 | void BrowsingContextState::SetCurrentOrigin( |
| 283 | const url::Origin& origin, |
| 284 | bool is_potentially_trustworthy_unique_origin) { |
| 285 | if (origin.IsSameOriginWith(replication_state_->origin) && |
| 286 | replication_state_->has_potentially_trustworthy_unique_origin == |
| 287 | is_potentially_trustworthy_unique_origin) { |
| 288 | return; |
| 289 | } |
| 290 | |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 291 | ExecuteRemoteFramesBroadcastMethod( |
| 292 | base::BindRepeating( |
| 293 | [](const url::Origin& origin, |
| 294 | bool is_potentially_trustworthy_unique_origin, |
| 295 | RenderFrameProxyHost* proxy) { |
| 296 | proxy->GetAssociatedRemoteFrame()->SetReplicatedOrigin( |
| 297 | origin, is_potentially_trustworthy_unique_origin); |
| 298 | }, |
| 299 | std::ref(origin), std::ref(is_potentially_trustworthy_unique_origin)), |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 300 | /*group_to_skip=*/nullptr, /*outer_delegate_proxy=*/nullptr); |
Harkiran Bolaria | e352143 | 2021-12-14 11:27:43 | [diff] [blame] | 301 | |
| 302 | replication_state_->origin = origin; |
| 303 | replication_state_->has_potentially_trustworthy_unique_origin = |
| 304 | is_potentially_trustworthy_unique_origin; |
| 305 | } |
| 306 | |
| 307 | void BrowsingContextState::SetInsecureRequestPolicy( |
| 308 | blink::mojom::InsecureRequestPolicy policy) { |
| 309 | if (policy == replication_state_->insecure_request_policy) |
| 310 | return; |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 311 | ExecuteRemoteFramesBroadcastMethod( |
| 312 | base::BindRepeating( |
| 313 | [](blink::mojom::InsecureRequestPolicy policy, |
| 314 | RenderFrameProxyHost* proxy) { |
| 315 | proxy->GetAssociatedRemoteFrame()->EnforceInsecureRequestPolicy( |
| 316 | policy); |
| 317 | }, |
| 318 | policy), |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 319 | /*group_to_skip=*/nullptr, /*outer_delegate_proxy=*/nullptr); |
Harkiran Bolaria | e352143 | 2021-12-14 11:27:43 | [diff] [blame] | 320 | replication_state_->insecure_request_policy = policy; |
| 321 | } |
| 322 | |
| 323 | void BrowsingContextState::SetInsecureNavigationsSet( |
| 324 | const std::vector<uint32_t>& insecure_navigations_set) { |
| 325 | DCHECK(std::is_sorted(insecure_navigations_set.begin(), |
| 326 | insecure_navigations_set.end())); |
| 327 | if (insecure_navigations_set == replication_state_->insecure_navigations_set) |
| 328 | return; |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 329 | ExecuteRemoteFramesBroadcastMethod( |
| 330 | base::BindRepeating( |
| 331 | [](const std::vector<uint32_t>& insecure_navigations_set, |
| 332 | RenderFrameProxyHost* proxy) { |
| 333 | proxy->GetAssociatedRemoteFrame()->EnforceInsecureNavigationsSet( |
| 334 | insecure_navigations_set); |
| 335 | }, |
| 336 | std::ref(insecure_navigations_set)), |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 337 | /*group_to_skip=*/nullptr, /*outer_delegate_proxy=*/nullptr); |
Harkiran Bolaria | e352143 | 2021-12-14 11:27:43 | [diff] [blame] | 338 | replication_state_->insecure_navigations_set = insecure_navigations_set; |
| 339 | } |
| 340 | |
| 341 | void BrowsingContextState::OnSetHadStickyUserActivationBeforeNavigation( |
| 342 | bool value) { |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 343 | ExecuteRemoteFramesBroadcastMethod( |
| 344 | base::BindRepeating( |
| 345 | [](bool value, RenderFrameProxyHost* proxy) { |
| 346 | proxy->GetAssociatedRemoteFrame() |
| 347 | ->SetHadStickyUserActivationBeforeNavigation(value); |
| 348 | }, |
| 349 | value), |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 350 | /*group_to_skip=*/nullptr, /*outer_delegate_proxy=*/nullptr); |
Harkiran Bolaria | e352143 | 2021-12-14 11:27:43 | [diff] [blame] | 351 | replication_state_->has_received_user_gesture_before_nav = value; |
| 352 | } |
| 353 | |
David Bokan | fa230cc | 2022-07-22 18:02:33 | [diff] [blame] | 354 | void BrowsingContextState::SetIsAdFrame(bool is_ad_frame) { |
| 355 | if (is_ad_frame == replication_state_->is_ad_frame) |
Harkiran Bolaria | e352143 | 2021-12-14 11:27:43 | [diff] [blame] | 356 | return; |
| 357 | |
David Bokan | fa230cc | 2022-07-22 18:02:33 | [diff] [blame] | 358 | replication_state_->is_ad_frame = is_ad_frame; |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 359 | ExecuteRemoteFramesBroadcastMethod( |
| 360 | base::BindRepeating( |
David Bokan | fa230cc | 2022-07-22 18:02:33 | [diff] [blame] | 361 | [](bool is_ad_frame, RenderFrameProxyHost* proxy) { |
| 362 | proxy->GetAssociatedRemoteFrame()->SetReplicatedIsAdFrame( |
| 363 | is_ad_frame); |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 364 | }, |
David Bokan | fa230cc | 2022-07-22 18:02:33 | [diff] [blame] | 365 | is_ad_frame), |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 366 | /*group_to_skip=*/nullptr, /*outer_delegate_proxy=*/nullptr); |
Harkiran Bolaria | e352143 | 2021-12-14 11:27:43 | [diff] [blame] | 367 | } |
| 368 | |
Harkiran Bolaria | e182a594 | 2021-12-20 17:23:31 | [diff] [blame] | 369 | void BrowsingContextState::ActiveFrameCountIsZero( |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 370 | SiteInstanceGroup* site_instance_group) { |
Sharon Yang | 417a5df | 2024-04-23 17:57:15 | [diff] [blame^] | 371 | CheckIfSiteInstanceGroupIsUnused(site_instance_group, kActiveFrameCount); |
| 372 | } |
| 373 | |
| 374 | void BrowsingContextState::KeepAliveCountIsZero( |
| 375 | SiteInstanceGroup* site_instance_group) { |
| 376 | CheckIfSiteInstanceGroupIsUnused(site_instance_group, kKeepAliveCount); |
| 377 | } |
| 378 | |
| 379 | void BrowsingContextState::CheckIfSiteInstanceGroupIsUnused( |
| 380 | SiteInstanceGroup* site_instance_group, |
| 381 | RefCountType ref_count_type) { |
| 382 | // Only delete the proxy if both counts are zero. |
| 383 | if (site_instance_group->keep_alive_count() > 0 || |
| 384 | site_instance_group->active_frame_count() > 0) { |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | // |site_instance_group| no longer contains any active RenderFrameHosts or |
| 389 | // NavigationStateKeepAlive objects, so we don't need to maintain a proxy |
| 390 | // there anymore. |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 391 | RenderFrameProxyHost* proxy = GetRenderFrameProxyHost(site_instance_group); |
Harkiran Bolaria | e182a594 | 2021-12-20 17:23:31 | [diff] [blame] | 392 | CHECK(proxy); |
| 393 | |
Sharon Yang | 417a5df | 2024-04-23 17:57:15 | [diff] [blame^] | 394 | if (kActiveFrameCount) { |
| 395 | TRACE_EVENT_INSTANT("navigation", |
| 396 | "BrowsingContextState::ActiveFrameCountIsZero", |
| 397 | ChromeTrackEvent::kBrowsingContextState, this, |
| 398 | ChromeTrackEvent::kRenderFrameProxyHost, proxy); |
| 399 | } else if (kKeepAliveCount) { |
| 400 | TRACE_EVENT_INSTANT("navigation", |
| 401 | "BrowsingContextState::KeepAliveCountIsZero", |
| 402 | ChromeTrackEvent::kBrowsingContextState, this, |
| 403 | ChromeTrackEvent::kRenderFrameProxyHost, proxy); |
| 404 | } |
Harkiran Bolaria | a834778 | 2022-04-06 09:25:11 | [diff] [blame] | 405 | |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 406 | DeleteRenderFrameProxyHost(site_instance_group); |
Harkiran Bolaria | e182a594 | 2021-12-20 17:23:31 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | void BrowsingContextState::RenderProcessGone( |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 410 | SiteInstanceGroup* site_instance_group, |
Harkiran Bolaria | e182a594 | 2021-12-20 17:23:31 | [diff] [blame] | 411 | const ChildProcessTerminationInfo& info) { |
Harkiran Bolaria | e45272d | 2022-04-12 08:05:01 | [diff] [blame] | 412 | GetRenderFrameProxyHost(site_instance_group, |
| 413 | ProxyAccessMode::kAllowOuterDelegate) |
Sharon Yang | a2fe85e | 2022-02-09 21:38:29 | [diff] [blame] | 414 | ->SetRenderFrameProxyCreated(false); |
Harkiran Bolaria | e182a594 | 2021-12-20 17:23:31 | [diff] [blame] | 415 | } |
| 416 | |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 417 | void BrowsingContextState::SendFramePolicyUpdatesToProxies( |
Sharon Yang | 571baee | 2022-03-18 19:01:54 | [diff] [blame] | 418 | SiteInstanceGroup* parent_group, |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 419 | const blink::FramePolicy& frame_policy) { |
| 420 | // Notify all of the frame's proxies about updated policies, excluding |
| 421 | // the parent process since it already knows the latest state. |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 422 | ExecuteRemoteFramesBroadcastMethod( |
| 423 | base::BindRepeating( |
| 424 | [](SiteInstanceGroup* parent_group, |
| 425 | const blink::FramePolicy& frame_policy, |
| 426 | RenderFrameProxyHost* proxy) { |
| 427 | if (proxy->site_instance_group() == parent_group) |
| 428 | return; |
| 429 | proxy->GetAssociatedRemoteFrame()->DidUpdateFramePolicy( |
| 430 | frame_policy); |
| 431 | }, |
| 432 | base::Unretained(parent_group), std::ref(frame_policy)), |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 433 | /*group_to_skip=*/nullptr, /*outer_delegate_proxy=*/nullptr); |
Harkiran Bolaria | 5ce2763 | 2022-01-20 15:05:05 | [diff] [blame] | 434 | } |
| 435 | |
Harkiran Bolaria | 3f83fba7 | 2022-03-10 17:48:40 | [diff] [blame] | 436 | void BrowsingContextState::OnDidStartLoading() { |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 437 | ExecuteRemoteFramesBroadcastMethod( |
| 438 | base::BindRepeating([](RenderFrameProxyHost* proxy) { |
| 439 | proxy->GetAssociatedRemoteFrame()->DidStartLoading(); |
| 440 | }), |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 441 | /*group_to_skip=*/nullptr, /*outer_delegate_proxy=*/nullptr); |
Harkiran Bolaria | 3f83fba7 | 2022-03-10 17:48:40 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void BrowsingContextState::OnDidStopLoading() { |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 445 | ExecuteRemoteFramesBroadcastMethod( |
| 446 | base::BindRepeating([](RenderFrameProxyHost* proxy) { |
| 447 | proxy->GetAssociatedRemoteFrame()->DidStopLoading(); |
| 448 | }), |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 449 | /*group_to_skip=*/nullptr, /*outer_delegate_proxy=*/nullptr); |
Harkiran Bolaria | 3f83fba7 | 2022-03-10 17:48:40 | [diff] [blame] | 450 | } |
| 451 | |
Harkiran Bolaria | 0b3bdef0 | 2022-03-10 13:04:40 | [diff] [blame] | 452 | void BrowsingContextState::ResetProxyHosts() { |
| 453 | for (const auto& pair : proxy_hosts_) { |
| 454 | pair.second->site_instance_group()->RemoveObserver(this); |
| 455 | } |
| 456 | proxy_hosts_.clear(); |
| 457 | } |
| 458 | |
Sharon Yang | 571baee | 2022-03-18 19:01:54 | [diff] [blame] | 459 | void BrowsingContextState::UpdateOpener( |
| 460 | SiteInstanceGroup* source_site_instance_group) { |
Harkiran Bolaria | 3f83fba7 | 2022-03-10 17:48:40 | [diff] [blame] | 461 | for (const auto& pair : proxy_hosts_) { |
Sharon Yang | 571baee | 2022-03-18 19:01:54 | [diff] [blame] | 462 | if (pair.second->site_instance_group() == source_site_instance_group) |
Harkiran Bolaria | 3f83fba7 | 2022-03-10 17:48:40 | [diff] [blame] | 463 | continue; |
| 464 | pair.second->UpdateOpener(); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | void BrowsingContextState::OnDidUpdateFrameOwnerProperties( |
| 469 | const blink::mojom::FrameOwnerProperties& properties) { |
| 470 | // Notify this frame's proxies if they live in a different process from its |
| 471 | // parent. This is only currently needed for the allowFullscreen property, |
| 472 | // since that can be queried on RemoteFrame ancestors. |
| 473 | // |
| 474 | // TODO(alexmos): It would be sufficient to only send this update to proxies |
| 475 | // in the current FrameTree. |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 476 | ExecuteRemoteFramesBroadcastMethod( |
| 477 | base::BindRepeating( |
| 478 | [](SiteInstanceGroup* parent_group, |
| 479 | const blink::mojom::FrameOwnerProperties& properties, |
| 480 | RenderFrameProxyHost* proxy) { |
| 481 | if (proxy->site_instance_group() == parent_group) |
| 482 | return; |
| 483 | proxy->GetAssociatedRemoteFrame()->SetFrameOwnerProperties( |
| 484 | properties.Clone()); |
| 485 | }, |
| 486 | base::Unretained(parent_->GetSiteInstance()->group()), |
| 487 | std::ref(properties)), |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 488 | /*group_to_skip=*/nullptr, /*outer_delegate_proxy=*/nullptr); |
Harkiran Bolaria | 3f83fba7 | 2022-03-10 17:48:40 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | void BrowsingContextState::ExecuteRemoteFramesBroadcastMethod( |
| 492 | base::RepeatingCallback<void(RenderFrameProxyHost*)> callback, |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 493 | SiteInstanceGroup* group_to_skip, |
Harkiran Bolaria | 3f83fba7 | 2022-03-10 17:48:40 | [diff] [blame] | 494 | RenderFrameProxyHost* outer_delegate_proxy) { |
| 495 | for (const auto& pair : proxy_hosts_) { |
| 496 | if (outer_delegate_proxy == pair.second.get()) |
| 497 | continue; |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 498 | if (pair.second->site_instance_group() == group_to_skip) { |
Harkiran Bolaria | 3f83fba7 | 2022-03-10 17:48:40 | [diff] [blame] | 499 | continue; |
Sharon Yang | 6b531343 | 2023-03-24 05:07:57 | [diff] [blame] | 500 | } |
Harkiran Bolaria | 3f83fba7 | 2022-03-10 17:48:40 | [diff] [blame] | 501 | if (!pair.second->is_render_frame_proxy_live()) |
| 502 | continue; |
| 503 | callback.Run(pair.second.get()); |
| 504 | } |
| 505 | } |
| 506 | |
Harkiran Bolaria | 3bf5457f | 2022-03-10 20:04:37 | [diff] [blame] | 507 | void BrowsingContextState::WriteIntoTrace( |
Alexander Timin | 074cd18 | 2022-03-23 18:11:22 | [diff] [blame] | 508 | perfetto::TracedProto<TraceProto> proto) const { |
Arthur Hemery | 08d8288 | 2023-04-27 12:33:14 | [diff] [blame] | 509 | if (browsing_instance_id_.has_value()) { |
Harkiran Bolaria | 3bf5457f | 2022-03-10 20:04:37 | [diff] [blame] | 510 | proto->set_browsing_instance_id(browsing_instance_id_.value().value()); |
Arthur Hemery | 08d8288 | 2023-04-27 12:33:14 | [diff] [blame] | 511 | } |
| 512 | |
Arthur Hemery | a3e593f | 2023-05-11 17:15:57 | [diff] [blame] | 513 | if (coop_related_group_token_.has_value()) { |
| 514 | proto->set_coop_related_group_token( |
| 515 | coop_related_group_token_.value().ToString()); |
Arthur Hemery | 08d8288 | 2023-04-27 12:33:14 | [diff] [blame] | 516 | } |
Alexander Timin | 074cd18 | 2022-03-23 18:11:22 | [diff] [blame] | 517 | |
| 518 | perfetto::TracedDictionary dict = std::move(proto).AddDebugAnnotations(); |
| 519 | dict.Add("this", static_cast<const void*>(this)); |
Harkiran Bolaria | 3bf5457f | 2022-03-10 20:04:37 | [diff] [blame] | 520 | } |
| 521 | |
Harkiran Bolaria | 875d4f6 | 2022-05-17 16:18:23 | [diff] [blame] | 522 | base::SafeRef<BrowsingContextState> BrowsingContextState::GetSafeRef() { |
| 523 | return weak_factory_.GetSafeRef(); |
| 524 | } |
| 525 | |
Alexander Timin | 07cad076 | 2022-03-15 00:33:17 | [diff] [blame] | 526 | } // namespace content |