Skip to content

Visual Studio Code, rebuilt in Rust

Rust
16.9 KB
Raw Blame
//! The workbench's side of `exthost::main_thread::MainThread`: a request the workbench answers goes to the GPUI
//! thread while the session thread waits, and what the workbench queued for the host goes out before the reply.

use std::sync::mpsc::{Receiver, Sender, TryRecvError, channel};
use std::time::Duration;

use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use exthost::extension_management::WindowExtensions;
use exthost::main_thread::{MainThread, Path, serialized_error};
use exthost::main_thread_files::{Providers, resource};
use exthost::rpc::{Arg, Reply};
use exthost::session::{CallResult, Session, URL_PROTOCOL};
use files::FileSystem;
use futures::channel::mpsc::UnboundedSender;
use futures::channel::oneshot;
use serde_json::{Value, json};

use crate::host_sync::HostMessage;
use crate::uri::Uri;
use crate::workbench::text_model_resolver::ContentProviders;

#[cfg(test)]
mod tests;

pub struct HostRequest {
    pub proxy: &'static str,
    pub method: String,
    pub args: Vec<Value>,
    pub reply: Sender<Result<Value, Value>>,
}

pub struct WorkbenchMainThread {
    pub requests: UnboundedSender<HostRequest>,
    pub messages: Receiver<HostMessage>,
    /// Host calls the workbench made with `HostSync::call`, waiting for their answer, and whether a dropped receiver
    /// still has to cancel the request.
    pub waiting: Vec<(u32, bool, oneshot::Sender<Result<Value, Value>>)>,
    pub content_providers: ContentProviders,
    pub extensions: WindowExtensions,
}

impl MainThread for WorkbenchMainThread {
    fn handle(&mut self, session: &mut Session, proxy: &'static str, method: &str, args: &[Arg]) -> Option<Result<Reply, Value>> {
        let mut args: Vec<Value> = args
            .iter()
            .map(|arg| match arg {
                Arg::Json(value) => value.clone(),
                _ => Value::Null,
            })
            .collect();
        match (proxy, method) {
            ("MainThreadDocumentContentProviders", "$registerTextContentProvider") => {
                self.content_providers.register(args[0].clone(), args[1].as_str().unwrap_or_default().to_string());
                return Some(Ok(Reply::Empty));
            }
            ("MainThreadDocumentContentProviders", "$unregisterTextContentProvider") => {
                self.content_providers.unregister(&args[0]);
                return Some(Ok(Reply::Empty));
            }
            ("MainThreadWindow", "$asExternalUri") => return Some(as_external_uri(session, &args[0])),
            // Recorded for Developer: Startup Performance; the session still handles them.
            ("MainThreadExtensionService", "$setPerformanceMarks") => {
                crate::perf::set_ext_host_marks(&args[0]);
                return None;
            }
            ("MainThreadExtensionService", "$onDidActivateExtension") => {
                crate::perf::extension_activated(&args);
                return None;
            }
            ("MainThreadExtensionService", "$onExtensionRuntimeError") => {
                crate::perf::extension_runtime_error(&args);
                return None;
            }
            // `DataTransferFileCache.resolveFileData`: the workbench names the transfer's file, its bytes are read here.
            ("MainThreadLanguageFeatures", "$resolvePasteFileData" | "$resolveDocumentOnDropFileData") => {
                return Some(self.ask(proxy, method, args).and_then(|path| {
                    std::fs::read(path.as_str().unwrap_or_default()).map(Reply::Buffer).map_err(|error| serialized_error(&error.to_string()))
                }));
            }
            _ if crate::main_thread_commands::path(proxy, method) != Some(Path::Blocking) => return None,
            _ => {}
        }
        // mainThreadFileSystem.ts reads and writes through the extension host; the session thread can call it, the GPUI thread cannot.
        // Other host requests can come without arguments; only these two carry the document uri first.
        let is_document_request = matches!(method, "$tryOpenDocument" | "$trySaveDocument");
        let scheme = if is_document_request { args[0]["scheme"].as_str().unwrap_or_default() } else { "" };
        let capabilities = if is_document_request { (Providers { session: &mut *session }).capabilities(scheme) } else { None };
        // host_requests.rs opens `vscode` documents: the default settings content lives in the workbench.
        let through_provider = scheme != "file" && scheme != "vscode" && capabilities.is_some();
        if method == "$tryOpenDocument" {
            // host_requests.rs reads the provider's bytes at 2 and whether paths ignore case at 3.
            args.resize(3, Value::Null);
            args.push(json!(capabilities.is_some_and(|capabilities| capabilities & files::PATH_CASE_SENSITIVE == 0)));
        }
        if method == "$trySaveDocument" && self.ask("workbench", "isDirty", vec![args[0].clone()]) == Ok(json!(true)) {
            participate_in_save(session, &args[0]);
        }
        let result = match (method, through_provider) {
            ("$tryOpenDocument", true) => match files::read_file(&mut Providers { session: &mut *session }, &resource(&args[0])) {
                Ok(bytes) => {
                    args[2] = json!(bytes);
                    self.ask(proxy, method, args)
                }
                Err(error) => Err(serialized_error(&format!("cannot open {}. Detail: {error}", uri_text(&args[0])))),
            },
            ("$trySaveDocument", true) => self.save_through_provider(session, &args[0]),
            ("$trySaveDocument", false) => self.ask_while_pumping(session, proxy, method, args),
            ("$tryOpenDocument", false) if self.has_content_provider(&args[0]) && self.ask("workbench", "hasModel", vec![args[0].clone()]) == Ok(json!(false)) => {
                match self.provide_text_content(session, &args[0]) {
                    Ok(text) => {
                        args[2] = json!(text);
                        self.ask(proxy, method, args)
                    }
                    Err(detail) => Err(serialized_error(&format!("cannot open {}. Detail: {detail}", uri_text(&args[0])))),
                }
            }
            _ => self.ask(proxy, method, args),
        };
        Some(result.map(|value| if value.is_null() { Reply::Empty } else { Reply::Json(value) }))
    }

    fn polls(&self) -> bool {
        self.waiting.iter().any(|(_, cancellable, _)| *cancellable)
    }

    fn flush(&mut self, session: &mut Session) {
        for message in self.messages.try_iter() {
            if let Some(work) = message.files {
                work.run(&mut Providers { session: &mut *session });
                continue;
            }
            if let Some((job, reply)) = message.extensions {
                let _ = reply.send(self.extensions.run(session, job).map_err(|error| format!("{error:#}")));
                continue;
            }
            if (message.proxy, message.method) == ("workbench", "stopExtensionHost") {
                session.host.terminate();
                continue;
            }
            if (message.proxy, message.method) == ("workbench", "setWorkspaceFolders") {
                if let Some(workspace) = session.workspace.as_mut() {
                    workspace.set_folders(message.args.iter().filter_map(Value::as_str).map(std::path::PathBuf::from).collect());
                }
                continue;
            }
            if (message.proxy, message.method) == ("workbench", "resolveTextContent") {
                let content = self.resolve_text_content(session, &message.args[0]);
                drop(message.reply.expect("resolveTextContent waits for its answer").send(content));
                continue;
            }
            if std::env::var_os("EXTHOST_TRACE").is_some() {
                eprintln!("main> {}.{} {}", message.proxy, message.method, Value::Array(message.args.clone()));
            }
            let req = if message.cancellable {
                session.notify_cancellable(message.proxy, message.method, message.args)
            } else {
                let mut args: Vec<Arg> = message.args.into_iter().map(Arg::from_value).collect();
                // extHostWebview.ts `$onMessage` reads its last argument as a SerializableObjectWithBuffers of VSBuffers,
                // which the workbench passes as base64 strings.
                if (message.proxy, message.method) == ("ExtHostWebviews", "$onMessage") && let Some(Arg::Json(encoded)) = args.pop() {
                    let buffers: Vec<Vec<u8>> = encoded.as_array().into_iter().flatten().map(|text| STANDARD.decode(text.as_str().unwrap_or_default()).unwrap_or_default()).collect();
                    let json = (0..buffers.len()).map(|index| json!({ "$$ref$$": index })).collect();
                    args.push(Arg::JsonWithBuffers { json, buffers });
                }
                // webviewViewPane.ts passes the title it was never given as `undefined`, which the view's getter returns.
                if (message.proxy, message.method) == ("ExtHostWebviewViews", "$resolveWebviewView") && matches!(args.get(2), Some(Arg::Json(Value::Null))) {
                    args[2] = Arg::Undefined;
                }
                match TOKEN_METHODS.contains(&(message.proxy, message.method)) {
                    true => session.send_cancellable_request(message.proxy, message.method, args),
                    false => session.send_request(message.proxy, message.method, args),
                }
            };
            match message.reply {
                Some(reply) => self.waiting.push((req, message.cancellable, reply)),
                None => session.forget_result(req),
            }
        }
        let mut still_waiting = Vec::new();
        for (req, cancellable, reply) in self.waiting.drain(..) {
            match session.take_result(req) {
                Some(result) => drop(reply.send(result)),
                None if cancellable && reply.is_canceled() => {
                    session.cancel(req);
                    still_waiting.push((req, false, reply));
                }
                None => still_waiting.push((req, cancellable, reply)),
            }
        }
        self.waiting = still_waiting;
    }
}

impl WorkbenchMainThread {
    /// Runs one request on the GPUI thread and waits for its answer.
    fn ask(&self, proxy: &'static str, method: &str, args: Vec<Value>) -> Result<Value, Value> {
        let (reply, receiver) = channel();
        let request = HostRequest { proxy, method: method.to_string(), args, reply };
        // The workbench is gone once its window closes; the host hears the request was canceled.
        if self.requests.unbounded_send(request).is_err() {
            return Err(serialized_error("Canceled"));
        }
        receiver.recv().unwrap_or_else(|_| Err(serialized_error("Canceled")))
    }

    /// `ask` for an answer that waits on the workbench's dialogs and host calls (a save's dialogs and format on save):
    /// the workbench's calls go out and their answers come in meanwhile; the host's own requests wait, as in `serve`.
    fn ask_while_pumping(&mut self, session: &mut Session, proxy: &'static str, method: &str, args: Vec<Value>) -> Result<Value, Value> {
        let (reply, receiver) = channel();
        let request = HostRequest { proxy, method: method.to_string(), args, reply };
        if self.requests.unbounded_send(request).is_err() {
            return Err(serialized_error("Canceled"));
        }
        loop {
            match receiver.try_recv() {
                Ok(result) => return result,
                Err(TryRecvError::Disconnected) => return Err(serialized_error("Canceled")),
                Err(TryRecvError::Empty) => {}
            }
            self.flush(session);
            if !matches!(session.pump(Duration::from_millis(20)), Ok(true)) {
                return Err(serialized_error("Canceled"));
            }
        }
    }

    fn has_content_provider(&self, uri: &Value) -> bool {
        self.content_providers.has(uri["scheme"].as_str().unwrap_or_default())
    }

    /// `ResourceModelCollection.ensureResolvedTextModelContent`: the first provider of the scheme that gives a string;
    /// a provider that fails fails the open.
    fn provide_text_content(&self, session: &mut Session, uri: &Value) -> Result<String, String> {
        for handle in self.content_providers.handles(uri["scheme"].as_str().unwrap_or_default()) {
            let args = vec![Arg::Json(handle), Arg::Json(uri.clone())];
            match session.request("ExtHostDocumentContentProviders", "$provideTextDocumentContent", args, None) {
                Ok(CallResult::Ok(Reply::Json(Value::String(text)))) => return Ok(text),
                Ok(CallResult::Ok(_)) => {}
                Ok(CallResult::Err(error)) => return Err(error.and_then(|error| error["message"].as_str().map(String::from)).unwrap_or_default()),
                Err(error) => return Err(error.to_string()),
            }
        }
        Err(format!("Unable to resolve text model content for resource {}", uri_text(uri)))
    }

    /// `ITextModelService.createModelReference` for a resource the workbench has no model of: the text of the scheme's
    /// content provider, else the bytes of its file system provider, in the form `$tryOpenDocument` reads at 2.
    fn resolve_text_content(&self, session: &mut Session, uri: &Value) -> Result<Value, Value> {
        if self.has_content_provider(uri) {
            return self.provide_text_content(session, uri).map(Value::String).map_err(|detail| serialized_error(&format!("cannot open {}. Detail: {detail}", uri_text(uri))));
        }
        match files::read_file(&mut Providers { session }, &resource(uri)) {
            Ok(bytes) => Ok(json!(bytes)),
            Err(error) => Err(serialized_error(&format!("cannot open {}. Detail: {error}", uri_text(uri)))),
        }
    }

    /// `$trySaveDocument` of a provider's document: the workbench gives the bytes, the provider writes it, the model is clean.
    fn save_through_provider(&self, session: &mut Session, uri: &Value) -> Result<Value, Value> {
        let bytes = match self.ask("workbench", "providerSaveText", vec![uri.clone()])? {
            bytes @ Value::Array(_) => serde_json::from_value::<Vec<u8>>(bytes).expect("bytes from the workbench"),
            saved_or_missing => return Ok(saved_or_missing),
        };
        if let Err(error) = files::write_file(&mut Providers { session }, &resource(uri), &bytes) {
            return Err(serialized_error(&format!("Unable to write file '{}' ({error})", uri_text(uri))));
        }
        self.ask("workbench", "providerSaved", vec![uri.clone()])
    }
}


/// Workbench calls whose host method ends with a `CancellationToken` (extHost.protocol.ts): rpcProtocol.ts sends
/// them as requests with cancellation, so the host passes a token instead of `undefined`.
const TOKEN_METHODS: [(&str, &str); 14] = [
    ("ExtHostDebugService", "$provideDebugConfigurations"),
    ("ExtHostDebugService", "$provideDebugVisualizers"),
    ("ExtHostDebugService", "$resolveDebugVisualizer"),
    ("ExtHostDebugService", "$resolveDebugConfiguration"),
    ("ExtHostDebugService", "$resolveDebugConfigurationWithSubstitutedVariables"),
    ("ExtHostTesting", "$runControllerTests"),
    ("ExtHostTesting", "$startContinuousRun"),
    ("ExtHostTesting", "$getCoverageDetails"),
    ("ExtHostTesting", "$refreshTests"),
    ("ExtHostTesting", "$getTestsRelatedToCode"),
    ("ExtHostTesting", "$getCodeRelatedToTest"),
    ("ExtHostTesting", "$provideTestFollowups"),
    ("ExtHostNotebook", "$dataToNotebook"),
    ("ExtHostNotebook", "$notebookToData"),
];

/// mainThreadSaveParticipant.ts: the host's `onWillSaveTextDocument` listeners apply their edits through
/// `$tryApplyWorkspaceEdit` while this waits; a failed or late listener is logged and the save goes on.
fn participate_in_save(session: &mut Session, uri: &Value) {
    // SaveReason.EXPLICIT
    let args = vec![uri.clone(), json!(1)];
    if session.request_cancellable("ExtHostDocumentSaveParticipant", "$participateInSave", args, Duration::from_millis(1750)).is_none() {
        eprintln!("[exthost] Aborted onWillSaveTextDocument-event after 1750ms");
    }
}

/// window.ts `resolveExternalUri` without tunnels: a resource a file system provider serves becomes an app uri that
/// opens it; nothing else resolves, and extHost.api.impl.ts then keeps an http or https uri as it is.
fn as_external_uri(session: &mut Session, uri: &Value) -> Result<Reply, Value> {
    let uri = Uri::from_json(uri).map_err(|error| serialized_error(&error))?;
    if (Providers { session }).capabilities(&uri.scheme).is_none() {
        return Err(serialized_error(&format!("Could not resolve external URI: {}", uri.to_string(false))));
    }
    Ok(Reply::Json(json!({ "$mid": 1, "scheme": URL_PROTOCOL, "path": "workspace", "query": uri.to_string(false) })))
}

fn uri_text(uri: &Value) -> String {
    let part = |key: &str| uri[key].as_str().unwrap_or_default();
    match part("authority") {
        "" => format!("{}:{}", part("scheme"), part("path")),
        authority => format!("{}://{authority}{}", part("scheme"), part("path")),
    }
}