summaryrefslogtreecommitdiffstats
path: root/remoting/base/plugin_message_loop_proxy.cc
blob: 9022a78df0efb0fd31275e8513a119feeabc55a2 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// 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.

#include "remoting/base/plugin_message_loop_proxy.h"

#include "base/bind.h"

namespace remoting {

PluginMessageLoopProxy::PluginMessageLoopProxy(Delegate* delegate)
    : delegate_(delegate) {
}

PluginMessageLoopProxy::~PluginMessageLoopProxy() {
}

void PluginMessageLoopProxy::Detach() {
  base::AutoLock auto_lock(lock_);
  if (delegate_) {
    DCHECK(delegate_->IsPluginThread());
    delegate_ = NULL;
  }
}

// MessageLoopProxy interface implementation.
bool PluginMessageLoopProxy::PostTask(
    const tracked_objects::Location& from_here,
    Task* task) {
  return PostDelayedTask(from_here, task, 0);
}

bool PluginMessageLoopProxy::PostDelayedTask(
    const tracked_objects::Location& from_here,
    Task* task,
    int64 delay_ms) {
  base::AutoLock auto_lock(lock_);
  if (!delegate_)
    return false;

  base::Closure* springpad_closure = new base::Closure(base::Bind(
      &PluginMessageLoopProxy::RunTaskIf, this, task));
  return delegate_->RunOnPluginThread(
      delay_ms, &PluginMessageLoopProxy::TaskSpringboard, springpad_closure);
}

bool PluginMessageLoopProxy::PostNonNestableTask(
    const tracked_objects::Location& from_here,
    Task* task) {
  // All tasks running on this message loop are non-nestable.
  return PostTask(from_here, task);
}

bool PluginMessageLoopProxy::PostNonNestableDelayedTask(
      const tracked_objects::Location& from_here,
      Task* task,
      int64 delay_ms) {
  // All tasks running on this message loop are non-nestable.
  return PostDelayedTask(from_here, task, delay_ms);
}

bool PluginMessageLoopProxy::PostTask(
    const tracked_objects::Location& from_here,
    const base::Closure& task) {
  return PostDelayedTask(from_here, task, 0);
}

bool PluginMessageLoopProxy::PostDelayedTask(
    const tracked_objects::Location& from_here,
    const base::Closure& task,
    int64 delay_ms) {
  base::AutoLock auto_lock(lock_);
  if (!delegate_)
    return false;

  base::Closure* springpad_closure = new base::Closure(base::Bind(
      &PluginMessageLoopProxy::RunClosureIf, this, task));
  return delegate_->RunOnPluginThread(
      delay_ms, &PluginMessageLoopProxy::TaskSpringboard, springpad_closure);
}

bool PluginMessageLoopProxy::PostNonNestableTask(
    const tracked_objects::Location& from_here,
    const base::Closure& task) {
  // All tasks running on this message loop are non-nestable.
  return PostTask(from_here, task);
}

bool PluginMessageLoopProxy::PostNonNestableDelayedTask(
    const tracked_objects::Location& from_here,
    const base::Closure& task,
    int64 delay_ms) {
  // All tasks running on this message loop are non-nestable.
  return PostDelayedTask(from_here, task, delay_ms);
}

bool PluginMessageLoopProxy::BelongsToCurrentThread() {
  base::AutoLock auto_lock(lock_);
  if (!delegate_)
    return false;

  return delegate_->IsPluginThread();
}

// static
void PluginMessageLoopProxy::TaskSpringboard(void* data) {
  base::Closure* task = reinterpret_cast<base::Closure*>(data);
  task->Run();
  delete task;
}

void PluginMessageLoopProxy::RunTaskIf(Task* task) {
  // |delegate_| can be changed only from our thread, so it's safe to
  // access it without acquiring |lock_|.
  if (delegate_)
    task->Run();
  delete task;
}

void PluginMessageLoopProxy::RunClosureIf(const base::Closure& task) {
  // |delegate_| can be changed only from our thread, so it's safe to
  // access it without acquiring |lock_|.
  if (delegate_)
    task.Run();
}

}  // namespace remoting