Commit e08ed6c3 authored by Michael Hansen's avatar Michael Hansen Committed by Chromium LUCI CQ

[Nearby] Fix keyboard events in sharesheet UI.

This updates NearbyShareAction to implement content::WebContentsDelegate
so that we can handle keyboard events. This was preventing users from
opening the feedback window.

This change also removes the need to subclass views::WebView, so
NearbyShareWebView is deleted. I confirmed via manual testing that links
are still working.

WebViewExample was used as a reference:
https://source.chromium.org/chromium/chromium/src/+/master:ui/views/examples/webview_example.h;drc=d5caa87ad320cc8ceb307f9cc525b7d4f9604dd1

Fixed: 1133817
Change-Id: Id9122ac0a92229a1ebbff66232d31d78ef2ddfcb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2597900
Commit-Queue: Michael Hansen <hansenmichael@google.com>
Reviewed-by: default avatarJosh Nohle <nohle@chromium.org>
Cr-Commit-Position: refs/heads/master@{#838735}
parent b857e5fd
......@@ -4425,8 +4425,6 @@ static_library("browser") {
"nearby_sharing/share_target_info.h",
"nearby_sharing/sharesheet/nearby_share_action.cc",
"nearby_sharing/sharesheet/nearby_share_action.h",
"nearby_sharing/sharesheet/nearby_share_web_view.cc",
"nearby_sharing/sharesheet/nearby_share_web_view.h",
"nearby_sharing/transfer_metadata.cc",
"nearby_sharing/transfer_metadata.h",
"nearby_sharing/transfer_metadata_builder.cc",
......
......@@ -16,9 +16,11 @@
#include "chrome/browser/nearby_sharing/file_attachment.h"
#include "chrome/browser/nearby_sharing/nearby_sharing_service.h"
#include "chrome/browser/nearby_sharing/nearby_sharing_service_factory.h"
#include "chrome/browser/nearby_sharing/sharesheet/nearby_share_web_view.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/sharesheet/sharesheet_types.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
#include "chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
......@@ -96,21 +98,22 @@ void NearbyShareAction::LaunchAction(
controller->SetSharesheetSize(size.width(), size.height());
auto* profile = controller->GetProfile();
auto view = std::make_unique<NearbyShareWebView>(profile);
auto view = std::make_unique<views::WebView>(profile);
// If this is not done, we don't see anything in our view.
view->SetPreferredSize(size);
views::WebView* web_view = root_view->AddChildView(std::move(view));
web_view_ = root_view->AddChildView(std::move(view));
web_view_->GetWebContents()->SetDelegate(this);
// TODO(vecore): Query this from the container view
web_view->holder()->SetCornerRadii(gfx::RoundedCornersF(kCornerRadius));
web_view_->holder()->SetCornerRadii(gfx::RoundedCornersF(kCornerRadius));
// load chrome://nearby into the webview
web_view->LoadInitialURL(GURL(chrome::kChromeUINearbyShareURL));
web_view_->LoadInitialURL(GURL(chrome::kChromeUINearbyShareURL));
// Without requesting focus, the sharesheet will launch in an unfocused state
// which raises accessibility issues with the "Device name" input.
web_view->RequestFocus();
web_view_->RequestFocus();
auto* webui = web_view->GetWebContents()->GetWebUI();
auto* webui = web_view_->GetWebContents()->GetWebUI();
DCHECK(webui != nullptr);
nearby_ui_ =
......@@ -154,3 +157,24 @@ void NearbyShareAction::OnClosing(
nearby_ui_ = nullptr;
}
}
bool NearbyShareAction::HandleKeyboardEvent(
content::WebContents* source,
const content::NativeWebKeyboardEvent& event) {
return unhandled_keyboard_event_handler_.HandleKeyboardEvent(
event, web_view_->GetFocusManager());
}
void NearbyShareAction::WebContentsCreated(
content::WebContents* source_contents,
int opener_render_process_id,
int opener_render_frame_id,
const std::string& frame_name,
const GURL& target_url,
content::WebContents* new_contents) {
chrome::ScopedTabbedBrowserDisplayer displayer(
Profile::FromBrowserContext(web_view_->GetBrowserContext()));
NavigateParams nav_params(displayer.browser(), target_url,
ui::PageTransition::PAGE_TRANSITION_LINK);
Navigate(&nav_params);
}
......@@ -7,9 +7,16 @@
#include "chrome/browser/sharesheet/share_action.h"
#include "chrome/browser/ui/webui/nearby_share/nearby_share_dialog_ui.h"
#include "content/public/browser/web_contents_delegate.h"
#include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
namespace views {
class WebView;
} // namespace views
class NearbyShareAction : public sharesheet::ShareAction,
nearby_share::NearbyShareDialogUI::Observer {
nearby_share::NearbyShareDialogUI::Observer,
content::WebContentsDelegate {
public:
NearbyShareAction();
~NearbyShareAction() override;
......@@ -29,9 +36,22 @@ class NearbyShareAction : public sharesheet::ShareAction,
// nearby_share::NearbyShareDialogUI::Observer:
void OnClose() override;
// content::WebContentsDelegate:
bool HandleKeyboardEvent(
content::WebContents* source,
const content::NativeWebKeyboardEvent& event) override;
void WebContentsCreated(content::WebContents* source_contents,
int opener_render_process_id,
int opener_render_frame_id,
const std::string& frame_name,
const GURL& target_url,
content::WebContents* new_contents) override;
private:
sharesheet::SharesheetController* controller_ = nullptr;
nearby_share::NearbyShareDialogUI* nearby_ui_ = nullptr;
views::WebView* web_view_;
views::UnhandledKeyboardEventHandler unhandled_keyboard_event_handler_;
};
#endif // CHROME_BROWSER_NEARBY_SHARING_SHARESHEET_NEARBY_SHARE_ACTION_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/nearby_sharing/sharesheet/nearby_share_web_view.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
NearbyShareWebView::NearbyShareWebView(content::BrowserContext* browser_context)
: WebView(browser_context) {}
void NearbyShareWebView::WebContentsCreated(
content::WebContents* source_contents,
int opener_render_process_id,
int opener_render_frame_id,
const std::string& frame_name,
const GURL& target_url,
content::WebContents* new_contents) {
chrome::ScopedTabbedBrowserDisplayer displayer(
Profile::FromBrowserContext(GetBrowserContext()));
NavigateParams nav_params(displayer.browser(), target_url,
ui::PageTransition::PAGE_TRANSITION_LINK);
Navigate(&nav_params);
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NEARBY_SHARING_SHARESHEET_NEARBY_SHARE_WEB_VIEW_H_
#define CHROME_BROWSER_NEARBY_SHARING_SHARESHEET_NEARBY_SHARE_WEB_VIEW_H_
#include "ui/views/controls/webview/webview.h"
namespace content {
class BrowserContext;
class WebContents;
} // namespace content
// NearbyShareWebView is used in place of the general views::WebView when
// creating the UI for the sharesheet action so that we can handle navigation
// to open a new tab when a link is clicked.
class NearbyShareWebView : public views::WebView {
public:
explicit NearbyShareWebView(content::BrowserContext* browser_context);
~NearbyShareWebView() override = default;
// content::WebContentsDelegate:
void WebContentsCreated(content::WebContents* source_contents,
int opener_render_process_id,
int opener_render_frame_id,
const std::string& frame_name,
const GURL& target_url,
content::WebContents* new_contents) override;
};
#endif // CHROME_BROWSER_NEARBY_SHARING_SHARESHEET_NEARBY_SHARE_WEB_VIEW_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment