File: caption_bubble_context_views.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 (162 lines) | stat: -rw-r--r-- 5,347 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
// 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/accessibility/caption_bubble_context_views.h"

#include <memory>

#include "base/location.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_tab_strip_tracker.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/accessibility/caption_bubble_session_observer_views.h"
#include "components/live_caption/caption_util.h"
#include "components/live_caption/views/caption_bubble.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/referrer.h"
#include "ui/base/window_open_disposition.h"
#include "ui/views/widget/widget.h"

namespace captions {

// Static
std::unique_ptr<CaptionBubbleContextBrowser>
CaptionBubbleContextBrowser::Create(content::WebContents* web_contents) {
  return std::make_unique<CaptionBubbleContextViews>(web_contents);
}

CaptionBubbleContextViews::CaptionBubbleContextViews(
    content::WebContents* web_contents)
    : CaptionBubbleContextBrowser(web_contents),
      web_contents_(web_contents),
      web_contents_observer_(
          std::make_unique<CaptionBubbleSessionObserverViews>(web_contents)) {
  auto* browser = chrome::FindBrowserWithTab(web_contents_);
  if (!browser || !browser->is_type_normal()) {
    return;
  }
  browser->tab_strip_model()->AddObserver(this);
}

CaptionBubbleContextViews::~CaptionBubbleContextViews() {
  caption_bubble_ = nullptr;
  auto* browser = chrome::FindBrowserWithTab(web_contents_);
  if (!browser || !browser->is_type_normal()) {
    return;
  }
  browser->tab_strip_model()->RemoveObserver(this);
}

void CaptionBubbleContextViews::GetBounds(GetBoundsCallback callback) const {
  if (!web_contents_) {
    return;
  }

  views::Widget* context_widget = views::Widget::GetTopLevelWidgetForNativeView(
      web_contents_->GetNativeView());
  if (!context_widget) {
    return;
  }

  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback),
                                context_widget->GetClientAreaBoundsInScreen()));
}

const std::string CaptionBubbleContextViews::GetSessionId() const {
  return web_contents_->GetBrowserContext()->UniqueId();
}

void CaptionBubbleContextViews::Activate() {
  if (!web_contents_) {
    return;
  }
  // Activate the web contents and the browser window that the web contents is
  // in. Order matters: web contents needs to be active in order for the widget
  // getter to work.
  Browser* browser = chrome::FindBrowserWithTab(web_contents_);
  if (!browser) {
    return;
  }
  TabStripModel* tab_strip_model = browser->tab_strip_model();
  if (!tab_strip_model) {
    return;
  }
  int index = tab_strip_model->GetIndexOfWebContents(web_contents_);
  if (index == TabStripModel::kNoTab) {
    return;
  }
  tab_strip_model->ActivateTabAt(index);
  views::Widget* context_widget = views::Widget::GetTopLevelWidgetForNativeView(
      web_contents_->GetNativeView());
  if (context_widget) {
    context_widget->Activate();
  }
}

bool CaptionBubbleContextViews::IsActivatable() const {
  Browser* browser = chrome::FindBrowserWithTab(web_contents_);
  if (!browser) {
    return false;
  }
  TabStripModel* tab_strip_model = browser->tab_strip_model();
  if (!tab_strip_model) {
    return false;
  }

  return tab_strip_model->GetIndexOfWebContents(web_contents_) !=
         tab_strip_model->active_index();
}

bool CaptionBubbleContextViews::ShouldAvoidOverlap() const {
  // Avoid overlap if web_contents_ doesn't belong to a tab.
  return !chrome::FindBrowserWithTab(web_contents_);
}

std::unique_ptr<CaptionBubbleSessionObserver>
CaptionBubbleContextViews::GetCaptionBubbleSessionObserver() {
  if (web_contents_observer_) {
    return std::move(web_contents_observer_);
  }

  return nullptr;
}

void CaptionBubbleContextViews::OnTabStripModelChanged(
    TabStripModel* tab_strip_model,
    const TabStripModelChange& change,
    const TabStripSelectionChange& selection) {
  if (caption_bubble_) {
    caption_bubble_->OnContextActivatabilityChanged();
  }
}

OpenCaptionSettingsCallback
CaptionBubbleContextViews::GetOpenCaptionSettingsCallback() {
  // Unretained is safe because the caption bubble context outlives the caption
  // bubble that uses this callback.
  return base::BindRepeating(&CaptionBubbleContextViews::OpenCaptionSettings,
                             base::Unretained(this));
}

void CaptionBubbleContextViews::SetContextActivatabilityObserver(
    CaptionBubble* caption_bubble) {
  caption_bubble_ = caption_bubble;
}

void CaptionBubbleContextViews::RemoveContextActivatabilityObserver() {
  caption_bubble_ = nullptr;
}

void CaptionBubbleContextViews::OpenCaptionSettings() {
  content::OpenURLParams params(GURL(GetCaptionSettingsUrl()),
                                content::Referrer(),
                                WindowOpenDisposition::NEW_FOREGROUND_TAB,
                                ui::PAGE_TRANSITION_LINK, false);
  web_contents_->OpenURL(params, /*navigation_handle_callback=*/{});
}
}  // namespace captions