-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClipboardService.cs
269 lines (216 loc) · 9.27 KB
/
ClipboardService.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Squared.Task;
using Squared.Task.IO;
using Squared.Task.Http;
using System.Windows.Forms;
using System.Web;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Linq;
using ICSharpCode.SharpZipLib.GZip;
namespace Tsunagaro {
public class ClipboardService {
public readonly TaskScheduler Scheduler;
private readonly Signal ClipboardChangedSignal = new Signal();
private static readonly HashSet<string> BlacklistedFormats = new HashSet<string> {
"FileContents",
"FileDrop",
"FileName",
"FileNameW",
"DeviceIndependentBitmap",
"PaintDotNet.Rendering.MaskedSurface",
"Bitmap",
"System.Drawing.Bitmap",
"PNG",
"Format17" // CF_DIBV5. WTF?
};
public ClipboardService (TaskScheduler scheduler) {
Scheduler = scheduler;
}
public IEnumerator<object> Initialize () {
Program.MainThreadJobQueue.ClipboardChanged += (s, e) =>
ClipboardChangedSignal.Set();
Scheduler.Start(ListenTask(), TaskExecutionPolicy.RunAsBackgroundTask);
Program.Control.Handlers.Add("/clipboard", ServeClipboard);
Program.Control.Handlers.Add("/clipboard/data", ServeClipboardData);
Program.Peer.MessageHandlers.Add("ClipboardChanged", OnClipboardChanged);
Program.Peer.MessageHandlers.Add("ClipboardGetDataPresent", OnClipboardGetDataPresent);
yield break;
}
private static SignalFuture RunOnMainThread (Action fn) {
var result = new SignalFuture();
Program.MainThreadJobQueue.QueueWorkItem(() => {
try {
fn();
result.Complete();
} catch (Exception exc) {
result.Fail(exc);
}
});
return result;
}
private static Future<T> RunOnMainThread<T> (Func<T> fn) {
var result = new Future<T>();
Program.MainThreadJobQueue.QueueWorkItem(() => {
try {
var value = fn();
result.Complete(value);
} catch (Exception exc) {
result.Fail(exc);
}
});
return result;
}
private static Future<string[]> GetFormats () {
return RunOnMainThread(() => Clipboard.GetDataObject().GetFormats());
}
private static Future<bool> GetDataPresent (string format, bool autoConvert) {
return RunOnMainThread(() => Clipboard.GetDataObject().GetDataPresent(format, autoConvert));
}
private static Future<object> GetData (string format, bool autoConvert) {
return RunOnMainThread(() => Clipboard.GetDataObject().GetData(format, autoConvert));
}
private static IFuture SetClipboardData (IDataObject obj) {
return RunOnMainThread(
() => Clipboard.SetDataObject(obj, false, 5, 15)
);
}
private IEnumerator<object> OnClipboardChanged (PeerService.Connection sender, Dictionary<string, object> message) {
var formatsRaw = (JArray)message["Formats"];
var formats = (from v in formatsRaw.Children() select v.Value<string>())
.Concat(new[] { ClipboardDataProxy.SentinelFormat })
.ToArray();
yield return PlaceProxyClipboard(sender, formats);
Program.Feedback(sender.HostName + " owns the clipboard", false);
}
private IEnumerator<object> OnClipboardGetDataPresent (PeerService.Connection sender, Dictionary<string, object> message) {
var format = Convert.ToString(message["Format"]);
var fResult = GetDataPresent(format, true);
yield return fResult;
yield return new Result(fResult.Result);
}
private IEnumerator<object> ProcessClipboardChange () {
var fSentinelPresent = GetDataPresent(ClipboardDataProxy.SentinelFormat, true);
yield return fSentinelPresent;
if (fSentinelPresent.Result) {
} else {
var fFormats = GetFormats();
yield return fFormats;
var payload = new Dictionary<string, object> {
{"Owner", Program.Control.URL},
{"Formats", fFormats.Result}
};
Program.Feedback("I own the clipboard", false);
yield return Program.Peer.Broadcast("ClipboardChanged", payload);
}
}
public IEnumerator<object> ListenTask () {
while (true) {
yield return ClipboardChangedSignal.Wait();
yield return Scheduler.Start(ProcessClipboardChange(), TaskExecutionPolicy.RunAsBackgroundTask);
}
}
public IFuture PlaceProxyClipboard (PeerService.Connection owner, string[] formats) {
var proxy = new ClipboardDataProxy(owner, formats);
return SetClipboardData(proxy);
}
private static string GenerateClipboardDataInfo (IDataObject data, string format) {
if (BlacklistedFormats.Contains(format))
return format;
try {
var dataObject = data.GetData(format);
var text = dataObject as string;
if (text != null)
return String.Format("{0} ({1} character(s))", format, text.Length);
var stream = dataObject as Stream;
if (stream != null) {
using (stream)
return String.Format("{0} ({1} byte(s))", format, stream.Length);
}
return string.Format("{0} (unknown size)", format);
} catch {
return String.Format("{0} (unsupported format)", format);
}
}
public IEnumerator<object> ServeClipboard (HttpServer.Request request) {
request.Response.ContentType = "text/html; charset=utf-8";
var fFormats = GetFormats();
var fSentinelData = GetData(ClipboardDataProxy.SentinelFormat, true);
yield return fFormats;
yield return fSentinelData;
var html = String.Format(
@"<html>
<head>
<title>Clipboard on {0}</title>
<meta charset=""UTF-8"">
<meta http-equiv=""refresh"" content=""10"">
</head>
<body>
<h2>Clipboard on {0}</h2>
{1}
<h2>Formats</h2>
<pre>
{2}
</pre>
</body>
</html>",
System.Net.Dns.GetHostName(),
(fSentinelData.Result != null)
? "Remote data from " + (string)fSentinelData.Result
: "Local data",
String.Join(
"<br>",
(
from fmt in fFormats.Result
select String.Format(
"{0} <a href=\"/clipboard/data?format={1}\">View</a>",
fmt, HttpUtility.UrlEncode(fmt)
)
)
)
);
yield return ControlService.WriteResponseBody(request, html);
}
public IEnumerator<object> ServeClipboardData (HttpServer.Request request) {
var format = request.QueryString["format"];
Stream resultStream = null;
var fData = GetData(format, true);
yield return fData;
var data = fData.Result;
if (data is string) {
var s = (string)data;
var bytes = (new UTF8Encoding(false)).GetBytes(s);
request.Response.ContentType = "text/plain; charset=utf-8";
resultStream = new MemoryStream(bytes);
} else if (data is Stream) {
request.Response.ContentType = "application/octet-stream";
resultStream = (Stream)data;
} else {
yield return ControlService.ServeError(request, 501, "Could not retrieve data in the requested format");
yield break;
}
// If supported, gzip-compress the data.
Header header;
if (request.Headers.TryGetValue("Accept-Encoding", out header)) {
if (header.Value.ToLower().Contains("gzip")) {
request.Response.Headers.Add(new Header("Content-Encoding", "gzip"));
var uncompressedStream = resultStream;
resultStream = new MemoryStream();
using (uncompressedStream)
using (var gzipStream = new GZipOutputStream(resultStream) {
IsStreamOwner = false
}) {
uncompressedStream.CopyTo(gzipStream, 1024 * 16);
}
resultStream.Seek(0, SeekOrigin.Begin);
}
}
request.Response.ContentLength = (int)resultStream.Length;
using (resultStream)
yield return ControlService.WriteResponseBody(request, resultStream);
}
}
}