summaryrefslogtreecommitdiffstats
path: root/ipc/dbus/DBusWatcher.cpp
blob: 329bed35860016cd03a9ee45601475306c74e431 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* 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/. */

#include "DBusWatcher.h"
#include "mozilla/Unused.h"
#include "nsThreadUtils.h"

namespace mozilla {
namespace ipc {

DBusWatcher::DBusWatcher(DBusConnection* aConnection, DBusWatch* aWatch)
  : mConnection(aConnection)
  , mWatch(aWatch)
{
  MOZ_ASSERT(mConnection);
  MOZ_ASSERT(mWatch);
}

DBusWatcher::~DBusWatcher()
{ }

DBusConnection*
DBusWatcher::GetConnection()
{
  return mConnection;
}

void
DBusWatcher::StartWatching()
{
  MOZ_ASSERT(!NS_IsMainThread());

  auto flags = dbus_watch_get_flags(mWatch);

  if (!(flags & (DBUS_WATCH_READABLE|DBUS_WATCH_WRITABLE))) {
    return;
  }

  auto ioLoop = MessageLoopForIO::current();

  auto fd = dbus_watch_get_unix_fd(mWatch);

  if (flags & DBUS_WATCH_READABLE) {
    ioLoop->WatchFileDescriptor(fd, true, MessageLoopForIO::WATCH_READ,
                                &mReadWatcher, this);
  }
  if (flags & DBUS_WATCH_WRITABLE) {
    ioLoop->WatchFileDescriptor(fd, true, MessageLoopForIO::WATCH_WRITE,
                                &mWriteWatcher, this);
  }
}

void
DBusWatcher::StopWatching()
{
  MOZ_ASSERT(!NS_IsMainThread());

  auto flags = dbus_watch_get_flags(mWatch);

  if (flags & DBUS_WATCH_READABLE) {
    mReadWatcher.StopWatchingFileDescriptor();
  }
  if (flags & DBUS_WATCH_WRITABLE) {
    mWriteWatcher.StopWatchingFileDescriptor();
  }
}

// DBus utility functions, used as function pointers in DBus setup

void
DBusWatcher::FreeFunction(void* aData)
{
  UniquePtr<DBusWatcher> watcher(static_cast<DBusWatcher*>(aData));
}

dbus_bool_t
DBusWatcher::AddWatchFunction(DBusWatch* aWatch, void* aData)
{
  MOZ_ASSERT(!NS_IsMainThread());

  auto connection = static_cast<DBusConnection*>(aData);

  UniquePtr<DBusWatcher> dbusWatcher =
    MakeUnique<DBusWatcher>(connection, aWatch);

  dbus_watch_set_data(aWatch, dbusWatcher.get(), DBusWatcher::FreeFunction);

  if (dbus_watch_get_enabled(aWatch)) {
    dbusWatcher->StartWatching();
  }

  Unused << dbusWatcher.release(); // picked up in |FreeFunction|

  return TRUE;
}

void
DBusWatcher::RemoveWatchFunction(DBusWatch* aWatch, void* aData)
{
  MOZ_ASSERT(!NS_IsMainThread());

  auto dbusWatcher = static_cast<DBusWatcher*>(dbus_watch_get_data(aWatch));

  dbusWatcher->StopWatching();
}

void
DBusWatcher::ToggleWatchFunction(DBusWatch* aWatch, void* aData)
{
  MOZ_ASSERT(!NS_IsMainThread());

  auto dbusWatcher = static_cast<DBusWatcher*>(dbus_watch_get_data(aWatch));

  if (dbus_watch_get_enabled(aWatch)) {
    dbusWatcher->StartWatching();
  } else {
    dbusWatcher->StopWatching();
  }
}

// I/O-loop callbacks

void
DBusWatcher::OnFileCanReadWithoutBlocking(int aFd)
{
  MOZ_ASSERT(!NS_IsMainThread());

  dbus_watch_handle(mWatch, DBUS_WATCH_READABLE);

  DBusDispatchStatus dbusDispatchStatus;

  do {
    dbusDispatchStatus = dbus_connection_dispatch(mConnection);
  } while (dbusDispatchStatus == DBUS_DISPATCH_DATA_REMAINS);
}

void
DBusWatcher::OnFileCanWriteWithoutBlocking(int aFd)
{
  MOZ_ASSERT(!NS_IsMainThread());

  dbus_watch_handle(mWatch, DBUS_WATCH_WRITABLE);
}

} // namespace ipc
} // namespace mozilla