1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/ui/views/bubble/webui_bubble_view.h"
6 
7 #include "content/public/browser/keyboard_event_processing_result.h"
8 #include "content/public/browser/native_web_keyboard_event.h"
9 #include "ui/gfx/geometry/rounded_corners_f.h"
10 #include "ui/views/widget/widget.h"
11 
12 namespace {
13 
IsEscapeEvent(const content::NativeWebKeyboardEvent & event)14 bool IsEscapeEvent(const content::NativeWebKeyboardEvent& event) {
15   return event.GetType() ==
16              content::NativeWebKeyboardEvent::Type::kRawKeyDown &&
17          event.windows_key_code == ui::VKEY_ESCAPE;
18 }
19 
20 // The min / max size available to the WebUIBubbleView.
21 // These are arbitrary sizes that match those set by ExtensionPopup.
22 // TODO(tluk): Determine the correct size constraints for the
23 // WebUIBubbleView.
24 constexpr gfx::Size kMinSize(25, 25);
25 constexpr gfx::Size kMaxSize(800, 600);
26 
27 }  // namespace
28 
WebUIBubbleView(content::BrowserContext * browser_context)29 WebUIBubbleView::WebUIBubbleView(content::BrowserContext* browser_context)
30     : WebView(browser_context) {
31   EnableSizingFromWebContents(kMinSize, kMaxSize);
32   SetVisible(false);
33 
34   // Allow the embedder to handle accelerators not handled by the WebContents.
35   set_allow_accelerators(true);
36 }
37 
38 WebUIBubbleView::~WebUIBubbleView() = default;
39 
PreferredSizeChanged()40 void WebUIBubbleView::PreferredSizeChanged() {
41   WebView::PreferredSizeChanged();
42   if (host_)
43     host_->OnWebViewSizeChanged();
44 }
45 
HandleContextMenu(content::RenderFrameHost * render_frame_host,const content::ContextMenuParams & params)46 bool WebUIBubbleView::HandleContextMenu(
47     content::RenderFrameHost* render_frame_host,
48     const content::ContextMenuParams& params) {
49   // Ignores context menu.
50   return true;
51 }
52 
PreHandleKeyboardEvent(content::WebContents * source,const content::NativeWebKeyboardEvent & event)53 content::KeyboardEventProcessingResult WebUIBubbleView::PreHandleKeyboardEvent(
54     content::WebContents* source,
55     const content::NativeWebKeyboardEvent& event) {
56   // Close the bubble if an escape event is detected. Handle this here to
57   // prevent the renderer from capturing the event and not propagating it up.
58   if (IsEscapeEvent(event) && GetWidget()) {
59     GetWidget()->CloseWithReason(views::Widget::ClosedReason::kEscKeyPressed);
60     return content::KeyboardEventProcessingResult::HANDLED;
61   }
62   return content::KeyboardEventProcessingResult::NOT_HANDLED;
63 }
64 
HandleKeyboardEvent(content::WebContents * source,const content::NativeWebKeyboardEvent & event)65 bool WebUIBubbleView::HandleKeyboardEvent(
66     content::WebContents* source,
67     const content::NativeWebKeyboardEvent& event) {
68   return unhandled_keyboard_event_handler_.HandleKeyboardEvent(
69       event, GetFocusManager());
70 }
71 
ShowUI()72 void WebUIBubbleView::ShowUI() {
73   if (host_)
74     host_->ShowUI();
75 }
76 
CloseUI()77 void WebUIBubbleView::CloseUI() {
78   if (host_)
79     host_->CloseUI();
80 }
81