summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/gtk/g_object_weak_ref.h
blob: 0010bca8f8c7a8aeefbc00c24df7ccb5a8674b0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) 2011 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.

#ifndef CHROME_BROWSER_UI_GTK_G_OBJECT_WEAK_REF_H_
#define CHROME_BROWSER_UI_GTK_G_OBJECT_WEAK_REF_H_
#pragma once

#include <gtk/gtk.h>

#include "base/memory/ref_counted.h"

// GObjectWeakRef is like base::WeakRef, but for GLib objects. It can be useful
// when passing GObject/GtkWidget arguments to closures (e.g. base::Bind()) that
// may not necessarily outlast the closure. It is not thread-safe. Use like so:
//
// void DelayedAction(const GObjectWeakRef& object) {
//   if (!object.get()) return;
//   GtkWidget* widget = GTK_WIDGET(object.get());
//   // code using |widget|
// }
//
// GtkWidget* widget = ...;
// MessageLoop::current()->PostTask(
//     FROM_HERE, base::Bind(&DelayedAction, GObjectWeakRef(widget)));

class GObjectWeakRef {
 public:
  // GObjectWeakRef is just a thin wrapper around GObjectWeakRefDelegate that
  // can be copied and assigned, even though the delegate is reference counted.
  explicit GObjectWeakRef(GObject* object);
  explicit GObjectWeakRef(GtkWidget* widget);
  GObjectWeakRef(const GObjectWeakRef& ref);  // NOLINT: explicitly non-explicit
  ~GObjectWeakRef();

  // Assignment operators for GObject, GtkWidget, and GObjectWeakRef.
  GObjectWeakRef& operator=(GObject* object);
  GObjectWeakRef& operator=(GtkWidget* widget);
  GObjectWeakRef& operator=(const GObjectWeakRef& ref);

  GObject* get() const;

 private:
  class GObjectWeakRefDelegate;

  void reset(GObject* object);

  scoped_refptr<GObjectWeakRefDelegate> ref_;
};

#endif  // CHROME_BROWSER_UI_GTK_G_OBJECT_WEAK_REF_H_