Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Antonio Sartori | 88ca179 | 2020-10-09 06:26:45 | [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 | |
Antonio Sartori | 9290b6b | 2020-11-09 10:09:33 | [diff] [blame] | 5 | #include "content/browser/renderer_host/policy_container_host.h" |
Antonio Sartori | 88ca179 | 2020-10-09 06:26:45 | [diff] [blame] | 6 | |
Peter Kasting | 1557e5f | 2025-01-28 01:14:08 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Daniel Cheng | 8db1f0e | 2024-02-21 21:14:41 | [diff] [blame] | 9 | #include "base/memory/scoped_refptr.h" |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 10 | #include "content/browser/renderer_host/frame_navigation_entry.h" |
| 11 | #include "content/browser/renderer_host/frame_tree_node.h" |
Jonathan Hao | 9ebd246 | 2023-07-27 16:06:07 | [diff] [blame] | 12 | #include "content/browser/renderer_host/private_network_access_util.h" |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 13 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 14 | #include "content/public/browser/browser_thread.h" |
Antonio Sartori | d7af7d8 | 2025-04-22 09:24:19 | [diff] [blame] | 15 | #include "services/network/public/cpp/cross_origin_opener_policy.h" |
| 16 | #include "services/network/public/cpp/document_isolation_policy.h" |
Jonathan Hao | f6cd769 | 2022-08-26 09:18:21 | [diff] [blame] | 17 | #include "services/network/public/cpp/is_potentially_trustworthy.h" |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 18 | #include "services/network/public/mojom/content_security_policy.mojom.h" |
Yoav Weiss | 93d38b50 | 2025-05-06 15:55:51 | [diff] [blame] | 19 | #include "services/network/public/mojom/integrity_policy.mojom.h" |
| 20 | |
| 21 | namespace { |
| 22 | template <typename T> |
| 23 | std::string ConvertToString(const std::vector<T>& array) { |
| 24 | std::ostringstream oss; |
| 25 | size_t array_size = array.size(); |
| 26 | for (size_t i = 0; i < array_size; ++i) { |
| 27 | oss << array[i]; |
| 28 | if (i == array_size - 1) { |
| 29 | oss << ", "; |
| 30 | } |
| 31 | } |
| 32 | return oss.str(); |
| 33 | } |
| 34 | } // namespace |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 35 | |
Antonio Sartori | 88ca179 | 2020-10-09 06:26:45 | [diff] [blame] | 36 | namespace content { |
| 37 | |
Titouan Rigoudy | 2f995bc | 2021-02-19 19:39:41 | [diff] [blame] | 38 | std::ostream& operator<<(std::ostream& out, |
| 39 | const PolicyContainerPolicies& policies) { |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 40 | out << "{ referrer_policy: " << policies.referrer_policy |
| 41 | << ", ip_address_space: " << policies.ip_address_space |
| 42 | << ", is_web_secure_context: " << policies.is_web_secure_context |
| 43 | << ", content_security_policies: "; |
| 44 | |
Pâris MEULEMAN | 541156ae | 2021-04-29 07:52:02 | [diff] [blame] | 45 | if (policies.content_security_policies.empty()) { |
| 46 | out << "[]"; |
| 47 | } else { |
| 48 | out << "[ "; |
| 49 | auto it = policies.content_security_policies.begin(); |
| 50 | for (; it + 1 != policies.content_security_policies.end(); ++it) { |
| 51 | out << (*it)->header->header_value << ", "; |
| 52 | } |
| 53 | out << (*it)->header->header_value << " ]"; |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 54 | } |
Pâris MEULEMAN | 541156ae | 2021-04-29 07:52:02 | [diff] [blame] | 55 | |
| 56 | out << ", cross_origin_opener_policy: " |
Camille Lamy | 10109fb | 2025-05-06 12:52:44 | [diff] [blame] | 57 | << "{ value: " << policies.cross_origin_opener_policy.value |
Pâris MEULEMAN | 541156ae | 2021-04-29 07:52:02 | [diff] [blame] | 58 | << ", reporting_endpoint: " |
| 59 | << policies.cross_origin_opener_policy.reporting_endpoint.value_or( |
| 60 | "<null>") |
| 61 | << ", report_only_value: " |
| 62 | << policies.cross_origin_opener_policy.report_only_value |
| 63 | << ", report_only_reporting_endpoint: " |
| 64 | << policies.cross_origin_opener_policy.report_only_reporting_endpoint |
| 65 | .value_or("<null>") |
Camille Lamy | b988f14 | 2021-08-20 15:47:28 | [diff] [blame] | 66 | << ", soap_by_default_value: " |
| 67 | << policies.cross_origin_opener_policy.soap_by_default_value << " }"; |
Pâris MEULEMAN | 541156ae | 2021-04-29 07:52:02 | [diff] [blame] | 68 | |
Pâris Meuleman | 00d62b2 | 2022-01-14 13:43:42 | [diff] [blame] | 69 | out << ", cross_origin_embedder_policy: " |
| 70 | << "{ value: " << policies.cross_origin_embedder_policy.value |
| 71 | << ", reporting_endpoint: " |
| 72 | << policies.cross_origin_embedder_policy.reporting_endpoint.value_or( |
| 73 | "<null>") |
| 74 | << ", report_only_value: " |
| 75 | << policies.cross_origin_embedder_policy.report_only_value |
| 76 | << ", report_only_reporting_endpoint: " |
| 77 | << policies.cross_origin_embedder_policy.report_only_reporting_endpoint |
| 78 | .value_or("<null>") |
| 79 | << " }"; |
| 80 | |
Camille Lamy | ab87007 | 2024-04-16 15:18:10 | [diff] [blame] | 81 | out << ", document_isolation_policy: " << "{ value: " |
| 82 | << policies.document_isolation_policy.value << ", reporting_endpoint: " |
| 83 | << policies.document_isolation_policy.reporting_endpoint.value_or( |
| 84 | "<null>") |
| 85 | << ", report_only_value: " |
| 86 | << policies.document_isolation_policy.report_only_value |
| 87 | << ", report_only_reporting_endpoint: " |
| 88 | << policies.document_isolation_policy.report_only_reporting_endpoint |
| 89 | .value_or("<null>") |
| 90 | << " }"; |
| 91 | |
Yoav Weiss | 93d38b50 | 2025-05-06 15:55:51 | [diff] [blame] | 92 | out << ", integrity_policy: " << "{ blocked-destinations: " |
| 93 | << ConvertToString<::network::mojom::IntegrityPolicy_Destination>( |
| 94 | policies.integrity_policy.blocked_destinations) |
| 95 | << ", sources: " |
| 96 | << ConvertToString<::network::mojom::IntegrityPolicy_Source>( |
| 97 | policies.integrity_policy.sources) |
| 98 | << ", endpoints: " |
| 99 | << ConvertToString<std::string>(policies.integrity_policy.endpoints) |
| 100 | << " }"; |
| 101 | |
Pâris Meuleman | 687f700 | 2022-02-16 11:11:34 | [diff] [blame] | 102 | out << ", sandbox_flags: " << policies.sandbox_flags; |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 103 | out << ", is_credentialless: " << policies.is_credentialless; |
Liam Brady | e6de25e | 2022-10-11 17:02:30 | [diff] [blame] | 104 | out << ", can_navigate_top_without_user_gesture: " |
| 105 | << policies.can_navigate_top_without_user_gesture; |
Camille Lamy | 1ce9a206 | 2025-02-05 16:02:20 | [diff] [blame] | 106 | out << ", cross_origin_isolationi_enabled_by_dip: " |
| 107 | << policies.cross_origin_isolation_enabled_by_dip; |
Pâris Meuleman | 687f700 | 2022-02-16 11:11:34 | [diff] [blame] | 108 | |
Pâris MEULEMAN | 541156ae | 2021-04-29 07:52:02 | [diff] [blame] | 109 | return out << " }"; |
Titouan Rigoudy | 2f995bc | 2021-02-19 19:39:41 | [diff] [blame] | 110 | } |
| 111 | |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 112 | PolicyContainerPolicies::PolicyContainerPolicies() = default; |
| 113 | |
| 114 | PolicyContainerPolicies::PolicyContainerPolicies( |
| 115 | network::mojom::ReferrerPolicy referrer_policy, |
| 116 | network::mojom::IPAddressSpace ip_address_space, |
| 117 | bool is_web_secure_context, |
| 118 | std::vector<network::mojom::ContentSecurityPolicyPtr> |
Pâris MEULEMAN | 541156ae | 2021-04-29 07:52:02 | [diff] [blame] | 119 | content_security_policies, |
Pâris Meuleman | 00d62b2 | 2022-01-14 13:43:42 | [diff] [blame] | 120 | const network::CrossOriginOpenerPolicy& cross_origin_opener_policy, |
Pâris Meuleman | 687f700 | 2022-02-16 11:11:34 | [diff] [blame] | 121 | const network::CrossOriginEmbedderPolicy& cross_origin_embedder_policy, |
Camille Lamy | ab87007 | 2024-04-16 15:18:10 | [diff] [blame] | 122 | const network::DocumentIsolationPolicy& document_isolation_policy, |
Yoav Weiss | 93d38b50 | 2025-05-06 15:55:51 | [diff] [blame] | 123 | network::IntegrityPolicy integrity_policy, |
| 124 | network::IntegrityPolicy integrity_policy_report_only, |
Arthur Sonzogni | e41678a | 2022-06-16 15:51:19 | [diff] [blame] | 125 | network::mojom::WebSandboxFlags sandbox_flags, |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 126 | bool is_credentialless, |
Jonathan Hao | 81558f6 | 2023-07-11 12:15:22 | [diff] [blame] | 127 | bool can_navigate_top_without_user_gesture, |
Camille Lamy | 1ce9a206 | 2025-02-05 16:02:20 | [diff] [blame] | 128 | bool cross_origin_isolation_enabled_by_dip) |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 129 | : referrer_policy(referrer_policy), |
| 130 | ip_address_space(ip_address_space), |
| 131 | is_web_secure_context(is_web_secure_context), |
Pâris MEULEMAN | 541156ae | 2021-04-29 07:52:02 | [diff] [blame] | 132 | content_security_policies(std::move(content_security_policies)), |
Pâris Meuleman | 00d62b2 | 2022-01-14 13:43:42 | [diff] [blame] | 133 | cross_origin_opener_policy(cross_origin_opener_policy), |
Pâris Meuleman | 687f700 | 2022-02-16 11:11:34 | [diff] [blame] | 134 | cross_origin_embedder_policy(cross_origin_embedder_policy), |
Camille Lamy | ab87007 | 2024-04-16 15:18:10 | [diff] [blame] | 135 | document_isolation_policy(document_isolation_policy), |
Yoav Weiss | 93d38b50 | 2025-05-06 15:55:51 | [diff] [blame] | 136 | integrity_policy(std::move(integrity_policy)), |
| 137 | integrity_policy_report_only(std::move(integrity_policy_report_only)), |
Arthur Sonzogni | e41678a | 2022-06-16 15:51:19 | [diff] [blame] | 138 | sandbox_flags(sandbox_flags), |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 139 | is_credentialless(is_credentialless), |
Liam Brady | e6de25e | 2022-10-11 17:02:30 | [diff] [blame] | 140 | can_navigate_top_without_user_gesture( |
Jonathan Hao | 81558f6 | 2023-07-11 12:15:22 | [diff] [blame] | 141 | can_navigate_top_without_user_gesture), |
Camille Lamy | 1ce9a206 | 2025-02-05 16:02:20 | [diff] [blame] | 142 | cross_origin_isolation_enabled_by_dip( |
| 143 | cross_origin_isolation_enabled_by_dip) {} |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 144 | |
Jonathan Hao | f6cd769 | 2022-08-26 09:18:21 | [diff] [blame] | 145 | PolicyContainerPolicies::PolicyContainerPolicies( |
Patrick Meenan | dd8e2822 | 2025-02-06 15:41:40 | [diff] [blame] | 146 | const blink::mojom::PolicyContainerPolicies& policies, |
| 147 | bool is_web_secure_context) |
Antonio Sartori | d7af7d8 | 2025-04-22 09:24:19 | [diff] [blame] | 148 | : PolicyContainerPolicies(policies.referrer_policy, |
| 149 | policies.ip_address_space, |
| 150 | is_web_secure_context, |
| 151 | mojo::Clone(policies.content_security_policies), |
| 152 | network::CrossOriginOpenerPolicy(), |
| 153 | policies.cross_origin_embedder_policy, |
| 154 | network::DocumentIsolationPolicy(), |
Yoav Weiss | 93d38b50 | 2025-05-06 15:55:51 | [diff] [blame] | 155 | std::move(policies.integrity_policy), |
| 156 | std::move(policies.integrity_policy_report_only), |
Antonio Sartori | d7af7d8 | 2025-04-22 09:24:19 | [diff] [blame] | 157 | policies.sandbox_flags, |
| 158 | policies.is_credentialless, |
| 159 | policies.can_navigate_top_without_user_gesture, |
Antonio Sartori | d7af7d8 | 2025-04-22 09:24:19 | [diff] [blame] | 160 | policies.cross_origin_isolation_enabled_by_dip) {} |
Jonathan Hao | f6cd769 | 2022-08-26 09:18:21 | [diff] [blame] | 161 | |
| 162 | PolicyContainerPolicies::PolicyContainerPolicies( |
| 163 | const GURL& url, |
| 164 | network::mojom::URLResponseHead* response_head, |
| 165 | ContentBrowserClient* client) |
| 166 | : PolicyContainerPolicies( |
| 167 | network::mojom::ReferrerPolicy::kDefault, |
| 168 | CalculateIPAddressSpace(url, response_head, client), |
| 169 | network::IsUrlPotentiallyTrustworthy(url), |
| 170 | mojo::Clone(response_head->parsed_headers->content_security_policy), |
| 171 | response_head->parsed_headers->cross_origin_opener_policy, |
| 172 | response_head->parsed_headers->cross_origin_embedder_policy, |
Camille Lamy | ab87007 | 2024-04-16 15:18:10 | [diff] [blame] | 173 | response_head->parsed_headers->document_isolation_policy, |
Yoav Weiss | 93d38b50 | 2025-05-06 15:55:51 | [diff] [blame] | 174 | response_head->parsed_headers->integrity_policy, |
| 175 | response_head->parsed_headers->integrity_policy_report_only, |
Jonathan Hao | f6cd769 | 2022-08-26 09:18:21 | [diff] [blame] | 176 | network::mojom::WebSandboxFlags::kNone, |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 177 | /*is_credentialless=*/false, |
Jonathan Hao | 81558f6 | 2023-07-11 12:15:22 | [diff] [blame] | 178 | /*can_navigate_top_without_user_gesture=*/true, |
Camille Lamy | 1ce9a206 | 2025-02-05 16:02:20 | [diff] [blame] | 179 | /*cross_origin_isolation_enabled_by_dip=*/false) { |
Jonathan Hao | f6cd769 | 2022-08-26 09:18:21 | [diff] [blame] | 180 | for (auto& content_security_policy : |
| 181 | response_head->parsed_headers->content_security_policy) { |
| 182 | sandbox_flags |= content_security_policy->sandbox; |
| 183 | } |
| 184 | } |
| 185 | |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 186 | PolicyContainerPolicies::PolicyContainerPolicies(PolicyContainerPolicies&&) = |
| 187 | default; |
| 188 | |
| 189 | PolicyContainerPolicies& PolicyContainerPolicies::operator=( |
| 190 | PolicyContainerPolicies&&) = default; |
| 191 | |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 192 | PolicyContainerPolicies::~PolicyContainerPolicies() = default; |
| 193 | |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 194 | PolicyContainerPolicies PolicyContainerPolicies::Clone() const { |
| 195 | return PolicyContainerPolicies( |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 196 | referrer_policy, ip_address_space, is_web_secure_context, |
Pâris Meuleman | 00d62b2 | 2022-01-14 13:43:42 | [diff] [blame] | 197 | mojo::Clone(content_security_policies), cross_origin_opener_policy, |
Camille Lamy | ab87007 | 2024-04-16 15:18:10 | [diff] [blame] | 198 | cross_origin_embedder_policy, mojo::Clone(document_isolation_policy), |
Yoav Weiss | 93d38b50 | 2025-05-06 15:55:51 | [diff] [blame] | 199 | integrity_policy, integrity_policy_report_only, sandbox_flags, |
| 200 | is_credentialless, can_navigate_top_without_user_gesture, |
Camille Lamy | 10109fb | 2025-05-06 12:52:44 | [diff] [blame] | 201 | cross_origin_isolation_enabled_by_dip); |
Antonio Sartori | 5d09b30f | 2021-03-02 09:27:16 | [diff] [blame] | 202 | } |
| 203 | |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 204 | std::unique_ptr<PolicyContainerPolicies> PolicyContainerPolicies::ClonePtr() |
| 205 | const { |
| 206 | return std::make_unique<PolicyContainerPolicies>(Clone()); |
| 207 | } |
| 208 | |
arthursonzogni | adb8088 | 2021-03-11 10:43:30 | [diff] [blame] | 209 | void PolicyContainerPolicies::AddContentSecurityPolicies( |
| 210 | std::vector<network::mojom::ContentSecurityPolicyPtr> policies) { |
| 211 | content_security_policies.insert(content_security_policies.end(), |
| 212 | std::make_move_iterator(policies.begin()), |
| 213 | std::make_move_iterator(policies.end())); |
| 214 | } |
| 215 | |
Jonathan Hao | f6cd769 | 2022-08-26 09:18:21 | [diff] [blame] | 216 | blink::mojom::PolicyContainerPoliciesPtr |
| 217 | PolicyContainerPolicies::ToMojoPolicyContainerPolicies() const { |
| 218 | return blink::mojom::PolicyContainerPolicies::New( |
Yoav Weiss | 93d38b50 | 2025-05-06 15:55:51 | [diff] [blame] | 219 | cross_origin_embedder_policy, integrity_policy, |
| 220 | integrity_policy_report_only, referrer_policy, |
Arthur Sonzogni | 6445759 | 2022-11-22 11:08:59 | [diff] [blame] | 221 | mojo::Clone(content_security_policies), is_credentialless, sandbox_flags, |
Jonathan Hao | 81558f6 | 2023-07-11 12:15:22 | [diff] [blame] | 222 | ip_address_space, can_navigate_top_without_user_gesture, |
Camille Lamy | 10109fb | 2025-05-06 12:52:44 | [diff] [blame] | 223 | cross_origin_isolation_enabled_by_dip); |
Jonathan Hao | f6cd769 | 2022-08-26 09:18:21 | [diff] [blame] | 224 | } |
| 225 | |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 226 | PolicyContainerHost::PolicyContainerHost() = default; |
Titouan Rigoudy | 6ec7040 | 2021-02-02 15:42:19 | [diff] [blame] | 227 | |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 228 | PolicyContainerHost::PolicyContainerHost(PolicyContainerPolicies policies) |
| 229 | : policies_(std::move(policies)) {} |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 230 | |
Sharon Yang | 417a5df | 2024-04-23 17:57:15 | [diff] [blame] | 231 | PolicyContainerHost::~PolicyContainerHost() = default; |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 232 | |
| 233 | void PolicyContainerHost::AssociateWithFrameToken( |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 234 | const blink::LocalFrameToken& frame_token, |
| 235 | int process_id) { |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 236 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 237 | frame_token_ = frame_token; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 238 | process_id_ = process_id; |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 239 | } |
Antonio Sartori | 88ca179 | 2020-10-09 06:26:45 | [diff] [blame] | 240 | |
Antonio Sartori | 9290b6b | 2020-11-09 10:09:33 | [diff] [blame] | 241 | void PolicyContainerHost::SetReferrerPolicy( |
Antonio Sartori | 88ca179 | 2020-10-09 06:26:45 | [diff] [blame] | 242 | network::mojom::ReferrerPolicy referrer_policy) { |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 243 | policies_.referrer_policy = referrer_policy; |
Nate Chapin | 63db0d1 | 2022-01-20 22:03:30 | [diff] [blame] | 244 | if (frame_token_) { |
| 245 | if (RenderFrameHostImpl* rfh = RenderFrameHostImpl::FromFrameToken( |
| 246 | process_id_, frame_token_.value())) { |
| 247 | rfh->DidChangeReferrerPolicy(referrer_policy); |
| 248 | } |
| 249 | } |
Antonio Sartori | 88ca179 | 2020-10-09 06:26:45 | [diff] [blame] | 250 | } |
| 251 | |
Antonio Sartori | 9c850b66 | 2021-03-02 15:13:02 | [diff] [blame] | 252 | void PolicyContainerHost::AddContentSecurityPolicies( |
| 253 | std::vector<network::mojom::ContentSecurityPolicyPtr> |
| 254 | content_security_policies) { |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 255 | policies_.AddContentSecurityPolicies(std::move(content_security_policies)); |
Antonio Sartori | 9c850b66 | 2021-03-02 15:13:02 | [diff] [blame] | 256 | } |
| 257 | |
Antonio Sartori | 9290b6b | 2020-11-09 10:09:33 | [diff] [blame] | 258 | blink::mojom::PolicyContainerPtr |
| 259 | PolicyContainerHost::CreatePolicyContainerForBlink() { |
Antonio Sartori | 5b2f804 | 2020-10-23 18:13:26 | [diff] [blame] | 260 | // This function might be called several times, for example if we need to |
| 261 | // recreate the RenderFrame after the renderer process died. We gracefully |
| 262 | // handle this by resetting the receiver and creating a new one. It would be |
| 263 | // good to find a way to check that the previous remote has been deleted or is |
| 264 | // not needed anymore. Unfortunately, this cannot be done with a disconnect |
| 265 | // handler, since the mojo disconnect notification is not guaranteed to be |
| 266 | // received before we try to create a new remote. |
| 267 | policy_container_host_receiver_.reset(); |
Antonio Sartori | 9859aca | 2021-01-29 14:32:51 | [diff] [blame] | 268 | mojo::PendingAssociatedRemote<blink::mojom::PolicyContainerHost> remote; |
| 269 | Bind(blink::mojom::PolicyContainerBindParams::New( |
| 270 | remote.InitWithNewEndpointAndPassReceiver())); |
| 271 | |
Antonio Sartori | 9290b6b | 2020-11-09 10:09:33 | [diff] [blame] | 272 | return blink::mojom::PolicyContainer::New( |
Jonathan Hao | f6cd769 | 2022-08-26 09:18:21 | [diff] [blame] | 273 | policies_.ToMojoPolicyContainerPolicies(), std::move(remote)); |
Antonio Sartori | 5b2f804 | 2020-10-23 18:13:26 | [diff] [blame] | 274 | } |
| 275 | |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 276 | scoped_refptr<PolicyContainerHost> PolicyContainerHost::Clone() const { |
Titouan Rigoudy | 72f892d | 2022-05-02 18:21:23 | [diff] [blame] | 277 | return base::MakeRefCounted<PolicyContainerHost>(policies_.Clone()); |
Antonio Sartori | 88ca179 | 2020-10-09 06:26:45 | [diff] [blame] | 278 | } |
| 279 | |
Antonio Sartori | 9290b6b | 2020-11-09 10:09:33 | [diff] [blame] | 280 | void PolicyContainerHost::Bind( |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 281 | blink::mojom::PolicyContainerBindParamsPtr bind_params) { |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 282 | policy_container_host_receiver_.Bind(std::move(bind_params->receiver)); |
Antonio Sartori | 9859aca | 2021-01-29 14:32:51 | [diff] [blame] | 283 | |
| 284 | // Keep the PolicyContainerHost alive, as long as its PolicyContainer (owning |
| 285 | // the mojo remote) in the renderer process alive. |
| 286 | scoped_refptr<PolicyContainerHost> copy = this; |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 287 | policy_container_host_receiver_.set_disconnect_handler(base::BindOnce( |
Daniel Cheng | b7b2f5b | 2021-09-25 21:37:19 | [diff] [blame] | 288 | [](scoped_refptr<PolicyContainerHost>) {}, std::move(copy))); |
Antonio Sartori | db967c5 | 2021-01-20 09:54:30 | [diff] [blame] | 289 | } |
| 290 | |
Antonio Sartori | 88ca179 | 2020-10-09 06:26:45 | [diff] [blame] | 291 | } // namespace content |