Skip to content

Commit

Permalink
Bug 1665373 - Stop using nsCountedRef in widget/gtk. r=karlt
Browse files Browse the repository at this point in the history
Instead, use RefPtrTraits.

Differential Revision: https://phabricator.services.mozilla.com/D90397
  • Loading branch information
emilio committed Sep 18, 2020
1 parent 486c097 commit 639fbe5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
32 changes: 32 additions & 0 deletions widget/gtk/GRefPtr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef GRefPtr_h_
#define GRefPtr_h_

// Allows to use RefPtr<T> with various kinds of GObjects

#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include "mozilla/RefPtr.h"

namespace mozilla {

template <typename T>
struct GObjectRefPtrTraits {
static void AddRef(T* aObject) { g_object_ref(aObject); }
static void Release(T* aObject) { g_object_unref(aObject); }
};

template <>
struct RefPtrTraits<GtkWidget> : public GObjectRefPtrTraits<GtkWidget> {};

template <>
struct RefPtrTraits<GdkDragContext>
: public GObjectRefPtrTraits<GdkDragContext> {};

} // namespace mozilla

#endif
10 changes: 2 additions & 8 deletions widget/gtk/ProcInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "mozilla/Sprintf.h"
#include "mozilla/Logging.h"
#include "mozilla/ipc/GeckoChildProcessHost.h"
#include "nsAutoRef.h"
#include "nsLocalFile.h"
#include "nsMemoryReporterManager.h"
#include "nsNetCID.h"
Expand All @@ -21,12 +20,6 @@

#define NANOPERSEC 1000000000.

template <>
class nsAutoRefTraits<DIR> : public nsPointerRefTraits<DIR> {
public:
static void Release(DIR* dirHandle) { closedir(dirHandle); }
};

namespace mozilla {

// StatReader can parse and tokenize a POSIX stat file.
Expand Down Expand Up @@ -253,14 +246,15 @@ RefPtr<ProcInfoPromise> GetProcInfo(nsTArray<ProcInfoRequest>&& aRequests) {
// Let's look at the threads
nsCString taskPath;
taskPath.AppendPrintf("/proc/%u/task", request.pid);
nsAutoRef<DIR> dirHandle(opendir(taskPath.get()));
DIR* dirHandle = opendir(taskPath.get());
if (!dirHandle) {
// For some reason, we have no data on the threads for this process.
// Most likely reason is that we have just lost a race condition and
// the process is dead.
// Let's stop here and ignore the entire process.
continue;
}
auto cleanup = mozilla::MakeScopeExit([&] { closedir(dirHandle); });

// If we can't read some thread info, we ignore that thread.
dirent* entry;
Expand Down
3 changes: 2 additions & 1 deletion widget/gtk/nsDragService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "mozilla/Services.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/PresShell.h"
#include "GRefPtr.h"

#include "gfxXlibSurface.h"
#include "gfxContext.h"
Expand Down Expand Up @@ -1897,7 +1898,7 @@ gboolean nsDragService::RunScheduledTask() {
// We still reply appropriately to indicate that the drop will or didn't
// succeeed.
mTargetWidget = mTargetWindow->GetMozContainerWidget();
mTargetDragContext.steal(mPendingDragContext);
mTargetDragContext = std::move(mPendingDragContext);
#ifdef MOZ_WAYLAND
mTargetWaylandDragContext = std::move(mPendingWaylandDragContext);
#endif
Expand Down
32 changes: 4 additions & 28 deletions widget/gtk/nsDragService.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "mozilla/RefPtr.h"
#include "nsBaseDragService.h"
#include "nsIObserver.h"
#include "nsAutoRef.h"
#include <gtk/gtk.h>

class nsWindow;
Expand All @@ -22,29 +21,6 @@ class SourceSurface;
}
} // namespace mozilla

#ifndef HAVE_NSGOBJECTREFTRAITS
# define HAVE_NSGOBJECTREFTRAITS
template <class T>
class nsGObjectRefTraits : public nsPointerRefTraits<T> {
public:
static void Release(T* aPtr) { g_object_unref(aPtr); }
static void AddRef(T* aPtr) { g_object_ref(aPtr); }
};
#endif

#ifndef HAVE_NSAUTOREFTRAITS_GTKWIDGET
# define HAVE_NSAUTOREFTRAITS_GTKWIDGET
template <>
class nsAutoRefTraits<GtkWidget> : public nsGObjectRefTraits<GtkWidget> {};
#endif

#ifndef HAVE_NSAUTOREFTRAITS_GDKDRAGCONTEXT
# define HAVE_NSAUTOREFTRAITS_GDKDRAGCONTEXT
template <>
class nsAutoRefTraits<GdkDragContext>
: public nsGObjectRefTraits<GdkDragContext> {};
#endif

/**
* Native GTK DragService wrapper
*/
Expand Down Expand Up @@ -148,7 +124,7 @@ class nsDragService final : public nsBaseDragService, public nsIObserver {
// will be nullptr if the scheduled task is eDragTaskLeave.
RefPtr<nsWindow> mPendingWindow;
mozilla::LayoutDeviceIntPoint mPendingWindowPoint;
nsCountedRef<GdkDragContext> mPendingDragContext;
RefPtr<GdkDragContext> mPendingDragContext;
#ifdef MOZ_WAYLAND
RefPtr<nsWaylandDragContext> mPendingWaylandDragContext;
#endif
Expand All @@ -161,14 +137,14 @@ class nsDragService final : public nsBaseDragService, public nsIObserver {
mozilla::LayoutDeviceIntPoint mTargetWindowPoint;
// mTargetWidget and mTargetDragContext are set only while dispatching
// motion or drop events. mTime records the corresponding timestamp.
nsCountedRef<GtkWidget> mTargetWidget;
nsCountedRef<GdkDragContext> mTargetDragContext;
RefPtr<GtkWidget> mTargetWidget;
RefPtr<GdkDragContext> mTargetDragContext;
#ifdef MOZ_WAYLAND
RefPtr<nsWaylandDragContext> mTargetWaylandDragContext;
#endif
// mTargetDragContextForRemote is set while waiting for a reply from
// a child process.
nsCountedRef<GdkDragContext> mTargetDragContextForRemote;
RefPtr<GdkDragContext> mTargetDragContextForRemote;
#ifdef MOZ_WAYLAND
RefPtr<nsWaylandDragContext> mTargetWaylandDragContextForRemote;
#endif
Expand Down

0 comments on commit 639fbe5

Please sign in to comment.