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 
5 #include "components/sessions/content/session_tab_helper.h"
6 
7 #include "base/memory/ptr_util.h"
8 #include "components/sessions/content/content_serialized_navigation_builder.h"
9 #include "components/sessions/content/session_tab_helper_delegate.h"
10 #include "components/sessions/core/serialized_navigation_entry.h"
11 #include "components/sessions/core/serialized_user_agent_override.h"
12 #include "content/public/browser/navigation_details.h"
13 #include "content/public/browser/web_contents.h"
14 #include "extensions/buildflags/buildflags.h"
15 
16 #if BUILDFLAG(ENABLE_EXTENSIONS)
17 #include "extensions/common/extension_messages.h"
18 #endif
19 
20 namespace sessions {
21 
SessionTabHelper(content::WebContents * contents,DelegateLookup lookup)22 SessionTabHelper::SessionTabHelper(content::WebContents* contents,
23                                    DelegateLookup lookup)
24     : content::WebContentsObserver(contents),
25       delegate_lookup_(std::move(lookup)),
26       session_id_(SessionID::NewUnique()),
27       window_id_(SessionID::InvalidValue()) {}
28 
29 SessionTabHelper::~SessionTabHelper() = default;
30 
CreateForWebContents(content::WebContents * contents,DelegateLookup lookup)31 void SessionTabHelper::CreateForWebContents(content::WebContents* contents,
32                                             DelegateLookup lookup) {
33   DCHECK(contents);
34   if (!FromWebContents(contents)) {
35     contents->SetUserData(UserDataKey(), base::WrapUnique(new SessionTabHelper(
36                                              contents, std::move(lookup))));
37   }
38 }
39 
SetWindowID(const SessionID & id)40 void SessionTabHelper::SetWindowID(const SessionID& id) {
41   window_id_ = id;
42 
43 #if BUILDFLAG(ENABLE_EXTENSIONS)
44   // Extension code in the renderer holds the ID of the window that hosts it.
45   // Notify it that the window ID changed.
46   web_contents()->SendToAllFrames(
47       new ExtensionMsg_UpdateBrowserWindowId(MSG_ROUTING_NONE, id.id()));
48 #endif
49 }
50 
51 // static
IdForTab(const content::WebContents * tab)52 SessionID SessionTabHelper::IdForTab(const content::WebContents* tab) {
53   const SessionTabHelper* session_tab_helper =
54       tab ? SessionTabHelper::FromWebContents(tab) : nullptr;
55   return session_tab_helper ? session_tab_helper->session_id()
56                             : SessionID::InvalidValue();
57 }
58 
59 // static
IdForWindowContainingTab(const content::WebContents * tab)60 SessionID SessionTabHelper::IdForWindowContainingTab(
61     const content::WebContents* tab) {
62   const SessionTabHelper* session_tab_helper =
63       tab ? SessionTabHelper::FromWebContents(tab) : nullptr;
64   return session_tab_helper ? session_tab_helper->window_id()
65                             : SessionID::InvalidValue();
66 }
67 
UserAgentOverrideSet(const blink::UserAgentOverride & ua_override)68 void SessionTabHelper::UserAgentOverrideSet(
69     const blink::UserAgentOverride& ua_override) {
70   SessionTabHelperDelegate* delegate = GetDelegate();
71   if (delegate) {
72     sessions::SerializedUserAgentOverride serialized_override;
73     serialized_override.ua_string_override = ua_override.ua_string_override;
74     serialized_override.opaque_ua_metadata_override =
75         blink::UserAgentMetadata::Marshal(ua_override.ua_metadata_override);
76     delegate->SetTabUserAgentOverride(window_id(), session_id(),
77                                       serialized_override);
78   }
79 }
80 
NavigationEntryCommitted(const content::LoadCommittedDetails & load_details)81 void SessionTabHelper::NavigationEntryCommitted(
82     const content::LoadCommittedDetails& load_details) {
83   SessionTabHelperDelegate* delegate = GetDelegate();
84   if (!delegate)
85     return;
86 
87   int current_entry_index =
88       web_contents()->GetController().GetCurrentEntryIndex();
89   delegate->SetSelectedNavigationIndex(window_id(), session_id(),
90                                        current_entry_index);
91   const SerializedNavigationEntry navigation =
92       ContentSerializedNavigationBuilder::FromNavigationEntry(
93           current_entry_index,
94           web_contents()->GetController().GetEntryAtIndex(current_entry_index));
95   delegate->UpdateTabNavigation(window_id(), session_id(), navigation);
96 }
97 
NavigationListPruned(const content::PrunedDetails & pruned_details)98 void SessionTabHelper::NavigationListPruned(
99     const content::PrunedDetails& pruned_details) {
100   SessionTabHelperDelegate* delegate = GetDelegate();
101   if (!delegate)
102     return;
103 
104   delegate->TabNavigationPathPruned(window_id(), session_id(),
105                                     pruned_details.index, pruned_details.count);
106 }
107 
NavigationEntriesDeleted()108 void SessionTabHelper::NavigationEntriesDeleted() {
109   SessionTabHelperDelegate* delegate = GetDelegate();
110   if (!delegate)
111     return;
112 
113   delegate->TabNavigationPathEntriesDeleted(window_id(), session_id());
114 }
115 
NavigationEntryChanged(const content::EntryChangedDetails & change_details)116 void SessionTabHelper::NavigationEntryChanged(
117     const content::EntryChangedDetails& change_details) {
118   SessionTabHelperDelegate* delegate = GetDelegate();
119   if (!delegate)
120     return;
121 
122   const SerializedNavigationEntry navigation =
123       ContentSerializedNavigationBuilder::FromNavigationEntry(
124           change_details.index, change_details.changed_entry);
125   delegate->UpdateTabNavigation(window_id(), session_id(), navigation);
126 }
127 
GetDelegate()128 SessionTabHelperDelegate* SessionTabHelper::GetDelegate() {
129   return delegate_lookup_ ? delegate_lookup_.Run(web_contents()) : nullptr;
130 }
131 
132 WEB_CONTENTS_USER_DATA_KEY_IMPL(SessionTabHelper)
133 
134 }  // namespace sessions
135