-
Notifications
You must be signed in to change notification settings - Fork 70
/
TargetApplicationProxy.cpp
232 lines (193 loc) · 6.35 KB
/
TargetApplicationProxy.cpp
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
#include "TargetApplicationProxy.h"
#include "ExternalObjectProxy.h"
#include "GdbLibraryInjector.h"
#include "lib/InspectorServer.h"
#include "lib/NetstringWriter.h"
#include "lib/ObjectProxy.h"
#include "lib/VariantSerializer.h"
#include "lib/inspector.pb.h"
#include <QtCore/QTime>
TargetApplicationProxy::TargetApplicationProxy()
: m_socket(new QLocalSocket(this))
{
connect(m_socket,SIGNAL(error(QLocalSocket::LocalSocketError)),
this,SLOT(socketError(QLocalSocket::LocalSocketError)));
connect(m_socket,SIGNAL(stateChanged(QLocalSocket::LocalSocketState)),
this,SLOT(socketStateChanged(QLocalSocket::LocalSocketState)));
}
bool TargetApplicationProxy::connectToTarget(int pid)
{
// connect to the socket set up by the
// helper library
m_socket->connectToServer(InspectorServer::socketName(pid));
if (!m_socket->waitForConnected(3000))
{
qWarning() << "Failed to connect to local socket server in target process" << pid;
return false;
}
return true;
}
void TargetApplicationProxy::disconnect()
{
m_socket->disconnectFromServer();
}
QList<ObjectProxy::Pointer> TargetApplicationProxy::fetchTopLevelWidgets()
{
service::InspectorRequest request;
service::InspectorResponse response;
QList<ObjectProxy::Pointer> objects;
request.set_type(service::InspectorRequest::FetchObjectTree);
if (!sendRequest(request,&response))
{
qWarning() << "Failed to send top-level widget request fetch to child process";
return objects;
}
for (int i=0; i < response.object_size(); i++)
{
int id = response.object(i).id();
ObjectProxy::Pointer proxy = fetchProxy(id);
updateProxy(response.object(i),proxy.dynamicCast<ExternalObjectProxy>().data());
}
for (int i=0; i < response.rootobjectid_size(); i++)
{
int id = response.rootobjectid(i);
objects << fetchProxy(id);
}
return objects;
}
ObjectProxy::Pointer TargetApplicationProxy::fetchProxy(int objectId)
{
ExternalObjectProxy::Pointer proxy = m_objectProxies.value(objectId);
if (proxy)
{
return proxy;
}
proxy = ObjectProxy::Pointer(new ExternalObjectProxy(this,objectId));
m_objectProxies.insert(objectId,proxy);
return proxy;
}
bool TargetApplicationProxy::fetchObject(ExternalObjectProxy* proxy, bool fetchProperties)
{
service::InspectorRequest request;
service::InspectorResponse response;
request.set_type(service::InspectorRequest::FetchObjectRequest);
request.set_objectid(proxy->objectId());
request.set_fetchproperties(fetchProperties);
if (!sendRequest(request,&response))
{
qWarning() << "Failed to send object fetch request to child process";
return false;
}
if (response.object_size() == 1)
{
service::QtObject object = response.object(0);
updateProxy(object,proxy);
return true;
}
else
{
qWarning() << "Expected to receive one object but received" << response.object_size();
return false;
}
}
void TargetApplicationProxy::updateProxy(const service::QtObject& object, ExternalObjectProxy* proxy)
{
proxy->setLoaded(true);
proxy->setClassName(QString::fromStdString(object.classname()));
proxy->setObjectName(QString::fromStdString(object.objectname()));
proxy->setAddress(object.address());
for (int i=0; i < object.property_size(); i++)
{
proxy->setPropertiesLoaded(true);
ObjectProxy::Property property;
property.name = QString::fromStdString(object.property(i).name());
QByteArray propertyBytes(object.property(i).value().c_str(),object.property(i).value().size());
property.value = VariantSerializer::decode(propertyBytes);
property.isWritable = object.property(i).iswritable();
proxy->addProperty(property);
}
QList<int> childIds;
for (int i=0; i < object.childid_size(); i++)
{
childIds << object.childid(i);
}
proxy->setChildIds(childIds);
}
void TargetApplicationProxy::updateProperty(int objectId, const ObjectProxy::Property& property)
{
service::InspectorRequest request;
service::InspectorResponse response;
request.set_type(service::InspectorRequest::WritePropertyRequest);
request.set_objectid(objectId);
request.mutable_propertyupdate()->set_name(property.name.toStdString());
QByteArray valueBuffer = VariantSerializer::encode(property.value);
request.mutable_propertyupdate()->set_value(std::string(valueBuffer.constData(),valueBuffer.count()));
request.mutable_propertyupdate()->set_iswritable(true);
sendRequest(request,&response);
}
ObjectProxy::Pointer TargetApplicationProxy::pickWidget()
{
service::InspectorRequest request;
service::InspectorResponse response;
request.set_type(service::InspectorRequest::PickWidgetRequest);
sendRequest(request,&response);
if (response.object_size() == 1)
{
service::QtObject object = response.object(0);
ObjectProxy::Pointer proxy = fetchProxy(object.id());
updateProxy(object,proxy.dynamicCast<ExternalObjectProxy>().data());
return proxy;
}
else
{
return ObjectProxy::Pointer();
}
}
bool TargetApplicationProxy::sendRequest(const service::InspectorRequest& request,
service::InspectorResponse* response)
{
if (m_socket->state() != QLocalSocket::ConnectedState)
{
return false;
}
QByteArray requestData(request.ByteSize(),0);
request.SerializeToArray(requestData.data(),requestData.count());
QByteArray messageData = NetstringWriter::toMessage(requestData);
int bytesWritten = m_socket->write(messageData);
if (bytesWritten != messageData.count())
{
qWarning() << "Tried to write" << messageData.count() << "bytes to helper but only sent" << bytesWritten;
}
m_socket->waitForBytesWritten();
int maxDelay = 10000;
while (m_messageReader.messageCount() < 1 &&
m_socket->state() == QLocalSocket::ConnectedState)
{
if (!m_socket->waitForReadyRead(maxDelay))
{
qWarning() << "Failed to read response from process";
return false;
}
QByteArray data = m_socket->readAll();
m_messageReader.parse(data.constData(),data.count());
}
if (m_messageReader.messageCount() < 1)
{
return false;
}
QByteArray responseData = m_messageReader.nextMessage();
response->ParseFromArray(responseData.constData(),responseData.size());
return true;
}
void TargetApplicationProxy::socketError(QLocalSocket::LocalSocketError error)
{
qWarning() << "Local socket reported error" << error;
}
void TargetApplicationProxy::socketStateChanged(QLocalSocket::LocalSocketState state)
{
qWarning() << "Local socket state changed" << state;
}
QList<ObjectProxy::Pointer> TargetApplicationProxy::rootObjects()
{
return fetchTopLevelWidgets();
}