/
SessionWorkbench.cpp
417 lines (344 loc) · 12.8 KB
/
SessionWorkbench.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* SessionWorkbench.cpp
*
* Copyright (C) 2009-11 by RStudio, Inc.
*
* This program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#include "SessionWorkbench.hpp"
#include <algorithm>
#include <boost/function.hpp>
#include <boost/format.hpp>
#include <core/Error.hpp>
#include <core/Exec.hpp>
#include <core/StringUtils.hpp>
#include <core/FileSerializer.hpp>
#include <core/http/Request.hpp>
#include <core/http/Response.hpp>
#include <core/json/JsonRpc.hpp>
#include <r/ROptions.hpp>
#include <r/session/RSession.hpp>
#include <r/session/RClientState.hpp>
#include <r/RFunctionHook.hpp>
#include <session/SessionModuleContext.hpp>
#include <session/SessionUserSettings.hpp>
#include "SessionVCS.hpp"
#include <R_ext/RStartup.h>
extern "C" SA_TYPE SaveAction;
using namespace core;
namespace session {
namespace modules {
namespace workbench {
namespace {
Error setClientState(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
pResponse->setSuppressDetectChanges(true);
// extract params
json::Object temporaryState, persistentState, projPersistentState;
Error error = json::readParams(request.params,
&temporaryState,
&persistentState,
&projPersistentState);
if (error)
return error ;
// set state
r::session::ClientState& clientState = r::session::clientState();
clientState.putTemporary(temporaryState);
clientState.putPersistent(persistentState);
clientState.putProjectPersistent(projPersistentState);
return Success();
}
// IN: WorkbenchMetrics object
// OUT: Void
Error setWorkbenchMetrics(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
// extract fields
r::session::RClientMetrics metrics ;
Error error = json::readObjectParam(request.params, 0,
"consoleWidth", &(metrics.consoleWidth),
"graphicsWidth", &(metrics.graphicsWidth),
"graphicsHeight", &(metrics.graphicsHeight));
if (error)
return error;
// set the metrics
r::session::setClientMetrics(metrics);
return Success();
}
CRANMirror toCRANMirror(const json::Object& cranMirrorJson)
{
CRANMirror cranMirror;
json::readObject(cranMirrorJson,
"name", &cranMirror.name,
"host", &cranMirror.host,
"url", &cranMirror.url,
"country", &cranMirror.country);
return cranMirror;
}
BioconductorMirror toBioconductorMirror(const json::Object& mirrorJson)
{
BioconductorMirror mirror;
json::readObject(mirrorJson,
"name", &mirror.name,
"url", &mirror.url);
return mirror;
}
Error setPrefs(const json::JsonRpcRequest& request, json::JsonRpcResponse*)
{
// read params
json::Object generalPrefs, historyPrefs, packagesPrefs, projectsPrefs,
sourceControlPrefs;
Error error = json::readObjectParam(request.params, 0,
"general_prefs", &generalPrefs,
"history_prefs", &historyPrefs,
"packages_prefs", &packagesPrefs,
"projects_prefs", &projectsPrefs,
"source_control_prefs", &sourceControlPrefs);
if (error)
return error;
json::Object uiPrefs;
error = json::readParam(request.params, 1, &uiPrefs);
if (error)
return error;
// read and set general prefs
int saveAction;
bool loadRData;
std::string initialWorkingDir;
error = json::readObject(generalPrefs,
"save_action", &saveAction,
"load_rdata", &loadRData,
"initial_working_dir", &initialWorkingDir);
if (error)
return error;
userSettings().beginUpdate();
userSettings().setSaveAction(saveAction);
userSettings().setLoadRData(loadRData);
userSettings().setInitialWorkingDirectory(FilePath(initialWorkingDir));
userSettings().endUpdate();
// sync underlying R save action
module_context::syncRSaveAction();
// read and set history prefs
bool alwaysSave, removeDuplicates;
error = json::readObject(historyPrefs,
"always_save", &alwaysSave,
"remove_duplicates", &removeDuplicates);
if (error)
return error;
userSettings().beginUpdate();
userSettings().setAlwaysSaveHistory(alwaysSave);
userSettings().setRemoveHistoryDuplicates(removeDuplicates);
userSettings().endUpdate();
// read and set packages prefs
json::Object cranMirrorJson;
error = json::readObject(packagesPrefs,
"cran_mirror", &cranMirrorJson);
/* see note on bioconductor below
"bioconductor_mirror", &bioconductorMirrorJson);
*/
if (error)
return error;
userSettings().beginUpdate();
userSettings().setCRANMirror(toCRANMirror(cranMirrorJson));
// NOTE: currently there is no UI for bioconductor mirror so we
// don't want to set it (would have side effect of overwriting
// user-specified BioC_Mirror option)
/*
userSettings().setBioconductorMirror(toBioconductorMirror(
bioconductorMirrorJson));
*/
userSettings().endUpdate();
// read and set projects prefs
bool restoreLastProject;
error = json::readObject(projectsPrefs,
"restore_last_project", &restoreLastProject);
if (error)
return error;
userSettings().beginUpdate();
userSettings().setAlwaysRestoreLastProject(restoreLastProject);
userSettings().endUpdate();
// read and set source control prefs
bool vcsEnabled;
std::string gitBinDir, sshKeyPath;
error = json::readObject(sourceControlPrefs,
"vcs_enabled", &vcsEnabled,
"git_bin_dir", &gitBinDir);
if (error)
return error;
userSettings().beginUpdate();
userSettings().setVcsEnabled(vcsEnabled);
FilePath gitBinDirPath(gitBinDir);
if (gitBinDirPath == git::detectedGitBinDir())
userSettings().setGitBinDir(FilePath());
else
userSettings().setGitBinDir(gitBinDirPath);
userSettings().setSshKeyPath(sshKeyPath);
userSettings().endUpdate();
// set ui prefs
userSettings().setUiPrefs(uiPrefs);
return Success();
}
Error setUiPrefs(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
json::Object uiPrefs;
Error error = json::readParams(request.params, &uiPrefs);
if (error)
return error;
userSettings().setUiPrefs(uiPrefs);
return Success();
}
json::Object toCRANMirrorJson(const CRANMirror& cranMirror)
{
json::Object cranMirrorJson;
cranMirrorJson["name"] = cranMirror.name;
cranMirrorJson["host"] = cranMirror.host;
cranMirrorJson["url"] = cranMirror.url;
cranMirrorJson["country"] = cranMirror.country;
return cranMirrorJson;
}
json::Object toBioconductorMirrorJson(
const BioconductorMirror& bioconductorMirror)
{
json::Object bioconductorMirrorJson;
bioconductorMirrorJson["name"] = bioconductorMirror.name;
bioconductorMirrorJson["url"] = bioconductorMirror.url;
return bioconductorMirrorJson;
}
Error getRPrefs(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
// get general prefs
json::Object generalPrefs;
generalPrefs["save_action"] = userSettings().saveAction();
generalPrefs["load_rdata"] = userSettings().loadRData();
generalPrefs["initial_working_dir"] = module_context::createAliasedPath(
userSettings().initialWorkingDirectory());
// get history prefs
json::Object historyPrefs;
historyPrefs["always_save"] = userSettings().alwaysSaveHistory();
historyPrefs["remove_duplicates"] = userSettings().removeHistoryDuplicates();
// get packages prefs
json::Object packagesPrefs;
packagesPrefs["cran_mirror"] = toCRANMirrorJson(
userSettings().cranMirror());
packagesPrefs["bioconductor_mirror"] = toBioconductorMirrorJson(
userSettings().bioconductorMirror());
// get projects prefs
json::Object projectsPrefs;
projectsPrefs["restore_last_project"] = userSettings().alwaysRestoreLastProject();
// get source control prefs
json::Object sourceControlPrefs;
sourceControlPrefs["vcs_enabled"] = userSettings().vcsEnabled();
FilePath gitBinDir = userSettings().gitBinDir();
if (gitBinDir.empty())
gitBinDir = git::detectedGitBinDir();
sourceControlPrefs["git_bin_dir"] = gitBinDir.absolutePath();
sourceControlPrefs["have_rsa_public_key"] =
modules::source_control::defaultSshKeyDir().childPath(
"id_rsa.pub").exists();
// initialize and set result object
json::Object result;
result["general_prefs"] = generalPrefs;
result["history_prefs"] = historyPrefs;
result["packages_prefs"] = packagesPrefs;
result["projects_prefs"] = projectsPrefs;
result["source_control_prefs"] = sourceControlPrefs;
pResponse->setResult(result);
return Success();
}
Error setCRANMirror(const json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
json::Object cranMirrorJson;
Error error = json::readParam(request.params, 0, &cranMirrorJson);
if (error)
return error;
CRANMirror cranMirror = toCRANMirror(cranMirrorJson);
userSettings().beginUpdate();
userSettings().setCRANMirror(cranMirror);
userSettings().endUpdate();
return Success();
}
// options("pdfviewer")
void viewPdfPostback(const std::string& pdfPath,
const module_context::PostbackHandlerContinuation& cont)
{
module_context::showFile(FilePath(pdfPath));
cont(EXIT_SUCCESS, "");
}
void handleFileShow(const http::Request& request, http::Response* pResponse)
{
// get the file path
FilePath filePath(request.queryParamValue("path"));
if (!filePath.exists())
{
pResponse->setError(http::status::NotFound, "File not found");
return;
}
// send it back
pResponse->setCacheableFile(filePath, request);
}
SEXP capabilitiesX11Hook(SEXP call, SEXP op, SEXP args, SEXP rho)
{
r::sexp::Protect rProtect;
return r::sexp::create(false, &rProtect);
}
void onUserSettingsChanged()
{
// sync underlying R save action
module_context::syncRSaveAction();
// fire event notifying the client that uiPrefs changed
json::Object dataJson;
dataJson["type"] = "global";
dataJson["prefs"] = userSettings().uiPrefs();
ClientEvent event(client_events::kUiPrefsChanged, dataJson);
module_context::enqueClientEvent(event);
}
} // anonymous namespace
Error initialize()
{
// register for change notifications on user settings
userSettings().onChanged.connect(onUserSettingsChanged);
// register postback handler for viewPDF (server-only)
if (session::options().programMode() == kSessionProgramModeServer)
{
std::string pdfShellCommand ;
Error error = module_context::registerPostbackHandler("pdfviewer",
viewPdfPostback,
&pdfShellCommand);
if (error)
return error ;
// set pdfviewer option
error = r::options::setOption("pdfviewer", pdfShellCommand);
if (error)
return error ;
// ensure that capabilitiesX11 always returns false
error = r::function_hook::registerReplaceHook("capabilitiesX11",
capabilitiesX11Hook,
(CCODE*)NULL);
if (error)
return error;
}
// complete initialization
using boost::bind;
using namespace module_context;
ExecBlock initBlock ;
initBlock.addFunctions()
(bind(registerUriHandler, "/file_show", handleFileShow))
(bind(registerRpcMethod, "set_client_state", setClientState))
(bind(registerRpcMethod, "set_workbench_metrics", setWorkbenchMetrics))
(bind(registerRpcMethod, "set_prefs", setPrefs))
(bind(registerRpcMethod, "set_ui_prefs", setUiPrefs))
(bind(registerRpcMethod, "get_r_prefs", getRPrefs))
(bind(registerRpcMethod, "set_cran_mirror", setCRANMirror));
return initBlock.execute();
}
} // namepsace workbench
} // namespace modules
} // namesapce session