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
|
// 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/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 "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)) {}
CaptionBubbleContextViews::~CaptionBubbleContextViews() = default;
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 {
return true;
}
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;
}
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::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
|