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
|
// Copyright 2022 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_web_ui_view.h"
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "chrome/browser/extensions/api/bookmark_manager_private/bookmark_manager_private_api.h"
#include "chrome/browser/feature_engagement/tracker_factory.h"
#include "chrome/browser/performance_manager/public/side_panel_loading_policy.h"
#include "chrome/browser/ui/bookmarks/bookmark_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/side_panel/side_panel_content_proxy.h"
#include "chrome/browser/ui/views/side_panel/side_panel_entry_scope.h"
#include "chrome/browser/ui/views/side_panel/side_panel_util.h"
#include "chrome/browser/ui/webui/webui_embedding_context.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/feature_engagement/public/tracker.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/menu_source_type.mojom.h"
#include "ui/views/controls/menu/menu_runner.h"
SidePanelWebUIView::SidePanelWebUIView(SidePanelEntryScope& scope,
base::RepeatingClosure on_show_cb,
base::RepeatingClosure close_cb,
WebUIContentsWrapper* contents_wrapper)
: on_show_cb_(std::move(on_show_cb)),
close_cb_(std::move(close_cb)),
contents_wrapper_(contents_wrapper) {
const bool is_ready_to_show = contents_wrapper->is_ready_to_show();
SidePanelUtil::GetSidePanelContentProxy(this)->SetAvailable(is_ready_to_show);
SetVisible(is_ready_to_show);
SetID(kSidePanelWebViewId);
contents_wrapper_->SetHost(weak_factory_.GetWeakPtr());
SetWebContents(contents_wrapper_->web_contents());
// The mechanism that ensure the Side Panel will load at high priority even
// while it is still not visible requires the WebContents to be tagged.
performance_manager::execution_context_priority::MarkAsSidePanel(
contents_wrapper_->web_contents());
// For per-window side panels the scoped browser does not change. The browser
// is cleared automatically when the browser is closed.
if (scope.get_scope_type() == SidePanelEntryScope::ScopeType::kBrowser) {
webui::SetBrowserWindowInterface(contents_wrapper_->web_contents(),
&scope.GetBrowserWindowInterface());
} else if (scope.get_scope_type() == SidePanelEntryScope::ScopeType::kTab) {
webui::SetTabInterface(contents_wrapper_->web_contents(),
&scope.GetTabInterface());
// TODO(crbug.com/371950942): Once the new discard implementation has landed
// there is no need to re-set the interface on discard and this can be
// removed.
tab_will_discard_subscription_ =
scope.GetTabInterface().RegisterWillDiscardContents(base::BindRepeating(
[](tabs::TabInterface* tab, content::WebContents* old_contents,
content::WebContents* new_contents) {
webui::SetTabInterface(new_contents, tab);
}));
}
}
SidePanelWebUIView::~SidePanelWebUIView() = default;
void SidePanelWebUIView::ViewHierarchyChanged(
const views::ViewHierarchyChangedDetails& details) {
WebView::ViewHierarchyChanged(details);
// Ensure the WebContents is in a visible state after being added to the
// side panel so the correct lifecycle hooks are triggered.
if (details.is_add && details.child == this) {
contents_wrapper_->web_contents()->WasShown();
}
}
void SidePanelWebUIView::ShowUI() {
SetVisible(true);
SidePanelUtil::GetSidePanelContentProxy(this)->SetAvailable(true);
if (on_show_cb_) {
on_show_cb_.Run();
}
}
void SidePanelWebUIView::CloseUI() {
if (close_cb_) {
close_cb_.Run();
}
}
void SidePanelWebUIView::ShowCustomContextMenu(
gfx::Point point,
std::unique_ptr<ui::MenuModel> menu_model) {
ConvertPointToScreen(this, &point);
context_menu_model_ = std::move(menu_model);
context_menu_runner_ = std::make_unique<views::MenuRunner>(
context_menu_model_.get(),
views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU);
context_menu_runner_->RunMenuAt(
GetWidget(), nullptr, gfx::Rect(point, gfx::Size()),
views::MenuAnchorPosition::kTopLeft, ui::mojom::MenuSourceType::kMouse,
contents_wrapper_->web_contents()->GetContentNativeView());
}
void SidePanelWebUIView::HideCustomContextMenu() {
if (context_menu_runner_) {
context_menu_runner_->Cancel();
}
}
bool SidePanelWebUIView::HandleKeyboardEvent(
content::WebContents* source,
const input::NativeWebKeyboardEvent& event) {
return unhandled_keyboard_event_handler_.HandleKeyboardEvent(
event, GetFocusManager());
}
BEGIN_METADATA(SidePanelWebUIView)
END_METADATA
|