Daniel Cheng | 69e45da | 2023-11-13 02:03:05 | [diff] [blame] | 1 | // Copyright 2023 The Chromium Authors |
| 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/document_associated_data.h" |
| 6 | |
Daniel Cheng | f549b26 | 2024-09-06 01:29:24 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
Daniel Cheng | 69e45da | 2023-11-13 02:03:05 | [diff] [blame] | 9 | #include "base/check.h" |
Nan Lin | 389716b | 2025-02-28 22:59:23 | [diff] [blame] | 10 | #include "base/check_op.h" |
Daniel Cheng | 69e45da | 2023-11-13 02:03:05 | [diff] [blame] | 11 | #include "base/containers/map_util.h" |
Nan Lin | 389716b | 2025-02-28 22:59:23 | [diff] [blame] | 12 | #include "base/containers/queue.h" |
| 13 | #include "base/functional/callback_forward.h" |
David Sanders | f583e50 | 2025-02-03 01:55:00 | [diff] [blame] | 14 | #include "base/metrics/histogram_functions.h" |
Daniel Cheng | 69e45da | 2023-11-13 02:03:05 | [diff] [blame] | 15 | #include "base/no_destructor.h" |
| 16 | #include "content/browser/navigation_or_document_handle.h" |
| 17 | #include "content/browser/renderer_host/frame_tree.h" |
| 18 | #include "content/browser/renderer_host/page_factory.h" |
| 19 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
| 20 | #include "content/public/browser/document_service.h" |
| 21 | #include "content/public/browser/document_service_internal.h" |
Nan Lin | 389716b | 2025-02-28 22:59:23 | [diff] [blame] | 22 | #include "content/public/browser/render_frame_host.h" |
Chris Fredrickson | 2b6b8c0 | 2025-05-09 16:15:35 | [diff] [blame] | 23 | #include "net/cookies/cookie_setting_override.h" |
Daniel Cheng | 0d143e5 | 2025-04-03 19:43:16 | [diff] [blame] | 24 | #include "third_party/abseil-cpp/absl/container/flat_hash_map.h" |
Daniel Cheng | 69e45da | 2023-11-13 02:03:05 | [diff] [blame] | 25 | #include "third_party/blink/public/common/tokens/tokens.h" |
| 26 | |
| 27 | namespace content { |
| 28 | |
| 29 | namespace { |
| 30 | auto& GetDocumentTokenMap() { |
Daniel Cheng | 0d143e5 | 2025-04-03 19:43:16 | [diff] [blame] | 31 | static base::NoDestructor< |
| 32 | absl::flat_hash_map<blink::DocumentToken, RenderFrameHostImpl*>> |
Daniel Cheng | 69e45da | 2023-11-13 02:03:05 | [diff] [blame] | 33 | map; |
| 34 | return *map; |
| 35 | } |
| 36 | } // namespace |
| 37 | |
| 38 | RenderFrameHostImpl* DocumentAssociatedData::GetDocumentFromToken( |
| 39 | base::PassKey<RenderFrameHostImpl>, |
| 40 | const blink::DocumentToken& token) { |
| 41 | return base::FindPtrOrNull(GetDocumentTokenMap(), token); |
| 42 | } |
| 43 | |
| 44 | DocumentAssociatedData::DocumentAssociatedData( |
| 45 | RenderFrameHostImpl& document, |
| 46 | const blink::DocumentToken& token) |
| 47 | : token_(token), weak_factory_(&document) { |
| 48 | auto [_, inserted] = GetDocumentTokenMap().insert({token_, &document}); |
| 49 | CHECK(inserted); |
| 50 | |
| 51 | // Only create page object for the main document as the PageImpl is 1:1 with |
| 52 | // main document. |
| 53 | if (!document.GetParent()) { |
| 54 | PageDelegate* page_delegate = document.frame_tree()->page_delegate(); |
| 55 | DCHECK(page_delegate); |
| 56 | owned_page_ = PageFactory::Create(document, *page_delegate); |
| 57 | } |
| 58 | } |
| 59 | |
Daniel Cheng | 69e45da | 2023-11-13 02:03:05 | [diff] [blame] | 60 | DocumentAssociatedData::~DocumentAssociatedData() { |
Rakina Zata Amni | 50805f2 | 2024-09-11 06:23:50 | [diff] [blame] | 61 | TRACE_EVENT0("navigation", "DocumentAssociatedData::~DocumentAssociatedData"); |
| 62 | base::ScopedUmaHistogramTimer histogram_timer( |
| 63 | "Navigation.DocumentAssociatedDataDestructor"); |
Daniel Cheng | f549b26 | 2024-09-06 01:29:24 | [diff] [blame] | 64 | decltype(services_) services; |
| 65 | std::swap(services_, services); |
| 66 | for (auto& service : services) { |
| 67 | service->WillBeDestroyed( |
| 68 | DocumentServiceDestructionReason::kEndOfDocumentLifetime); |
| 69 | service->ResetAndDeleteThisInternal({}); |
| 70 | } |
Daniel Cheng | 69e45da | 2023-11-13 02:03:05 | [diff] [blame] | 71 | |
| 72 | // Explicitly clear all user data here, so that the other fields of |
| 73 | // DocumentAssociatedData are still valid while user data is being destroyed. |
| 74 | ClearAllUserData(); |
| 75 | |
Alex Kalugin | cc9dc3c | 2024-03-07 06:54:32 | [diff] [blame] | 76 | // Explicitly clear all PageUserData here before destruction of |owned_page_| |
| 77 | // (A std::unique_ptr's stored pointer value is (intentionally) undefined |
| 78 | // during destruction (e.g. it could be nullptr)), so that |owned_page_| and |
| 79 | // the other fields of DocumentAssociatedData are still valid and accessible |
| 80 | // from RenderFrameHost interface while its page user data is being destroyed. |
| 81 | if (owned_page_) { |
| 82 | owned_page_->ClearAllUserData(); |
| 83 | } |
| 84 | |
Daniel Cheng | 69e45da | 2023-11-13 02:03:05 | [diff] [blame] | 85 | // Last in case any DocumentService / DocumentUserData service destructors try |
| 86 | // to look up RenderFrameHosts by DocumentToken. |
| 87 | CHECK_EQ(1u, GetDocumentTokenMap().erase(token_)); |
| 88 | } |
| 89 | |
| 90 | void DocumentAssociatedData::set_navigation_or_document_handle( |
| 91 | scoped_refptr<NavigationOrDocumentHandle> handle) { |
| 92 | navigation_or_document_handle_ = std::move(handle); |
| 93 | } |
| 94 | |
Daniel Cheng | f549b26 | 2024-09-06 01:29:24 | [diff] [blame] | 95 | void DocumentAssociatedData::AddService( |
| 96 | internal::DocumentServiceBase* service, |
| 97 | base::PassKey<internal::DocumentServiceBase>) { |
| 98 | services_.push_back(service); |
| 99 | } |
| 100 | |
| 101 | void DocumentAssociatedData::RemoveService( |
| 102 | internal::DocumentServiceBase* service, |
| 103 | base::PassKey<internal::DocumentServiceBase>) { |
| 104 | std::erase(services_, service); |
| 105 | } |
| 106 | |
Nan Lin | 389716b | 2025-02-28 22:59:23 | [diff] [blame] | 107 | void DocumentAssociatedData::AddPostPrerenderingActivationStep( |
| 108 | base::OnceClosure callback) { |
| 109 | CHECK_EQ(GetSafeRef()->GetLifecycleState(), |
| 110 | RenderFrameHost::LifecycleState::kPrerendering); |
| 111 | post_prerendering_activation_callbacks_.push(std::move(callback)); |
| 112 | } |
| 113 | |
| 114 | void DocumentAssociatedData::RunPostPrerenderingActivationSteps() { |
| 115 | CHECK_NE(GetSafeRef()->GetLifecycleState(), |
| 116 | RenderFrameHost::LifecycleState::kPrerendering); |
| 117 | while (!post_prerendering_activation_callbacks_.empty()) { |
| 118 | std::move(post_prerendering_activation_callbacks_.front()).Run(); |
| 119 | post_prerendering_activation_callbacks_.pop(); |
Nan Lin | c643c96 | 2025-02-21 01:47:43 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Chris Fredrickson | 2b6b8c0 | 2025-05-09 16:15:35 | [diff] [blame] | 123 | void DocumentAssociatedData::PutCookieSettingOverride( |
| 124 | net::CookieSettingOverride cookie_setting_override) { |
| 125 | cookie_setting_overrides_.Put(cookie_setting_override); |
| 126 | } |
| 127 | |
| 128 | void DocumentAssociatedData::RemoveCookieSettingOverride( |
| 129 | net::CookieSettingOverride cookie_setting_override) { |
| 130 | cookie_setting_overrides_.Remove(cookie_setting_override); |
| 131 | } |
| 132 | |
Daniel Cheng | 69e45da | 2023-11-13 02:03:05 | [diff] [blame] | 133 | } // namespace content |