session_tab_helper.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/sessions/content/session_tab_helper.h"
  5. #include "base/memory/ptr_util.h"
  6. #include "components/sessions/content/content_serialized_navigation_builder.h"
  7. #include "components/sessions/content/session_tab_helper_delegate.h"
  8. #include "components/sessions/core/serialized_navigation_entry.h"
  9. #include "components/sessions/core/serialized_user_agent_override.h"
  10. #include "content/public/browser/navigation_details.h"
  11. #include "content/public/browser/web_contents.h"
  12. namespace sessions {
  13. SessionTabHelper::SessionTabHelper(content::WebContents* contents,
  14. DelegateLookup lookup)
  15. : content::WebContentsObserver(contents),
  16. content::WebContentsUserData<SessionTabHelper>(*contents),
  17. delegate_lookup_(std::move(lookup)),
  18. session_id_(SessionID::NewUnique()),
  19. window_id_(SessionID::InvalidValue()) {}
  20. SessionTabHelper::~SessionTabHelper() = default;
  21. void SessionTabHelper::SetWindowID(const SessionID& id) {
  22. window_id_ = id;
  23. window_id_changed_callbacks_.Notify(id);
  24. }
  25. // static
  26. SessionID SessionTabHelper::IdForTab(const content::WebContents* tab) {
  27. const SessionTabHelper* session_tab_helper =
  28. tab ? SessionTabHelper::FromWebContents(tab) : nullptr;
  29. return session_tab_helper ? session_tab_helper->session_id()
  30. : SessionID::InvalidValue();
  31. }
  32. // static
  33. SessionID SessionTabHelper::IdForWindowContainingTab(
  34. const content::WebContents* tab) {
  35. const SessionTabHelper* session_tab_helper =
  36. tab ? SessionTabHelper::FromWebContents(tab) : nullptr;
  37. return session_tab_helper ? session_tab_helper->window_id()
  38. : SessionID::InvalidValue();
  39. }
  40. base::CallbackListSubscription SessionTabHelper::RegisterForWindowIdChanged(
  41. WindowIdChangedCallbackList::CallbackType callback) {
  42. return window_id_changed_callbacks_.Add(std::move(callback));
  43. }
  44. void SessionTabHelper::UserAgentOverrideSet(
  45. const blink::UserAgentOverride& ua_override) {
  46. SessionTabHelperDelegate* delegate = GetDelegate();
  47. if (delegate) {
  48. sessions::SerializedUserAgentOverride serialized_override;
  49. serialized_override.ua_string_override = ua_override.ua_string_override;
  50. serialized_override.opaque_ua_metadata_override =
  51. blink::UserAgentMetadata::Marshal(ua_override.ua_metadata_override);
  52. delegate->SetTabUserAgentOverride(window_id(), session_id(),
  53. serialized_override);
  54. }
  55. }
  56. void SessionTabHelper::NavigationEntryCommitted(
  57. const content::LoadCommittedDetails& load_details) {
  58. SessionTabHelperDelegate* delegate = GetDelegate();
  59. if (!delegate)
  60. return;
  61. int current_entry_index =
  62. web_contents()->GetController().GetCurrentEntryIndex();
  63. delegate->SetSelectedNavigationIndex(window_id(), session_id(),
  64. current_entry_index);
  65. const SerializedNavigationEntry navigation =
  66. ContentSerializedNavigationBuilder::FromNavigationEntry(
  67. current_entry_index,
  68. web_contents()->GetController().GetEntryAtIndex(current_entry_index));
  69. delegate->UpdateTabNavigation(window_id(), session_id(), navigation);
  70. }
  71. void SessionTabHelper::NavigationListPruned(
  72. const content::PrunedDetails& pruned_details) {
  73. SessionTabHelperDelegate* delegate = GetDelegate();
  74. if (!delegate)
  75. return;
  76. delegate->TabNavigationPathPruned(window_id(), session_id(),
  77. pruned_details.index, pruned_details.count);
  78. }
  79. void SessionTabHelper::NavigationEntriesDeleted() {
  80. SessionTabHelperDelegate* delegate = GetDelegate();
  81. if (!delegate)
  82. return;
  83. delegate->TabNavigationPathEntriesDeleted(window_id(), session_id());
  84. }
  85. void SessionTabHelper::NavigationEntryChanged(
  86. const content::EntryChangedDetails& change_details) {
  87. SessionTabHelperDelegate* delegate = GetDelegate();
  88. if (!delegate)
  89. return;
  90. const SerializedNavigationEntry navigation =
  91. ContentSerializedNavigationBuilder::FromNavigationEntry(
  92. change_details.index, change_details.changed_entry);
  93. delegate->UpdateTabNavigation(window_id(), session_id(), navigation);
  94. }
  95. SessionTabHelperDelegate* SessionTabHelper::GetDelegate() {
  96. return delegate_lookup_ ? delegate_lookup_.Run(web_contents()) : nullptr;
  97. }
  98. WEB_CONTENTS_USER_DATA_KEY_IMPL(SessionTabHelper);
  99. } // namespace sessions