Commit eb417d2b authored by Ryan Daum's avatar Ryan Daum Committed by Commit Bot

[chromecast] Obtain initial focus in webviews

  * Observes visibility of the aura window that contains the webview
    and adds a new OnVisible hook when the window is made visible.
  * Requests initial focus when visible.
  * Same OnVisible hook will be used to register IME observers in a
    forthcoming cl.

Bug: internal b/169848935, internal b/161811385
Test: manual on device and with wayland webview client
Change-Id: I37d0dad610f99398363c6b8f91579cb0a42853c2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2487700
Commit-Queue: Ryan Daum <rdaum@chromium.org>
Reviewed-by: default avatarDaniel Nicoara <dnicoara@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819648}
parent ecea347c
...@@ -31,6 +31,33 @@ ...@@ -31,6 +31,33 @@
namespace chromecast { namespace chromecast {
WebContentController::WebviewWindowVisibilityObserver::
WebviewWindowVisibilityObserver(aura::Window* window,
WebContentController* controller)
: window_(window), controller_(controller) {
DCHECK(window_);
DCHECK(controller_);
window_->AddObserver(this);
}
void WebContentController::WebviewWindowVisibilityObserver::
OnWindowVisibilityChanged(aura::Window* window, bool visible) {
if (window == window_ && visible && window->CanFocus())
controller_->OnVisible(window);
}
void WebContentController::WebviewWindowVisibilityObserver::OnWindowDestroyed(
aura::Window* window) {
if (window == window_)
window_ = nullptr;
}
WebContentController::WebviewWindowVisibilityObserver::
~WebviewWindowVisibilityObserver() {
if (window_)
window_->RemoveObserver(this);
}
WebContentController::WebContentController(Client* client) : client_(client) { WebContentController::WebContentController(Client* client) : client_(client) {
js_channels_ = std::make_unique<WebContentJsChannels>(client_); js_channels_ = std::make_unique<WebContentJsChannels>(client_);
JsClientInstance::AddObserver(this); JsClientInstance::AddObserver(this);
...@@ -149,6 +176,11 @@ void WebContentController::ProcessRequest( ...@@ -149,6 +176,11 @@ void WebContentController::ProcessRequest(
} }
void WebContentController::AttachTo(aura::Window* window, int window_id) { void WebContentController::AttachTo(aura::Window* window, int window_id) {
// Register our observer on the window so we can act later once it
// becomes visible.
window_visibility_observer_ =
std::make_unique<WebviewWindowVisibilityObserver>(window, this);
content::WebContents* contents = GetWebContents(); content::WebContents* contents = GetWebContents();
auto* contents_window = contents->GetNativeView(); auto* contents_window = contents->GetNativeView();
contents_window->set_id(window_id); contents_window->set_id(window_id);
...@@ -170,6 +202,11 @@ void WebContentController::AttachTo(aura::Window* window, int window_id) { ...@@ -170,6 +202,11 @@ void WebContentController::AttachTo(aura::Window* window, int window_id) {
HandleResize(contents_window->bounds().size()); HandleResize(contents_window->bounds().size());
} }
void WebContentController::OnVisible(aura::Window* window) {
// Acquire initial focus.
GetWebContents()->SetInitialFocus();
}
void WebContentController::ProcessInputEvent(const webview::InputEvent& ev) { void WebContentController::ProcessInputEvent(const webview::InputEvent& ev) {
content::WebContents* contents = GetWebContents(); content::WebContents* contents = GetWebContents();
DCHECK(contents); DCHECK(contents);
......
...@@ -63,6 +63,9 @@ class WebContentController ...@@ -63,6 +63,9 @@ class WebContentController
// Attach this web contents to an aura window as a child. // Attach this web contents to an aura window as a child.
void AttachTo(aura::Window* window, int window_id); void AttachTo(aura::Window* window, int window_id);
// Invoked when the aura window becomes visible and is fully initialized.
void OnVisible(aura::Window* window);
protected: protected:
// content::WebContentsObserver // content::WebContentsObserver
void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override; void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override;
...@@ -139,6 +142,30 @@ class WebContentController ...@@ -139,6 +142,30 @@ class WebContentController
std::set<content::RenderFrameHost*> current_render_frame_set_; std::set<content::RenderFrameHost*> current_render_frame_set_;
std::set<content::RenderWidgetHost*> current_render_widget_set_; std::set<content::RenderWidgetHost*> current_render_widget_set_;
// Observes the aura window and calls back to the controller for visibility
// events.
class WebviewWindowVisibilityObserver : public aura::WindowObserver {
public:
explicit WebviewWindowVisibilityObserver(aura::Window* window,
WebContentController* controller);
~WebviewWindowVisibilityObserver() override;
WebviewWindowVisibilityObserver(const WebviewWindowVisibilityObserver&) =
delete;
WebviewWindowVisibilityObserver& operator=(
const WebviewWindowVisibilityObserver&) = delete;
private:
// aura::WindowObserver
void OnWindowVisibilityChanged(aura::Window* window, bool visible) override;
void OnWindowDestroyed(aura::Window* window) override;
aura::Window* window_;
WebContentController* controller_;
};
std::unique_ptr<WebviewWindowVisibilityObserver> window_visibility_observer_;
base::WeakPtrFactory<WebContentController> weak_ptr_factory_{this}; base::WeakPtrFactory<WebContentController> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(WebContentController); DISALLOW_COPY_AND_ASSIGN(WebContentController);
......
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