File: tab_model.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (129 lines) | stat: -rw-r--r-- 4,221 bytes parent folder | download | duplicates (5)
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
// Copyright 2012 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/ui/android/tab_model/tab_model.h"

#include "base/metrics/histogram_functions.h"
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/glue/synced_window_delegate_android.h"
#include "chrome/browser/sync/session_sync_service_factory.h"
#include "chrome/browser/sync/sessions/sync_sessions_web_contents_router.h"
#include "chrome/browser/sync/sessions/sync_sessions_web_contents_router_factory.h"
#include "components/omnibox/browser/location_bar_model_impl.h"
#include "components/sync_sessions/open_tabs_ui_delegate.h"
#include "components/sync_sessions/session_sync_service.h"

using chrome::android::ActivityType;

// Keep this in sync with
// chrome/android/java/src/org/chromium/chrome/browser/tabmodel/TabList.java
static int INVALID_TAB_INDEX = -1;

namespace {
sync_sessions::OpenTabsUIDelegate* GetOpenTabsUIDelegate(Profile* profile) {
  sync_sessions::SessionSyncService* service =
      SessionSyncServiceFactory::GetForProfile(profile);

  return service->GetOpenTabsUIDelegate();
}
}  // namespace

TabModel::TabModel(Profile* profile, ActivityType activity_type)
    : profile_(profile),
      activity_type_(activity_type),
      live_tab_context_(new AndroidLiveTabContext(this)),
      synced_window_delegate_(new browser_sync::SyncedWindowDelegateAndroid(
          this,
          activity_type == ActivityType::kTabbed)),
      session_id_(SessionID::NewUnique()) {}

TabModel::~TabModel() = default;

Profile* TabModel::GetProfile() const {
  return profile_;
}

bool TabModel::IsOffTheRecord() const {
  return GetProfile()->IsOffTheRecord();
}

sync_sessions::SyncedWindowDelegate* TabModel::GetSyncedWindowDelegate() const {
  return synced_window_delegate_.get();
}

SessionID TabModel::GetSessionId() const {
  return session_id_;
}

sessions::LiveTabContext* TabModel::GetLiveTabContext() const {
  return live_tab_context_.get();
}

content::WebContents* TabModel::GetActiveWebContents() const {
  int active_index = GetActiveIndex();
  if (active_index == INVALID_TAB_INDEX) {
    return nullptr;
  }
  return GetWebContentsAt(active_index);
}

void TabModel::BroadcastSessionRestoreComplete() {
  sync_sessions::SyncSessionsWebContentsRouter* router =
      sync_sessions::SyncSessionsWebContentsRouterFactory::GetForProfile(
          GetProfile());
  if (router) {
    router->NotifySessionRestoreComplete();
  }

  RecordActualSyncedTabsHistogram();
}

// This logic is loosely based off of
// TabContentsSyncedTabDelegate::ShouldSync().
void TabModel::RecordActualSyncedTabsHistogram() {
  sync_sessions::OpenTabsUIDelegate* open_tabs_delegate =
      GetOpenTabsUIDelegate(GetProfile());
  // This null check will early exit if the user is in incognito mode or
  // if the user has opted out of syncing tabs.
  if (!open_tabs_delegate) {
    return;
  }

  // This null check will early exit if the user has a null sync session,
  // which includes but is not limited to: no synced tabs or an opt out of
  // syncing tabs even if sync is enabled.
  const sync_sessions::SyncedSession* local_session = nullptr;
  if (!open_tabs_delegate->GetLocalSession(&local_session)) {
    return;
  }

  // This check will early exit if there are no tabs in the local model.
  if (GetTabCount() == 0) {
    return;
  }

  int synced_tabs_count = 0;
  for (const auto& [window_id, window] : local_session->windows) {
    synced_tabs_count += window->wrapped_window.tabs.size();
  }

  int eligible_tabs_count = 0;
  for (int i = 0; i < GetTabCount(); i++) {
    if (SessionSyncServiceFactory::ShouldSyncURLForTestingAndMetrics(
            GetTabAt(i)->GetURL())) {
      eligible_tabs_count++;
    }
  }

  // Prevent dividing by 0 in case all tabs are filtered out.
  if (eligible_tabs_count == 0) {
    return;
  }

  int percent_synced = synced_tabs_count * 100 / eligible_tabs_count;
  base::UmaHistogramPercentage("Android.Sync.ActualSyncedTabCountPercentage",
                               percent_synced);
}