summaryrefslogtreecommitdiffstats
path: root/remoting/base/plugin_thread_task_runner.h
blob: 5ebafe12b447cd4e35ff97c3f79c512bcbfa2c42 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) 2012 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 REMOTING_BASE_PLUGIN_THREAD_TASK_RUNNER_H_
#define REMOTING_BASE_PLUGIN_THREAD_TASK_RUNNER_H_

#include "base/callback_forward.h"
#include "base/compiler_specific.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/lock.h"
#include "base/threading/platform_thread.h"

namespace remoting {

// SingleThreadTaskRunner for plugin main threads.
class PluginThreadTaskRunner : public base::SingleThreadTaskRunner {
 public:
  class Delegate {
   public:
    Delegate() { }
    virtual ~Delegate() { }

    virtual bool RunOnPluginThread(
        base::TimeDelta delay, void(function)(void*), void* data) = 0;
  };

  // Caller keeps ownership of delegate.
  PluginThreadTaskRunner(Delegate* delegate);

  void Detach();

  // base::SingleThreadTaskRunner interface.
  virtual bool PostDelayedTask(
      const tracked_objects::Location& from_here,
      const base::Closure& task,
      base::TimeDelta delay) OVERRIDE;
  virtual bool PostNonNestableDelayedTask(
      const tracked_objects::Location& from_here,
      const base::Closure& task,
      base::TimeDelta delay) OVERRIDE;

  virtual bool RunsTasksOnCurrentThread() const OVERRIDE;

 protected:
  virtual ~PluginThreadTaskRunner();

 private:
  static void TaskSpringboard(void* data);

  void RunClosureIf(const base::Closure& task);

  base::PlatformThreadId plugin_thread_id_;

  // |lock_| must be acquired when accessing |delegate_|.
  base::Lock lock_;
  Delegate* delegate_;

  DISALLOW_COPY_AND_ASSIGN(PluginThreadTaskRunner);
};

}  // namespace remoting

#endif  // REMOTING_BASE_PLUGIN_THREAD_TASK_RUNNER_H_