Commit 0d0d3a60 authored by Wei Li's avatar Wei Li Committed by Commit Bot

Add a new SetContentsView API to accept unique_ptr

Since the majority of use cases is to let Widget to own its content
view, and we are moving towards removing those cases otherwise, this CL
adds a new API to make the ownership transfer clearer. In the
followup CL, we should move most of current use cases to adopt
this new API.

BUG=1044687

Change-Id: Ie21fd78cda1c74252171d543f1840405b8d48a10
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2198363Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Commit-Queue: Wei Li <weili@chromium.org>
Cr-Commit-Position: refs/heads/master@{#768877}
parent 9a56c394
This diff is collapsed.
...@@ -497,7 +497,18 @@ class VIEWS_EXPORT Widget : public internal::NativeWidgetDelegate, ...@@ -497,7 +497,18 @@ class VIEWS_EXPORT Widget : public internal::NativeWidgetDelegate,
// Sets the specified view as the contents of this Widget. There can only // Sets the specified view as the contents of this Widget. There can only
// be one contents view child of this Widget's RootView. This view is sized to // be one contents view child of this Widget's RootView. This view is sized to
// fit the entire size of the RootView. The RootView takes ownership of this // fit the entire size of the RootView. The RootView takes ownership of this
// View, unless it is set as not being parent-owned. // View, unless it is passed in as a raw pointer and set as not being
// parent-owned. Prefer using SetContentsView(std::unique_ptr) over passing a
// raw pointer for new code.
template <typename T>
T* SetContentsView(std::unique_ptr<T> view) {
DCHECK(!view->owned_by_client())
<< "This should only be called if the client is passing over the "
"ownership of |view|.";
T* raw_pointer = view.get();
SetContentsView(view.release());
return raw_pointer;
}
void SetContentsView(View* view); void SetContentsView(View* view);
// NOTE: This may not be the same view as WidgetDelegate::GetContentsView(). // NOTE: This may not be the same view as WidgetDelegate::GetContentsView().
......
...@@ -1333,9 +1333,8 @@ class DesktopAuraPaintWidgetTest : public DesktopWidgetTest { ...@@ -1333,9 +1333,8 @@ class DesktopAuraPaintWidgetTest : public DesktopWidgetTest {
CreateParamsForTestWidget(type)); CreateParamsForTestWidget(type));
paint_widget_ = widget.get(); paint_widget_ = widget.get();
View* contents_view = new View(); View* contents_view = widget->SetContentsView(std::make_unique<View>());
contents_view->SetFocusBehavior(View::FocusBehavior::ALWAYS); contents_view->SetFocusBehavior(View::FocusBehavior::ALWAYS);
widget->SetContentsView(contents_view);
widget->Show(); widget->Show();
widget->Activate(); widget->Activate();
...@@ -2009,7 +2008,8 @@ TEST_F(WidgetTest, WidgetDeleted_InOnMousePressed) { ...@@ -2009,7 +2008,8 @@ TEST_F(WidgetTest, WidgetDeleted_InOnMousePressed) {
CreateParams(views::Widget::InitParams::TYPE_POPUP); CreateParams(views::Widget::InitParams::TYPE_POPUP);
widget->Init(std::move(params)); widget->Init(std::move(params));
widget->SetContentsView(new CloseWidgetView(ui::ET_MOUSE_PRESSED)); widget->SetContentsView(
std::make_unique<CloseWidgetView>(ui::ET_MOUSE_PRESSED));
widget->SetSize(gfx::Size(100, 100)); widget->SetSize(gfx::Size(100, 100));
widget->Show(); widget->Show();
...@@ -2034,7 +2034,8 @@ TEST_F(WidgetTest, WidgetDeleted_InDispatchGestureEvent) { ...@@ -2034,7 +2034,8 @@ TEST_F(WidgetTest, WidgetDeleted_InDispatchGestureEvent) {
CreateParams(views::Widget::InitParams::TYPE_POPUP); CreateParams(views::Widget::InitParams::TYPE_POPUP);
widget->Init(std::move(params)); widget->Init(std::move(params));
widget->SetContentsView(new CloseWidgetView(ui::ET_GESTURE_TAP_DOWN)); widget->SetContentsView(
std::make_unique<CloseWidgetView>(ui::ET_GESTURE_TAP_DOWN));
widget->SetSize(gfx::Size(100, 100)); widget->SetSize(gfx::Size(100, 100));
widget->Show(); widget->Show();
......
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