Commit e7d5d8d3 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

ArcCustomTab cleanup, part 2.

ArcCustomTabView was the lone implementation of this abstract base.
Merge the two classes and simplify the construction to happen directly,
rather than through a factory method.

Further simplify the class by making it not be a View override.
ArcCustomTabView didn't do anything related to being a View other than
call GetWidget(), which it can just as easily call on (what was) its
child NativeViewHost.  Now that view is made a direct child of the
widget ContentsView, without the ArcCustomTabView in between.  For now,
lifetimes stay the same -- the NativeViewHost becomes
set_owned_by_client() instead of |this| in order to preserve the
property that the custom tab and its |host_| are deleted together,
irrespective of the lifetime of the widget ContentsView.

Bug: none
Change-Id: I921189cf74b63cc34d3789332e1b53a627e83602
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2225493Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarYusuke Sato <yusukes@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#774344}
parent 42bb5d70
...@@ -241,8 +241,7 @@ component("ash") { ...@@ -241,8 +241,7 @@ component("ash") {
"bluetooth_devices_observer.h", "bluetooth_devices_observer.h",
"cancel_mode.cc", "cancel_mode.cc",
"cancel_mode.h", "cancel_mode.h",
"custom_tab/arc_custom_tab_view.cc", "custom_tab/arc_custom_tab.cc",
"custom_tab/arc_custom_tab_view.h",
"dbus/ash_dbus_services.cc", "dbus/ash_dbus_services.cc",
"dbus/ash_dbus_services.h", "dbus/ash_dbus_services.h",
"dbus/display_service_provider.cc", "dbus/display_service_provider.cc",
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "ash/custom_tab/arc_custom_tab_view.h" #include "ash/public/cpp/arc_custom_tab.h"
#include <memory> #include <memory>
#include <string> #include <string>
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "components/exo/surface.h" #include "components/exo/surface.h"
#include "ui/aura/window.h" #include "ui/aura/window.h"
#include "ui/aura/window_targeter.h" #include "ui/aura/window_targeter.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/widget/widget.h" #include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h" #include "ui/views/widget/widget_delegate.h"
...@@ -31,41 +30,23 @@ void EnumerateSurfaces(aura::Window* window, std::vector<exo::Surface*>* out) { ...@@ -31,41 +30,23 @@ void EnumerateSurfaces(aura::Window* window, std::vector<exo::Surface*>* out) {
} // namespace } // namespace
ArcCustomTab::ArcCustomTab() = default; ArcCustomTab::ArcCustomTab(aura::Window* arc_app_window,
ArcCustomTab::~ArcCustomTab() = default; int32_t surface_id,
int32_t top_margin)
// static
std::unique_ptr<ArcCustomTab> ArcCustomTab::Create(aura::Window* arc_app_window,
int32_t surface_id,
int32_t top_margin) {
views::Widget* widget =
views::Widget::GetWidgetForNativeWindow(arc_app_window);
if (!widget) {
LOG(ERROR) << "No widget for the ARC app window.";
return nullptr;
}
auto* parent = widget->widget_delegate()->GetContentsView();
parent->SetLayoutManager(std::make_unique<views::FillLayout>());
auto view = std::make_unique<ArcCustomTabView>(arc_app_window, surface_id,
top_margin);
parent->AddChildView(view.get());
return std::move(view);
}
ArcCustomTabView::ArcCustomTabView(aura::Window* arc_app_window,
int32_t surface_id,
int32_t top_margin)
: arc_app_window_(arc_app_window), : arc_app_window_(arc_app_window),
surface_id_(surface_id), surface_id_(surface_id),
top_margin_(top_margin) { top_margin_(top_margin) {
set_owned_by_client();
other_windows_observer_.Add(arc_app_window_); other_windows_observer_.Add(arc_app_window_);
host_->set_owned_by_client();
auto* const widget = views::Widget::GetWidgetForNativeWindow(arc_app_window_);
DCHECK(widget);
widget->GetContentsView()->AddChildView(host_.get());
} }
ArcCustomTabView::~ArcCustomTabView() = default; ArcCustomTab::~ArcCustomTab() = default;
void ArcCustomTabView::Attach(gfx::NativeView view) { void ArcCustomTab::Attach(gfx::NativeView view) {
DCHECK(view); DCHECK(view);
DCHECK(!GetHostView()); DCHECK(!GetHostView());
host_->Attach(view); host_->Attach(view);
...@@ -76,34 +57,34 @@ void ArcCustomTabView::Attach(gfx::NativeView view) { ...@@ -76,34 +57,34 @@ void ArcCustomTabView::Attach(gfx::NativeView view) {
UpdateSurfaceIfNecessary(); UpdateSurfaceIfNecessary();
} }
gfx::NativeView ArcCustomTabView::GetHostView() { gfx::NativeView ArcCustomTab::GetHostView() {
return host_->native_view(); return host_->native_view();
} }
void ArcCustomTabView::OnWindowHierarchyChanged( void ArcCustomTab::OnWindowHierarchyChanged(
const HierarchyChangeParams& params) { const HierarchyChangeParams& params) {
if ((params.receiver == arc_app_window_) && if ((params.receiver == arc_app_window_) &&
exo::Surface::AsSurface(params.target) && params.new_parent) exo::Surface::AsSurface(params.target) && params.new_parent)
UpdateSurfaceIfNecessary(); UpdateSurfaceIfNecessary();
} }
void ArcCustomTabView::OnWindowBoundsChanged(aura::Window* window, void ArcCustomTab::OnWindowBoundsChanged(aura::Window* window,
const gfx::Rect& old_bounds, const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds, const gfx::Rect& new_bounds,
ui::PropertyChangeReason reason) { ui::PropertyChangeReason reason) {
if (surface_window_observer_.IsObserving(window) && if (surface_window_observer_.IsObserving(window) &&
old_bounds.size() != new_bounds.size()) old_bounds.size() != new_bounds.size())
OnSurfaceBoundsMaybeChanged(window); OnSurfaceBoundsMaybeChanged(window);
} }
void ArcCustomTabView::OnWindowPropertyChanged(aura::Window* window, void ArcCustomTab::OnWindowPropertyChanged(aura::Window* window,
const void* key, const void* key,
intptr_t old) { intptr_t old) {
if (surfaces_observer_.IsObserving(window) && key == exo::kClientSurfaceIdKey) if (surfaces_observer_.IsObserving(window) && key == exo::kClientSurfaceIdKey)
UpdateSurfaceIfNecessary(); UpdateSurfaceIfNecessary();
} }
void ArcCustomTabView::OnWindowStackingChanged(aura::Window* window) { void ArcCustomTab::OnWindowStackingChanged(aura::Window* window) {
if (window == host_->GetNativeViewContainer() && if (window == host_->GetNativeViewContainer() &&
!weak_ptr_factory_.HasWeakPtrs()) { !weak_ptr_factory_.HasWeakPtrs()) {
// Reordering should happen asynchronously -- some entity (like // Reordering should happen asynchronously -- some entity (like
...@@ -112,12 +93,12 @@ void ArcCustomTabView::OnWindowStackingChanged(aura::Window* window) { ...@@ -112,12 +93,12 @@ void ArcCustomTabView::OnWindowStackingChanged(aura::Window* window) {
// window/layer ordering and causes weird graphical effects. // window/layer ordering and causes weird graphical effects.
// TODO(hashimoto): fix the views ordering and remove this handling. // TODO(hashimoto): fix the views ordering and remove this handling.
base::SequencedTaskRunnerHandle::Get()->PostTask( base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(&ArcCustomTabView::EnsureWindowOrders, FROM_HERE, base::BindOnce(&ArcCustomTab::EnsureWindowOrders,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
} }
void ArcCustomTabView::OnWindowDestroying(aura::Window* window) { void ArcCustomTab::OnWindowDestroying(aura::Window* window) {
if (surfaces_observer_.IsObserving(window)) if (surfaces_observer_.IsObserving(window))
surfaces_observer_.Remove(window); surfaces_observer_.Remove(window);
if (surface_window_observer_.IsObserving(window)) if (surface_window_observer_.IsObserving(window))
...@@ -126,8 +107,7 @@ void ArcCustomTabView::OnWindowDestroying(aura::Window* window) { ...@@ -126,8 +107,7 @@ void ArcCustomTabView::OnWindowDestroying(aura::Window* window) {
other_windows_observer_.Remove(window); other_windows_observer_.Remove(window);
} }
void ArcCustomTabView::OnSurfaceBoundsMaybeChanged( void ArcCustomTab::OnSurfaceBoundsMaybeChanged(aura::Window* surface_window) {
aura::Window* surface_window) {
DCHECK(surface_window); DCHECK(surface_window);
gfx::Point origin(0, top_margin_); gfx::Point origin(0, top_margin_);
gfx::Point bottom_right(surface_window->bounds().width(), gfx::Point bottom_right(surface_window->bounds().width(),
...@@ -138,20 +118,20 @@ void ArcCustomTabView::OnSurfaceBoundsMaybeChanged( ...@@ -138,20 +118,20 @@ void ArcCustomTabView::OnSurfaceBoundsMaybeChanged(
bottom_right.y() - origin.y()); bottom_right.y() - origin.y());
} }
void ArcCustomTabView::EnsureWindowOrders() { void ArcCustomTab::EnsureWindowOrders() {
aura::Window* const container = host_->GetNativeViewContainer(); aura::Window* const container = host_->GetNativeViewContainer();
if (container) if (container)
container->parent()->StackChildAtTop(container); container->parent()->StackChildAtTop(container);
} }
void ArcCustomTabView::ConvertPointFromWindow(aura::Window* window, void ArcCustomTab::ConvertPointFromWindow(aura::Window* window,
gfx::Point* point) { gfx::Point* point) {
aura::Window::ConvertPointToTarget(window, GetWidget()->GetNativeWindow(), views::Widget* const widget = host_->GetWidget();
point); aura::Window::ConvertPointToTarget(window, widget->GetNativeWindow(), point);
views::View::ConvertPointFromWidget(parent(), point); views::View::ConvertPointFromWidget(widget->GetContentsView(), point);
} }
void ArcCustomTabView::UpdateSurfaceIfNecessary() { void ArcCustomTab::UpdateSurfaceIfNecessary() {
std::vector<exo::Surface*> surfaces; std::vector<exo::Surface*> surfaces;
EnumerateSurfaces(arc_app_window_, &surfaces); EnumerateSurfaces(arc_app_window_, &surfaces);
......
// Copyright 2019 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_CUSTOM_TAB_ARC_CUSTOM_TAB_VIEW_H_
#define ASH_CUSTOM_TAB_ARC_CUSTOM_TAB_VIEW_H_
#include <memory>
#include "ash/public/cpp/arc_custom_tab.h"
#include "base/scoped_observer.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#include "ui/views/controls/native/native_view_host.h"
#include "ui/views/view.h"
namespace ash {
// A view-based implementation of ArcCustomTab which works in the classic
// environment.
class ArcCustomTabView : public ArcCustomTab,
public views::View,
public aura::WindowObserver {
public:
ArcCustomTabView(aura::Window* arc_app_window,
int32_t surface_id,
int32_t top_margin);
ArcCustomTabView(const ArcCustomTabView&) = delete;
ArcCustomTabView& operator=(const ArcCustomTabView&) = delete;
~ArcCustomTabView() override;
private:
// ArcCustomTab:
void Attach(gfx::NativeView view) override;
gfx::NativeView GetHostView() override;
// aura::WindowObserver:
void OnWindowHierarchyChanged(const HierarchyChangeParams& params) override;
void OnWindowBoundsChanged(aura::Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds,
ui::PropertyChangeReason reason) override;
void OnWindowPropertyChanged(aura::Window* window,
const void* key,
intptr_t old) override;
void OnWindowStackingChanged(aura::Window* window) override;
void OnWindowDestroying(aura::Window* window) override;
// Updates |host_|'s bounds to deal with changes in the bounds of the
// associated |surface_window|.
void OnSurfaceBoundsMaybeChanged(aura::Window* surface_window);
// Ensures the window/layer orders for the NativeViewHost.
void EnsureWindowOrders();
// Converts the point from the given window to this view.
void ConvertPointFromWindow(aura::Window* window, gfx::Point* point);
// Looks for the surface with |surface_id_|, and handles resultant changes.
void UpdateSurfaceIfNecessary();
views::NativeViewHost* const host_ =
AddChildView(std::make_unique<views::NativeViewHost>());
aura::Window* const arc_app_window_;
const int32_t surface_id_, top_margin_;
ScopedObserver<aura::Window, aura::WindowObserver> surfaces_observer_{this};
ScopedObserver<aura::Window, aura::WindowObserver> surface_window_observer_{
this};
ScopedObserver<aura::Window, aura::WindowObserver> other_windows_observer_{
this};
base::WeakPtrFactory<ArcCustomTabView> weak_ptr_factory_{this};
};
} // namespace ash
#endif // ASH_CUSTOM_TAB_ARC_CUSTOM_TAB_VIEW_H_
...@@ -9,30 +9,66 @@ ...@@ -9,30 +9,66 @@
#include "ash/ash_export.h" #include "ash/ash_export.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/scoped_observer.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#include "ui/gfx/native_widget_types.h" #include "ui/gfx/native_widget_types.h"
#include "ui/views/controls/native/native_view_host.h"
namespace ash { namespace ash {
// ArcCustomTab is responsible to embed an ARC++ custom tab. // ArcCustomTab is responsible to embed an ARC++ custom tab.
class ASH_EXPORT ArcCustomTab { class ASH_EXPORT ArcCustomTab : public aura::WindowObserver {
public: public:
// Creates a new ArcCustomTab instance. Returns null when the arguments are ArcCustomTab(aura::Window* arc_app_window,
// invalid. int32_t surface_id,
static std::unique_ptr<ArcCustomTab> Create(aura::Window* arc_app_window, int32_t top_margin);
int32_t surface_id, ArcCustomTab(const ArcCustomTab&) = delete;
int32_t top_margin); ArcCustomTab& operator=(const ArcCustomTab&) = delete;
~ArcCustomTab() override;
ArcCustomTab(); void Attach(gfx::NativeView view);
virtual ~ArcCustomTab();
virtual void Attach(gfx::NativeView view) = 0;
// Returns the view against which a view or dialog is positioned and parented // Returns the view against which a view or dialog is positioned and parented
// in an ArcCustomTab. // in an ArcCustomTab.
virtual gfx::NativeView GetHostView() = 0; gfx::NativeView GetHostView();
// aura::WindowObserver:
void OnWindowHierarchyChanged(const HierarchyChangeParams& params) override;
void OnWindowBoundsChanged(aura::Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds,
ui::PropertyChangeReason reason) override;
void OnWindowPropertyChanged(aura::Window* window,
const void* key,
intptr_t old) override;
void OnWindowStackingChanged(aura::Window* window) override;
void OnWindowDestroying(aura::Window* window) override;
private: private:
DISALLOW_COPY_AND_ASSIGN(ArcCustomTab); // Updates |host_|'s bounds to deal with changes in the bounds of the
// associated |surface_window|.
void OnSurfaceBoundsMaybeChanged(aura::Window* surface_window);
// Ensures the window/layer orders for the NativeViewHost.
void EnsureWindowOrders();
// Converts the point from the given window to this view.
void ConvertPointFromWindow(aura::Window* window, gfx::Point* point);
// Looks for the surface with |surface_id_|, and handles resultant changes.
void UpdateSurfaceIfNecessary();
std::unique_ptr<views::NativeViewHost> host_ =
std::make_unique<views::NativeViewHost>();
aura::Window* const arc_app_window_;
const int32_t surface_id_, top_margin_;
ScopedObserver<aura::Window, aura::WindowObserver> surfaces_observer_{this};
ScopedObserver<aura::Window, aura::WindowObserver> surface_window_observer_{
this};
ScopedObserver<aura::Window, aura::WindowObserver> other_windows_observer_{
this};
base::WeakPtrFactory<ArcCustomTab> weak_ptr_factory_{this};
}; };
} // namespace ash } // namespace ash
......
...@@ -100,9 +100,9 @@ IN_PROC_BROWSER_TEST_F(CustomTabSessionImplTest, ...@@ -100,9 +100,9 @@ IN_PROC_BROWSER_TEST_F(CustomTabSessionImplTest,
test_window.shell_surface()->GetWidget()->GetNativeWindow(); test_window.shell_surface()->GetWidget()->GetNativeWindow();
ASSERT_TRUE(aura_window); ASSERT_TRUE(aura_window);
auto custom_tab = ash::ArcCustomTab::Create(aura_window, /* surface_id= */ 0, auto custom_tab = std::make_unique<ash::ArcCustomTab>(aura_window,
/* top_margin= */ 0); /* surface_id= */ 0,
ASSERT_TRUE(custom_tab); /* top_margin= */ 0);
auto web_contents = arc::CreateArcCustomTabWebContents(browser()->profile(), auto web_contents = arc::CreateArcCustomTabWebContents(browser()->profile(),
GURL("http://foo/")); GURL("http://foo/"));
......
...@@ -110,7 +110,7 @@ void ArcPrintSpoolerBridge::OnPrintDocumentSaved( ...@@ -110,7 +110,7 @@ void ArcPrintSpoolerBridge::OnPrintDocumentSaved(
} }
auto custom_tab = auto custom_tab =
ash::ArcCustomTab::Create(arc_window, surface_id, top_margin); std::make_unique<ash::ArcCustomTab>(arc_window, surface_id, top_margin);
auto web_contents = CreateArcCustomTabWebContents(profile_, url); auto web_contents = CreateArcCustomTabWebContents(profile_, url);
// TODO(crbug.com/955171): Remove this temporary conversion to InterfacePtr // TODO(crbug.com/955171): Remove this temporary conversion to InterfacePtr
......
...@@ -212,7 +212,8 @@ mojo::PendingRemote<mojom::PrintSessionHost> PrintSessionImpl::Create( ...@@ -212,7 +212,8 @@ mojo::PendingRemote<mojom::PrintSessionHost> PrintSessionImpl::Create(
std::unique_ptr<content::WebContents> web_contents, std::unique_ptr<content::WebContents> web_contents,
std::unique_ptr<ash::ArcCustomTab> custom_tab, std::unique_ptr<ash::ArcCustomTab> custom_tab,
mojom::PrintSessionInstancePtr instance) { mojom::PrintSessionInstancePtr instance) {
if (!custom_tab || !instance) DCHECK(custom_tab);
if (!instance)
return mojo::NullRemote(); return mojo::NullRemote();
// This object will be deleted when the mojo connection is closed. // This object will be deleted when the mojo connection is closed.
......
...@@ -523,7 +523,7 @@ void ChromeNewWindowClient::OpenArcCustomTab( ...@@ -523,7 +523,7 @@ void ChromeNewWindowClient::OpenArcCustomTab(
} }
auto custom_tab = auto custom_tab =
ash::ArcCustomTab::Create(arc_window, surface_id, top_margin); std::make_unique<ash::ArcCustomTab>(arc_window, surface_id, top_margin);
auto web_contents = arc::CreateArcCustomTabWebContents(profile, url); auto web_contents = arc::CreateArcCustomTabWebContents(profile, url);
// |custom_tab_browser| will be destroyed when its tab strip becomes empty, // |custom_tab_browser| will be destroyed when its tab strip becomes empty,
......
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