File: local_tab_handler.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (184 lines) | stat: -rw-r--r-- 6,080 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/segmentation_platform/client_util/local_tab_handler.h"

#include "base/time/time.h"
#include "components/segmentation_platform/embedder/input_delegate/tab_session_source.h"
#include "components/segmentation_platform/embedder/tab_fetcher.h"
#include "components/segmentation_platform/internal/execution/processing/feature_processor_state.h"
#include "components/segmentation_platform/public/input_delegate.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"

#if BUILDFLAG(IS_ANDROID)

#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"

#else  // BUILDFLAG(IS_ANDROID)

#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/sync/browser_synced_tab_delegate.h"

#endif  // BUILDFLAG(IS_ANDROID)

namespace segmentation_platform::processing {

namespace {

GURL GetLocalTabURL(const TabFetcher::Tab& tab) {
  if (tab.webcontents) {
    return tab.webcontents->GetURL();
  }
#if BUILDFLAG(IS_ANDROID)
  if (tab.tab_android) {
    return tab.tab_android->GetURL();
  }
#endif
  return GURL();
}

// Returns the time since last time the tab was modified.
base::TimeDelta GetLocalTimeSinceModified(const TabFetcher::Tab& tab) {
  base::Time last_modified_timestamp;
  if (tab.webcontents) {
    auto* last_entry = tab.webcontents->GetController().GetLastCommittedEntry();
    if (last_entry) {
      last_modified_timestamp = last_entry->GetTimestamp();
    }
  }
#if BUILDFLAG(IS_ANDROID)
  if (tab.tab_android) {
    last_modified_timestamp = tab.tab_android->GetLastShownTimestamp();
  }
#endif
  return base::Time::Now() - last_modified_timestamp;
}

#if BUILDFLAG(IS_ANDROID)

// Returns a list of all tabs from tab model.
std::vector<TabFetcher::TabEntry> FetchTabs(const Profile* profile) {
  std::vector<TabFetcher::TabEntry> tabs;
  for (const TabModel* model : TabModelList::models()) {
    if (model->GetProfile() != profile) {
      continue;
    }
    // Store count in local variable since it makes expensive JNI call.
    int count = model->GetTabCount();
    for (int i = 0; i < count; ++i) {
      auto* web_contents = model->GetWebContentsAt(i);
      auto* tab_android = model->GetTabAt(i);
      auto tab_id = tab_android->GetSyncedTabDelegate()->GetSessionId();
      tabs.emplace_back(tab_id, web_contents, tab_android);
    }
  }
  return tabs;
}

TabFetcher::Tab FindLocalTabAndroid(const Profile* profile,
                                    const TabFetcher::TabEntry& entry) {
  for (const TabModel* model : TabModelList::models()) {
    if (model->GetProfile() != profile) {
      continue;
    }
    // Store count in local variable since it makes expensive JNI call.
    int count = model->GetTabCount();
    for (int i = 0; i < count; ++i) {
      auto* tab_android = model->GetTabAt(i);
      SessionID id = tab_android->GetSyncedTabDelegate()->GetSessionId();
      if (id != entry.tab_id) {
        continue;
      }
      TabFetcher::Tab result;
      result.webcontents = model->GetWebContentsAt(i);
      result.tab_android = tab_android;
      result.time_since_modified = GetLocalTimeSinceModified(result);
      result.tab_url = GetLocalTabURL(result);
      return result;
    }
  }
  return TabFetcher::Tab();
}

#else  // BUILDFLAG(IS_ANDROID)

// Returns a list of all tabs from tab strip model.
std::vector<TabFetcher::TabEntry> FetchTabs(const Profile* profile) {
  const BrowserList* browser_list = BrowserList::GetInstance();
  std::vector<TabFetcher::TabEntry> tabs;
  for (const Browser* browser : *browser_list) {
    if (browser->profile() != profile) {
      continue;
    }
    for (int i = 0; i < browser->tab_strip_model()->GetTabCount(); ++i) {
      auto* web_contents = browser->tab_strip_model()->GetWebContentsAt(i);
      auto* tab_delegate =
          BrowserSyncedTabDelegate::FromWebContents(web_contents);
      tabs.emplace_back(tab_delegate->GetSessionId(), web_contents, nullptr);
    }
  }
  return tabs;
}

#endif  // BUILDFLAG(IS_ANDROID)

}  // namespace

LocalTabHandler::LocalTabHandler(
    sync_sessions::SessionSyncService* session_sync_service,
    Profile* profile)
    : TabFetcher(session_sync_service), profile_(profile) {}

LocalTabHandler::~LocalTabHandler() = default;

bool LocalTabHandler::FillAllLocalTabsFromTabModel(
    std::vector<TabEntry>& tabs) {
  tabs = FetchTabs(profile_);
  return true;
}

TabFetcher::Tab LocalTabHandler::FindLocalTab(const TabEntry& entry) {
#if BUILDFLAG(IS_ANDROID)
  return FindLocalTabAndroid(profile_, entry);
#else
  TabFetcher::Tab result;
  // Fetch all tabs and verify if the `entry` is still valid.
  auto all_local_tabs = FetchTabs(profile_);
  for (auto& tab : all_local_tabs) {
    if (tab.tab_id == entry.tab_id) {
      result.webcontents =
          reinterpret_cast<content::WebContents*>(tab.web_contents_data.get());
      result.tab_android =
          reinterpret_cast<TabAndroid*>(tab.tab_android_data.get());
      result.time_since_modified = GetLocalTimeSinceModified(result);
      result.tab_url = GetLocalTabURL(result);
      break;
    }
  }
  return result;
#endif
}

LocalTabSource::LocalTabSource(
    sync_sessions::SessionSyncService* session_sync_service,
    TabFetcher* tab_fetcher)
    : TabSessionSource(session_sync_service, tab_fetcher) {}

LocalTabSource::~LocalTabSource() = default;

void LocalTabSource::AddLocalTabInfo(
    const TabFetcher::Tab& tab,
    FeatureProcessorState& feature_processor_state,
    Tensor& inputs) {
  inputs[TabSessionSource::kInputLocalTabTimeSinceModified] =
      ProcessedValue::FromFloat(
          BucketizeExp(GetLocalTimeSinceModified(tab).InSeconds(), /*max_buckets*/50));
}

}  // namespace segmentation_platform::processing