Commit 41b1fa51 authored by Steven Bennetts's avatar Steven Bennetts Committed by Commit Bot

Add ChromeKeyboardWebContents

This separates the WebContentsObserver and WebContentsDelegate code
from ChromeKeyboardUI.

Note: ChromeKeyboardWebContents has a dependency on ui::keyboard that
will need to be replaced with a mojo API call for multi process mash,
but the separation should at least help with the single process mash
split, and calls out / documents where the multi process mash change
needs to happen.

Bug: 843332
Change-Id: I09a8ea3ac0536737d395fda372b98919221c1a3a
Reviewed-on: https://chromium-review.googlesource.com/1182679Reviewed-by: default avatarDarren Shen <shend@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Commit-Queue: Steven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#585146}
parent b2610da4
...@@ -1772,6 +1772,8 @@ jumbo_split_static_library("ui") { ...@@ -1772,6 +1772,8 @@ jumbo_split_static_library("ui") {
"ash/chrome_browser_main_extra_parts_ash.h", "ash/chrome_browser_main_extra_parts_ash.h",
"ash/chrome_keyboard_ui.cc", "ash/chrome_keyboard_ui.cc",
"ash/chrome_keyboard_ui.h", "ash/chrome_keyboard_ui.h",
"ash/chrome_keyboard_web_contents.cc",
"ash/chrome_keyboard_web_contents.h",
"ash/chrome_launcher_prefs.cc", "ash/chrome_launcher_prefs.cc",
"ash/chrome_launcher_prefs.h", "ash/chrome_launcher_prefs.h",
"ash/chrome_new_window_client.cc", "ash/chrome_new_window_client.cc",
......
...@@ -14,27 +14,16 @@ ...@@ -14,27 +14,16 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "base/values.h" #include "base/values.h"
#include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" #include "chrome/browser/ui/ash/chrome_keyboard_web_contents.h"
#include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
#include "content/public/browser/host_zoom_map.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h" #include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_iterator.h" #include "content/public/browser/render_widget_host_iterator.h"
#include "content/public/browser/render_widget_host_view.h" #include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_ui.h"
#include "content/public/common/bindings_policy.h" #include "content/public/common/bindings_policy.h"
#include "extensions/browser/api/virtual_keyboard_private/virtual_keyboard_delegate.h" #include "extensions/browser/api/virtual_keyboard_private/virtual_keyboard_delegate.h"
#include "extensions/browser/api/virtual_keyboard_private/virtual_keyboard_private_api.h" #include "extensions/browser/api/virtual_keyboard_private/virtual_keyboard_private_api.h"
#include "extensions/browser/event_router.h" #include "extensions/browser/event_router.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/view_type_utils.h"
#include "extensions/common/api/virtual_keyboard_private.h" #include "extensions/common/api/virtual_keyboard_private.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension_messages.h" #include "extensions/common/extension_messages.h"
#include "ipc/ipc_message_macros.h" #include "ipc/ipc_message_macros.h"
#include "ui/accessibility/ax_enums.mojom.h" #include "ui/accessibility/ax_enums.mojom.h"
...@@ -52,8 +41,6 @@ ...@@ -52,8 +41,6 @@
#include "ui/keyboard/keyboard_controller.h" #include "ui/keyboard/keyboard_controller.h"
#include "ui/keyboard/keyboard_controller_observer.h" #include "ui/keyboard/keyboard_controller_observer.h"
#include "ui/keyboard/keyboard_resource_util.h" #include "ui/keyboard/keyboard_resource_util.h"
#include "ui/keyboard/keyboard_switches.h"
#include "ui/keyboard/keyboard_util.h"
#include "ui/wm/core/shadow_types.h" #include "ui/wm/core/shadow_types.h"
namespace virtual_keyboard_private = extensions::api::virtual_keyboard_private; namespace virtual_keyboard_private = extensions::api::virtual_keyboard_private;
...@@ -67,102 +54,6 @@ GURL& GetOverrideVirtualKeyboardUrl() { ...@@ -67,102 +54,6 @@ GURL& GetOverrideVirtualKeyboardUrl() {
return *url; return *url;
} }
// The WebContentsDelegate for the chrome keyboard.
// The delegate deletes itself when the keyboard is destroyed.
class ChromeKeyboardContentsDelegate : public content::WebContentsDelegate,
public content::WebContentsObserver {
public:
ChromeKeyboardContentsDelegate() = default;
~ChromeKeyboardContentsDelegate() override = default;
private:
// content::WebContentsDelegate:
content::WebContents* OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params) override {
source->GetController().LoadURL(params.url, params.referrer,
params.transition, params.extra_headers);
Observe(source);
return source;
}
bool CanDragEnter(content::WebContents* source,
const content::DropData& data,
blink::WebDragOperationsMask operations_allowed) override {
return false;
}
bool ShouldCreateWebContents(
content::WebContents* web_contents,
content::RenderFrameHost* opener,
content::SiteInstance* source_site_instance,
int32_t route_id,
int32_t main_frame_route_id,
int32_t main_frame_widget_route_id,
content::mojom::WindowContainerType window_container_type,
const GURL& opener_url,
const std::string& frame_name,
const GURL& target_url,
const std::string& partition_id,
content::SessionStorageNamespace* session_storage_namespace) override {
return false;
}
void SetContentsBounds(content::WebContents* source,
const gfx::Rect& bounds) override {
aura::Window* keyboard_window = source->GetNativeView();
// keyboard window must have been added to keyboard container window at this
// point. Otherwise, wrong keyboard bounds is used and may cause problem as
// described in crbug.com/367788.
DCHECK(keyboard_window->parent());
// keyboard window bounds may not set to |pos| after this call. If keyboard
// is in FULL_WIDTH mode, only the height of keyboard window will be
// changed.
keyboard_window->SetBounds(bounds);
}
// content::WebContentsDelegate:
void RequestMediaAccessPermission(
content::WebContents* web_contents,
const content::MediaStreamRequest& request,
content::MediaResponseCallback callback) override {
const extensions::Extension* extension = nullptr;
GURL origin(request.security_origin);
if (origin.SchemeIs(extensions::kExtensionScheme)) {
const extensions::ExtensionRegistry* registry =
extensions::ExtensionRegistry::Get(web_contents->GetBrowserContext());
extension = registry->enabled_extensions().GetByID(origin.host());
DCHECK(extension);
}
MediaCaptureDevicesDispatcher::GetInstance()->ProcessMediaAccessRequest(
web_contents, request, std::move(callback), extension);
}
// content::WebContentsDelegate:
bool PreHandleGestureEvent(content::WebContents* source,
const blink::WebGestureEvent& event) override {
switch (event.GetType()) {
// Scroll events are not suppressed because the menu to select IME should
// be scrollable.
case blink::WebInputEvent::kGestureScrollBegin:
case blink::WebInputEvent::kGestureScrollEnd:
case blink::WebInputEvent::kGestureScrollUpdate:
case blink::WebInputEvent::kGestureFlingStart:
case blink::WebInputEvent::kGestureFlingCancel:
return false;
default:
// Stop gesture events from being passed to renderer to suppress the
// context menu. crbug.com/685140
return true;
}
}
// content::WebContentsObserver:
void WebContentsDestroyed() override { delete this; }
DISALLOW_COPY_AND_ASSIGN(ChromeKeyboardContentsDelegate);
};
class AshKeyboardControllerObserver class AshKeyboardControllerObserver
: public keyboard::KeyboardControllerObserver { : public keyboard::KeyboardControllerObserver {
public: public:
...@@ -302,21 +193,20 @@ void ChromeKeyboardUI::UpdateInsetsForWindow(aura::Window* window) { ...@@ -302,21 +193,20 @@ void ChromeKeyboardUI::UpdateInsetsForWindow(aura::Window* window) {
aura::Window* ChromeKeyboardUI::GetKeyboardWindow() { aura::Window* ChromeKeyboardUI::GetKeyboardWindow() {
if (keyboard_contents_) if (keyboard_contents_)
return keyboard_contents_->GetNativeView(); return keyboard_contents_->web_contents()->GetNativeView();
GURL keyboard_url = GetVirtualKeyboardUrl(); GURL keyboard_url = GetVirtualKeyboardUrl();
content::WebContents::CreateParams web_contents_params( keyboard_contents_ = std::make_unique<ChromeKeyboardWebContents>(
browser_context_, browser_context_, keyboard_url);
content::SiteInstance::CreateForURL(browser_context_, keyboard_url));
keyboard_contents_ = content::WebContents::Create(web_contents_params); aura::Window* keyboard_window =
keyboard_contents_->SetDelegate(new ChromeKeyboardContentsDelegate()); keyboard_contents_->web_contents()->GetNativeView();
SetupWebContents(keyboard_contents_.get());
LoadContents(keyboard_url);
aura::Window* keyboard_window = keyboard_contents_->GetNativeView();
keyboard_window->AddObserver(this); keyboard_window->AddObserver(this);
keyboard_window->set_owned_by_parent(false); keyboard_window->set_owned_by_parent(false);
content::RenderWidgetHostView* view = content::RenderWidgetHostView* view =
keyboard_contents_->GetMainFrame()->GetView(); keyboard_contents_->web_contents()->GetMainFrame()->GetView();
// Only use transparent background when fullscreen handwriting or the new UI // Only use transparent background when fullscreen handwriting or the new UI
// is enabled. The old UI sometimes reloads itself, which will cause the // is enabled. The old UI sometimes reloads itself, which will cause the
...@@ -364,19 +254,7 @@ void ChromeKeyboardUI::SetController(keyboard::KeyboardController* controller) { ...@@ -364,19 +254,7 @@ void ChromeKeyboardUI::SetController(keyboard::KeyboardController* controller) {
void ChromeKeyboardUI::ReloadKeyboardIfNeeded() { void ChromeKeyboardUI::ReloadKeyboardIfNeeded() {
DCHECK(keyboard_contents_); DCHECK(keyboard_contents_);
GURL keyboard_url = GetVirtualKeyboardUrl(); keyboard_contents_->SetKeyboardUrl(GetVirtualKeyboardUrl());
if (keyboard_contents_->GetURL() != keyboard_url) {
if (keyboard_contents_->GetURL().GetOrigin() != keyboard_url.GetOrigin()) {
// Sets keyboard window rectangle to 0 and close current page before
// navigate to a keyboard in a different extension. This keeps the UX the
// same as Android. Note we need to explicitly close current page as it
// might try to resize keyboard window in javascript on a resize event.
TRACE_EVENT0("vk", "ReloadKeyboardIfNeeded");
GetKeyboardWindow()->SetBounds(gfx::Rect());
keyboard_contents_->ClosePage();
}
LoadContents(keyboard_url);
}
} }
void ChromeKeyboardUI::InitInsets(const gfx::Rect& new_bounds) { void ChromeKeyboardUI::InitInsets(const gfx::Rect& new_bounds) {
...@@ -442,16 +320,6 @@ void ChromeKeyboardUI::OnWindowParentChanged(aura::Window* window, ...@@ -442,16 +320,6 @@ void ChromeKeyboardUI::OnWindowParentChanged(aura::Window* window,
SetShadowAroundKeyboard(); SetShadowAroundKeyboard();
} }
void ChromeKeyboardUI::LoadContents(const GURL& url) {
if (keyboard_contents_) {
TRACE_EVENT0("vk", "LoadContents");
content::OpenURLParams params(url, content::Referrer(),
WindowOpenDisposition::SINGLETON_TAB,
ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false);
keyboard_contents_->OpenURL(params);
}
}
GURL ChromeKeyboardUI::GetVirtualKeyboardUrl() { GURL ChromeKeyboardUI::GetVirtualKeyboardUrl() {
const GURL& override_url = GetOverrideVirtualKeyboardUrl(); const GURL& override_url = GetOverrideVirtualKeyboardUrl();
if (!override_url.is_empty()) if (!override_url.is_empty())
...@@ -530,26 +398,3 @@ void ChromeKeyboardUI::SetShadowAroundKeyboard() { ...@@ -530,26 +398,3 @@ void ChromeKeyboardUI::SetShadowAroundKeyboard() {
keyboard_controller()->GetActiveContainerType() == keyboard_controller()->GetActiveContainerType() ==
keyboard::ContainerType::FULL_WIDTH); keyboard::ContainerType::FULL_WIDTH);
} }
void ChromeKeyboardUI::SetupWebContents(content::WebContents* contents) {
extensions::SetViewType(contents, extensions::VIEW_TYPE_COMPONENT);
extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
contents);
Observe(contents);
}
void ChromeKeyboardUI::RenderViewCreated(
content::RenderViewHost* render_view_host) {
content::HostZoomMap* zoom_map =
content::HostZoomMap::GetDefaultForBrowserContext(browser_context_);
DCHECK(zoom_map);
int render_process_id = render_view_host->GetProcess()->GetID();
int render_view_id = render_view_host->GetRoutingID();
zoom_map->SetTemporaryZoomLevel(render_process_id, render_view_id, 0);
}
void ChromeKeyboardUI::DidFinishLoad(
content::RenderFrameHost* render_frame_host,
const GURL& validated_url) {
keyboard_controller()->NotifyKeyboardWindowLoaded();
}
...@@ -8,13 +8,15 @@ ...@@ -8,13 +8,15 @@
#include <memory> #include <memory>
#include "base/macros.h" #include "base/macros.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/media_stream_request.h" #include "content/public/common/media_stream_request.h"
#include "ui/aura/window_observer.h" #include "ui/aura/window_observer.h"
#include "ui/base/ime/text_input_type.h" #include "ui/base/ime/text_input_type.h"
#include "ui/keyboard/keyboard_controller_observer.h" #include "ui/keyboard/keyboard_controller_observer.h"
#include "ui/keyboard/keyboard_ui.h" #include "ui/keyboard/keyboard_ui.h"
class ChromeKeyboardWebContents;
class GURL;
namespace aura { namespace aura {
class Window; class Window;
} }
...@@ -25,8 +27,7 @@ class Rect; ...@@ -25,8 +27,7 @@ class Rect;
namespace content { namespace content {
class BrowserContext; class BrowserContext;
class WebContents; }
} // namespace content
namespace ui { namespace ui {
class InputMethod; class InputMethod;
...@@ -36,8 +37,7 @@ class Shadow; ...@@ -36,8 +37,7 @@ class Shadow;
// Subclass of KeyboardUI. It is used by KeyboardController to get // Subclass of KeyboardUI. It is used by KeyboardController to get
// access to the virtual keyboard window and setup Chrome extension functions. // access to the virtual keyboard window and setup Chrome extension functions.
class ChromeKeyboardUI : public keyboard::KeyboardUI, class ChromeKeyboardUI : public keyboard::KeyboardUI,
public aura::WindowObserver, public aura::WindowObserver {
public content::WebContentsObserver {
public: public:
class TestApi { class TestApi {
public: public:
...@@ -75,11 +75,6 @@ class ChromeKeyboardUI : public keyboard::KeyboardUI, ...@@ -75,11 +75,6 @@ class ChromeKeyboardUI : public keyboard::KeyboardUI,
private: private:
class WindowBoundsChangeObserver; class WindowBoundsChangeObserver;
std::unique_ptr<content::WebContents> CreateWebContents();
// Loads the web contents for the given |url|.
void LoadContents(const GURL& url);
// Gets the virtual keyboard URL, either the default keyboard URL or the IME // Gets the virtual keyboard URL, either the default keyboard URL or the IME
// override URL. // override URL.
GURL GetVirtualKeyboardUrl(); GURL GetVirtualKeyboardUrl();
...@@ -101,22 +96,11 @@ class ChromeKeyboardUI : public keyboard::KeyboardUI, ...@@ -101,22 +96,11 @@ class ChromeKeyboardUI : public keyboard::KeyboardUI,
// this method creates it. // this method creates it.
void SetShadowAroundKeyboard(); void SetShadowAroundKeyboard();
// The implementation can choose to setup the WebContents before the virtual
// keyboard page is loaded (e.g. install a WebContentsObserver).
// SetupWebContents() is called right after creating the WebContents, before
// loading the keyboard page.
void SetupWebContents(content::WebContents* contents);
// content::WebContentsObserver overrides
void RenderViewCreated(content::RenderViewHost* render_view_host) override;
void DidFinishLoad(content::RenderFrameHost* render_frame_host,
const GURL& validated_url) override;
// The BrowserContext to use for creating the WebContents hosting the // The BrowserContext to use for creating the WebContents hosting the
// keyboard. // keyboard.
content::BrowserContext* const browser_context_; content::BrowserContext* const browser_context_;
std::unique_ptr<content::WebContents> keyboard_contents_; std::unique_ptr<ChromeKeyboardWebContents> keyboard_contents_;
std::unique_ptr<ui::Shadow> shadow_; std::unique_ptr<ui::Shadow> shadow_;
std::unique_ptr<keyboard::KeyboardControllerObserver> observer_; std::unique_ptr<keyboard::KeyboardControllerObserver> observer_;
......
// Copyright 2018 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/ui/ash/chrome_keyboard_web_contents.h"
#include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
#include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
#include "content/public/browser/host_zoom_map.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/view_type_utils.h"
#include "extensions/common/constants.h"
#include "third_party/blink/public/platform/web_gesture_event.h"
#include "ui/aura/window.h"
#include "ui/base/ui_base_features.h"
#include "ui/keyboard/keyboard_controller.h"
#include "ui/keyboard/keyboard_resource_util.h"
#include "ui/keyboard/keyboard_util.h"
namespace {
// Deletes itself when the associated WebContents is destroyed.
class ChromeKeyboardContentsDelegate : public content::WebContentsDelegate,
public content::WebContentsObserver {
public:
ChromeKeyboardContentsDelegate() = default;
~ChromeKeyboardContentsDelegate() override = default;
private:
// content::WebContentsDelegate:
content::WebContents* OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params) override {
source->GetController().LoadURL(params.url, params.referrer,
params.transition, params.extra_headers);
Observe(source);
return source;
}
bool CanDragEnter(content::WebContents* source,
const content::DropData& data,
blink::WebDragOperationsMask operations_allowed) override {
return false;
}
bool ShouldCreateWebContents(
content::WebContents* web_contents,
content::RenderFrameHost* opener,
content::SiteInstance* source_site_instance,
int32_t route_id,
int32_t main_frame_route_id,
int32_t main_frame_widget_route_id,
content::mojom::WindowContainerType window_container_type,
const GURL& opener_url,
const std::string& frame_name,
const GURL& target_url,
const std::string& partition_id,
content::SessionStorageNamespace* session_storage_namespace) override {
return false;
}
void SetContentsBounds(content::WebContents* source,
const gfx::Rect& bounds) override {
aura::Window* keyboard_window = source->GetNativeView();
// keyboard window must have been added to keyboard container window at this
// point. Otherwise, wrong keyboard bounds is used and may cause problem as
// described in https://crbug.com/367788.
DCHECK(keyboard_window->parent());
// keyboard window bounds may not set to |pos| after this call. If keyboard
// is in FULL_WIDTH mode, only the height of keyboard window will be
// changed.
keyboard_window->SetBounds(bounds);
}
// content::WebContentsDelegate:
void RequestMediaAccessPermission(
content::WebContents* web_contents,
const content::MediaStreamRequest& request,
content::MediaResponseCallback callback) override {
const extensions::Extension* extension = nullptr;
GURL origin(request.security_origin);
if (origin.SchemeIs(extensions::kExtensionScheme)) {
const extensions::ExtensionRegistry* registry =
extensions::ExtensionRegistry::Get(web_contents->GetBrowserContext());
extension = registry->enabled_extensions().GetByID(origin.host());
DCHECK(extension);
}
MediaCaptureDevicesDispatcher::GetInstance()->ProcessMediaAccessRequest(
web_contents, request, std::move(callback), extension);
}
// content::WebContentsDelegate:
bool PreHandleGestureEvent(content::WebContents* source,
const blink::WebGestureEvent& event) override {
switch (event.GetType()) {
// Scroll events are not suppressed because the menu to select IME should
// be scrollable.
case blink::WebInputEvent::kGestureScrollBegin:
case blink::WebInputEvent::kGestureScrollEnd:
case blink::WebInputEvent::kGestureScrollUpdate:
case blink::WebInputEvent::kGestureFlingStart:
case blink::WebInputEvent::kGestureFlingCancel:
return false;
default:
// Stop gesture events from being passed to renderer to suppress the
// context menu. https://crbug.com/685140
return true;
}
}
// content::WebContentsObserver:
void WebContentsDestroyed() override { delete this; }
DISALLOW_COPY_AND_ASSIGN(ChromeKeyboardContentsDelegate);
};
} // namespace
ChromeKeyboardWebContents::ChromeKeyboardWebContents(
content::BrowserContext* context,
const GURL& url) {
DCHECK(context);
content::WebContents::CreateParams web_contents_params(
context, content::SiteInstance::CreateForURL(context, url));
web_contents_ = content::WebContents::Create(web_contents_params);
web_contents_->SetDelegate(new ChromeKeyboardContentsDelegate());
extensions::SetViewType(web_contents_.get(), extensions::VIEW_TYPE_COMPONENT);
extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
web_contents_.get());
Observe(web_contents_.get());
LoadContents(url);
}
ChromeKeyboardWebContents::~ChromeKeyboardWebContents() = default;
void ChromeKeyboardWebContents::SetKeyboardUrl(const GURL& new_url) {
GURL old_url = web_contents_->GetURL();
if (old_url == new_url)
return;
if (old_url.GetOrigin() != new_url.GetOrigin()) {
// Sets keyboard window rectangle to 0 and closes the current page before
// navigating to a keyboard in a different extension. This keeps the UX the
// same as Android. Note we need to explicitly close the current page as it
// might try to resize the keyboard window in javascript on a resize event.
TRACE_EVENT0("vk", "ReloadKeyboardIfNeeded");
web_contents_->GetNativeView()->SetBounds(gfx::Rect());
web_contents_->ClosePage();
}
LoadContents(new_url);
}
void ChromeKeyboardWebContents::RenderViewCreated(
content::RenderViewHost* render_view_host) {
content::RenderProcessHost* render_process_host =
render_view_host->GetProcess();
content::HostZoomMap::GetDefaultForBrowserContext(
render_process_host->GetBrowserContext())
->SetTemporaryZoomLevel(render_process_host->GetID(),
render_view_host->GetRoutingID(), 0 /* level */);
}
void ChromeKeyboardWebContents::DidFinishLoad(
content::RenderFrameHost* render_frame_host,
const GURL& validated_url) {
// TODO(mash): Support virtual keyboard. https://crbug.com/843332.
if (!features::IsMultiProcessMash())
keyboard::KeyboardController::Get()->NotifyKeyboardWindowLoaded();
}
void ChromeKeyboardWebContents::LoadContents(const GURL& url) {
TRACE_EVENT0("vk", "LoadContents");
content::OpenURLParams params(url, content::Referrer(),
WindowOpenDisposition::SINGLETON_TAB,
ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false);
web_contents_->OpenURL(params);
}
// Copyright 2018 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_UI_ASH_CHROME_KEYBOARD_WEB_CONTENTS_H_
#define CHROME_BROWSER_UI_ASH_CHROME_KEYBOARD_WEB_CONTENTS_H_
#include <memory>
#include "base/macros.h"
#include "content/public/browser/web_contents_observer.h"
class GURL;
namespace content {
class WebContents;
}
// WebContents manager for the virtual keyboard. This observes the web
// contents, manages the content::HostZoomMap, and informs the virtual
// keyboard controller when the contents have loaded. It also provides a
// WebContentsDelegate implementation.
// TODO(mash): Remove keyboard::KeyboardController dependency to support
// multi process mash.
class ChromeKeyboardWebContents : public content::WebContentsObserver {
public:
ChromeKeyboardWebContents(content::BrowserContext* context, const GURL& url);
~ChromeKeyboardWebContents() override;
// Updates the keyboard URL if |url| does not match the existing url.
void SetKeyboardUrl(const GURL& url);
// Provide access to the native view (aura::Window) and frame
// (RenderWidgetHostView) through WebContents. TODO(stevenjb): Remove this
// once host window ownership is moved to ash.
content::WebContents* web_contents() { return web_contents_.get(); }
private:
// content::WebContentsObserver overrides
void RenderViewCreated(content::RenderViewHost* render_view_host) override;
void DidFinishLoad(content::RenderFrameHost* render_frame_host,
const GURL& validated_url) override;
// Loads the web contents for the given |url|.
void LoadContents(const GURL& url);
std::unique_ptr<content::WebContents> web_contents_;
DISALLOW_COPY_AND_ASSIGN(ChromeKeyboardWebContents);
};
#endif // CHROME_BROWSER_UI_ASH_CHROME_KEYBOARD_WEB_CONTENTS_H_
// Copyright 2018 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/ui/ash/chrome_keyboard_web_contents.h"
#include <memory>
#include "base/run_loop.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_ui.h"
#include "url/gurl.h"
namespace {
class ChromeKeyboardWebContentsTest : public ChromeRenderViewHostTestHarness {
public:
ChromeKeyboardWebContentsTest() = default;
~ChromeKeyboardWebContentsTest() override = default;
void TearDown() override {
chrome_keyboard_web_contents_.reset();
ChromeRenderViewHostTestHarness::TearDown();
}
void CreateWebContents(const GURL& url) {
chrome_keyboard_web_contents_ =
std::make_unique<ChromeKeyboardWebContents>(profile(), url);
}
protected:
std::unique_ptr<ChromeKeyboardWebContents> chrome_keyboard_web_contents_;
};
class TestDelegate : public content::WebContentsDelegate {
public:
explicit TestDelegate(content::WebContents* contents) {
contents->SetDelegate(this);
}
~TestDelegate() override = default;
// content::WebContentsDelegate:
content::WebContents* OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params) override {
opened_++;
return source;
}
int opened() const { return opened_; }
private:
int opened_ = 0;
};
} // namespace
// Calling SetKeyboardUrl with a different URL should open the new page.
TEST_F(ChromeKeyboardWebContentsTest, SetKeyboardUrl) {
CreateWebContents(GURL("http://foo.com"));
ASSERT_TRUE(chrome_keyboard_web_contents_->web_contents());
// Override the delegate to test that OpenURLFromTab gets called.
TestDelegate delegate(chrome_keyboard_web_contents_->web_contents());
chrome_keyboard_web_contents_->SetKeyboardUrl(GURL("http://bar.com"));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, delegate.opened());
}
...@@ -3399,6 +3399,7 @@ test("unit_tests") { ...@@ -3399,6 +3399,7 @@ test("unit_tests") {
"../browser/ui/ash/accessibility/ax_tree_source_aura_unittest.cc", "../browser/ui/ash/accessibility/ax_tree_source_aura_unittest.cc",
"../browser/ui/ash/browser_image_registrar_unittest.cc", "../browser/ui/ash/browser_image_registrar_unittest.cc",
"../browser/ui/ash/chrome_keyboard_ui_unittest.cc", "../browser/ui/ash/chrome_keyboard_ui_unittest.cc",
"../browser/ui/ash/chrome_keyboard_web_contents_unittest.cc",
"../browser/ui/ash/ime_controller_client_unittest.cc", "../browser/ui/ash/ime_controller_client_unittest.cc",
"../browser/ui/ash/ksv/keyboard_shortcut_viewer_metadata_unittest.cc", "../browser/ui/ash/ksv/keyboard_shortcut_viewer_metadata_unittest.cc",
"../browser/ui/ash/launcher/arc_app_shelf_id_unittest.cc", "../browser/ui/ash/launcher/arc_app_shelf_id_unittest.cc",
......
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