Commit 2489d7f5 authored by David Black's avatar David Black Committed by Commit Bot

Remove AssistantWebView.

This is achieved by flattening the existing AssistantWebView into
AssistantWebContainerView.

Note that a follow up CL will take the name 'AssistantWebView' and
reassign it to what is now called 'AssistantWebView2'.

Bug: b:146520500
Change-Id: I242085c039317936047341193ba05c857a144182
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2040199Reviewed-by: default avatarTao Wu <wutao@chromium.org>
Commit-Queue: David Black <dmblack@google.com>
Cr-Commit-Position: refs/heads/master@{#738778}
parent 9d6bfbc1
......@@ -36,8 +36,6 @@ component("ui") {
"assistant_view_ids.h",
"assistant_web_container_view.cc",
"assistant_web_container_view.h",
"assistant_web_view.cc",
"assistant_web_view.h",
"assistant_web_view_delegate.h",
"base/assistant_button.cc",
"base/assistant_button.h",
......
......@@ -11,7 +11,10 @@
#include "ash/assistant/model/assistant_ui_model.h"
#include "ash/assistant/ui/assistant_ui_constants.h"
#include "ash/assistant/ui/assistant_view_delegate.h"
#include "ash/assistant/ui/assistant_web_view.h"
#include "ash/assistant/ui/assistant_web_view_delegate.h"
#include "ash/assistant/util/deep_link_util.h"
#include "ash/public/cpp/assistant/assistant_web_view_factory.h"
#include "ui/base/window_open_disposition.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/views/background.h"
......@@ -22,13 +25,12 @@ namespace ash {
namespace {
constexpr int kPreferredWindowWidthDip = 768;
// This height includes the window's |non_client_frame_view|'s height.
constexpr int kPreferredWindowHeightDip = 768;
constexpr int kPreferredWindowWidthDip = 768;
// The minimum padding of the window to the edges of the screen.
constexpr int kPreferredPaddingMinDip = 48;
// The minimum margin of the window to the edges of the screen.
constexpr int kMinWindowMarginDip = 48;
} // namespace
......@@ -47,29 +49,96 @@ const char* AssistantWebContainerView::GetClassName() const {
}
gfx::Size AssistantWebContainerView::CalculatePreferredSize() const {
// TODO(b/142565300): Handle virtual keyboard resize.
const int non_client_frame_view_height =
views::GetCaptionButtonLayoutSize(
views::CaptionButtonLayoutSize::kNonBrowserCaption)
.height();
const gfx::Rect work_area =
display::Screen::GetScreen()
->GetDisplayNearestWindow(GetWidget()->GetNativeWindow())
.work_area();
const int width = std::min(work_area.width() - 2 * kPreferredPaddingMinDip,
const int width = std::min(work_area.width() - 2 * kMinWindowMarginDip,
kPreferredWindowWidthDip);
const int height = std::min(work_area.height() - 2 * kPreferredPaddingMinDip,
kPreferredWindowHeightDip);
const int non_client_frame_view_height =
views::GetCaptionButtonLayoutSize(
views::CaptionButtonLayoutSize::kNonBrowserCaption)
.height();
return gfx::Size(width, height - non_client_frame_view_height);
const int height = std::min(work_area.height() - 2 * kMinWindowMarginDip,
kPreferredWindowHeightDip) -
non_client_frame_view_height;
return gfx::Size(width, height);
}
void AssistantWebContainerView::ChildPreferredSizeChanged(views::View* child) {
// Because AssistantWebContainerView has a fixed size, it does not re-layout
// its children when their preferred size changes. To address this, we need to
// explicitly request a layout pass.
Layout();
SchedulePaint();
}
void AssistantWebContainerView::DidStopLoading() {
// We should only respond to the |DidStopLoading| event the first time, to add
// the view for contents to our view hierarchy and perform other one-time view
// initializations.
if (contents_view_->parent())
return;
contents_view_->SetPreferredSize(GetPreferredSize());
AddChildView(contents_view_.get());
SetFocusBehavior(FocusBehavior::ALWAYS);
}
void AssistantWebContainerView::DidSuppressNavigation(
const GURL& url,
WindowOpenDisposition disposition,
bool from_user_gesture) {
if (!from_user_gesture)
return;
// Deep links are always handled by the AssistantViewDelegate. If the
// |disposition| indicates a desire to open a new foreground tab, we also
// defer to the AssistantViewDelegate so that it can open the |url| in the
// browser.
if (assistant::util::IsDeepLinkUrl(url) ||
disposition == WindowOpenDisposition::NEW_FOREGROUND_TAB) {
assistant_view_delegate_->OpenUrlFromView(url);
return;
}
// Otherwise we'll allow our WebContents to navigate freely.
contents_view_->Navigate(url);
}
void AssistantWebContainerView::DidChangeCanGoBack(bool can_go_back) {
DCHECK(web_container_view_delegate_);
web_container_view_delegate_->UpdateBackButtonVisibility(GetWidget(),
can_go_back);
}
bool AssistantWebContainerView::GoBack() {
return assistant_web_view_->GoBack();
return contents_view_ && contents_view_->GoBack();
}
void AssistantWebContainerView::OpenUrl(const GURL& url) {
assistant_web_view_->OpenUrl(url);
RemoveContents();
AssistantWebView2::InitParams contents_params;
contents_params.suppress_navigation = true;
contents_view_ = AssistantWebViewFactory::Get()->Create(contents_params);
// We retain ownership of |contents_view_| as it is only added to the view
// hierarchy once loading stops and we want to ensure that it is cleaned up in
// the rare chance that that never occurs.
contents_view_->set_owned_by_client();
// We observe |contents_view_| so that we can handle events from the
// underlying WebContents.
contents_view_->AddObserver(this);
// Navigate to the specified |url|.
contents_view_->Navigate(url);
}
void AssistantWebContainerView::InitLayout() {
......@@ -83,10 +152,18 @@ void AssistantWebContainerView::InitLayout() {
SetLayoutManager(std::make_unique<views::FillLayout>());
SetBackground(views::CreateSolidBackground(SK_ColorWHITE));
}
void AssistantWebContainerView::RemoveContents() {
if (!contents_view_)
return;
RemoveChildView(contents_view_.get());
SetFocusBehavior(FocusBehavior::NEVER);
// Web view.
assistant_web_view_ = AddChildView(std::make_unique<AssistantWebView>(
assistant_view_delegate_, web_container_view_delegate_));
contents_view_->RemoveObserver(this);
contents_view_.reset();
}
} // namespace ash
......@@ -5,6 +5,7 @@
#ifndef ASH_ASSISTANT_UI_ASSISTANT_WEB_CONTAINER_VIEW_H_
#define ASH_ASSISTANT_UI_ASSISTANT_WEB_CONTAINER_VIEW_H_
#include "ash/public/cpp/assistant/assistant_web_view_2.h"
#include "base/component_export.h"
#include "base/macros.h"
#include "ui/views/widget/widget_delegate.h"
......@@ -13,13 +14,11 @@ namespace ash {
class AssistantViewDelegate;
class AssistantWebViewDelegate;
class AssistantWebView;
// TODO(dmblack): Merge AssistantWebView and AssistantWebContainerView once
// standalone Assistant UI has been removed.
// The container of assistant_web_view when Assistant web container is enabled.
// The container for hosting standalone WebContents in Assistant.
class COMPONENT_EXPORT(ASSISTANT_UI) AssistantWebContainerView
: public views::WidgetDelegateView {
: public views::WidgetDelegateView,
public AssistantWebView2::Observer {
public:
AssistantWebContainerView(
AssistantViewDelegate* assistant_view_delegate,
......@@ -29,6 +28,14 @@ class COMPONENT_EXPORT(ASSISTANT_UI) AssistantWebContainerView
// views::WidgetDelegateView:
const char* GetClassName() const override;
gfx::Size CalculatePreferredSize() const override;
void ChildPreferredSizeChanged(views::View* child) override;
// AssistantWebView2::Observer:
void DidStopLoading() override;
void DidSuppressNavigation(const GURL& url,
WindowOpenDisposition disposition,
bool from_user_gesture) override;
void DidChangeCanGoBack(bool can_go_back) override;
// Invoke to navigate back in the embedded WebContents' navigation stack. If
// backwards navigation is not possible, returns |false|. Otherwise |true| to
......@@ -40,12 +47,12 @@ class COMPONENT_EXPORT(ASSISTANT_UI) AssistantWebContainerView
private:
void InitLayout();
void RemoveContents();
AssistantViewDelegate* const assistant_view_delegate_;
AssistantWebViewDelegate* const web_container_view_delegate_;
// Owned by the views hierarchy.
AssistantWebView* assistant_web_view_ = nullptr;
std::unique_ptr<AssistantWebView2> contents_view_;
DISALLOW_COPY_AND_ASSIGN(AssistantWebContainerView);
};
......
// 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 "ash/assistant/ui/assistant_web_view.h"
#include <algorithm>
#include <utility>
#include "ash/assistant/ui/assistant_ui_constants.h"
#include "ash/assistant/ui/assistant_web_view_delegate.h"
#include "ash/assistant/util/deep_link_util.h"
#include "ash/public/cpp/assistant/assistant_web_view_factory.h"
#include "base/bind.h"
#include "base/callback.h"
#include "ui/aura/window.h"
#include "ui/base/window_open_disposition.h"
#include "ui/compositor/layer.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/canvas.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/widget/widget.h"
namespace ash {
// AssistantWebView ------------------------------------------------------------
AssistantWebView::AssistantWebView(
AssistantViewDelegate* assistant_view_delegate,
AssistantWebViewDelegate* web_container_view_delegate)
: assistant_view_delegate_(assistant_view_delegate),
web_container_view_delegate_(web_container_view_delegate) {
InitLayout();
}
AssistantWebView::~AssistantWebView() = default;
const char* AssistantWebView::GetClassName() const {
return "AssistantWebView";
}
gfx::Size AssistantWebView::CalculatePreferredSize() const {
return gfx::Size(kPreferredWidthDip, GetHeightForWidth(kPreferredWidthDip));
}
int AssistantWebView::GetHeightForWidth(int width) const {
return INT_MAX;
}
void AssistantWebView::ChildPreferredSizeChanged(views::View* child) {
// Because AssistantWebView has a fixed size, it does not re-layout its
// children when their preferred size changes. To address this, we need to
// explicitly request a layout pass.
Layout();
SchedulePaint();
}
void AssistantWebView::InitLayout() {
SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical));
}
void AssistantWebView::DidStopLoading() {
// We should only respond to the |DidStopLoading| event the first time, to add
// the view for contents to our view hierarchy and perform other one-time view
// initializations.
if (contents_view_->parent())
return;
contents_view_->SetPreferredSize(GetPreferredSize());
AddChildView(contents_view_.get());
SetFocusBehavior(FocusBehavior::ALWAYS);
}
void AssistantWebView::DidSuppressNavigation(const GURL& url,
WindowOpenDisposition disposition,
bool from_user_gesture) {
if (!from_user_gesture)
return;
// Deep links are always handled by AssistantViewDelegate. If the
// |disposition| indicates a desire to open a new foreground tab, we also
// defer to the AssistantViewDelegate so that it can open the |url| in the
// browser.
if (assistant::util::IsDeepLinkUrl(url) ||
disposition == WindowOpenDisposition::NEW_FOREGROUND_TAB) {
assistant_view_delegate_->OpenUrlFromView(url);
return;
}
// Otherwise we'll allow our WebContents to navigate freely.
contents_view_->Navigate(url);
}
void AssistantWebView::DidChangeCanGoBack(bool can_go_back) {
DCHECK(web_container_view_delegate_);
web_container_view_delegate_->UpdateBackButtonVisibility(GetWidget(),
can_go_back);
}
bool AssistantWebView::GoBack() {
return contents_view_ && contents_view_->GoBack();
}
void AssistantWebView::OpenUrl(const GURL& url) {
RemoveContents();
AssistantWebView2::InitParams contents_params;
contents_params.suppress_navigation = true;
contents_view_ = AssistantWebViewFactory::Get()->Create(contents_params);
// We retain ownership of |contents_view_| as it is only added to the view
// hierarchy once loading stops and we want to ensure that it is cleaned up in
// the rare chance that that never occurs.
contents_view_->set_owned_by_client();
// We observe |contents_view_| so that we can handle events from the
// underlying WebContents.
contents_view_->AddObserver(this);
// Navigate to the specified |url|.
contents_view_->Navigate(url);
}
void AssistantWebView::RemoveContents() {
if (!contents_view_)
return;
RemoveChildView(contents_view_.get());
SetFocusBehavior(FocusBehavior::NEVER);
contents_view_->RemoveObserver(this);
contents_view_.reset();
}
} // namespace ash
// 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 ASH_ASSISTANT_UI_ASSISTANT_WEB_VIEW_H_
#define ASH_ASSISTANT_UI_ASSISTANT_WEB_VIEW_H_
#include <map>
#include <memory>
#include <string>
#include "ash/assistant/ui/assistant_view_delegate.h"
#include "ash/public/cpp/assistant/assistant_web_view_2.h"
#include "base/component_export.h"
#include "base/macros.h"
#include "base/optional.h"
#include "ui/views/view.h"
namespace ash {
enum class AssistantButtonId;
class AssistantWebViewDelegate;
// TODO(b/146520500): Merge into AssistantWebContainerView.
class COMPONENT_EXPORT(ASSISTANT_UI) AssistantWebView
: public views::View,
public AssistantWebView2::Observer {
public:
AssistantWebView(AssistantViewDelegate* assistant_view_delegate,
AssistantWebViewDelegate* web_container_view_delegate);
~AssistantWebView() override;
// views::View:
const char* GetClassName() const override;
gfx::Size CalculatePreferredSize() const override;
int GetHeightForWidth(int width) const override;
void ChildPreferredSizeChanged(views::View* child) override;
// AssistantWebView2::Observer:
void DidStopLoading() override;
void DidSuppressNavigation(const GURL& url,
WindowOpenDisposition disposition,
bool from_user_gesture) override;
void DidChangeCanGoBack(bool can_go_back) override;
// Invoke to navigate back in the embedded WebContents' navigation stack. If
// backwards navigation is not possible, returns |false|. Otherwise |true| to
// indicate success.
bool GoBack();
// Invoke to open the specified |url|.
void OpenUrl(const GURL& url);
private:
void InitLayout();
void RemoveContents();
// TODO(b/143177141): Remove AssistantViewDelegate.
AssistantViewDelegate* const assistant_view_delegate_;
AssistantWebViewDelegate* const web_container_view_delegate_;
std::unique_ptr<AssistantWebView2> contents_view_;
DISALLOW_COPY_AND_ASSIGN(AssistantWebView);
};
} // namespace ash
#endif // ASH_ASSISTANT_UI_ASSISTANT_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