File: recently_closed_tabs_bridge.cc

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (433 lines) | stat: -rw-r--r-- 14,257 bytes parent folder | download
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Copyright 2013 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/android/recently_closed_tabs_bridge.h"

#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/token_android.h"
#include "base/containers/span.h"
#include "base/numerics/safe_conversions.h"
#include "base/token.h"
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/flags/android/chrome_feature_list.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/session_restore.h"
#include "chrome/browser/sessions/tab_restore_service_factory.h"
#include "chrome/browser/ui/android/tab_model/android_live_tab_context.h"
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#include "components/sessions/core/live_tab.h"
#include "components/sessions/core/tab_restore_service.h"
#include "content/public/browser/web_contents.h"
#include "url/android/gurl_android.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/android/chrome_jni_headers/RecentlyClosedBridge_jni.h"
#include "chrome/android/chrome_jni_headers/RecentlyClosedTab_jni.h"

using base::android::AttachCurrentThread;
using base::android::JavaParamRef;
using base::android::JavaRef;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;

namespace recent_tabs {
namespace {

bool TabEntryWithIdExists(const sessions::TabRestoreService::Entries& entries,
                          SessionID session_id) {
  const auto end = TabIterator::end(entries);
  for (auto it = TabIterator::begin(entries); it != end; ++it) {
    if (it->id == session_id) {
      return true;
    }
  }
  return false;
}

jni_zero::ScopedJavaLocalRef<jobject> CreateJavaRecentlyClosedTab(
    JNIEnv* env,
    const sessions::tab_restore::Tab& tab) {
  const sessions::SerializedNavigationEntry& current_navigation =
      tab.navigations.at(tab.current_navigation_index);
  return Java_RecentlyClosedTab_Constructor(
      env, tab.id.id(), tab.timestamp.InMillisecondsSinceUnixEpoch(),
      current_navigation.title(), current_navigation.virtual_url(),
      tab.group ? std::optional<base::Token>(tab.group->token())
                : std::nullopt);
}

std::vector<jni_zero::ScopedJavaLocalRef<jobject>> PrepareTabs(
    JNIEnv* env,
    TabIterator& it,
    const sessions::TabRestoreService::Entries::const_iterator& current_entry,
    size_t tab_count) {
  std::vector<jni_zero::ScopedJavaLocalRef<jobject>> ret;
  ret.reserve(tab_count);
  while (it.CurrentEntry() == current_entry) {
    ret.push_back(CreateJavaRecentlyClosedTab(env, *it));
    ++it;
  }
  return ret;
}

// Add a tab entry to the main entries list.
void AddTabToEntries(JNIEnv* env,
                     const sessions::tab_restore::Tab& tab,
                     const JavaRef<jobject>& jentries) {
  Java_RecentlyClosedBridge_addTabToEntries(
      env, jentries, CreateJavaRecentlyClosedTab(env, tab));
}

void AddGroupToEntries(
    JNIEnv* env,
    TabIterator& it,
    const sessions::TabRestoreService::Entries::const_iterator& current_entry,
    const sessions::tab_restore::Group& group,
    const JavaRef<jobject>& jentries) {
  std::vector<jni_zero::ScopedJavaLocalRef<jobject>> tabs =
      PrepareTabs(env, it, current_entry, group.tabs.size());

  Java_RecentlyClosedBridge_addGroupToEntries(
      env, jentries, group.id.id(),
      group.timestamp.InMillisecondsSinceUnixEpoch(), group.visual_data.title(),
      static_cast<int>(group.visual_data.color()), tabs);
}

void AddBulkEventToEntries(
    JNIEnv* env,
    TabIterator& it,
    const sessions::TabRestoreService::Entries::const_iterator& current_entry,
    const sessions::tab_restore::Window& window,
    const JavaRef<jobject>& jentries) {
  std::vector<jni_zero::ScopedJavaLocalRef<jobject>> tabs =
      PrepareTabs(env, it, current_entry, window.tabs.size());

  std::vector<std::optional<base::Token>> group_ids;
  std::vector<const std::u16string*> group_titles;

  const size_t group_count = window.tab_groups.size();
  group_ids.reserve(group_count);
  group_titles.reserve(group_count);
  for (const auto& tab_group : window.tab_groups) {
    group_ids.push_back(tab_group.first.token());
    group_titles.push_back(&tab_group.second->visual_data.title());
  }

  Java_RecentlyClosedBridge_addBulkEventToEntries(
      env, jentries, window.id.id(),
      window.timestamp.InMillisecondsSinceUnixEpoch(), group_ids, group_titles,
      tabs);
}

// Add `entries` to `jentries`.
void AddEntriesToList(JNIEnv* env,
                      const sessions::TabRestoreService::Entries& entries,
                      const JavaRef<jobject>& jentries,
                      int max_entry_count) {
  int added_count = 0;
  for (auto it = TabIterator::begin(entries);
       it != TabIterator::end(entries) && added_count < max_entry_count;
       ++added_count) {
    if (it.IsCurrentEntryTab()) {
      AddTabToEntries(env, *it, jentries);
      ++it;
      continue;
    }

    auto entry = it.CurrentEntry();
    if ((*entry)->type == sessions::tab_restore::Type::GROUP) {
      const auto& group =
          static_cast<const sessions::tab_restore::Group&>(**entry);
      AddGroupToEntries(env, it, entry, group, jentries);
      continue;
    }
    if ((*entry)->type == sessions::tab_restore::Type::WINDOW) {
      const auto& window =
          static_cast<const sessions::tab_restore::Window&>(**entry);
      AddBulkEventToEntries(env, it, entry, window, jentries);
      continue;
    }
    NOTREACHED();
  }
}

}  // namespace

TabIterator::TabIterator(
    const sessions::TabRestoreService::Entries& entries,
    sessions::TabRestoreService::Entries::const_iterator it)
    : entries_(entries), current_entry_(it) {
  SetupInnerTabList();
}
TabIterator::~TabIterator() = default;

// static.
TabIterator TabIterator::begin(
    const sessions::TabRestoreService::Entries& entries) {
  return TabIterator(entries, entries.cbegin());
}

// static.
TabIterator TabIterator::end(
    const sessions::TabRestoreService::Entries& entries) {
  return TabIterator(entries, entries.cend());
}

bool TabIterator::IsCurrentEntryTab() const {
  return (*current_entry_)->type == sessions::tab_restore::Type::TAB;
}

sessions::TabRestoreService::Entries::const_iterator TabIterator::CurrentEntry()
    const {
  return current_entry_;
}

TabIterator& TabIterator::operator++() {
  // Early out at end.
  if (current_entry_ == entries_->cend()) {
    return *this;
  }

  // Iterate backward over current set of tabs if possible.
  if (current_tab_ && tabs_ && current_tab_ != tabs_->crend()) {
    (*current_tab_)++;
    if (*current_tab_ != tabs_->crend()) {
      return *this;
    }
  }

  // At the end of an entry then go to the next entry.
  tabs_ = nullptr;
  current_tab_ = std::nullopt;
  current_entry_++;
  if (current_entry_ == entries_->cend()) {
    return *this;
  }

  SetupInnerTabList();

  return *this;
}

TabIterator TabIterator::operator++(int) {
  TabIterator retval = *this;
  ++(*this);
  return retval;
}

bool TabIterator::operator==(TabIterator other) const {
  return current_entry_ == other.current_entry_ &&
         current_tab_ == other.current_tab_;
}

bool TabIterator::operator!=(TabIterator other) const {
  return !(*this == other);
}

const sessions::tab_restore::Tab& TabIterator::operator*() const {
  return current_tab_
             ? ***current_tab_
             : static_cast<const sessions::tab_restore::Tab&>(**current_entry_);
}

const sessions::tab_restore::Tab* TabIterator::operator->() const {
  return current_tab_ ? (*current_tab_)->get()
                      : static_cast<const sessions::tab_restore::Tab*>(
                            current_entry_->get());
}

void TabIterator::SetupInnerTabList() {
  if (current_entry_ == entries_->cend()) {
    return;
  }

  if ((*current_entry_)->type == sessions::tab_restore::Type::GROUP) {
    tabs_ =
        &static_cast<const sessions::tab_restore::Group*>(current_entry_->get())
             ->tabs;
  }
  if ((*current_entry_)->type == sessions::tab_restore::Type::WINDOW) {
    tabs_ = &static_cast<const sessions::tab_restore::Window*>(
                 current_entry_->get())
                 ->tabs;
  }
  if (tabs_) {
    current_tab_ = tabs_->crbegin();
    if (current_tab_ == tabs_->crend()) {
      ++(*this);
    }
  }
}

RecentlyClosedTabsBridge::RecentlyClosedTabsBridge(
    ScopedJavaGlobalRef<jobject> jbridge,
    Profile* profile)
    : bridge_(std::move(jbridge)),
      profile_(profile),
      tab_restore_service_(nullptr) {}

RecentlyClosedTabsBridge::~RecentlyClosedTabsBridge() {
  if (tab_restore_service_) {
    tab_restore_service_->RemoveObserver(this);
  }
}

void RecentlyClosedTabsBridge::Destroy(JNIEnv* env) {
  delete this;
}

jboolean RecentlyClosedTabsBridge::GetRecentlyClosedEntries(
    JNIEnv* env,
    const JavaParamRef<jobject>& jentries_list,
    jint max_entry_count) {
  EnsureTabRestoreService();
  if (!tab_restore_service_) {
    return false;
  }

  AddEntriesToList(env, tab_restore_service_->entries(), jentries_list,
                   max_entry_count);
  return true;
}

jboolean RecentlyClosedTabsBridge::OpenRecentlyClosedTab(
    JNIEnv* env,
    const JavaParamRef<jobject>& jtab_model,
    jint tab_session_id,
    jint j_disposition) {
  if (!tab_restore_service_) {
    return false;
  }

  SessionID entry_id = SessionID::FromSerializedValue(tab_session_id);
  // Ensure the corresponding tab entry from TabRestoreService is a tab.
  if (!TabEntryWithIdExists(tab_restore_service_->entries(), entry_id)) {
    return false;
  }

  auto* model = TabModelList::FindNativeTabModelForJavaObject(
      ScopedJavaLocalRef<jobject>(env, jtab_model.obj()));
  if (model == nullptr) {
    return false;
  }

  AndroidLiveTabContextRestoreWrapper restore_context(model);
  std::vector<sessions::LiveTab*> restored_tabs =
      tab_restore_service_->RestoreEntryById(
          &restore_context, entry_id,
          static_cast<WindowOpenDisposition>(j_disposition));
  return !restored_tabs.empty();
}

jboolean RecentlyClosedTabsBridge::OpenRecentlyClosedEntry(
    JNIEnv* env,
    const JavaParamRef<jobject>& jtab_model,
    jint entry_session_id) {
  // This should only be called when in bulk restore mode otherwise per-tab
  // restore should always be used.
  if (!tab_restore_service_) {
    return false;
  }

  auto* model = TabModelList::FindNativeTabModelForJavaObject(
      ScopedJavaLocalRef<jobject>(env, jtab_model.obj()));
  if (model == nullptr) {
    return false;
  }

  AndroidLiveTabContextRestoreWrapper restore_context(model);
  std::vector<sessions::LiveTab*> restored_tabs =
      tab_restore_service_->RestoreEntryById(
          &restore_context, SessionID::FromSerializedValue(entry_session_id),
          WindowOpenDisposition::NEW_BACKGROUND_TAB);
  RestoreAndroidTabGroups(env, jtab_model, restore_context.GetTabGroups());
  return !restored_tabs.empty();
}

jboolean RecentlyClosedTabsBridge::OpenMostRecentlyClosedEntry(
    JNIEnv* env,
    const JavaParamRef<jobject>& jtab_model) {
  EnsureTabRestoreService();
  if (!tab_restore_service_ || tab_restore_service_->entries().empty()) {
    return false;
  }

  auto* model = TabModelList::FindNativeTabModelForJavaObject(
      ScopedJavaLocalRef<jobject>(env, jtab_model.obj()));
  if (model == nullptr) {
    return false;
  }

  AndroidLiveTabContextRestoreWrapper restore_context(model);
  std::vector<sessions::LiveTab*> restored_tabs;
  // Do not use OpenMostRecentEntry as it uses WindowOpenDisposition::UNKNOWN.
  // WindowOpenDisposition::UNKNOWN looks for a desktop window to use (N/A on
  // Android) this ends up replacing `restore_context` with the base
  // AndroidLiveTabContext. `restore_context` is required to rebuild groups
  // information. To avoid this just use the first entry in entries when
  // restoring.
  restored_tabs = tab_restore_service_->RestoreEntryById(
      &restore_context, tab_restore_service_->entries().front()->id,
      WindowOpenDisposition::NEW_BACKGROUND_TAB);
  RestoreAndroidTabGroups(env, jtab_model, restore_context.GetTabGroups());
  return !restored_tabs.empty();
}

void RecentlyClosedTabsBridge::ClearRecentlyClosedEntries(JNIEnv* env) {
  EnsureTabRestoreService();
  if (tab_restore_service_) {
    tab_restore_service_->ClearEntries();
  }
}

void RecentlyClosedTabsBridge::TabRestoreServiceChanged(
    sessions::TabRestoreService* service) {
  Java_RecentlyClosedBridge_onUpdated(AttachCurrentThread(), bridge_);
}

void RecentlyClosedTabsBridge::TabRestoreServiceDestroyed(
    sessions::TabRestoreService* service) {
  tab_restore_service_ = nullptr;
}

void RecentlyClosedTabsBridge::EnsureTabRestoreService() {
  if (tab_restore_service_) {
    return;
  }

  tab_restore_service_ = TabRestoreServiceFactory::GetForProfile(profile_);

  // TabRestoreServiceFactory::GetForProfile() can return nullptr (e.g. in
  // incognito mode).
  if (tab_restore_service_) {
    // This does nothing if the tabs have already been loaded or they
    // shouldn't be loaded.
    tab_restore_service_->LoadTabsFromLastSession();
    tab_restore_service_->AddObserver(this);
  }
}

void RecentlyClosedTabsBridge::RestoreAndroidTabGroups(
    JNIEnv* env,
    const base::android::JavaParamRef<jobject>& jtab_model,
    const std::map<tab_groups::TabGroupId,
                   AndroidLiveTabContextRestoreWrapper::TabGroup>& groups) {
  for (const auto& group : groups) {
    Java_RecentlyClosedBridge_restoreTabGroup(
        env, bridge_, jtab_model, group.second.visual_data.title(),
        (int)group.second.visual_data.color(), group.second.tab_ids);
  }
}

static jlong JNI_RecentlyClosedBridge_Init(JNIEnv* env,
                                           const JavaParamRef<jobject>& jbridge,
                                           Profile* profile) {
  RecentlyClosedTabsBridge* bridge = new RecentlyClosedTabsBridge(
      ScopedJavaGlobalRef<jobject>(env, jbridge.obj()), profile);
  return reinterpret_cast<intptr_t>(bridge);
}

}  // namespace recent_tabs