File: side_panel_web_ui_view.h

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 (99 lines) | stat: -rw-r--r-- 3,685 bytes parent folder | download | duplicates (5)
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
// 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.

#ifndef CHROME_BROWSER_UI_VIEWS_SIDE_PANEL_SIDE_PANEL_WEB_UI_VIEW_H_
#define CHROME_BROWSER_UI_VIEWS_SIDE_PANEL_SIDE_PANEL_WEB_UI_VIEW_H_

#include <memory>

#include "base/callback_list.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/ui/webui/top_chrome/webui_contents_wrapper.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
#include "ui/views/controls/webview/webview.h"

namespace ui {
class MenuModel;
}  // namespace ui

namespace views {
class MenuRunner;
}  // namespace views

class SidePanelEntryScope;

// SidePanelWebUIView holds generic behavior for side panel entry views hosting
// WebUI. This includes keyboard event handling, context menu triggering, and
// handling visibility updates.
class SidePanelWebUIView : public views::WebView,
                           public WebUIContentsWrapper::Host {
  METADATA_HEADER(SidePanelWebUIView, views::WebView)

 public:
  static inline constexpr int kSidePanelWebViewId = 777;
  SidePanelWebUIView(SidePanelEntryScope& scope,
                     base::RepeatingClosure on_show_cb,
                     base::RepeatingClosure close_cb,
                     WebUIContentsWrapper* contents_wrapper);
  SidePanelWebUIView(const SidePanelWebUIView&) = delete;
  SidePanelWebUIView& operator=(const SidePanelWebUIView&) = delete;
  ~SidePanelWebUIView() override;

  // views::WebView:
  void ViewHierarchyChanged(
      const views::ViewHierarchyChangedDetails& details) override;

  // WebUIContentsWrapper::Host:
  void ShowUI() override;
  void CloseUI() override;
  void ShowCustomContextMenu(
      gfx::Point point,
      std::unique_ptr<ui::MenuModel> menu_model) override;
  void HideCustomContextMenu() override;
  bool HandleKeyboardEvent(content::WebContents* source,
                           const input::NativeWebKeyboardEvent& event) override;

 private:
  base::RepeatingClosure on_show_cb_;
  base::RepeatingClosure close_cb_;
  raw_ptr<WebUIContentsWrapper, DanglingUntriaged> contents_wrapper_;
  std::unique_ptr<views::MenuRunner> context_menu_runner_;
  std::unique_ptr<ui::MenuModel> context_menu_model_;
  // A handler to handle unhandled keyboard messages coming back from the
  // renderer process.
  views::UnhandledKeyboardEventHandler unhandled_keyboard_event_handler_;
  base::CallbackListSubscription tab_will_discard_subscription_;
  base::WeakPtrFactory<SidePanelWebUIView> weak_factory_{this};
};

template <class T>
class SidePanelWebUIViewT : public SidePanelWebUIView {
  METADATA_TEMPLATE_HEADER(SidePanelWebUIViewT, SidePanelWebUIView)

 public:
  SidePanelWebUIViewT(
      SidePanelEntryScope& scope,
      base::RepeatingClosure on_show_cb,
      base::RepeatingClosure close_cb,
      std::unique_ptr<WebUIContentsWrapperT<T>> contents_wrapper)
      : SidePanelWebUIView(scope,
                           std::move(on_show_cb),
                           std::move(close_cb),
                           contents_wrapper.get()),
        contents_wrapper_(std::move(contents_wrapper)) {}
  SidePanelWebUIViewT(const SidePanelWebUIViewT&) = delete;
  SidePanelWebUIViewT& operator=(const SidePanelWebUIViewT&) = delete;
  ~SidePanelWebUIViewT() override = default;

  WebUIContentsWrapperT<T>* contents_wrapper() {
    return contents_wrapper_.get();
  }

 private:
  std::unique_ptr<WebUIContentsWrapperT<T>> contents_wrapper_;
};

#endif  // CHROME_BROWSER_UI_VIEWS_SIDE_PANEL_SIDE_PANEL_WEB_UI_VIEW_H_