From 9d350e8367dbce79d96f9dd8f22639f12b07a75c Mon Sep 17 00:00:00 2001 From: "estade@chromium.org" Date: Tue, 3 Aug 2010 21:02:16 +0000 Subject: Move owned_widget_gtk.* out of common/, into browser/gtk/. common/ is for files shared between the renderer and browser processes, and OwnedWidgetGtk is not used outside of browser/. BUG=none TEST=compile Review URL: http://codereview.chromium.org/3063023 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@54817 0039d316-1c4b-4281-b951-d872f2087c98 --- .../autocomplete/autocomplete_edit_view_gtk.h | 2 +- chrome/browser/gtk/bookmark_bar_gtk.h | 2 +- chrome/browser/gtk/bookmark_menu_controller_gtk.h | 2 +- chrome/browser/gtk/browser_actions_toolbar_gtk.h | 2 +- chrome/browser/gtk/browser_toolbar_gtk.h | 2 +- chrome/browser/gtk/constrained_window_gtk.h | 2 +- chrome/browser/gtk/custom_button.h | 2 +- chrome/browser/gtk/download_item_gtk.h | 2 +- chrome/browser/gtk/download_shelf_gtk.h | 2 +- chrome/browser/gtk/find_bar_gtk.h | 2 +- chrome/browser/gtk/gtk_theme_provider.h | 2 +- chrome/browser/gtk/infobar_container_gtk.h | 2 +- chrome/browser/gtk/infobar_gtk.h | 2 +- chrome/browser/gtk/location_bar_view_gtk.h | 2 +- chrome/browser/gtk/overflow_button.h | 2 +- chrome/browser/gtk/owned_widget_gtk.cc | 38 +++++++++ chrome/browser/gtk/owned_widget_gtk.h | 90 ++++++++++++++++++++++ chrome/browser/gtk/reload_button_gtk.h | 2 +- chrome/browser/gtk/sad_tab_gtk.h | 2 +- chrome/browser/gtk/slide_animator_gtk.h | 2 +- chrome/browser/gtk/status_bubble_gtk.h | 2 +- chrome/browser/gtk/tab_contents_container_gtk.h | 2 +- chrome/browser/gtk/tabs/tab_renderer_gtk.h | 2 +- chrome/browser/gtk/tabs/tab_strip_gtk.h | 2 +- .../renderer_host/gtk_key_bindings_handler.h | 2 +- .../renderer_host/render_widget_host_view_gtk.h | 2 +- .../browser/tab_contents/tab_contents_view_gtk.h | 2 +- chrome/common/owned_widget_gtk.cc | 38 --------- chrome/common/owned_widget_gtk.h | 90 ---------------------- 29 files changed, 153 insertions(+), 153 deletions(-) create mode 100644 chrome/browser/gtk/owned_widget_gtk.cc create mode 100644 chrome/browser/gtk/owned_widget_gtk.h delete mode 100644 chrome/common/owned_widget_gtk.cc delete mode 100644 chrome/common/owned_widget_gtk.h diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_gtk.h b/chrome/browser/autocomplete/autocomplete_edit_view_gtk.h index 95ebd57..80056bc 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_gtk.h +++ b/chrome/browser/autocomplete/autocomplete_edit_view_gtk.h @@ -16,10 +16,10 @@ #include "base/scoped_ptr.h" #include "base/string_util.h" #include "chrome/browser/autocomplete/autocomplete_edit_view.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/browser/toolbar_model.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" #include "chrome/common/page_transition_types.h" #include "gfx/rect.h" #include "webkit/glue/window_open_disposition.h" diff --git a/chrome/browser/gtk/bookmark_bar_gtk.h b/chrome/browser/gtk/bookmark_bar_gtk.h index f159c98..caef663 100644 --- a/chrome/browser/gtk/bookmark_bar_gtk.h +++ b/chrome/browser/gtk/bookmark_bar_gtk.h @@ -19,11 +19,11 @@ #include "chrome/browser/bookmarks/bookmark_model_observer.h" #include "chrome/browser/gtk/bookmark_bar_instructions_gtk.h" #include "chrome/browser/gtk/menu_bar_helper.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/browser/gtk/view_id_util.h" #include "chrome/browser/sync/profile_sync_service.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" #include "gfx/point.h" #include "gfx/size.h" diff --git a/chrome/browser/gtk/bookmark_menu_controller_gtk.h b/chrome/browser/gtk/bookmark_menu_controller_gtk.h index fbf9335..21f2bb9 100644 --- a/chrome/browser/gtk/bookmark_menu_controller_gtk.h +++ b/chrome/browser/gtk/bookmark_menu_controller_gtk.h @@ -13,7 +13,7 @@ #include "base/scoped_ptr.h" #include "chrome/browser/bookmarks/base_bookmark_model_observer.h" #include "chrome/browser/bookmarks/bookmark_context_menu_controller.h" -#include "chrome/common/owned_widget_gtk.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "webkit/glue/window_open_disposition.h" class Browser; diff --git a/chrome/browser/gtk/browser_actions_toolbar_gtk.h b/chrome/browser/gtk/browser_actions_toolbar_gtk.h index 045214d..9d2e44d 100644 --- a/chrome/browser/gtk/browser_actions_toolbar_gtk.h +++ b/chrome/browser/gtk/browser_actions_toolbar_gtk.h @@ -18,9 +18,9 @@ #include "chrome/browser/extensions/extension_toolbar_model.h" #include "chrome/browser/gtk/menu_gtk.h" #include "chrome/browser/gtk/overflow_button.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" class Browser; class BrowserActionButton; diff --git a/chrome/browser/gtk/browser_toolbar_gtk.h b/chrome/browser/gtk/browser_toolbar_gtk.h index 30b4de0..29dd47b 100644 --- a/chrome/browser/gtk/browser_toolbar_gtk.h +++ b/chrome/browser/gtk/browser_toolbar_gtk.h @@ -18,11 +18,11 @@ #include "chrome/browser/command_updater.h" #include "chrome/browser/gtk/custom_button.h" #include "chrome/browser/gtk/menu_gtk.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/browser/pref_member.h" #include "chrome/browser/wrench_menu_model.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" class BackForwardButtonGtk; class Browser; diff --git a/chrome/browser/gtk/constrained_window_gtk.h b/chrome/browser/gtk/constrained_window_gtk.h index 9fcc404..2312cb3 100644 --- a/chrome/browser/gtk/constrained_window_gtk.h +++ b/chrome/browser/gtk/constrained_window_gtk.h @@ -10,8 +10,8 @@ #include "app/gtk_signal.h" #include "base/basictypes.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/browser/tab_contents/constrained_window.h" -#include "chrome/common/owned_widget_gtk.h" class TabContents; class TabContentsViewGtk; diff --git a/chrome/browser/gtk/custom_button.h b/chrome/browser/gtk/custom_button.h index 0142e18..5cb38cb 100644 --- a/chrome/browser/gtk/custom_button.h +++ b/chrome/browser/gtk/custom_button.h @@ -12,9 +12,9 @@ #include "app/slide_animation.h" #include "base/scoped_ptr.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" #include "gfx/rect.h" #include "third_party/skia/include/core/SkColor.h" diff --git a/chrome/browser/gtk/download_item_gtk.h b/chrome/browser/gtk/download_item_gtk.h index 9df51d5..5e4d64e 100644 --- a/chrome/browser/gtk/download_item_gtk.h +++ b/chrome/browser/gtk/download_item_gtk.h @@ -16,9 +16,9 @@ #include "base/time.h" #include "chrome/browser/download/download_item.h" #include "chrome/browser/icon_manager.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" class BaseDownloadItemModel; class DownloadShelfContextMenuGtk; diff --git a/chrome/browser/gtk/download_shelf_gtk.h b/chrome/browser/gtk/download_shelf_gtk.h index cd3301e..bd8ff0f 100644 --- a/chrome/browser/gtk/download_shelf_gtk.h +++ b/chrome/browser/gtk/download_shelf_gtk.h @@ -13,10 +13,10 @@ #include "app/gtk_signal.h" #include "base/scoped_ptr.h" #include "chrome/browser/download/download_shelf.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/browser/gtk/slide_animator_gtk.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" #include "gfx/native_widget_types.h" class BaseDownloadItemModel; diff --git a/chrome/browser/gtk/find_bar_gtk.h b/chrome/browser/gtk/find_bar_gtk.h index 573e0ba..5e5873e 100644 --- a/chrome/browser/gtk/find_bar_gtk.h +++ b/chrome/browser/gtk/find_bar_gtk.h @@ -12,10 +12,10 @@ #include "base/scoped_ptr.h" #include "chrome/browser/find_bar.h" #include "chrome/browser/gtk/focus_store_gtk.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/browser/gtk/slide_animator_gtk.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" #include "gfx/point.h" class Browser; diff --git a/chrome/browser/gtk/gtk_theme_provider.h b/chrome/browser/gtk/gtk_theme_provider.h index 323592a..9bbd559c 100644 --- a/chrome/browser/gtk/gtk_theme_provider.h +++ b/chrome/browser/gtk/gtk_theme_provider.h @@ -14,8 +14,8 @@ #include "app/gtk_signal.h" #include "base/scoped_ptr.h" #include "chrome/browser/browser_theme_provider.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/common/notification_observer.h" -#include "chrome/common/owned_widget_gtk.h" #include "gfx/color_utils.h" class CairoCachedSurface; diff --git a/chrome/browser/gtk/infobar_container_gtk.h b/chrome/browser/gtk/infobar_container_gtk.h index 55b6773..76b0251 100644 --- a/chrome/browser/gtk/infobar_container_gtk.h +++ b/chrome/browser/gtk/infobar_container_gtk.h @@ -7,8 +7,8 @@ #pragma once #include "base/basictypes.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" class InfoBarDelegate; class Profile; diff --git a/chrome/browser/gtk/infobar_gtk.h b/chrome/browser/gtk/infobar_gtk.h index 61e602c..ce1762a 100644 --- a/chrome/browser/gtk/infobar_gtk.h +++ b/chrome/browser/gtk/infobar_gtk.h @@ -9,11 +9,11 @@ #include "app/gtk_signal.h" #include "base/basictypes.h" #include "base/scoped_ptr.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/browser/gtk/slide_animator_gtk.h" #include "chrome/browser/tab_contents/infobar_delegate.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" class CustomDrawButton; class GtkThemeProvider; diff --git a/chrome/browser/gtk/location_bar_view_gtk.h b/chrome/browser/gtk/location_bar_view_gtk.h index bb2052b..87714b3 100644 --- a/chrome/browser/gtk/location_bar_view_gtk.h +++ b/chrome/browser/gtk/location_bar_view_gtk.h @@ -22,11 +22,11 @@ #include "chrome/browser/first_run/first_run.h" #include "chrome/browser/gtk/info_bubble_gtk.h" #include "chrome/browser/gtk/menu_gtk.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/browser/location_bar.h" #include "chrome/common/content_settings_types.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" #include "chrome/common/page_transition_types.h" #include "third_party/skia/include/core/SkBitmap.h" #include "webkit/glue/window_open_disposition.h" diff --git a/chrome/browser/gtk/overflow_button.h b/chrome/browser/gtk/overflow_button.h index f499c48..647221e 100644 --- a/chrome/browser/gtk/overflow_button.h +++ b/chrome/browser/gtk/overflow_button.h @@ -6,9 +6,9 @@ #define CHROME_BROWSER_GTK_OVERFLOW_BUTTON_H_ #pragma once +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" typedef struct _GtkWidget GtkWidget; class Profile; diff --git a/chrome/browser/gtk/owned_widget_gtk.cc b/chrome/browser/gtk/owned_widget_gtk.cc new file mode 100644 index 0000000..db3bd30 --- /dev/null +++ b/chrome/browser/gtk/owned_widget_gtk.cc @@ -0,0 +1,38 @@ +// Copyright (c) 2010 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. + +#include "chrome/browser/gtk/owned_widget_gtk.h" + +#include + +#include "base/logging.h" + +OwnedWidgetGtk::~OwnedWidgetGtk() { + DCHECK(!widget_) << "You must explicitly call OwnerWidgetGtk::Destroy()."; +} + +void OwnedWidgetGtk::Own(GtkWidget* widget) { + DCHECK(!widget_); + // We want to make sure that Own() was called properly, right after the + // widget was created. There should be a floating reference. + DCHECK(g_object_is_floating(widget)); + + // Sink the floating reference, we should now own this reference. + g_object_ref_sink(widget); + widget_ = widget; +} + +void OwnedWidgetGtk::Destroy() { + if (!widget_) + return; + + GtkWidget* widget = widget_; + widget_ = NULL; + gtk_widget_destroy(widget); + + DCHECK(!g_object_is_floating(widget)); + // NOTE: Assumes some implementation details about glib internals. + DCHECK_EQ(G_OBJECT(widget)->ref_count, 1U); + g_object_unref(widget); +} diff --git a/chrome/browser/gtk/owned_widget_gtk.h b/chrome/browser/gtk/owned_widget_gtk.h new file mode 100644 index 0000000..b5299c9 --- /dev/null +++ b/chrome/browser/gtk/owned_widget_gtk.h @@ -0,0 +1,90 @@ +// Copyright (c) 2009 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. + +// This class assists you in dealing with a specific situation when managing +// ownership between a C++ object and a GTK widget. It is common to have a +// C++ object which encapsulates a GtkWidget, and that widget is exposed from +// the object for use outside of the class. In this situation, you commonly +// want the GtkWidget's lifetime to match its C++ object's lifetime. Using an +// OwnedWigetGtk will take ownership over the initial reference of the +// GtkWidget, so that it is "owned" by the C++ object. Example usage: +// +// class FooViewGtk() { +// public: +// FooViewGtk() { } +// ~FooViewGtk() { widget_.Destroy(); } +// void Init() { vbox_.Own(gtk_vbox_new()); } +// GtkWidget* widget() { return vbox_.get() }; // Host my widget! +// private: +// OwnedWidgetGtk vbox_; +// }; +// +// This design will ensure that the widget stays alive from the call to Own() +// until the call to Destroy(). +// +// - Details of the problem and OwnedWidgetGtk's solution: +// In order to make passing ownership more convenient for newly created +// widgets, GTK has a concept of a "floating" reference. All GtkObjects (and +// thus GtkWidgets) inherit from GInitiallyUnowned. When they are created, the +// object starts with a reference count of 1, but has its floating flag set. +// When it is put into a container for the first time, that container will +// "sink" the floating reference, and the count will still be 1. Now the +// container owns the widget, and if we remove the widget from the container, +// the widget is destroyed. This style of ownership often causes problems when +// you have an object encapsulating the widget. If we just use a raw +// GtkObject* with no specific ownership management, we push the widget's +// ownership onto the user of the class. Now the C++ object can't depend on +// the widget being valid, since it doesn't manage its lifetime. If the widget +// was removed from a container, removing its only reference, it would be +// destroyed (from the C++ object's perspective) unexpectantly destroyed. The +// solution is fairly simple, make sure that the C++ object owns the widget, +// and thus it is also responsible for destroying it. This boils down to: +// GtkWidget* widget = gtk_widget_new(); +// g_object_ref_sink(widget); // Claim the initial floating reference. +// ... +// gtk_destroy_widget(widget); // Ask all code to destroy their references. +// g_object_unref(widget); // Destroy the initial reference we had claimed. + +#ifndef CHROME_BROWSER_GTK_OWNED_WIDGET_GTK_H_ +#define CHROME_BROWSER_GTK_OWNED_WIDGET_GTK_H_ +#pragma once + +#include "base/basictypes.h" + +typedef struct _GtkWidget GtkWidget; + +class OwnedWidgetGtk { + public: + // Create an instance that isn't managing any ownership. + OwnedWidgetGtk() : widget_(NULL) { } + // Create an instance that owns |widget|. + explicit OwnedWidgetGtk(GtkWidget* widget) : widget_(NULL) { Own(widget); } + + ~OwnedWidgetGtk(); + + // Return the currently owned widget, or NULL if no widget is owned. + GtkWidget* get() const { return widget_; } + GtkWidget* operator->() const { return widget_; } + + // Takes ownership of a widget, by taking the initial floating reference of + // the GtkWidget. It is expected that Own() is called right after the widget + // has been created, and before any other references to the widget might have + // been added. It is valid to never call Own(), in which case Destroy() will + // do nothing. If Own() has been called, you must explicitly call Destroy(). + void Own(GtkWidget* widget); + + // You must call Destroy() after you have called Own(). Calling Destroy() + // will call gtk_widget_destroy(), and drop our reference to the widget. + // After a call to Destroy(), you may call Own() again. NOTE: It is expected + // that after gtk_widget_destroy we will be holding the only reference left + // on the object. We assert this in debug mode to help catch any leaks. + void Destroy(); + + private: + GtkWidget* widget_; + + DISALLOW_COPY_AND_ASSIGN(OwnedWidgetGtk); +}; + +#endif // CHROME_BROWSER_GTK_OWNED_WIDGET_GTK_H_ diff --git a/chrome/browser/gtk/reload_button_gtk.h b/chrome/browser/gtk/reload_button_gtk.h index beee958..2de1c4c 100644 --- a/chrome/browser/gtk/reload_button_gtk.h +++ b/chrome/browser/gtk/reload_button_gtk.h @@ -12,9 +12,9 @@ #include "base/basictypes.h" #include "base/timer.h" #include "chrome/browser/gtk/custom_button.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" class Browser; class GtkThemeProvider; diff --git a/chrome/browser/gtk/sad_tab_gtk.h b/chrome/browser/gtk/sad_tab_gtk.h index 6d7b9d5..9c5489b 100644 --- a/chrome/browser/gtk/sad_tab_gtk.h +++ b/chrome/browser/gtk/sad_tab_gtk.h @@ -9,7 +9,7 @@ typedef struct _GtkWidget GtkWidget; #include "app/gtk_signal.h" -#include "chrome/common/owned_widget_gtk.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" class TabContents; diff --git a/chrome/browser/gtk/slide_animator_gtk.h b/chrome/browser/gtk/slide_animator_gtk.h index 1de91af..df6d29b 100644 --- a/chrome/browser/gtk/slide_animator_gtk.h +++ b/chrome/browser/gtk/slide_animator_gtk.h @@ -19,7 +19,7 @@ #include "app/animation.h" #include "base/scoped_ptr.h" -#include "chrome/common/owned_widget_gtk.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" class SlideAnimation; diff --git a/chrome/browser/gtk/status_bubble_gtk.h b/chrome/browser/gtk/status_bubble_gtk.h index 4a8748f..18942ec 100644 --- a/chrome/browser/gtk/status_bubble_gtk.h +++ b/chrome/browser/gtk/status_bubble_gtk.h @@ -14,10 +14,10 @@ #include "app/slide_animation.h" #include "base/scoped_ptr.h" #include "base/timer.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/browser/status_bubble.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" #include "gfx/point.h" #include "googleurl/src/gurl.h" diff --git a/chrome/browser/gtk/tab_contents_container_gtk.h b/chrome/browser/gtk/tab_contents_container_gtk.h index b72844a..8133620 100644 --- a/chrome/browser/gtk/tab_contents_container_gtk.h +++ b/chrome/browser/gtk/tab_contents_container_gtk.h @@ -9,9 +9,9 @@ #include #include "base/basictypes.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/browser/gtk/view_id_util.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" class RenderViewHost; class StatusBubbleGtk; diff --git a/chrome/browser/gtk/tabs/tab_renderer_gtk.h b/chrome/browser/gtk/tabs/tab_renderer_gtk.h index c733dd5..0a03a8a6 100644 --- a/chrome/browser/gtk/tabs/tab_renderer_gtk.h +++ b/chrome/browser/gtk/tabs/tab_renderer_gtk.h @@ -14,9 +14,9 @@ #include "app/slide_animation.h" #include "base/basictypes.h" #include "base/string16.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" #include "gfx/canvas.h" #include "gfx/font.h" #include "gfx/rect.h" diff --git a/chrome/browser/gtk/tabs/tab_strip_gtk.h b/chrome/browser/gtk/tabs/tab_strip_gtk.h index c47c894..e812ba1 100644 --- a/chrome/browser/gtk/tabs/tab_strip_gtk.h +++ b/chrome/browser/gtk/tabs/tab_strip_gtk.h @@ -13,12 +13,12 @@ #include "base/basictypes.h" #include "base/task.h" #include "base/message_loop.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/browser/gtk/tabstrip_origin_provider.h" #include "chrome/browser/gtk/tabs/tab_gtk.h" #include "chrome/browser/gtk/view_id_util.h" #include "chrome/browser/tabs/tab_strip_model.h" #include "chrome/common/notification_observer.h" -#include "chrome/common/owned_widget_gtk.h" #include "gfx/rect.h" class BrowserWindowGtk; diff --git a/chrome/browser/renderer_host/gtk_key_bindings_handler.h b/chrome/browser/renderer_host/gtk_key_bindings_handler.h index 737a63d..4a58a4c 100644 --- a/chrome/browser/renderer_host/gtk_key_bindings_handler.h +++ b/chrome/browser/renderer_host/gtk_key_bindings_handler.h @@ -10,8 +10,8 @@ #include +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/common/edit_command.h" -#include "chrome/common/owned_widget_gtk.h" struct NativeWebKeyboardEvent; diff --git a/chrome/browser/renderer_host/render_widget_host_view_gtk.h b/chrome/browser/renderer_host/render_widget_host_view_gtk.h index 14d8092..14c992b 100644 --- a/chrome/browser/renderer_host/render_widget_host_view_gtk.h +++ b/chrome/browser/renderer_host/render_widget_host_view_gtk.h @@ -14,8 +14,8 @@ #include "base/scoped_ptr.h" #include "base/time.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/browser/renderer_host/render_widget_host_view.h" -#include "chrome/common/owned_widget_gtk.h" #include "gfx/native_widget_types.h" #include "webkit/glue/plugins/gtk_plugin_container_manager.h" #include "webkit/glue/webcursor.h" diff --git a/chrome/browser/tab_contents/tab_contents_view_gtk.h b/chrome/browser/tab_contents/tab_contents_view_gtk.h index 4ce2078..586b399 100644 --- a/chrome/browser/tab_contents/tab_contents_view_gtk.h +++ b/chrome/browser/tab_contents/tab_contents_view_gtk.h @@ -11,10 +11,10 @@ #include "app/gtk_signal.h" #include "base/scoped_ptr.h" #include "chrome/browser/gtk/focus_store_gtk.h" +#include "chrome/browser/gtk/owned_widget_gtk.h" #include "chrome/browser/tab_contents/tab_contents_view.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" -#include "chrome/common/owned_widget_gtk.h" class ConstrainedWindowGtk; class GtkThemeProperties; diff --git a/chrome/common/owned_widget_gtk.cc b/chrome/common/owned_widget_gtk.cc deleted file mode 100644 index f9b8bef..0000000 --- a/chrome/common/owned_widget_gtk.cc +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2009 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. - -#include "chrome/common/owned_widget_gtk.h" - -#include - -#include "base/logging.h" - -OwnedWidgetGtk::~OwnedWidgetGtk() { - DCHECK(!widget_) << "You must explicitly call OwnerWidgetGtk::Destroy()."; -} - -void OwnedWidgetGtk::Own(GtkWidget* widget) { - DCHECK(!widget_); - // We want to make sure that Own() was called properly, right after the - // widget was created. There should be a floating reference. - DCHECK(g_object_is_floating(widget)); - - // Sink the floating reference, we should now own this reference. - g_object_ref_sink(widget); - widget_ = widget; -} - -void OwnedWidgetGtk::Destroy() { - if (!widget_) - return; - - GtkWidget* widget = widget_; - widget_ = NULL; - gtk_widget_destroy(widget); - - DCHECK(!g_object_is_floating(widget)); - // NOTE: Assumes some implementation details about glib internals. - DCHECK_EQ(G_OBJECT(widget)->ref_count, 1U); - g_object_unref(widget); -} diff --git a/chrome/common/owned_widget_gtk.h b/chrome/common/owned_widget_gtk.h deleted file mode 100644 index 75d87f7..0000000 --- a/chrome/common/owned_widget_gtk.h +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright (c) 2009 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. - -// This class assists you in dealing with a specific situation when managing -// ownership between a C++ object and a GTK widget. It is common to have a -// C++ object which encapsulates a GtkWidget, and that widget is exposed from -// the object for use outside of the class. In this situation, you commonly -// want the GtkWidget's lifetime to match its C++ object's lifetime. Using an -// OwnedWigetGtk will take ownership over the initial reference of the -// GtkWidget, so that it is "owned" by the C++ object. Example usage: -// -// class FooViewGtk() { -// public: -// FooViewGtk() { } -// ~FooViewGtk() { widget_.Destroy(); } -// void Init() { vbox_.Own(gtk_vbox_new()); } -// GtkWidget* widget() { return vbox_.get() }; // Host my widget! -// private: -// OwnedWidgetGtk vbox_; -// }; -// -// This design will ensure that the widget stays alive from the call to Own() -// until the call to Destroy(). -// -// - Details of the problem and OwnedWidgetGtk's solution: -// In order to make passing ownership more convenient for newly created -// widgets, GTK has a concept of a "floating" reference. All GtkObjects (and -// thus GtkWidgets) inherit from GInitiallyUnowned. When they are created, the -// object starts with a reference count of 1, but has its floating flag set. -// When it is put into a container for the first time, that container will -// "sink" the floating reference, and the count will still be 1. Now the -// container owns the widget, and if we remove the widget from the container, -// the widget is destroyed. This style of ownership often causes problems when -// you have an object encapsulating the widget. If we just use a raw -// GtkObject* with no specific ownership management, we push the widget's -// ownership onto the user of the class. Now the C++ object can't depend on -// the widget being valid, since it doesn't manage its lifetime. If the widget -// was removed from a container, removing its only reference, it would be -// destroyed (from the C++ object's perspective) unexpectantly destroyed. The -// solution is fairly simple, make sure that the C++ object owns the widget, -// and thus it is also responsible for destroying it. This boils down to: -// GtkWidget* widget = gtk_widget_new(); -// g_object_ref_sink(widget); // Claim the initial floating reference. -// ... -// gtk_destroy_widget(widget); // Ask all code to destroy their references. -// g_object_unref(widget); // Destroy the initial reference we had claimed. - -#ifndef CHROME_COMMON_OWNED_WIDGET_GTK_H_ -#define CHROME_COMMON_OWNED_WIDGET_GTK_H_ -#pragma once - -#include "base/basictypes.h" - -typedef struct _GtkWidget GtkWidget; - -class OwnedWidgetGtk { - public: - // Create an instance that isn't managing any ownership. - OwnedWidgetGtk() : widget_(NULL) { } - // Create an instance that owns |widget|. - explicit OwnedWidgetGtk(GtkWidget* widget) : widget_(NULL) { Own(widget); } - - ~OwnedWidgetGtk(); - - // Return the currently owned widget, or NULL if no widget is owned. - GtkWidget* get() const { return widget_; } - GtkWidget* operator->() const { return widget_; } - - // Takes ownership of a widget, by taking the initial floating reference of - // the GtkWidget. It is expected that Own() is called right after the widget - // has been created, and before any other references to the widget might have - // been added. It is valid to never call Own(), in which case Destroy() will - // do nothing. If Own() has been called, you must explicitly call Destroy(). - void Own(GtkWidget* widget); - - // You must call Destroy() after you have called Own(). Calling Destroy() - // will call gtk_widget_destroy(), and drop our reference to the widget. - // After a call to Destroy(), you may call Own() again. NOTE: It is expected - // that after gtk_widget_destroy we will be holding the only reference left - // on the object. We assert this in debug mode to help catch any leaks. - void Destroy(); - - private: - GtkWidget* widget_; - - DISALLOW_COPY_AND_ASSIGN(OwnedWidgetGtk); -}; - -#endif // CHROME_COMMON_OWNED_WIDGET_GTK_H_ -- cgit v1.1