Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/layers/APZThreadUtils.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_layers_APZThreadUtils_h
8
#define mozilla_layers_APZThreadUtils_h
9
10
#include "base/message_loop.h"
11
#include "nsINamed.h"
12
#include "nsITimer.h"
13
14
namespace mozilla {
15
16
class Runnable;
17
18
namespace layers {
19
20
class APZThreadUtils
21
{
22
public:
23
  /**
24
   * In the gtest environment everything runs on one thread, so we
25
   * shouldn't assert that we're on a particular thread. This enables
26
   * that behaviour.
27
   */
28
  static void SetThreadAssertionsEnabled(bool aEnabled);
29
  static bool GetThreadAssertionsEnabled();
30
31
  /**
32
   * Set the controller thread.
33
   */
34
  static void SetControllerThread(MessageLoop* aLoop);
35
36
  /**
37
   * This can be used to assert that the current thread is the
38
   * controller/UI thread (on which input events are received).
39
   * This does nothing if thread assertions are disabled.
40
   */
41
  static void AssertOnControllerThread();
42
43
  /**
44
   * Run the given task on the APZ "controller thread" for this platform. If
45
   * this function is called from the controller thread itself then the task is
46
   * run immediately without getting queued.
47
   */
48
  static void RunOnControllerThread(already_AddRefed<Runnable> aTask);
49
50
  /**
51
   * Returns true if currently on APZ "controller thread".
52
   */
53
  static bool IsControllerThread();
54
};
55
56
// A base class for GenericNamedTimerCallback<Function>.
57
// This is necessary because NS_IMPL_ISUPPORTS doesn't work for a class
58
// template.
59
class GenericNamedTimerCallbackBase : public nsITimerCallback,
60
                                      public nsINamed
61
{
62
public:
63
  NS_DECL_THREADSAFE_ISUPPORTS
64
65
protected:
66
0
  virtual ~GenericNamedTimerCallbackBase() {}
67
};
68
69
// An nsITimerCallback implementation with nsINamed that can be used with any
70
// function object that's callable with no arguments.
71
template <typename Function>
72
class GenericNamedTimerCallback final : public GenericNamedTimerCallbackBase
73
{
74
public:
75
  explicit GenericNamedTimerCallback(const Function& aFunction,
76
                                     const char* aName)
77
    : mFunction(aFunction)
78
    , mName(aName)
79
  {
80
  }
81
82
  NS_IMETHOD Notify(nsITimer*) override
83
  {
84
    mFunction();
85
    return NS_OK;
86
  }
87
88
  NS_IMETHOD GetName(nsACString& aName) override
89
  {
90
    aName = mName;
91
    return NS_OK;
92
  }
93
94
private:
95
  Function mFunction;
96
  nsCString mName;
97
};
98
99
// Convenience function for constructing a GenericNamedTimerCallback.
100
// Returns a raw pointer, suitable for passing directly as an argument to
101
// nsITimer::InitWithCallback(). The intention is to enable the following
102
// terse inline usage:
103
//    timer->InitWithCallback(NewNamedTimerCallback([](){ ... }, name), delay);
104
template <typename Function>
105
GenericNamedTimerCallback<Function>*
106
  NewNamedTimerCallback(const Function& aFunction,
107
                        const char* aName)
108
{
109
  return new GenericNamedTimerCallback<Function>(aFunction, aName);
110
}
111
112
} // namespace layers
113
} // namespace mozilla
114
115
#endif /* mozilla_layers_APZThreadUtils_h */