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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/*
* This file is part of TelepathyQt4
*
* Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
* Copyright (C) 2010 Nokia Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <TelepathyQt4/DBusProxyFactory>
#include "TelepathyQt4/dbus-proxy-factory-internal.h"
#include "TelepathyQt4/_gen/dbus-proxy-factory.moc.hpp"
#include "TelepathyQt4/_gen/dbus-proxy-factory-internal.moc.hpp"
#include "TelepathyQt4/debug-internal.h"
#include <TelepathyQt4/DBusProxy>
#include <TelepathyQt4/ReadyObject>
#include <TelepathyQt4/PendingReady>
#include <QDBusConnection>
namespace Tp
{
struct DBusProxyFactory::Private
{
Private(const QDBusConnection &bus)
: bus(bus),
cache(new Cache)
{
}
~Private()
{
delete cache;
}
QDBusConnection bus;
Cache *cache;
};
/**
* \class DBusProxyFactory
* \ingroup clientreadiness
* \headerfile TelepathyQt4/dbus-proxy-factory.h <TelepathyQt4/DBusProxyFactory>
*
* Base class for all D-Bus proxy factory classes. Handles proxy caching and making them ready as
* appropriate.
*/
/**
* Class constructor.
*
* The intention for storing the bus here is that it generally doesn't make sense to construct
* proxies for multiple buses in the same context. Allowing that would lead to more complex keying
* needs in the cache, as well.
*
* \param bus The D-Bus bus connection for the objects constructed using this factory.
*/
DBusProxyFactory::DBusProxyFactory(const QDBusConnection &bus)
: mPriv(new Private(bus))
{
}
/**
* Class destructor.
*/
DBusProxyFactory::~DBusProxyFactory()
{
delete mPriv;
}
/**
* Returns the D-Bus connection all of the proxies from this factory communicate with.
*
* \return The connection.
*/
const QDBusConnection &DBusProxyFactory::dbusConnection() const
{
return mPriv->bus;
}
/**
* Returns a cached proxy with the given \a busName and \a objectPath.
*
* If a proxy has not been previously put into the cache by nowHaveProxy for those identifying
* attributes, or a previously cached proxy has since been invalidated and/or destroyed, a \c Null
* shared pointer is returned instead.
*
* \param busName Bus name of the proxy to return.
* \param objectPath Object path of the proxy to return.
* \return The proxy, if any.
*/
DBusProxyPtr DBusProxyFactory::cachedProxy(const QString &busName,
const QString &objectPath) const
{
QString finalName = finalBusNameFrom(busName);
return mPriv->cache->get(Cache::Key(finalName, objectPath));
}
/**
* Should be called by subclasses when they have a proxy, be it a newly-constructed one or one from
* the cache.
*
* This function will then do the rest of the factory work, including caching the proxy if it's not
* cached already, doing any initialPrepare()/readyPrepare() work if appropriate, and making the
* features from featuresFor() ready if they aren't already.
*
* The returned PendingReady only finishes when the initialPrepare() and readyPrepare() operations
* for the proxy has completed, and the requested features have all been made ready (or found unable
* to be made ready). Note that this might have happened already before calling this function, if
* the proxy was not a newly created one, but was looked up from the cache. DBusProxyFactory handles
* the necessary subleties for this to work.
*
* Access to the proxy instance is allowed as soon as this method returns through
* PendingReady::proxy(), if the proxy is needed in a context where it's not required to be ready.
*
* \param proxy The proxy which the factory should now make sure is prepared and made ready.
* \return Readifying operation, which finishes when the proxy is usable.
*/
PendingReady *DBusProxyFactory::nowHaveProxy(const DBusProxyPtr &proxy) const
{
Q_ASSERT(!proxy.isNull());
mPriv->cache->put(proxy);
return new PendingReady(SharedPtr<DBusProxyFactory>((DBusProxyFactory*) this),
proxy, featuresFor(proxy));
}
/**
* \fn QString DBusProxyFactory::finalBusNameFrom(const QString &uniqueOrWellKnown) const
* "Normalize" a bus name according to the rules for the proxy class to construct.
*
* Should be implemented by subclasses to transform the application-specified name \a
* uniqueOrWellKnown to whatever the proxy constructed for that name would have in its
* DBusProxy::busName() in the end.
*
* For StatelessDBusProxy sub-classes this should mostly be an identity transform, while for
* StatefulDBusProxy sub-classes StatefulDBusProxy::uniqueNameFrom() or an equivalent thereof should
* be used in most cases.
*
* If this is not implemented correctly, caching won't work properly.
*
* \param uniqueOrWellKnown Any valid D-Bus service name, either unique or well-known.
* \return Whatever that name would turn to, when a proxy is constructed for it.
*/
/**
* Allows subclasses to do arbitrary manipulation on the proxy before it is attempted to be made
* ready.
*
* If a non-\c NULL operation is returned, the completion of that operation is waited for before
* starting to make the object ready whenever nowHaveProxy() is called the first time around for a
* given proxy.
*
* \todo FIXME actually implement this... :) Currently just a vtable placeholder.
* \param proxy The just-constructed proxy to be prepared.
* \return \c NULL ie. nothing to do.
*/
PendingOperation *DBusProxyFactory::initialPrepare(const DBusProxyPtr &proxy) const
{
// Nothing we could think about needs doing
return NULL;
}
/**
* Allows subclasses to do arbitrary manipulation on the proxy after it has been made ready.
*
* If a non-\c NULL operation is returned, the completion of that operation is waited for before
* signaling that the object is ready for use after ReadyObject::becomeReady() for it has finished
* whenever nowHaveProxy() is called the first time around for a given proxy.
*
* \todo FIXME actually implement this... :) Currently just a vtable placeholder.
* \param proxy The just-readified proxy to be prepared.
* \return \c NULL ie. nothing to do.
*/
PendingOperation *DBusProxyFactory::readyPrepare(const DBusProxyPtr &proxy) const
{
// Nothing we could think about needs doing
return NULL;
}
/**
* \fn Features DBusProxyFactory::featuresFor(const SharedPtr<RefCounted> &proxy) const
* Specifies features which should be made ready on a given proxy.
*
* This can be used to implement instance-specific features based on arbitrary criteria.
* FixedFeatureFactory implements this as a fixed set of features independent of the instance,
* however.
*
* It should be noted that if an empty set of features is returned, ReadyObject::becomeReady() is
* not called at all. In other words, any "core feature" is not automatically added to the requested
* features. This is to enable setting a factory to not make proxies ready at all, which is useful
* eg. in the case of account editing UIs which aren't interested in the state of Connection objects
* for the Account objects they're editing.
*
* \param proxy The proxy on which the returned features will be made ready.
* \return Features to make ready on the proxy.
*/
DBusProxyFactory::Cache::Cache()
{
}
DBusProxyFactory::Cache::~Cache()
{
}
DBusProxyPtr DBusProxyFactory::Cache::get(const Key &key) const
{
DBusProxyPtr proxy(proxies.value(key));
if (proxy.isNull() || !proxy->isValid()) {
// Weak pointer invalidated or proxy invalidated during this mainloop iteration and we still
// haven't got the invalidated() signal for it
return DBusProxyPtr();
}
return proxy;
}
void DBusProxyFactory::Cache::put(const DBusProxyPtr &proxy)
{
if (proxy->busName().isEmpty()) {
debug() << "Not inserting proxy" << proxy.data() << "with no bus name to factory cache";
return;
} else if (!proxy->isValid()) {
debug() << "Not inserting to factory cache invalid proxy - proxy is for" <<
proxy->busName() << ',' << proxy->objectPath();
return;
}
Key key(proxy->busName(), proxy->objectPath());
DBusProxyPtr existingProxy(proxies.value(key));
if (!existingProxy || existingProxy != proxy) {
// Disconnect the invalidated signal from the proxy we're replacing, so it won't uselessly
// cause the new (hopefully valid) proxy to be dropped from the cache if it arrives late.
//
// The window in which this makes a difference is very slim but existent; namely, somebody
// must request a proxy from the factory in the same mainloop iteration as an otherwise
// matching proxy has invalidated itself. The invalidation signal would be delivered and
// processed only during the next mainloop iteration.
if (existingProxy) {
Q_ASSERT(!existingProxy->isValid());
existingProxy->disconnect(
SIGNAL(invalidated(Tp::DBusProxy*,QString,QString)),
this,
SLOT(onProxyInvalidated(Tp::DBusProxy*)));
debug() << "Replacing invalidated proxy" << existingProxy.data() << "in cache for name"
<< existingProxy->busName() << ',' << existingProxy->objectPath();
}
connect(proxy.data(),
SIGNAL(invalidated(Tp::DBusProxy*,QString,QString)),
SLOT(onProxyInvalidated(Tp::DBusProxy*)));
debug() << "Inserting to factory cache proxy for" << key;
proxies.insert(key, QWeakPointer<DBusProxy>(proxy.data()));
}
}
void DBusProxyFactory::Cache::onProxyInvalidated(Tp::DBusProxy *proxy)
{
Key key(proxy->busName(), proxy->objectPath());
// Not having it would indicate invalidated() signaled twice for the same proxy, or us having
// connected to two proxies with the same key, neither of which should happen
Q_ASSERT(proxies.contains(key));
debug() << "Removing from factory cache invalidated proxy for" << key;
proxies.remove(key);
}
}
|