Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rust/crates/plugin-rpc/src/failures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub fn host_rpc_code(code: &str) -> i64 {
// -32072 to -32077 are the toolchain provider protocol's.
codes::HOST_REMOTE_DISCONNECTED => -32078,
codes::HOST_PLATFORM_UNAVAILABLE => -32079,
codes::HOST_ALREADY_EXISTS => -32080,
codes::HOST_DISK_CHANGED => -32081,
_ => -32603,
}
}
Expand Down Expand Up @@ -91,6 +93,8 @@ pub fn host_failure(code: Option<&str>) -> (&'static str, i64) {
Some(codes::HOST_FOREIGN_CONTRIBUTION) => codes::HOST_FOREIGN_CONTRIBUTION,
Some(codes::HOST_REMOTE_DISCONNECTED) => codes::HOST_REMOTE_DISCONNECTED,
Some(codes::HOST_PLATFORM_UNAVAILABLE) => codes::HOST_PLATFORM_UNAVAILABLE,
Some(codes::HOST_ALREADY_EXISTS) => codes::HOST_ALREADY_EXISTS,
Some(codes::HOST_DISK_CHANGED) => codes::HOST_DISK_CHANGED,
// A backend exit is reported to pages, never to the backend itself.
Some(codes::BACKEND_EXITED) => codes::HOST_FAILED,
_ => codes::HOST_FAILED,
Expand Down
50 changes: 47 additions & 3 deletions rust/crates/plugin-rpc/src/host_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ pub(crate) enum HostParams {
/// `plugin.host.invalidParams` or, for a field it does not know,
/// `plugin.host.unsupportedOption` itself; for methods whose params
/// Core cannot type without owning their domain.
// No row uses it yet; the file, editor, and window services that follow
// add their rows with it.
#[allow(dead_code)]
Object,
}

Expand Down Expand Up @@ -268,6 +265,33 @@ pub(crate) const HOST_METHODS: &[HostMethod] = &[
permission: Some("workspace.search"),
params: HostParams::Object,
},
// Plan API-11: file changes the host makes on the backend's behalf. The
// host never overwrites a file, saves an open document before it moves
// or trashes its file, and moves to the trash instead of deleting.
HostMethod {
method: "lithe/host/workspace/create",
service: "workspace",
permission: Some(PERMISSION_WORKSPACE_FILES_WRITE),
params: HostParams::Object,
},
HostMethod {
method: "lithe/host/workspace/rename",
service: "workspace",
permission: Some(PERMISSION_WORKSPACE_FILES_WRITE),
params: HostParams::Object,
},
HostMethod {
method: "lithe/host/workspace/copy",
service: "workspace",
permission: Some(PERMISSION_WORKSPACE_FILES_WRITE),
params: HostParams::Object,
},
HostMethod {
method: "lithe/host/workspace/trash",
service: "workspace",
permission: Some(PERMISSION_WORKSPACE_FILES_WRITE),
params: HostParams::Object,
},
HostMethod {
method: "lithe/host/documents/list",
service: "documents",
Expand All @@ -280,6 +304,26 @@ pub(crate) const HOST_METHODS: &[HostMethod] = &[
permission: Some("documents.read"),
params: HostParams::Object,
},
// Plan API-11: saving and closing an open document through its owner,
// which keeps the document's version, dirty state and undo history.
HostMethod {
method: "lithe/host/documents/save",
service: "documents",
permission: Some(PERMISSION_DOCUMENTS_SAVE),
params: HostParams::Object,
},
HostMethod {
method: "lithe/host/documents/saveAs",
service: "documents",
permission: Some(PERMISSION_DOCUMENTS_SAVE),
params: HostParams::Object,
},
HostMethod {
method: "lithe/host/documents/close",
service: "documents",
permission: Some(PERMISSION_DOCUMENTS_SAVE),
params: HostParams::Object,
},
HostMethod {
method: "lithe/host/editor/registerActions",
service: "editor",
Expand Down
113 changes: 113 additions & 0 deletions rust/crates/plugin-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ pub const HOST_PERMISSIONS: &[&str] = &[
"debug.start",
PERMISSION_LANGUAGE_SERVER_REQUEST,
PERMISSION_DOCUMENTS_CONTENT_PROVIDER,
PERMISSION_DOCUMENTS_SAVE,
PERMISSION_WORKSPACE_FILES_WRITE,
];

/// Permission of the `languageServer` host service (plan API-25): a backend
Expand All @@ -168,6 +170,17 @@ pub const PERMISSION_LANGUAGE_SERVER_REQUEST: &str = "languageServer.request";
/// backend supplies the read-only text of URIs of a scheme it registers.
pub(crate) const PERMISSION_DOCUMENTS_CONTENT_PROVIDER: &str = "documents.contentProvider";

/// Permission of `documents/save`, `saveAs` and `close` (plan API-11): the
/// backend asks the document's owner to write or close an open document. A
/// close saves first; the owner never discards the user's edits for it.
pub(crate) const PERMISSION_DOCUMENTS_SAVE: &str = "documents.save";

/// Permission of `workspace/create`, `rename`, `copy` and `trash` (plan
/// API-11): the backend changes files inside the context's root folders.
/// None of them overwrites a file, and `trash` moves to the system trash
/// instead of deleting.
pub(crate) const PERMISSION_WORKSPACE_FILES_WRITE: &str = "workspace.files.write";

const CREDENTIALS_GET: &str = "lithe/host/credentials/get";

const CREDENTIALS_SET: &str = "lithe/host/credentials/set";
Expand Down Expand Up @@ -309,6 +322,14 @@ pub mod codes {
/// The plugin has no package or managed runtime for the remote host's
/// platform, so the process the request needs cannot run there.
pub const HOST_PLATFORM_UNAVAILABLE: &str = "plugin.host.platformUnavailable";
/// The file or folder the request would create, or the target of a
/// rename, copy or save as, already exists. The host never overwrites
/// it.
pub const HOST_ALREADY_EXISTS: &str = "plugin.host.alreadyExists";
/// The file on disk changed, or was deleted, after the document last
/// read or wrote it, so a save would overwrite a change the document
/// does not hold. Nothing was written; the user settles the conflict.
pub const HOST_DISK_CHANGED: &str = "plugin.host.diskChanged";
/// A page's call or a host operation ended because the backend process
/// exited. Reported by hosts to pages, never sent to a backend.
pub const BACKEND_EXITED: &str = "plugin.backend.exited";
Expand Down Expand Up @@ -866,6 +887,98 @@ mod tests {
.contains(&json!(WORKSPACE_GET_HOST_PATHS)));
}

/// Saving and file changes are method grants of their own (plan
/// API-11): a session granted the `workspace` service, or only reading,
/// never writes, and each new failure code has a number of its own.
#[test]
fn saves_and_file_changes_are_method_grants_of_their_own() {
assert_eq!(
methods_for_permission(PERMISSION_DOCUMENTS_SAVE),
[
"lithe/host/documents/save",
"lithe/host/documents/saveAs",
"lithe/host/documents/close",
]
);
let writes = methods_for_permission(PERMISSION_WORKSPACE_FILES_WRITE);
assert_eq!(
writes,
[
"lithe/host/workspace/create",
"lithe/host/workspace/rename",
"lithe/host/workspace/copy",
"lithe/host/workspace/trash",
]
);
let context = json!({ "contextID": "w-3f2a", "generation": 2 });
let params = json!({ "context": context, "resource": { "rootID": "r0", "path": "a.txt" } });
let reading = BTreeSet::from([
HOST_SERVICE_WORKSPACE.to_string(),
"lithe/host/workspace/readFile".to_string(),
]);
for method in &writes {
assert!(is_host_grant(method), "{method}");
assert_eq!(
host_request(method, Some(&params), Some(0), &reading)
.unwrap_err()
.code,
codes::HOST_PERMISSION_DENIED,
"{method}"
);
let granted = BTreeSet::from([method.to_string()]);
assert_eq!(
host_request(method, Some(&params), Some(0), &granted),
Ok(HostRoute::Forward(HOST_SERVICE_WORKSPACE)),
"{method}"
);
}
let save = BTreeSet::from(["lithe/host/documents/save".to_string()]);
assert_eq!(
host_request(
"lithe/host/documents/save",
Some(&json!({ "context": context, "documentID": "d1" })),
Some(0),
&save,
),
Ok(HostRoute::Forward("documents"))
);
// Every code a host may report keeps a number of its own.
let host_codes = [
codes::HOST_UNAVAILABLE,
codes::HOST_INVALID_PARAMS,
codes::HOST_CANCELLED,
codes::HOST_PERMISSION_DENIED,
codes::HOST_CONTEXT_EXPIRED,
codes::HOST_AUTHORITY_UNSUPPORTED,
codes::HOST_VERSION_CONFLICT,
codes::HOST_LIMIT_EXCEEDED,
codes::HOST_TIMEOUT,
codes::HOST_NOT_FOUND,
codes::HOST_READ_ONLY,
codes::HOST_UNSUPPORTED_OPTION,
codes::HOST_PRECONDITION_FAILED,
codes::HOST_BUSY,
codes::HOST_RATE_LIMITED,
codes::HOST_LANGUAGE_SERVER_UNAVAILABLE,
codes::HOST_LANGUAGE_SERVER_ERROR,
codes::HOST_METHOD_NOT_DECLARED,
codes::HOST_TOOL_UNRESOLVED,
codes::HOST_FOREIGN_CONTRIBUTION,
codes::HOST_REMOTE_DISCONNECTED,
codes::HOST_PLATFORM_UNAVAILABLE,
codes::HOST_ALREADY_EXISTS,
codes::HOST_DISK_CHANGED,
codes::HOST_FAILED,
];
for code in host_codes {
assert_eq!(host_failure(Some(code)).0, code, "{code}");
}
let numbers: BTreeSet<i64> = host_codes.iter().map(|code| host_rpc_code(code)).collect();
assert_eq!(numbers.len(), host_codes.len());
assert_eq!(host_rpc_code(codes::HOST_ALREADY_EXISTS), -32080);
assert_eq!(host_rpc_code(codes::HOST_DISK_CHANGED), -32081);
}

/// A message over the bound is cut at a character boundary, never inside
/// a multi-byte character.
#[test]
Expand Down
20 changes: 20 additions & 0 deletions rust/fixtures/plugins/host-api-v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@
"input": { "method": "lithe/host/workspace/getHostPaths", "service": "workspace", "grants": ["workspace", "lithe/host/workspace/getHostPaths"] },
"expected": { "allowed": true }
},
{
"name": "a read grant does not grant file changes",
"input": { "method": "lithe/host/workspace/create", "service": "workspace", "grants": ["workspace", "lithe/host/workspace/readFile"] },
"expected": { "allowed": false, "code": "plugin.host.permissionDenied" }
},
{
"name": "the file-change grant admits its own method",
"input": { "method": "lithe/host/workspace/trash", "service": "workspace", "grants": ["lithe/host/workspace/trash"] },
"expected": { "allowed": true }
},
{
"name": "a document read grant does not grant saving",
"input": { "method": "lithe/host/documents/save", "service": "documents", "grants": ["lithe/host/documents/getSnapshot"] },
"expected": { "allowed": false, "code": "plugin.host.permissionDenied" }
},
{
"name": "the save grant admits saving",
"input": { "method": "lithe/host/documents/save", "service": "documents", "grants": ["lithe/host/documents/save"] },
"expected": { "allowed": true }
},
{
"name": "a method cannot be attributed to another service",
"input": { "method": "lithe/host/editor/applyEdits", "service": "workspace", "grants": ["workspace"] },
Expand Down
Loading
Loading