File: side_panel_entry.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 (122 lines) | stat: -rw-r--r-- 4,091 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
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
// Copyright 2021 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/views/side_panel/side_panel_entry.h"

#include "base/functional/callback_helpers.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "chrome/browser/ui/views/side_panel/side_panel_entry_observer.h"
#include "chrome/browser/ui/views/side_panel/side_panel_enums.h"
#include "chrome/browser/ui/views/side_panel/side_panel_util.h"

DEFINE_UI_CLASS_PROPERTY_KEY(bool, kShouldShowTitleInSidePanelHeaderKey, true)

SidePanelEntry::SidePanelEntry(
    Key key,
    CreateContentCallback create_content_callback,
    base::RepeatingCallback<GURL()> open_in_new_tab_url_callback,
    base::RepeatingCallback<std::unique_ptr<ui::MenuModel>()>
        more_info_callback,
    int default_content_width)
    : key_(key),
      create_content_callback_(std::move(create_content_callback)),
      open_in_new_tab_url_callback_(std::move(open_in_new_tab_url_callback)),
      more_info_callback_(std::move(more_info_callback)),
      default_content_width_(default_content_width) {
  DCHECK(create_content_callback_);
  CHECK(!default_content_width ||
        default_content_width >= kSidePanelDefaultContentWidth)
      << "The default width must be greater than or equal to the default side "
         "panel width: "
      << kSidePanelDefaultContentWidth;
}

SidePanelEntry::SidePanelEntry(Key key,
                               CreateContentCallback create_content_callback,
                               int default_content_width)
    : SidePanelEntry(key,
                     std::move(create_content_callback),
                     base::NullCallback(),
                     base::NullCallback(),
                     default_content_width) {}

SidePanelEntry::~SidePanelEntry() = default;

std::unique_ptr<views::View> SidePanelEntry::GetContent() {
  CHECK(scope_);
  if (content_view_) {
    return std::move(content_view_);
  }
  entry_show_triggered_timestamp_ = base::TimeTicks::Now();
  return create_content_callback_.Run(*scope_);
}

void SidePanelEntry::CacheView(std::unique_ptr<views::View> view) {
  content_view_ = std::move(view);
}

void SidePanelEntry::ClearCachedView() {
  content_view_.reset(nullptr);
}

void SidePanelEntry::OnEntryShown() {
  entry_shown_timestamp_ = base::TimeTicks::Now();
  SidePanelUtil::RecordEntryShownMetrics(key_.id(),
                                         entry_show_triggered_timestamp_);
  // After the initial load time is recorded, we need to reset the triggered
  // timestamp so we don't keep recording this entry after its selected from the
  // combobox.
  ResetLoadTimestamp();
  observers_.Notify(&SidePanelEntryObserver::OnEntryShown, this);
}

void SidePanelEntry::OnEntryWillHide(SidePanelEntryHideReason reason) {
  observers_.Notify(&SidePanelEntryObserver::OnEntryWillHide, this, reason);
}

void SidePanelEntry::OnEntryHidden() {
  SidePanelUtil::RecordEntryHiddenMetrics(key_.id(), entry_shown_timestamp_);
  observers_.Notify(&SidePanelEntryObserver::OnEntryHidden, this);
}

void SidePanelEntry::AddObserver(SidePanelEntryObserver* observer) {
  observers_.AddObserver(observer);
}

void SidePanelEntry::RemoveObserver(SidePanelEntryObserver* observer) {
  observers_.RemoveObserver(observer);
}

GURL SidePanelEntry::GetOpenInNewTabURL() const {
  if (open_in_new_tab_url_callback_.is_null()) {
    return GURL();
  }

  return open_in_new_tab_url_callback_.Run();
}

std::unique_ptr<ui::MenuModel> SidePanelEntry::GetMoreInfoMenuModel() const {
  if (more_info_callback_.is_null()) {
    return nullptr;
  }

  return more_info_callback_.Run();
}

bool SidePanelEntry::SupportsNewTabButton() {
  return !open_in_new_tab_url_callback_.is_null();
}

bool SidePanelEntry::SupportsMoreInfoButton() {
  return !more_info_callback_.is_null();
}

void SidePanelEntry::ResetLoadTimestamp() {
  entry_show_triggered_timestamp_ = base::TimeTicks();
}

int SidePanelEntry::GetDefaultContentWidth() const {
  return default_content_width_;
}