Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Unified Diff: ui/base/glib/glib_util.h

Issue 2803873003: Linux native notifications: Support closing and updating notifications (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/base/glib/glib_util.h
diff --git a/ui/base/glib/glib_util.h b/ui/base/glib/glib_util.h
new file mode 100644
index 0000000000000000000000000000000000000000..3bfc93dc2f4f19afef51b88ec00bf3e5fdaa318a
--- /dev/null
+++ b/ui/base/glib/glib_util.h
@@ -0,0 +1,54 @@
+// Copyright 2017 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 UI_BASE_GLIB_GLIB_UTIL_H_
Lei Zhang 2017/04/06 03:59:54 How about scoped_gobject.h?
Tom (Use chromium acct) 2017/04/06 05:05:35 Done.
+#define UI_BASE_GLIB_GLIB_UTIL_H_
+
+#include "base/logging.h"
+
+// Similar in spirit to a std::unique_ptr.
+template <typename T>
+class ScopedGObject {
+ public:
+ ScopedGObject() : obj_(nullptr) {}
+
+ explicit ScopedGObject(T* obj) : obj_(obj) {
+ // Remove the floating reference from |obj_| if it has one.
+ if (g_object_is_floating(obj_))
+ g_object_ref_sink(obj_);
+ DCHECK(G_OBJECT(obj_)->ref_count == 1);
+ }
+
+ ScopedGObject(const ScopedGObject<T>& other) = delete;
+
+ ScopedGObject(ScopedGObject<T>&& other) : obj_(other.obj_) {
+ other.obj_ = nullptr;
+ }
+
+ ~ScopedGObject() { reset(nullptr); }
+
+ ScopedGObject<T>& operator=(const ScopedGObject<T>& other) = delete;
+
+ ScopedGObject<T>& operator=(ScopedGObject<T>&& other) {
+ g_object_unref(obj_);
+ obj_ = other.obj_;
+ other.obj_ = nullptr;
+ return *this;
+ }
+
+ operator T*() { return obj_; }
+
+ void reset(T* obj) {
+ if (obj_)
+ Unref();
+ obj_ = obj;
+ }
+
+ private:
+ void Unref() { g_object_unref(obj_); }
+
+ T* obj_;
+};
+
+#endif // UI_BASE_GLIB_GLIB_UTIL_H_
« chrome/browser/ui/libgtkui/gtk_util.h ('K') | « chrome/browser/ui/libgtkui/gtk_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698