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