File: tab_state_storage_service.cc

package info (click to toggle)
chromium 142.0.7444.175-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,295,352 kB
  • sloc: cpp: 35,488,378; ansic: 7,479,680; javascript: 4,259,373; python: 1,466,843; xml: 757,444; asm: 710,716; pascal: 187,980; sh: 89,247; perl: 88,690; objc: 79,984; sql: 56,984; cs: 42,192; fortran: 24,137; makefile: 22,913; tcl: 15,277; php: 14,018; yacc: 9,005; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (83 lines) | stat: -rw-r--r-- 2,703 bytes parent folder | download | duplicates (4)
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
// Copyright 2025 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/tab/tab_state_storage_service.h"

#include "base/token.h"

namespace tabs {

TabStateStorageService::TabStateStorageService(
    std::unique_ptr<TabStateStorageBackend> tab_backend)
    : tab_backend_(std::move(tab_backend)) {
  tab_backend_->Initialize();
}

TabStateStorageService::~TabStateStorageService() = default;

void TabStateStorageService::SaveTab(
    int id,
    int parent_tab_id,
    int root_id,
    long timestamp_millis,
    const std::string* web_content_state_string,
    int web_content_state_version,
    std::string_view opener_app_id,
    int theme_color,
    int launch_type_at_creation,
    int user_agent,
    long last_navigation_committed_timestamp_millis,
    const base::Token* tab_group_id,
    bool tab_has_sensitive_content,
    bool is_pinned) {
  tabs_pb::TabState tab_state;
  tab_state.set_parent_id(parent_tab_id);
  tab_state.set_root_id(root_id);
  tab_state.set_timestamp_millis(timestamp_millis);

  if (web_content_state_string) {
    tab_state.set_web_contents_state_bytes(*web_content_state_string);
  }
  tab_state.set_web_contents_state_version(web_content_state_version);

  tab_state.set_opener_app_id(opener_app_id);
  tab_state.set_theme_color(theme_color);
  tab_state.set_launch_type_at_creation(launch_type_at_creation);
  tab_state.set_user_agent(user_agent);
  tab_state.set_last_navigation_committed_timestamp_millis(
      last_navigation_committed_timestamp_millis);

  if (tab_group_id) {
    tab_state.set_tab_group_id_high(tab_group_id->high());
    tab_state.set_tab_group_id_low(tab_group_id->low());
  }

  tab_state.set_tab_has_sensitive_content(tab_has_sensitive_content);
  tab_state.set_is_pinned(is_pinned);
  std::string payload;
  tab_state.SerializeToString(&payload);
  tab_backend_->SaveNode(id, 1, std::move(payload), "");
}

void TabStateStorageService::LoadAllTabs(LoadAllTabsCallback callback) {
  tab_backend_->LoadAllNodes(
      base::BindOnce(&TabStateStorageService::OnAllTabsLoaded,
                     base::Unretained(this), std::move(callback)));
}

void TabStateStorageService::OnAllTabsLoaded(LoadAllTabsCallback callback,
                                             std::vector<NodeState> entries) {
  std::vector<tabs_pb::TabState> tab_states;
  for (auto& entry : entries) {
    if (entry.type == 1) {
      tabs_pb::TabState tab_state;
      if (tab_state.ParseFromString(entry.payload)) {
        tab_states.emplace_back(std::move(tab_state));
      }
    }
  }
  std::move(callback).Run(std::move(tab_states));
}

}  // namespace tabs