Commit 51620d0b authored by ben@chromium.org's avatar ben@chromium.org

Make Widget ownership a little clearer by expressing it in terms of an enum.

BUG=72040
TEST=none
Review URL: http://codereview.chromium.org/7031053

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86669 0039d316-1c4b-4281-b951-d872f2087c98
parent e20d6934
...@@ -270,7 +270,7 @@ void BalloonViewImpl::ViewHierarchyChanged( ...@@ -270,7 +270,7 @@ void BalloonViewImpl::ViewHierarchyChanged(
views::Widget::InitParams params( views::Widget::InitParams params(
views::Widget::InitParams::TYPE_CONTROL); views::Widget::InitParams::TYPE_CONTROL);
params.double_buffer = true; params.double_buffer = true;
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.parent = GetParentNativeView(); params.parent = GetParentNativeView();
control_view_host_->Init(params); control_view_host_->Init(params);
NotificationControlView* control = new NotificationControlView(this); NotificationControlView* control = new NotificationControlView(this);
......
...@@ -63,7 +63,7 @@ void DropdownBarHost::Init(views::View* view, ...@@ -63,7 +63,7 @@ void DropdownBarHost::Init(views::View* view,
// Initialize the host. // Initialize the host.
host_.reset(new views::Widget); host_.reset(new views::Widget);
views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL);
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.parent_widget = browser_view_->GetWidget(); params.parent_widget = browser_view_->GetWidget();
host_->Init(params); host_->Init(params);
host_->SetContentsView(view_); host_->SetContentsView(view_);
......
...@@ -133,7 +133,7 @@ FullscreenExitBubble::FullscreenExitBubble( ...@@ -133,7 +133,7 @@ FullscreenExitBubble::FullscreenExitBubble(
views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
params.transparent = true; params.transparent = true;
params.can_activate = false; params.can_activate = false;
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.parent = frame->GetNativeView(); params.parent = frame->GetNativeView();
params.bounds = GetPopupRect(false); params.bounds = GetPopupRect(false);
popup_->Init(params); popup_->Init(params);
...@@ -154,14 +154,14 @@ FullscreenExitBubble::FullscreenExitBubble( ...@@ -154,14 +154,14 @@ FullscreenExitBubble::FullscreenExitBubble(
FullscreenExitBubble::~FullscreenExitBubble() { FullscreenExitBubble::~FullscreenExitBubble() {
// This is tricky. We may be in an ATL message handler stack, in which case // This is tricky. We may be in an ATL message handler stack, in which case
// the popup cannot be deleted yet. We also can't blindly use // the popup cannot be deleted yet. We also can't set the popup's ownership
// set_delete_on_destroy(true) on the popup to delete it when it closes, // model to NATIVE_WIDGET_OWNS_WIDGET because if the user closed the last tab
// because if the user closed the last tab while in fullscreen mode, Windows // while in fullscreen mode, Windows has already destroyed the popup HWND by
// has already destroyed the popup HWND by the time we get here, and thus // the time we get here, and thus either the popup will already have been
// either the popup will already have been deleted (if we set this in our // deleted (if we set this in our constructor) or the popup will never get
// constructor) or the popup will never get another OnFinalMessage() call (if // another OnFinalMessage() call (if not, as currently). So instead, we tell
// not, as currently). So instead, we tell the popup to synchronously hide, // the popup to synchronously hide, and then asynchronously close and delete
// and then asynchronously close and delete itself. // itself.
popup_->Close(); popup_->Close();
MessageLoop::current()->DeleteSoon(FROM_HERE, popup_); MessageLoop::current()->DeleteSoon(FROM_HERE, popup_);
} }
......
...@@ -567,7 +567,7 @@ void StatusBubbleViews::Init() { ...@@ -567,7 +567,7 @@ void StatusBubbleViews::Init() {
Widget::InitParams params(Widget::InitParams::TYPE_POPUP); Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
params.transparent = true; params.transparent = true;
params.accept_events = false; params.accept_events = false;
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.parent = frame->GetNativeView(); params.parent = frame->GetNativeView();
popup_->Init(params); popup_->Init(params);
popup_->SetOpacity(0x00); popup_->SetOpacity(0x00);
......
...@@ -105,7 +105,7 @@ void NativeTabContentsViewGtk::RemoveConstrainedWindow( ...@@ -105,7 +105,7 @@ void NativeTabContentsViewGtk::RemoveConstrainedWindow(
void NativeTabContentsViewGtk::InitNativeTabContentsView() { void NativeTabContentsViewGtk::InitNativeTabContentsView() {
views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL);
params.native_widget = this; params.native_widget = this;
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
GetWidget()->Init(params); GetWidget()->Init(params);
// We need to own the widget in order to attach/detach the native view to a // We need to own the widget in order to attach/detach the native view to a
......
...@@ -75,7 +75,7 @@ void NativeTabContentsViewWin::EndDragging() { ...@@ -75,7 +75,7 @@ void NativeTabContentsViewWin::EndDragging() {
void NativeTabContentsViewWin::InitNativeTabContentsView() { void NativeTabContentsViewWin::InitNativeTabContentsView() {
views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL);
params.native_widget = this; params.native_widget = this;
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.parent = GetHiddenTabHostWindow(); params.parent = GetHiddenTabHostWindow();
GetWidget()->Init(params); GetWidget()->Init(params);
......
...@@ -43,7 +43,7 @@ DraggedTabView::DraggedTabView(const std::vector<views::View*>& renderers, ...@@ -43,7 +43,7 @@ DraggedTabView::DraggedTabView(const std::vector<views::View*>& renderers,
views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
params.transparent = true; params.transparent = true;
params.keep_on_top = true; params.keep_on_top = true;
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(PreferredContainerSize()); params.bounds = gfx::Rect(PreferredContainerSize());
container_->Init(params); container_->Init(params);
container_->SetContentsView(this); container_->SetContentsView(this);
......
...@@ -1595,7 +1595,7 @@ TEST_F(FocusManagerTest, CreationForNativeRoot) { ...@@ -1595,7 +1595,7 @@ TEST_F(FocusManagerTest, CreationForNativeRoot) {
// Create a view window parented to native dialog. // Create a view window parented to native dialog.
scoped_ptr<Widget> widget1(new Widget); scoped_ptr<Widget> widget1(new Widget);
Widget::InitParams params(Widget::InitParams::TYPE_CONTROL); Widget::InitParams params(Widget::InitParams::TYPE_CONTROL);
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.parent = hwnd; params.parent = hwnd;
params.bounds = gfx::Rect(0, 0, 100, 100); params.bounds = gfx::Rect(0, 0, 100, 100);
widget1->Init(params); widget1->Init(params);
......
...@@ -295,7 +295,7 @@ TEST_F(ViewTest, MouseEvent) { ...@@ -295,7 +295,7 @@ TEST_F(ViewTest, MouseEvent) {
scoped_ptr<Widget> widget(new Widget); scoped_ptr<Widget> widget(new Widget);
Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(50, 50, 650, 650); params.bounds = gfx::Rect(50, 50, 650, 650);
widget->Init(params); widget->Init(params);
View* root = widget->GetRootView(); View* root = widget->GetRootView();
...@@ -404,7 +404,7 @@ TEST_F(ViewTest, TouchEvent) { ...@@ -404,7 +404,7 @@ TEST_F(ViewTest, TouchEvent) {
scoped_ptr<Widget> widget(new Widget()); scoped_ptr<Widget> widget(new Widget());
Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(50, 50, 650, 650); params.bounds = gfx::Rect(50, 50, 650, 650);
widget->Init(params); widget->Init(params);
View* root = widget->GetRootView(); View* root = widget->GetRootView();
...@@ -935,7 +935,7 @@ TEST_F(ViewTest, ActivateAccelerator) { ...@@ -935,7 +935,7 @@ TEST_F(ViewTest, ActivateAccelerator) {
// Create a window and add the view as its child. // Create a window and add the view as its child.
scoped_ptr<Widget> widget(new Widget); scoped_ptr<Widget> widget(new Widget);
Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(0, 0, 100, 100); params.bounds = gfx::Rect(0, 0, 100, 100);
widget->Init(params); widget->Init(params);
View* root = widget->GetRootView(); View* root = widget->GetRootView();
...@@ -1000,7 +1000,7 @@ TEST_F(ViewTest, HiddenViewWithAccelerator) { ...@@ -1000,7 +1000,7 @@ TEST_F(ViewTest, HiddenViewWithAccelerator) {
scoped_ptr<Widget> widget(new Widget); scoped_ptr<Widget> widget(new Widget);
Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(0, 0, 100, 100); params.bounds = gfx::Rect(0, 0, 100, 100);
widget->Init(params); widget->Init(params);
View* root = widget->GetRootView(); View* root = widget->GetRootView();
...@@ -1822,7 +1822,7 @@ TEST_F(ViewTest, OnVisibleBoundsChanged) { ...@@ -1822,7 +1822,7 @@ TEST_F(ViewTest, OnVisibleBoundsChanged) {
scoped_ptr<Widget> widget(new Widget); scoped_ptr<Widget> widget(new Widget);
Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = viewport_bounds; params.bounds = viewport_bounds;
widget->Init(params); widget->Init(params);
widget->GetRootView()->SetBoundsRect(viewport_bounds); widget->GetRootView()->SetBoundsRect(viewport_bounds);
......
...@@ -289,7 +289,7 @@ NativeWidgetGtk::NativeWidgetGtk(internal::NativeWidgetDelegate* delegate) ...@@ -289,7 +289,7 @@ NativeWidgetGtk::NativeWidgetGtk(internal::NativeWidgetDelegate* delegate)
window_contents_(NULL), window_contents_(NULL),
child_(false), child_(false),
ALLOW_THIS_IN_INITIALIZER_LIST(close_widget_factory_(this)), ALLOW_THIS_IN_INITIALIZER_LIST(close_widget_factory_(this)),
delete_on_destroy_(true), ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET),
transparent_(false), transparent_(false),
ignore_events_(false), ignore_events_(false),
ignore_drag_leave_(false), ignore_drag_leave_(false),
...@@ -323,8 +323,9 @@ NativeWidgetGtk::~NativeWidgetGtk() { ...@@ -323,8 +323,9 @@ NativeWidgetGtk::~NativeWidgetGtk() {
// We need to delete the input method before calling DestroyRootView(), // We need to delete the input method before calling DestroyRootView(),
// because it'll set focus_manager_ to NULL. // because it'll set focus_manager_ to NULL.
input_method_.reset(); input_method_.reset();
DCHECK(delete_on_destroy_ || widget_ == NULL); DCHECK(ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET ||
if (delete_on_destroy_) widget_ == NULL);
if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
delete delegate_; delete delegate_;
// TODO(altimofeev): investigate why OnDestroy could not be called. // TODO(altimofeev): investigate why OnDestroy could not be called.
if (!child_) if (!child_)
...@@ -1334,7 +1335,7 @@ void NativeWidgetGtk::OnDestroy(GtkWidget* object) { ...@@ -1334,7 +1335,7 @@ void NativeWidgetGtk::OnDestroy(GtkWidget* object) {
// NULL out pointers here since we might still be in an observer list // NULL out pointers here since we might still be in an observer list
// until delstion happens. // until delstion happens.
widget_ = window_contents_ = NULL; widget_ = window_contents_ = NULL;
if (delete_on_destroy_) { if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) {
// Delays the deletion of this NativeWidgetGtk as we want its children to // Delays the deletion of this NativeWidgetGtk as we want its children to
// have access to it when destroyed. // have access to it when destroyed.
MessageLoop::current()->DeleteSoon(FROM_HERE, this); MessageLoop::current()->DeleteSoon(FROM_HERE, this);
...@@ -1415,7 +1416,7 @@ void NativeWidgetGtk::DispatchKeyEventPostIME(const KeyEvent& key) { ...@@ -1415,7 +1416,7 @@ void NativeWidgetGtk::DispatchKeyEventPostIME(const KeyEvent& key) {
void NativeWidgetGtk::SetInitParams(const Widget::InitParams& params) { void NativeWidgetGtk::SetInitParams(const Widget::InitParams& params) {
DCHECK(!GetNativeView()); DCHECK(!GetNativeView());
delete_on_destroy_ = params.delete_on_destroy; ownership_ = params.ownership;
child_ = params.child; child_ = params.child;
// TODO(beng): The secondary checks here actually obviate the need for // TODO(beng): The secondary checks here actually obviate the need for
......
...@@ -326,7 +326,7 @@ class NativeWidgetGtk : public NativeWidget, ...@@ -326,7 +326,7 @@ class NativeWidgetGtk : public NativeWidget,
ScopedRunnableMethodFactory<NativeWidgetGtk> close_widget_factory_; ScopedRunnableMethodFactory<NativeWidgetGtk> close_widget_factory_;
// See class documentation for Widget in widget.h for a note about ownership. // See class documentation for Widget in widget.h for a note about ownership.
bool delete_on_destroy_; Widget::InitParams::Ownership ownership_;
// See description above make_transparent for details. // See description above make_transparent for details.
bool transparent_; bool transparent_;
......
...@@ -18,7 +18,7 @@ NativeWidget* CreateNativeWidget() { ...@@ -18,7 +18,7 @@ NativeWidget* CreateNativeWidget() {
NativeWidget* CreateNativeWidgetWithContents(View* contents_view) { NativeWidget* CreateNativeWidgetWithContents(View* contents_view) {
Widget* widget = new Widget; Widget* widget = new Widget;
Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(10, 10, 200, 200); params.bounds = gfx::Rect(10, 10, 200, 200);
widget->Init(params); widget->Init(params);
return widget->native_widget(); return widget->native_widget();
...@@ -27,7 +27,7 @@ NativeWidget* CreateNativeWidgetWithContents(View* contents_view) { ...@@ -27,7 +27,7 @@ NativeWidget* CreateNativeWidgetWithContents(View* contents_view) {
NativeWidget* CreateNativeWidgetWithParent(NativeWidget* parent) { NativeWidget* CreateNativeWidgetWithParent(NativeWidget* parent) {
Widget* widget = new Widget; Widget* widget = new Widget;
Widget::InitParams params(Widget::InitParams::TYPE_CONTROL); Widget::InitParams params(Widget::InitParams::TYPE_CONTROL);
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.parent = parent ? parent->GetWidget()->GetNativeView() : NULL; params.parent = parent ? parent->GetWidget()->GetNativeView() : NULL;
params.bounds = gfx::Rect(10, 10, 200, 200); params.bounds = gfx::Rect(10, 10, 200, 200);
widget->Init(params); widget->Init(params);
......
...@@ -18,7 +18,7 @@ NativeWidget* CreateNativeWidget() { ...@@ -18,7 +18,7 @@ NativeWidget* CreateNativeWidget() {
NativeWidget* CreateNativeWidgetWithContents(View* contents_view) { NativeWidget* CreateNativeWidgetWithContents(View* contents_view) {
Widget* widget = new Widget; Widget* widget = new Widget;
Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(10, 10, 200, 200); params.bounds = gfx::Rect(10, 10, 200, 200);
widget->Init(params); widget->Init(params);
return widget->native_widget(); return widget->native_widget();
...@@ -27,7 +27,7 @@ NativeWidget* CreateNativeWidgetWithContents(View* contents_view) { ...@@ -27,7 +27,7 @@ NativeWidget* CreateNativeWidgetWithContents(View* contents_view) {
NativeWidget* CreateNativeWidgetWithParent(NativeWidget* parent) { NativeWidget* CreateNativeWidgetWithParent(NativeWidget* parent) {
Widget* widget = new Widget; Widget* widget = new Widget;
Widget::InitParams params(Widget::InitParams::TYPE_CONTROL); Widget::InitParams params(Widget::InitParams::TYPE_CONTROL);
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.child = false; // Implicitly set to true by ctor with TYPE_CONTROL. params.child = false; // Implicitly set to true by ctor with TYPE_CONTROL.
params.parent = parent ? parent->GetWidget()->GetNativeView() : NULL; params.parent = parent ? parent->GetWidget()->GetNativeView() : NULL;
params.bounds = gfx::Rect(10, 10, 200, 200); params.bounds = gfx::Rect(10, 10, 200, 200);
......
...@@ -139,7 +139,7 @@ NativeWidgetWin::NativeWidgetWin(internal::NativeWidgetDelegate* delegate) ...@@ -139,7 +139,7 @@ NativeWidgetWin::NativeWidgetWin(internal::NativeWidgetDelegate* delegate)
use_layered_buffer_(false), use_layered_buffer_(false),
layered_alpha_(255), layered_alpha_(255),
ALLOW_THIS_IN_INITIALIZER_LIST(paint_layered_window_factory_(this)), ALLOW_THIS_IN_INITIALIZER_LIST(paint_layered_window_factory_(this)),
delete_on_destroy_(true), ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET),
can_update_layered_window_(true), can_update_layered_window_(true),
is_window_(false), is_window_(false),
restore_focus_when_enabled_(false), restore_focus_when_enabled_(false),
...@@ -153,7 +153,7 @@ NativeWidgetWin::~NativeWidgetWin() { ...@@ -153,7 +153,7 @@ NativeWidgetWin::~NativeWidgetWin() {
// We need to delete the input method before calling DestroyRootView(), // We need to delete the input method before calling DestroyRootView(),
// because it'll set focus_manager_ to NULL. // because it'll set focus_manager_ to NULL.
input_method_.reset(); input_method_.reset();
if (delete_on_destroy_) if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
delete delegate_; delete delegate_;
} }
...@@ -954,7 +954,7 @@ void NativeWidgetWin::OnWindowPosChanged(WINDOWPOS* window_pos) { ...@@ -954,7 +954,7 @@ void NativeWidgetWin::OnWindowPosChanged(WINDOWPOS* window_pos) {
} }
void NativeWidgetWin::OnFinalMessage(HWND window) { void NativeWidgetWin::OnFinalMessage(HWND window) {
if (delete_on_destroy_) if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
delete this; delete this;
} }
...@@ -1048,7 +1048,7 @@ void NativeWidgetWin::PostProcessActivateMessage(NativeWidgetWin* widget, ...@@ -1048,7 +1048,7 @@ void NativeWidgetWin::PostProcessActivateMessage(NativeWidgetWin* widget,
void NativeWidgetWin::SetInitParams(const Widget::InitParams& params) { void NativeWidgetWin::SetInitParams(const Widget::InitParams& params) {
// Set non-style attributes. // Set non-style attributes.
delete_on_destroy_ = params.delete_on_destroy; ownership_ = params.ownership;
DWORD style = WS_CLIPCHILDREN | WS_CLIPSIBLINGS; DWORD style = WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
DWORD ex_style = 0; DWORD ex_style = 0;
......
...@@ -475,7 +475,7 @@ class NativeWidgetWin : public ui::WindowImpl, ...@@ -475,7 +475,7 @@ class NativeWidgetWin : public ui::WindowImpl,
ScopedRunnableMethodFactory<NativeWidgetWin> paint_layered_window_factory_; ScopedRunnableMethodFactory<NativeWidgetWin> paint_layered_window_factory_;
// See class documentation for Widget in widget.h for a note about ownership. // See class documentation for Widget in widget.h for a note about ownership.
bool delete_on_destroy_; Widget::InitParams::Ownership ownership_;
// True if we are allowed to update the layered window from the DIB backing // True if we are allowed to update the layered window from the DIB backing
// store if necessary. // store if necessary.
......
...@@ -45,7 +45,7 @@ class NativeWidgetWinTest : public testing::Test { ...@@ -45,7 +45,7 @@ class NativeWidgetWinTest : public testing::Test {
NativeWidgetWin* NativeWidgetWinTest::CreateNativeWidgetWin() { NativeWidgetWin* NativeWidgetWinTest::CreateNativeWidgetWin() {
scoped_ptr<Widget> widget(new Widget); scoped_ptr<Widget> widget(new Widget);
Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
params.delete_on_destroy = false; params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(50, 50, 650, 650); params.bounds = gfx::Rect(50, 50, 650, 650);
widget->Init(params); widget->Init(params);
return static_cast<NativeWidgetWin*>(widget.release()->native_widget()); return static_cast<NativeWidgetWin*>(widget.release()->native_widget());
......
...@@ -33,7 +33,7 @@ Widget::InitParams::InitParams() ...@@ -33,7 +33,7 @@ Widget::InitParams::InitParams()
accept_events(true), accept_events(true),
can_activate(true), can_activate(true),
keep_on_top(false), keep_on_top(false),
delete_on_destroy(true), ownership(NATIVE_WIDGET_OWNS_WIDGET),
mirror_origin_in_rtl(false), mirror_origin_in_rtl(false),
has_dropshadow(false), has_dropshadow(false),
double_buffer(false), double_buffer(false),
...@@ -50,7 +50,7 @@ Widget::InitParams::InitParams(Type type) ...@@ -50,7 +50,7 @@ Widget::InitParams::InitParams(Type type)
accept_events(true), accept_events(true),
can_activate(type != TYPE_POPUP && type != TYPE_MENU), can_activate(type != TYPE_POPUP && type != TYPE_MENU),
keep_on_top(type == TYPE_MENU), keep_on_top(type == TYPE_MENU),
delete_on_destroy(true), ownership(NATIVE_WIDGET_OWNS_WIDGET),
mirror_origin_in_rtl(false), mirror_origin_in_rtl(false),
has_dropshadow(false), has_dropshadow(false),
double_buffer(false), double_buffer(false),
...@@ -73,14 +73,14 @@ Widget::Widget() ...@@ -73,14 +73,14 @@ Widget::Widget()
native_widget_(NULL), native_widget_(NULL),
widget_delegate_(NULL), widget_delegate_(NULL),
dragged_view_(NULL), dragged_view_(NULL),
delete_on_destroy_(false), ownership_(InitParams::NATIVE_WIDGET_OWNS_WIDGET),
is_secondary_widget_(true) { is_secondary_widget_(true) {
} }
Widget::~Widget() { Widget::~Widget() {
DestroyRootView(); DestroyRootView();
if (!delete_on_destroy_) if (ownership_ == InitParams::WIDGET_OWNS_NATIVE_WIDGET)
delete native_widget_; delete native_widget_;
} }
...@@ -95,7 +95,7 @@ bool Widget::IsPureViews() { ...@@ -95,7 +95,7 @@ bool Widget::IsPureViews() {
} }
void Widget::Init(const InitParams& params) { void Widget::Init(const InitParams& params) {
delete_on_destroy_ = params.delete_on_destroy; ownership_ = params.ownership;
native_widget_ = native_widget_ =
params.native_widget ? params.native_widget params.native_widget ? params.native_widget
: NativeWidget::CreateNativeWidget(this); : NativeWidget::CreateNativeWidget(this);
......
...@@ -56,14 +56,14 @@ class RootView; ...@@ -56,14 +56,14 @@ class RootView;
// //
// A special note on ownership: // A special note on ownership:
// //
// Depending on the value of "delete_on_destroy", the Widget either owns or // Depending on the value of the InitParams' ownership field, the Widget
// is owned by its NativeWidget: // either owns or is owned by its NativeWidget:
// //
// delete_on_destroy = true (default) // ownership = NATIVE_WIDGET_OWNS_WIDGET (default)
// The Widget instance is owned by its NativeWidget. When the NativeWidget // The Widget instance is owned by its NativeWidget. When the NativeWidget
// is destroyed (in response to a native destruction message), it deletes // is destroyed (in response to a native destruction message), it deletes
// the Widget from its destructor. // the Widget from its destructor.
// delete_on_destroy = false (non-default) // ownership = WIDGET_OWNS_NATIVE_WIDGET (non-default)
// The Widget instance owns its NativeWidget. This state implies someone // The Widget instance owns its NativeWidget. This state implies someone
// else wants to control the lifetime of this object. When they destroy // else wants to control the lifetime of this object. When they destroy
// the Widget it is responsible for destroying the NativeWidget (from its // the Widget it is responsible for destroying the NativeWidget (from its
...@@ -80,6 +80,15 @@ class Widget : public internal::NativeWidgetDelegate, ...@@ -80,6 +80,15 @@ class Widget : public internal::NativeWidgetDelegate,
TYPE_MENU // An undecorated Window, with transient properties TYPE_MENU // An undecorated Window, with transient properties
// specialized to menus. // specialized to menus.
}; };
enum Ownership {
// Default. Creator is not responsible for managing the lifetime of the
// Widget, it is destroyed when the corresponding NativeWidget is
// destroyed.
NATIVE_WIDGET_OWNS_WIDGET,
// Used when the Widget is owned by someone other than the NativeWidget,
// e.g. a scoped_ptr in tests.
WIDGET_OWNS_NATIVE_WIDGET
};
InitParams(); InitParams();
explicit InitParams(Type type); explicit InitParams(Type type);
...@@ -91,14 +100,18 @@ class Widget : public internal::NativeWidgetDelegate, ...@@ -91,14 +100,18 @@ class Widget : public internal::NativeWidgetDelegate,
bool accept_events; bool accept_events;
bool can_activate; bool can_activate;
bool keep_on_top; bool keep_on_top;
bool delete_on_destroy; Ownership ownership;
bool mirror_origin_in_rtl; bool mirror_origin_in_rtl;
bool has_dropshadow; bool has_dropshadow;
// Should the widget be double buffered? Default is false. // Should the widget be double buffered? Default is false.
bool double_buffer; bool double_buffer;
gfx::NativeView parent; gfx::NativeView parent;
Widget* parent_widget; Widget* parent_widget;
// Specifies the initial bounds of the Widget. Default is empty, which means
// the NativeWidget may specify a default size.
gfx::Rect bounds; gfx::Rect bounds;
// When set, this value is used as the Widget's NativeWidget implementation.
// The Widget will not construct a default one. Default is NULL.
NativeWidget* native_widget; NativeWidget* native_widget;
}; };
static InitParams WindowInitParams(); static InitParams WindowInitParams();
...@@ -400,7 +413,7 @@ class Widget : public internal::NativeWidgetDelegate, ...@@ -400,7 +413,7 @@ class Widget : public internal::NativeWidgetDelegate,
scoped_refptr<ui::Compositor> compositor_; scoped_refptr<ui::Compositor> compositor_;
// See class documentation for Widget above for a note about ownership. // See class documentation for Widget above for a note about ownership.
bool delete_on_destroy_; InitParams::Ownership ownership_;
// See set_is_secondary_widget(). // See set_is_secondary_widget().
bool is_secondary_widget_; bool is_secondary_widget_;
......
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