summaryrefslogtreecommitdiff
path: root/open-vm-tools/services/plugins/dndcp/dndGuest/copyPasteRpcV3.cc
blob: b1dee0b67096ba2133413051cd1e7c970be8ba7a (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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*********************************************************
 * Copyright (C) 2007 VMware, Inc. All rights reserved.
 *
 * This program 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 version 2.1 and no later version.
 *
 * This program 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 Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*
 * CopyPasteRpcV3.cc --
 *
 *     Implementation of the CopyPasteRpcV3 interface.
 */


#include "copyPasteRpcV3.hh"
#include "tracer.hh"

extern "C" {
   #include "dndMsg.h"
   #include "debug.h"
   #include "dndClipboard.h"
}

/**
 * Constructor.
 *
 * @param[in] transport for sending packets.
 */

CopyPasteRpcV3::CopyPasteRpcV3(DnDCPTransport *transport)
   : mTransport(transport),
     mTransportInterface(TRANSPORT_GUEST_CONTROLLER_CP)
{
   ASSERT(mTransport);

   mUtil.Init(this);
}


/**
 * Destructor.
 */

CopyPasteRpcV3::~CopyPasteRpcV3(void)
{
}


/**
 * Init.
 */

void
CopyPasteRpcV3::Init(void)
{
   TRACE_CALL();
   ASSERT(mTransport);
   mTransport->RegisterRpc(this, mTransportInterface);
}


/**
 * Not needed for version 3.
 *
 * @param[ignored] caps capabilities mask
 */

void
CopyPasteRpcV3::SendPing(uint32 caps)
{
   TRACE_CALL();
}


/**
 * Not needed for version 3.
 *
 * @param[ignored] sessionId active session id the controller assigned
 * @param[ignored] isActive active or passive CopyPaste
 *
 * @return always true.
 */

bool
CopyPasteRpcV3::SrcRequestClip(uint32 sessionId,
                               bool isActive)
{
   TRACE_CALL();
   return true;
}


/**
 * Send cmd CP_GH_GET_CLIPBOARD_DONE to controller.
 *
 * @param[ignored] sessionId active session id the controller assigned earlier.
 * @param[ignored] isActive active or passive CopyPaste
 * @param[in] clip cross-platform clipboard data.
 *
 * @return true on success, false otherwise.
 */

bool
CopyPasteRpcV3::DestSendClip(uint32 sessionId,
                             bool isActive,
                             const CPClipboard* clip)
{
   TRACE_CALL();
   return mUtil.SendMsg(CP_GH_GET_CLIPBOARD_DONE, clip);
}


/**
 * Send cmd CP_HG_START_FILE_COPY to controller.
 *
 * @param[ignored] sessionId active session id the controller assigned earlier.
 * @param[in] stagingDirCP staging dir name in cross-platform format
 * @param[in] sz the staging dir name size in bytes
 *
 * @return true on success, false otherwise.
 */

bool
CopyPasteRpcV3::RequestFiles(uint32 sessionId,
                             const uint8 *stagingDirCP,
                             uint32 sz)
{
   DnDMsg msg;
   bool ret = false;

   TRACE_CALL();

   DnDMsg_Init(&msg);

   /* Construct msg with both cmd CP_HG_START_FILE_COPY and stagingDirCP. */
   DnDMsg_SetCmd(&msg, CP_HG_START_FILE_COPY);
   if (!DnDMsg_AppendArg(&msg, (void *)stagingDirCP, sz)) {
      g_debug("%s: DnDMsg_AppendData failed.\n", __FUNCTION__);
      goto exit;
   }

   ret = mUtil.SendMsg(&msg);

exit:
   DnDMsg_Destroy(&msg);
   return ret;
}


/**
 * Not needed for version 3.
 *
 * @param[ignored] sessionId active session id the controller assigned earlier.
 * @param[ignored] success the file transfer operation was successful or not
 * @param[ignored] stagingDirCP staging dir name in cross-platform format
 * @param[ignored] sz the staging dir name size in bytes
 *
 * @return always true.
 */

bool
CopyPasteRpcV3::SendFilesDone(uint32 sessionId,
                              bool success,
                              const uint8 *stagingDirCP,
                              uint32 sz)
{
   TRACE_CALL();
   return true;
}


/**
 * Not needed for version 3.
 *
 * @param[ignored] sessionId active session id the controller assigned earlier.
 * @param[ignored] success the file transfer operation was successful or not
 *
 * @return always true.
 */

bool
CopyPasteRpcV3::GetFilesDone(uint32 sessionId,
                             bool success)
{
   TRACE_CALL();
   return true;
}


/**
 * Send a packet.
 *
 * @param[in] destId destination address id.
 * @param[in] packet
 * @param[in] length packet length in byte
 *
 * @return true on success, false otherwise.
 */

bool
CopyPasteRpcV3::SendPacket(uint32 destId,
                           const uint8 *packet,
                           size_t length)
{
   TRACE_CALL();
   return mTransport->SendPacket(destId,
                                 mTransportInterface,
                                 packet,
                                 length);
}


/**
 * Handle a received message.
 *
 * @param[in] params parameter list for received message.
 * @param[in] binary attached binary data for received message.
 * @param[in] binarySize in byte
 */

void
CopyPasteRpcV3::HandleMsg(RpcParams *params,
                          const uint8 *binary,
                          uint32 binarySize)
{
   DnDMsg msg;
   DnDMsgErr ret;
   DynBuf *buf = NULL;

   DnDMsg_Init(&msg);

   ret = DnDMsg_UnserializeHeader(&msg, (void *)binary, binarySize);
   if (DNDMSG_SUCCESS != ret) {
      g_debug("%s: DnDMsg_UnserializeHeader failed %d\n", __FUNCTION__, ret);
      goto exit;
   }

   ret = DnDMsg_UnserializeArgs(&msg,
                                (void *)(binary + DNDMSG_HEADERSIZE_V3),
                                binarySize - DNDMSG_HEADERSIZE_V3);
   if (DNDMSG_SUCCESS != ret) {
      g_debug("%s: DnDMsg_UnserializeArgs failed with %d\n", __FUNCTION__, ret);
      goto exit;
   }

   g_debug("%s: Got %d, binary size %d.\n", __FUNCTION__, DnDMsg_GetCmd(&msg),
           binarySize);

   /*
    * Translate command and emit signal. Session Id 1 is used because version
    * 3 command does not provide session Id.
    */
   switch (DnDMsg_GetCmd(&msg)) {
   case CP_HG_SET_CLIPBOARD:
   {
      CPClipboard clip;

      /* Unserialize clipboard data for the command. */
      buf = DnDMsg_GetArg(&msg, 0);
      if (!CPClipboard_Unserialize(&clip, DynBuf_Get(buf), DynBuf_GetSize(buf))) {
         g_debug("%s: CPClipboard_Unserialize failed.\n", __FUNCTION__);
         goto exit;
      }
      srcRecvClipChanged.emit(1, false, &clip);
      CPClipboard_Destroy(&clip);
      break;
   }
   case CP_HG_FILE_COPY_DONE:
   {
      bool success = false;
      buf = DnDMsg_GetArg(&msg, 0);
      if (sizeof success == DynBuf_GetSize(buf)) {
         memcpy(&success, DynBuf_Get(buf), DynBuf_GetSize(buf));
      }
      getFilesDoneChanged.emit(1, success, NULL, 0);
      break;
   }
   case CP_GH_GET_CLIPBOARD:
   {
      destRequestClipChanged.emit(1, false);
      break;
   }
   default:
      g_debug("%s: got unsupported new command %d.\n", __FUNCTION__,
              DnDMsg_GetCmd(&msg));
   }
exit:
   DnDMsg_Destroy(&msg);
}


/**
 * Callback from transport layer after received a packet from srcId.
 *
 * @param[in] srcId addressId where the packet is from
 * @param[in] packet
 * @param[in] packetSize
 */

void
CopyPasteRpcV3::OnRecvPacket(uint32 srcId,
                             const uint8 *packet,
                             size_t packetSize)
{
   TRACE_CALL();
   mUtil.OnRecvPacket(srcId, packet, packetSize);
}