Visual Studio Code, rebuilt in Rust
Rust
//! Protocol urls (`code-oss://`): app.ts `handleProtocolUrl` for the urls that open a file or folder, then the
//! window's url handlers: extensionUrlHandler.ts `ExtensionUrlHandler` and the handlers of mainThreadUrls.ts.
use std::path::PathBuf;
use std::time::{Duration, Instant};
pub use exthost::session::URL_PROTOCOL;
use gpui::{AsyncWindowContext, Context, PromptLevel, WeakEntity, Window};
use serde_json::{Value, json};
use super::Workbench;
use super::opener::UriOpener;
use super::workspace_actions::Openable;
use crate::commands::Outcome;
use crate::dialog;
use crate::quick_input::path_label;
use crate::uri::Uri;
/// extensionUrlHandler.ts `USER_TRUSTED_EXTENSIONS_STORAGE_KEY`, in the profile storage.
const CONFIRMED_EXTENSIONS_KEY: &str = "extensionUrlHandler.confirmedExtensions";
/// Buffered urls of an extension that never registers a handler are dropped after five minutes.
const BUFFER_LIFETIME: Duration = Duration::from_secs(5 * 60);
#[derive(Default)]
pub struct Urls {
pub openers: Vec<UriOpener>,
pub(super) handlers: Vec<UriHandler>,
/// Urls for extensions that were activated to register their handler.
buffer: Vec<(String, Instant, Uri)>,
}
pub(super) struct UriHandler {
handle: u64,
/// `ExtensionIdentifier.toKey`.
extension: String,
display_name: String,
}
impl Workbench {
/// `$registerUriHandler`: urls buffered while the extension activated go to it now.
pub(crate) fn register_uri_handler(&mut self, handle: u64, extension_id: &str, display_name: &str) {
let extension = extension_id.to_lowercase();
self.urls.handlers.retain(|handler| handler.extension != extension);
self.urls.handlers.push(UriHandler { handle, extension: extension.clone(), display_name: display_name.to_owned() });
let buffered = std::mem::take(&mut self.urls.buffer);
for (owner, time, uri) in buffered {
if owner != extension {
self.urls.buffer.push((owner, time, uri));
} else if time.elapsed() < BUFFER_LIFETIME {
self.host.send("ExtHostUrls", "$handleExternalUri", vec![json!(handle), uri.to_json()]);
}
}
}
pub(crate) fn unregister_uri_handler(&mut self, handle: u64) {
self.urls.handlers.retain(|handler| handler.handle != handle);
}
/// `ElectronURLListener`: a url the system hands the app, or `--open-url`.
pub fn open_protocol_url(&mut self, url: &str, window: &mut Window, cx: &mut Context<Self>) {
let Ok(uri) = Uri::parse(url) else { return };
if uri.scheme != URL_PROTOCOL {
return;
}
cx.spawn_in(window, async move |workbench, cx| {
handle_url(&workbench, uri, false, cx).await;
}).detach();
}
}
/// app.ts `open-file`: Electron hands the documents one by one, so the ones that follow within 100 ms open together.
pub(crate) async fn with_following_documents(mut urls: Vec<String>, following: &mut (impl futures::Stream<Item = Vec<String>> + Unpin), cx: &gpui::AsyncApp) -> Vec<String> {
if !urls.iter().any(|url| url.starts_with("file:")) {
return urls;
}
loop {
let timeout = cx.background_executor().timer(Duration::from_millis(100));
match futures::future::select(futures::StreamExt::next(following), timeout).await {
futures::future::Either::Left((Some(more), _)) => urls.extend(more),
_ => return urls,
}
}
}
/// app.ts `open-file`: the documents Finder or the Dock hands the app open as the command line's paths would
/// (`start_open_window` in the `DOCK` context), any other url as `open-url`.
pub(crate) fn open_system_urls(urls: Vec<String>, cx: &mut gpui::App) {
let mut args = vec![String::new()];
for url in urls {
match Uri::parse(&url) {
// An absolute `fsPath` starts with `/` or a drive letter, so it never reads as a flag.
Ok(uri) if uri.scheme == "file" => args.push(uri.fs_path()),
_ => open_system_url(url, cx),
}
}
if args.len() > 1 {
super::host::start_open_window(args, PathBuf::from("/"), true, false, cx);
}
}
/// electron-main app.ts `open-url`: the last active window handles a url the system hands the running app; with no
/// window, a new one opens with it.
pub(crate) fn open_system_url(url: String, cx: &mut gpui::App) {
// The stack is ordered front to back but can be empty (no window ordered in yet): any window then.
let last_active = cx.window_stack().unwrap_or_default().into_iter().chain(cx.windows()).find_map(|window| window.downcast::<Workbench>());
match last_active {
Some(handle) => drop(handle.update(cx, |workbench, window, cx| {
window.activate_window();
workbench.open_protocol_url(&url, window, cx);
})),
None => crate::open_window_or_log(crate::WindowRequest { open_url: Some(url), ..crate::WindowRequest::default() }, cx),
}
}
/// urlService.ts `open`: answers whether a handler took the url.
pub(super) async fn handle_url(workbench: &WeakEntity<Workbench>, uri: Uri, trusted: bool, cx: &mut AsyncWindowContext) -> bool {
let mut uri = uri;
// Support 'workspace' URLs (https://github.com/microsoft/vscode/issues/124263)
if uri.path == "workspace" {
let path = Uri::parse(&uri.query).map(|inner| inner.path).unwrap_or_default();
let Ok(file_url) = uri.with(|uri| {
uri.authority = "file".into();
uri.path = path;
uri.query.clear();
}) else {
return false;
};
uri = file_url;
}
let query: Vec<&str> = uri.query.split('&').filter(|param| !param.is_empty()).collect();
let new_window = query.contains(&"windowId=_blank");
uri.query = query.into_iter().filter(|param| *param != "windowId=_blank").collect::<Vec<_>>().join("&");
if uri.authority == "file" && !uri.path.is_empty() {
return open_file_url(workbench, &uri.path, new_window, cx).await;
}
handle_extension_url(workbench, uri, trusted, cx).await
}
/// `getWindowOpenableFromProtocolUrl` and `shouldBlockOpenable` for a `file` url, opened in `gotoLineMode`.
async fn open_file_url(workbench: &WeakEntity<Workbench>, raw_path: &str, new_window: bool, cx: &mut AsyncWindowContext) -> bool {
let (path, line) = parse_line_and_column(raw_path);
// `URI.file(uri.fsPath)`: `/C:/a` is `c:\a` on Windows.
let path = PathBuf::from(Uri::file(&path).fs_path());
let openable = Openable::of(path.clone());
// A protocol url opens a workspace file, or a file or folder (`fileUri`).
let what = match path.extension().is_some_and(|extension| extension == "code-workspace") {
true => "this workspace file",
false => "this file or folder",
};
let label = path_label(&path, None);
let ask = workbench.update(cx, |workbench, _| workbench.setting("security.promptForLocalFileProtocolHandling") != json!(false)).unwrap_or(true);
if ask {
let message = format!("An external application wants to open '{label}' in {}. Do you want to open {what}?", crate::product::name_long());
let detail = "If you did not initiate this request, it may represent an attempted attack on your system. Unless you took an explicit action to initiate this request, you should press 'No'";
let Ok(answer) = workbench.update_in(cx, |_, window, cx| dialog::show_message_box(window, crate::dialog::InTests::Show, PromptLevel::Warning, &message, Some(detail), &["Yes", "No"], Some("Allow opening local paths without asking"), cx)) else {
return true;
};
let Ok((0, checked)) = answer.await else { return true };
if checked {
let _ = workbench.update(cx, |workbench, cx| {
workbench.update_setting("security.promptForLocalFileProtocolHandling", json!(false));
cx.notify();
});
}
}
let _ = workbench.update_in(cx, |workbench, window, cx| {
let at = line.map(|(line, column)| (line as u32, column as u32));
workbench.open_window(vec![openable], at, new_window, window, cx);
});
true
}
/// extpath.ts `parseLineAndColumnAware`: the column defaults to 1 when there is a line.
pub(super) fn parse_line_and_column(raw_path: &str) -> (String, Option<(f64, f64)>) {
let mut path: Option<String> = None;
let (mut line, mut column) = (None, None);
for segment in raw_path.split(':') {
// `Number('')` is 0, so an empty segment counts as a number too.
let number = match segment.trim() {
"" => Some(0.),
trimmed => trimmed.parse::<f64>().ok().filter(|number| !number.is_nan()),
};
match number {
None => path = Some(path.map_or(segment.to_owned(), |path| format!("{path}:{segment}"))),
Some(number) if line.is_none() => line = Some(number),
Some(number) if column.is_none() => column = Some(number),
Some(_) => {}
}
}
(path.unwrap_or_default(), line.map(|line| (line, column.unwrap_or(1.))))
}
/// `ExtensionUrlHandler.handleURL` followed by the extension's own handler.
async fn handle_extension_url(workbench: &WeakEntity<Workbench>, uri: Uri, trusted: bool, cx: &mut AsyncWindowContext) -> bool {
if !is_extension_id(&uri.authority) {
return false;
}
let extension = uri.authority.to_lowercase();
let Ok((handler, display_name, confirmed)) = workbench.update(cx, |workbench, _| {
let handler = workbench.urls.handlers.iter().find(|handler| handler.extension == extension);
let installed = workbench.extensions.iter().find(|installed| installed.id.eq_ignore_ascii_case(&extension));
let installed_name = installed.map(|installed| installed.description["displayName"].as_str().unwrap_or(&installed.id).to_owned());
let display_name = handler.map(|handler| handler.display_name.clone()).or(installed_name);
(handler.map(|handler| handler.handle), display_name, workbench.did_user_trust_extension(&extension))
}) else {
return false;
};
let Some(display_name) = display_name else {
if install(workbench, &uri, cx).await {
Box::pin(handle_extension_url(workbench, uri, true, cx)).await;
}
return true;
};
if !trusted && !confirmed {
let text = uri.to_string(false);
let chars: Vec<char> = text.chars().collect();
let label = match chars.len() > 40 {
true => format!("{}...{}", chars[..30].iter().collect::<String>(), chars[chars.len() - 5..].iter().collect::<String>()),
false => text,
};
let message = format!("Allow '{display_name}' extension to open this URI?");
let Ok(answer) = workbench.update_in(cx, |_, window, cx| dialog::show_message_box(window, crate::dialog::InTests::Confirm, PromptLevel::Info, &message, Some(&label), &["Open", "Cancel"], Some("Do not ask me again for this extension"), cx)) else {
return true;
};
let Ok((0, checked)) = answer.await else { return true };
if checked {
let _ = workbench.update(cx, |workbench, _| workbench.confirm_extension(&extension));
}
}
let _ = workbench.update(cx, |workbench, _| match handler {
Some(handle) => workbench.host.send("ExtHostUrls", "$handleExternalUri", vec![json!(handle), uri.to_json()]),
None => {
workbench.urls.buffer.push((extension.clone(), Instant::now(), uri));
// ActivationKind.Immediate
drop(workbench.host.call("ExtHostExtensionService", "$activateByEvent", vec![json!(format!("onUri:{extension}")), json!(1)]));
}
});
true
}
/// `handleUnhandledURL` up to the install: true once the extension runs, so the URI is handled again as trusted.
/// A cancelled install prompt ends here; a failed one shows its error.
async fn install(workbench: &WeakEntity<Workbench>, uri: &Uri, cx: &mut AsyncWindowContext) -> bool {
let justification = json!({ "reason": format!("This extension wants to open a URI:\n{}", uri.to_string(false)), "action": "Open URI" });
let args = vec![json!(uri.authority), json!({ "justification": justification, "enable": true, "installPreReleaseVersion": !crate::product::is_stable() })];
let Ok(outcome) = workbench.update_in(cx, |workbench, window, cx| workbench.execute_command("workbench.extensions.installExtension", args, window, cx)) else {
return false;
};
let installed = match outcome {
Outcome::Ready(result) => result.is_ok(),
Outcome::Pending(pending) => matches!(pending.await, Ok(Ok(_))),
};
installed && workbench.update(cx, |workbench, _| workbench.extensions.iter().any(|extension| extension.id.eq_ignore_ascii_case(&uri.authority))).unwrap_or(false)
}
fn is_extension_id(value: &str) -> bool {
let part = |part: &str| part.chars().next().is_some_and(|first| first.is_ascii_alphanumeric()) && part.chars().all(|char| char.is_ascii_alphanumeric() || char == '-');
value.split_once('.').is_some_and(|(publisher, name)| part(publisher) && part(name))
}
impl Workbench {
/// `didUserTrustExtension`: confirmed once with the checkbox, or listed in `extensions.confirmedUriHandlerExtensionIds`.
fn did_user_trust_extension(&self, extension: &str) -> bool {
let confirmed = self.confirmed_extensions();
let configured = self.setting("extensions.confirmedUriHandlerExtensionIds");
confirmed.iter().any(|id| id == extension) || configured.as_array().is_some_and(|ids| ids.iter().any(|id| id == extension))
}
fn confirmed_extensions(&self) -> Vec<String> {
let storage = self.profile_storage.lock().unwrap();
storage.get(CONFIRMED_EXTENSIONS_KEY).and_then(|ids| serde_json::from_str(ids).ok()).unwrap_or_default()
}
fn confirm_extension(&mut self, extension: &str) {
let mut confirmed = self.confirmed_extensions();
confirmed.push(extension.to_owned());
let value = Value::from(confirmed).to_string();
self.profile_storage.lock().unwrap().set(CONFIRMED_EXTENSIONS_KEY, value);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn url_path(path: &std::path::Path) -> String {
Uri::file(&path.to_string_lossy()).path
}
/// The path a file url opens: `fsPath` lowercases a Windows drive letter.
fn fs_path_of(path: &std::path::Path) -> PathBuf {
PathBuf::from(Uri::file(&path.to_string_lossy()).fs_path())
}
#[test]
fn parse_line_and_column_matches_extpath() {
assert_eq!(parse_line_and_column("/a/b.ts:10:5"), ("/a/b.ts".into(), Some((10., 5.))));
assert_eq!(parse_line_and_column("/a/b.ts:10"), ("/a/b.ts".into(), Some((10., 1.))));
assert_eq!(parse_line_and_column("/a/b.ts"), ("/a/b.ts".into(), None));
assert_eq!(parse_line_and_column("/a:b/c.ts:3"), ("/a:b/c.ts".into(), Some((3., 1.))));
}
#[gpui::test]
fn a_url_the_system_hands_the_app_opens_in_the_last_active_window(cx: &mut gpui::TestAppContext) {
let app_root = std::path::Path::new(env!("VSCODE_SRC")).to_path_buf();
let theme = crate::theme::load_theme(&[exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("vsrustium-open-url-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("User")).unwrap();
let file = dir.join("notes.txt");
std::fs::write(&file, "one\ntwo\n").unwrap();
let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), settings, crate::host_sync::HostSync::new(std::sync::mpsc::channel().0), cx));
workbench.update(cx, |workbench, _| workbench.update_setting("security.promptForLocalFileProtocolHandling", json!(false)));
cx.cx.update(|cx| open_system_url(format!("{URL_PROTOCOL}://file{}:2:1", url_path(&file)), cx));
cx.run_until_parked();
let opened = workbench.read_with(cx, |workbench, _| workbench.editors.active().map(|editor| (editor.document().path.clone(), editor.selections[0].start().line)));
assert_eq!(opened, Some((fs_path_of(&file), 1)));
std::fs::remove_dir_all(dir).unwrap();
}
/// app.ts `open-file`: a `.code-workspace` file Finder hands the running app opens in a window of its folders.
#[gpui::test]
fn a_workspace_file_from_finder_opens_a_window_of_its_folders(cx: &mut gpui::TestAppContext) {
let app_root = std::path::Path::new(env!("VSCODE_SRC")).to_path_buf();
let theme = crate::theme::load_theme(&[exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("vsrustium-open-file-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
for folder in ["User", "one", "two", "three"] {
std::fs::create_dir_all(dir.join(folder)).unwrap();
}
let workspace_file = dir.join("my team.code-workspace");
std::fs::write(&workspace_file, r#"{ "folders": [{ "path": "one" }, { "path": "two" }, { "path": "three" }] }"#).unwrap();
let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), settings, crate::host_sync::HostSync::new(std::sync::mpsc::channel().0), cx));
// NSURL `absoluteString`, as gpui hands it over.
let url = Uri::file(&workspace_file.to_string_lossy()).to_string(false);
cx.cx.update(|cx| open_system_urls(vec![url], cx));
cx.run_until_parked();
let windows: Vec<gpui::Entity<Workbench>> = cx.cx.update(|cx| cx.windows().into_iter().filter_map(|handle| handle.downcast::<Workbench>()).filter_map(|handle| handle.entity(cx).ok()).collect());
let opened: Vec<Vec<PathBuf>> = windows.iter().filter(|other| **other != workbench).filter_map(|other| other.read_with(cx, |other, _| other.workspace().map(|workspace| workspace.folders.iter().map(|folder| folder.path.clone()).collect()))).collect();
assert_eq!(opened, [["one", "two", "three"].map(|folder| fs_path_of(&dir.join(folder))).to_vec()]);
std::fs::remove_dir_all(dir).unwrap();
}
/// windowsMainService.ts `shouldOpenNewWindow`: on macOS `window.openFilesInNewWindow` `default` opens the files of
/// the `DOCK` context in a new window, `off` (its default value) in the last active one.
#[gpui::test]
fn a_file_from_finder_opens_in_a_new_window_with_open_files_in_new_window_default(cx: &mut gpui::TestAppContext) {
let app_root = std::path::Path::new(env!("VSCODE_SRC")).to_path_buf();
let theme = crate::theme::load_theme(&[exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("vsrustium-finder-file-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("User")).unwrap();
let file = dir.join("notes.txt");
std::fs::write(&file, "one\n").unwrap();
let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), settings, crate::host_sync::HostSync::new(std::sync::mpsc::channel().0), cx));
let open_from_finder = |cx: &mut gpui::VisualTestContext| {
cx.cx.update(|cx| open_system_urls(vec![Uri::file(&file.to_string_lossy()).to_string(false)], cx));
cx.run_until_parked();
};
workbench.update(cx, |workbench, _| workbench.update_setting("window.openFilesInNewWindow", json!("default")));
open_from_finder(cx);
assert_eq!(cx.cx.update(|cx| cx.windows().len()), 2, "a new window opens the file");
assert!(workbench.read_with(cx, |workbench, _| workbench.editors.active().is_none()));
workbench.update(cx, |workbench, _| workbench.update_setting("window.openFilesInNewWindow", json!("off")));
cx.cx.update(|cx| cx.windows().into_iter().find(|handle| handle.downcast::<Workbench>().is_some_and(|handle| handle.entity(cx).is_ok_and(|other| other != workbench))).unwrap().update(cx, |_, window, _| window.remove_window()).unwrap());
open_from_finder(cx);
assert_eq!(cx.cx.update(|cx| cx.windows().len()), 1);
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.editors.active().map(|editor| editor.document().path.clone())), Some(fs_path_of(&file)));
std::fs::remove_dir_all(dir).unwrap();
}
/// app.ts `open-file`: documents that follow within 100 ms open with the first; a later one on its own.
#[gpui::test]
fn documents_that_follow_within_100_ms_open_together(cx: &mut gpui::TestAppContext) {
let (sender, mut following) = futures::channel::mpsc::unbounded::<Vec<String>>();
sender.unbounded_send(vec!["file:///b".into()]).unwrap();
let burst = cx.spawn(|cx| async move {
let first = with_following_documents(vec!["file:///a".into()], &mut following, &cx).await;
let next = futures::StreamExt::next(&mut following).await.unwrap();
(first, next)
});
cx.run_until_parked();
cx.executor().advance_clock(Duration::from_millis(50));
sender.unbounded_send(vec!["file:///c".into()]).unwrap();
cx.run_until_parked();
cx.executor().advance_clock(Duration::from_millis(100));
cx.run_until_parked();
sender.unbounded_send(vec!["file:///d".into()]).unwrap();
let (first, next) = cx.foreground_executor().block_test(burst);
assert_eq!(first, ["file:///a", "file:///b", "file:///c"]);
assert_eq!(next, ["file:///d"]);
}
/// app.ts `handleProtocolUrl`: `windowId=_blank` opens the file in a new window (`forceNewWindow`), at its line.
#[gpui::test]
fn a_file_url_for_a_new_window_opens_the_file_there(cx: &mut gpui::TestAppContext) {
let app_root = std::path::Path::new(env!("VSCODE_SRC")).to_path_buf();
let theme = crate::theme::load_theme(&[exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("vsrustium-url-new-window-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("User")).unwrap();
let file = dir.join("notes.txt");
std::fs::write(&file, "one\ntwo\n").unwrap();
let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), settings, crate::host_sync::HostSync::new(std::sync::mpsc::channel().0), cx));
workbench.update(cx, |workbench, _| workbench.update_setting("security.promptForLocalFileProtocolHandling", json!(false)));
let url = format!("{URL_PROTOCOL}://file{}:2:1?windowId=_blank", url_path(&file));
workbench.update_in(cx, |workbench, window, cx| workbench.open_protocol_url(&url, window, cx));
cx.run_until_parked();
assert!(workbench.read_with(cx, |workbench, _| workbench.editors.active().is_none()), "this window opens nothing");
let others: Vec<gpui::Entity<Workbench>> = cx.cx.update(|cx| cx.windows().into_iter().filter_map(|handle| handle.downcast::<Workbench>()).filter_map(|handle| handle.entity(cx).ok()).collect());
let opened: Vec<_> = others.iter().filter(|other| **other != workbench).map(|other| other.read_with(cx, |other, _| other.editors.active().map(|editor| (editor.document().path.clone(), editor.selections[0].start().line)))).collect();
assert_eq!(opened, [Some((fs_path_of(&file), 1))]);
std::fs::remove_dir_all(dir).unwrap();
}
/// mainThreadWebviews.ts `onDidClickLink`: a webview's `code-oss:` link goes to the url service, as the opener's do.
#[gpui::test]
fn a_webview_app_link_goes_to_the_url_service(cx: &mut gpui::TestAppContext) {
let app_root = std::path::Path::new(env!("VSCODE_SRC")).to_path_buf();
let theme = crate::theme::load_theme(&[exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("vsrustium-webview-url-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("User")).unwrap();
let file = dir.join("notes.txt");
std::fs::write(&file, "one\ntwo\n").unwrap();
let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), settings, crate::host_sync::HostSync::new(std::sync::mpsc::channel().0), cx));
workbench.update(cx, |workbench, _| workbench.update_setting("security.promptForLocalFileProtocolHandling", json!(false)));
let link = format!("{URL_PROTOCOL}://file{}:2:1", url_path(&file));
workbench.update_in(cx, |workbench, window, cx| workbench.open_webview_link(&link, &json!(false), window, cx));
cx.run_until_parked();
let opened = workbench.read_with(cx, |workbench, _| workbench.editors.active().map(|editor| editor.document().path.clone()));
assert_eq!(opened, Some(fs_path_of(&file)));
std::fs::remove_dir_all(dir).unwrap();
}
#[gpui::test]
fn a_webview_link_reaches_the_handler_the_extension_registered(cx: &mut gpui::TestAppContext) {
let app_root = std::path::Path::new(env!("VSCODE_SRC")).to_path_buf();
let theme = crate::theme::load_theme(&[exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("vsrustium-webview-handler-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("User")).unwrap();
let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
let (sender, receiver) = std::sync::mpsc::channel();
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), settings, crate::host_sync::HostSync::new(sender), cx));
workbench.update_in(cx, |workbench, window, cx| {
workbench.register_uri_handler(7, "Pub.Ext", "Pub Ext");
workbench.confirm_extension("pub.ext");
workbench.open_webview_link(&format!("{URL_PROTOCOL}://pub.ext/callback?code=1"), &json!(false), window, cx);
});
cx.run_until_parked();
let handled: Vec<Value> = receiver.try_iter().filter(|message| message.method == "$handleExternalUri").map(|message| message.args[0].clone()).collect();
assert_eq!(handled, [json!(7)]);
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn extension_ids_are_publisher_dot_name() {
assert!(is_extension_id("vscode.git"));
assert!(is_extension_id("ms-python.python"));
assert!(!is_extension_id("file"));
assert!(!is_extension_id("a.b.c"));
assert!(!is_extension_id("-a.b"));
}
}