File: browser_tabs_model.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (96 lines) | stat: -rw-r--r-- 3,392 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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/phonehub/browser_tabs_model.h"

#include "chromeos/ash/components/multidevice/logging/logging.h"

namespace ash::phonehub {

const size_t BrowserTabsModel::kMaxMostRecentTabs = 2;

BrowserTabsModel::BrowserTabMetadata::BrowserTabMetadata(
    GURL url,
    const std::u16string& title,
    base::Time last_accessed_timestamp,
    const gfx::Image& favicon)
    : url(url),
      title(title),
      last_accessed_timestamp(last_accessed_timestamp),
      favicon(favicon) {}

BrowserTabsModel::BrowserTabMetadata::BrowserTabMetadata(
    const BrowserTabMetadata& other) = default;

BrowserTabsModel::BrowserTabMetadata&
BrowserTabsModel::BrowserTabMetadata::operator=(
    const BrowserTabMetadata& other) = default;

bool BrowserTabsModel::BrowserTabMetadata::operator==(
    const BrowserTabMetadata& other) const {
  // The favicon is not compared because equality of gfx::Image is defined
  // by the same storage space rather than the image itself.
  return url == other.url && title == other.title &&
         last_accessed_timestamp == other.last_accessed_timestamp;
}

bool BrowserTabsModel::BrowserTabMetadata::operator!=(
    const BrowserTabMetadata& other) const {
  return !(*this == other);
}

bool BrowserTabsModel::BrowserTabMetadata::operator<(
    const BrowserTabMetadata& other) const {
  // More recently visited tabs should come before earlier visited tabs.
  return std::tie(last_accessed_timestamp) >
         std::tie(other.last_accessed_timestamp);
}

BrowserTabsModel::BrowserTabsModel(
    bool is_tab_sync_enabled,
    const std::vector<BrowserTabMetadata>& most_recent_tabs)
    : is_tab_sync_enabled_(is_tab_sync_enabled),
      most_recent_tabs_(most_recent_tabs) {
  if (!is_tab_sync_enabled_ && !most_recent_tabs_.empty()) {
    PA_LOG(WARNING) << "Tab sync is not enabled, but tab metadata was "
                    << "provided; clearing metadata.";
    most_recent_tabs_.clear();
    return;
  }

  std::sort(most_recent_tabs_.begin(), most_recent_tabs_.end());
  if (most_recent_tabs_.size() > kMaxMostRecentTabs) {
    PA_LOG(WARNING) << "More than the max number of browser tab metadatas were "
                       "provided; truncating least recently visited browser "
                       "tabs' metadatas.";
    most_recent_tabs_.erase(most_recent_tabs_.begin() + kMaxMostRecentTabs,
                            most_recent_tabs_.end());
    return;
  }
}

BrowserTabsModel::BrowserTabsModel(const BrowserTabsModel& other) = default;

BrowserTabsModel::~BrowserTabsModel() = default;

bool BrowserTabsModel::operator==(const BrowserTabsModel& other) const {
  return is_tab_sync_enabled_ == other.is_tab_sync_enabled_ &&
         most_recent_tabs_ == other.most_recent_tabs_;
}

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

std::ostream& operator<<(
    std::ostream& stream,
    BrowserTabsModel::BrowserTabMetadata browser_tab_metadata) {
  stream << "{URL: " << browser_tab_metadata.url.spec() << ", "
         << "Title: " << browser_tab_metadata.title << ", "
         << "Timestamp: " << browser_tab_metadata.last_accessed_timestamp
         << "}";
  return stream;
}

}  // namespace ash::phonehub