OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/libgtk2ui/owned_widget_gtk2.h" | 5 #include "chrome/browser/ui/libgtk2ui/owned_widget_gtk2.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
11 namespace libgtk2ui { | 11 namespace libgtk2ui { |
12 | 12 |
13 OwnedWidgetGtk::~OwnedWidgetGtk() { | 13 OwnedWidgetGtk::~OwnedWidgetGtk() { |
14 Destroy(); | 14 Destroy(); |
15 } | 15 } |
16 | 16 |
17 void OwnedWidgetGtk::Own(GtkWidget* widget) { | 17 void OwnedWidgetGtk::Own(GtkWidget* widget) { |
18 if (!widget) | 18 if (!widget) |
19 return; | 19 return; |
20 | 20 |
21 DCHECK(!widget_); | 21 DCHECK(!widget_); |
22 // We want to make sure that Own() was called properly, right after the | |
23 // widget was created. There should be a floating reference. | |
24 DCHECK(g_object_is_floating(widget)); | |
25 | 22 |
26 // Sink the floating reference, we should now own this reference. | 23 // Keep a reference |
27 g_object_ref_sink(widget); | 24 g_object_ref_sink(widget); |
28 widget_ = widget; | 25 widget_ = widget; |
29 } | 26 } |
30 | 27 |
31 void OwnedWidgetGtk::Destroy() { | 28 void OwnedWidgetGtk::Destroy() { |
32 if (!widget_) | 29 if (!widget_) |
33 return; | 30 return; |
34 | 31 |
35 GtkWidget* widget = widget_; | 32 GtkWidget* widget = widget_; |
36 widget_ = NULL; | 33 widget_ = NULL; |
37 gtk_widget_destroy(widget); | 34 gtk_widget_destroy(widget); |
38 | 35 |
39 DCHECK(!g_object_is_floating(widget)); | 36 DCHECK(!g_object_is_floating(widget)); |
40 // NOTE: Assumes some implementation details about glib internals. | 37 // NOTE: Assumes some implementation details about glib internals. |
41 DCHECK_EQ(G_OBJECT(widget)->ref_count, 1U); | 38 DCHECK_EQ(G_OBJECT(widget)->ref_count, 1U); |
42 g_object_unref(widget); | 39 g_object_unref(widget); |
43 } | 40 } |
44 | 41 |
45 } // namespace libgtk2ui | 42 } // namespace libgtk2ui |
OLD | NEW |