File: tab_model.h

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 (298 lines) | stat: -rw-r--r-- 12,127 bytes parent folder | download | duplicates (2)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// 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.

#ifndef CHROME_BROWSER_UI_ANDROID_TAB_MODEL_TAB_MODEL_H_
#define CHROME_BROWSER_UI_ANDROID_TAB_MODEL_TAB_MODEL_H_

#include <memory>

#include "base/android/scoped_java_ref.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/flags/android/chrome_session_state.h"
#include "chrome/browser/ui/android/tab_model/android_live_tab_context.h"
#include "chrome/browser/ui/android/tab_model/tab_list_interface.h"
#include "components/omnibox/browser/location_bar_model.h"
#include "components/omnibox/browser/location_bar_model_delegate.h"
#include "components/sessions/core/session_id.h"
#include "components/sync_sessions/synced_window_delegate.h"

struct NavigateParams;

namespace browser_sync {
class SyncedWindowDelegateAndroid;
}

namespace content {
class WebContents;
}

namespace sync_sessions {
class SyncedWindowDelegate;
}

class Profile;
class TabAndroid;
class TabModelObserver;

// Abstract representation of a Tab Model for Android.  Since Android does
// not use Browser/BrowserList, this is required to allow Chrome to interact
// with Android's Tabs and Tab Model.
class TabModel : public TabListInterface {
 public:
  // LINT.IfChange(TabLaunchType)
  // Various ways tabs can be launched.
  // Values must be numbered from 0 and can't have gaps.
  // This enum is used to back a histogram, entries should not be renumbered or
  // reused.
  // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.tab
  enum class TabLaunchType {
    // Opened from a link. Sets up a relationship between the newly created tab
    // and its parent.
    FROM_LINK,
    // Opened by an external app.
    FROM_EXTERNAL_APP,
    // Catch-all for Tabs opened by Chrome UI not covered by more specific
    // TabLaunchTypes.
    // Examples include:
    // - Tabs created by the options menu.
    // - Tabs created via the New Tab button in the tab stack overview.
    // - Tabs created via Push Notifications.
    // - Tabs opened via a keyboard shortcut.
    FROM_CHROME_UI,
    // Opened during the restoration process on startup or when merging two
    //  instances of
    // Chrome in Android N+ multi-instance mode.
    FROM_RESTORE,
    // Opened from the long press context menu. Will be brought to the
    // foreground.
    // Like FROM_CHROME_UI, but also sets up a parent/child relationship like
    // FROM_LINK.
    FROM_LONGPRESS_FOREGROUND,
    // Opened from the long press context menu. Will not be brought to the
    // foreground.
    // Like FROM_CHROME_UI, but also sets up a parent/child relationship like
    // FROM_LINK.
    FROM_LONGPRESS_BACKGROUND,
    // Changed windows by moving from one activity to another. Will be opened
    // in the foreground.
    FROM_REPARENTING,
    // Opened from a launcher shortcut.
    FROM_LAUNCHER_SHORTCUT,
    // The tab is created by CCT in the background and detached from
    // ChromeActivity.
    FROM_SPECULATIVE_BACKGROUND_CREATION,
    // Opened in the background from Browser Actions context menu.
    FROM_BROWSER_ACTIONS,
    // Opened by an external application launching a new Chrome incognito tab.
    FROM_LAUNCH_NEW_INCOGNITO_TAB,
    // Opened a non-restored tab during the startup process
    FROM_STARTUP,
    // Opened from the start surface.
    // This is deprecated.
    FROM_START_SURFACE,
    // Opened from Tab group UI.
    // Tab group UI include:
    // - "+" button in the bottom tab strip
    // - "+" button in the tab grid dialog
    // - "New tab in group" option in the tab strip group context menu
    // - "Reopen" action in shared tab group messages.
    FROM_TAB_GROUP_UI,
    // Open from the long press context menu item 'Open in new tab in group'.
    // Will not be brought to the foreground.
    FROM_LONGPRESS_BACKGROUND_IN_GROUP,
    // Opened from an app widget.
    FROM_APP_WIDGET,
    // Open from the long press context menu item 'Open in Incognito Tab'.
    // This is deprecated; use `FROM_LONGPRESS_FOREGROUND` in new code.
    FROM_LONGPRESS_INCOGNITO,
    // Opened in background from Recent Tabs. This is a non-link launch with no
    // parent/child relationship. The tab is added to the end of the TabModel.
    // This does not include opening in the current tab.
    FROM_RECENT_TABS,
    // Opened from a Reading list. When going "back" on Android, the Reading
    // list should be reopened.
    FROM_READING_LIST,
    // Opened from Tab Switcher UI.
    FROM_TAB_SWITCHER_UI,
    // Opened from the Restore Tabs UI. When restoring synced tabs the first
    // tab is opened but not brought to the foreground.
    FROM_RESTORE_TABS_UI,
    // Opened to load an omnibox search query in a new tab.
    FROM_OMNIBOX,
    // Used for tab pre-warming where the reason for tab creation is not yet
    // known.
    UNSET,
    // Used when creating a tab to keep synced tab groups up to date.
    FROM_SYNC_BACKGROUND,
    // Open most recent tab in foregroud, used by ctrl-shift-t to restore
    // most recently closed tab or tabs.
    FROM_RECENT_TABS_FOREGROUND,
    // Open a new tab to prevent collaborations from having 0 tabs.
    FROM_COLLABORATION_BACKGROUND_IN_GROUP,
    // Opened from the bookmark bar. Will not be brought to the foreground.
    FROM_BOOKMARK_BAR_BACKGROUND,
    // Changed windows by moving from one activity to another. Will be opened
    // in the background. Use FROM_REPARENTING above to open the re-parented tab
    // in the foreground.
    FROM_REPARENTING_BACKGROUND,
    // From history navigation (back / forward) when opening a new tab/window in
    // the background.
    FROM_HISTORY_NAVIGATION_BACKGROUND,
    // From history navigation (back / forward) when opening a new tab/window in
    // the foreground.
    FROM_HISTORY_NAVIGATION_FOREGROUND,
    // Like FROM_LONGPRESS_FOREGROUND, but used when the parent tab is part of a
    // group.
    FROM_LONGPRESS_FOREGROUND_IN_GROUP,
    // Must be last.
    SIZE
  };
  // When adding a new TabLaunchType, make sure to update the following files.
  // Some of the files have multiple switch cases for TabLaunchType, so make
  // sure to update all of them! Note that there are likely other files that
  // need to be updated depending on the desired side-effects of the new
  // TabLaunchType.
  //
  // Long term, this would ideally be refactored to a traits system such that
  // the different types of side-effects are canonically defined, and listed
  // explicitly for each TabLaunchType in a single location.
  //
  // Multiple lines are not supported by IFTTT.
  // clang-format off
  // LINT.ThenChange(//tools/metrics/histograms/metadata/new_tab_page/enums.xml:TabLaunchType,//chrome/android/java/src/org/chromium/chrome/browser/tabmodel/ChromeTabCreator.java,//chrome/browser/tabpersistence/android/java/src/org/chromium/chrome/browser/tabpersistence/flatbuffer/tab_state_common.fbs,//chrome/browser/tabpersistence/android/java/src/org/chromium/chrome/browser/tabpersistence/FlatBufferTabStateSerializer.java)
  // clang-format on

  // Various ways tabs can be selected.
  // Values must be numbered from 0 and can't have gaps.
  // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.tab
  enum class TabSelectionType {
    // Selection of adjacent tab when the active tab is closed in foreground.
    FROM_CLOSE,
    // Selection of adjacent tab when the active tab is closed upon app exit.
    FROM_EXIT,
    // Selection of newly created tab (e.g. for a URL intent or NTP).
    FROM_NEW,
    // User-originated switch to existing tab or selection of main tab on app
    // startup.
    FROM_USER,
    // User-originated switch to existing tab from Omnibox tab switch
    // suggestions.
    FROM_OMNIBOX,
    // Selection of a previously closed tab when closure is undone.
    FROM_UNDO,
    // Must be last.
    SIZE
  };

  // Various types of user agent.
  // Values must be numbered from 0 and can't have gaps.
  // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.tab
  enum class TabUserAgent {
    // Choose user agent based on default setting.
    DEFAULT,
    // Use mobile user agent.
    MOBILE,
    // Use desktop user agent.
    DESKTOP,
    // User agent not set, due to an earlier version not having the user agent
    // bit.
    UNSET,
    // Must be last.
    SIZE
  };

  TabModel(const TabModel&) = delete;
  TabModel& operator=(const TabModel&) = delete;

  virtual Profile* GetProfile() const;
  virtual bool IsOffTheRecord() const;
  virtual sync_sessions::SyncedWindowDelegate* GetSyncedWindowDelegate() const;
  virtual SessionID GetSessionId() const;
  virtual sessions::LiveTabContext* GetLiveTabContext() const;

  virtual int GetTabCount() const = 0;
  virtual int GetActiveIndex() const = 0;
  virtual content::WebContents* GetActiveWebContents() const;
  virtual content::WebContents* GetWebContentsAt(int index) const = 0;
  // This will return NULL if the tab has not yet been initialized.
  virtual TabAndroid* GetTabAt(int index) const = 0;
  virtual base::android::ScopedJavaLocalRef<jobject> GetJavaObject() const = 0;

  virtual void SetActiveIndex(int index) = 0;
  virtual void ForceCloseAllTabs() = 0;
  virtual void CloseTabAt(int index) = 0;

  // Used for restoring tabs from synced foreign sessions.
  virtual void CreateTab(TabAndroid* parent,
                         content::WebContents* web_contents,
                         bool select) = 0;

  virtual void HandlePopupNavigation(TabAndroid* parent,
                                     NavigateParams* params) = 0;

  // Used by Developer Tools to create a new tab with a given URL.
  // Replaces CreateTabForTesting.
  virtual content::WebContents* CreateNewTabForDevTools(const GURL& url,
                                                        bool new_window) = 0;

  // Return true if we are currently restoring sessions asynchronously.
  virtual bool IsSessionRestoreInProgress() const = 0;

  // Return true if this class is the currently selected in the correspond
  // tab model selector.
  virtual bool IsActiveModel() const = 0;

  // Adds an observer to this TabModel.
  virtual void AddObserver(TabModelObserver* observer) = 0;

  // Removes an observer from this TabModel.
  virtual void RemoveObserver(TabModelObserver* observer) = 0;

  // Return the count of non-custom tabs that were created or had a navigation
  // committed within the time range [`begin_time`, `end_time`).
  virtual int GetTabCountNavigatedInTimeWindow(
      const base::Time& begin_time,
      const base::Time& end_time) const = 0;

  // Closes non-custom tabs that were created or had a navigation
  // committed within the time range [`begin_time`, `end_time`).
  virtual void CloseTabsNavigatedInTimeWindow(const base::Time& begin_time,
                                              const base::Time& end_time) = 0;

  chrome::android::ActivityType activity_type() const { return activity_type_; }

 protected:
  TabModel(Profile* profile, chrome::android::ActivityType activity_type);
  ~TabModel() override;

  // Instructs the TabModel to broadcast a notification that all tabs are now
  // loaded from storage.
  void BroadcastSessionRestoreComplete();

  LocationBarModel* GetLocationBarModel();

 private:
  raw_ptr<Profile, DanglingUntriaged> profile_;

  chrome::android::ActivityType activity_type_;

  // The LiveTabContext associated with TabModel.
  // Used to restore closed tabs through the TabRestoreService.
  std::unique_ptr<AndroidLiveTabContext> live_tab_context_;

  // The SyncedWindowDelegate associated with this TabModel.
  std::unique_ptr<browser_sync::SyncedWindowDelegateAndroid>
      synced_window_delegate_;

  // Unique identifier of this TabModel for session restore. This id is only
  // unique within the current session, and is not guaranteed to be unique
  // across sessions.
  SessionID session_id_;

  // Records metrics about which percentage of syncable tabs are actually
  // synced.
  void RecordActualSyncedTabsHistogram();
};

#endif  // CHROME_BROWSER_UI_ANDROID_TAB_MODEL_TAB_MODEL_H_