client.rs

   1#[cfg(any(test, feature = "test-support"))]
   2pub mod test;
   3
   4pub mod channel;
   5pub mod http;
   6pub mod telemetry;
   7pub mod user;
   8
   9use anyhow::{anyhow, Context, Result};
  10use async_recursion::async_recursion;
  11use async_tungstenite::tungstenite::{
  12    error::Error as WebsocketError,
  13    http::{Request, StatusCode},
  14};
  15use db::Db;
  16use futures::{future::LocalBoxFuture, FutureExt, SinkExt, StreamExt, TryStreamExt};
  17use gpui::{
  18    actions, serde_json::Value, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle,
  19    AnyWeakViewHandle, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle,
  20    MutableAppContext, Task, View, ViewContext, ViewHandle,
  21};
  22use http::HttpClient;
  23use lazy_static::lazy_static;
  24use parking_lot::RwLock;
  25use postage::watch;
  26use rand::prelude::*;
  27use rpc::proto::{AnyTypedEnvelope, EntityMessage, EnvelopedMessage, RequestMessage};
  28use std::{
  29    any::TypeId,
  30    collections::HashMap,
  31    convert::TryFrom,
  32    fmt::Write as _,
  33    future::Future,
  34    path::PathBuf,
  35    sync::{Arc, Weak},
  36    time::{Duration, Instant},
  37};
  38use telemetry::Telemetry;
  39use thiserror::Error;
  40use url::Url;
  41use util::{ResultExt, TryFutureExt};
  42
  43pub use channel::*;
  44pub use rpc::*;
  45pub use user::*;
  46
  47lazy_static! {
  48    pub static ref ZED_SERVER_URL: String =
  49        std::env::var("ZED_SERVER_URL").unwrap_or_else(|_| "https://zed.dev".to_string());
  50    pub static ref IMPERSONATE_LOGIN: Option<String> = std::env::var("ZED_IMPERSONATE")
  51        .ok()
  52        .and_then(|s| if s.is_empty() { None } else { Some(s) });
  53}
  54
  55pub const ZED_SECRET_CLIENT_TOKEN: &str = "618033988749894";
  56
  57actions!(client, [Authenticate]);
  58
  59pub fn init(client: Arc<Client>, cx: &mut MutableAppContext) {
  60    cx.add_global_action({
  61        let client = client.clone();
  62        move |_: &Authenticate, cx| {
  63            let client = client.clone();
  64            cx.spawn(
  65                |cx| async move { client.authenticate_and_connect(true, &cx).log_err().await },
  66            )
  67            .detach();
  68        }
  69    });
  70}
  71
  72pub struct Client {
  73    id: usize,
  74    peer: Arc<Peer>,
  75    http: Arc<dyn HttpClient>,
  76    telemetry: Arc<Telemetry>,
  77    state: RwLock<ClientState>,
  78
  79    #[allow(clippy::type_complexity)]
  80    #[cfg(any(test, feature = "test-support"))]
  81    authenticate: RwLock<
  82        Option<Box<dyn 'static + Send + Sync + Fn(&AsyncAppContext) -> Task<Result<Credentials>>>>,
  83    >,
  84
  85    #[allow(clippy::type_complexity)]
  86    #[cfg(any(test, feature = "test-support"))]
  87    establish_connection: RwLock<
  88        Option<
  89            Box<
  90                dyn 'static
  91                    + Send
  92                    + Sync
  93                    + Fn(
  94                        &Credentials,
  95                        &AsyncAppContext,
  96                    ) -> Task<Result<Connection, EstablishConnectionError>>,
  97            >,
  98        >,
  99    >,
 100}
 101
 102#[derive(Error, Debug)]
 103pub enum EstablishConnectionError {
 104    #[error("upgrade required")]
 105    UpgradeRequired,
 106    #[error("unauthorized")]
 107    Unauthorized,
 108    #[error("{0}")]
 109    Other(#[from] anyhow::Error),
 110    #[error("{0}")]
 111    Http(#[from] http::Error),
 112    #[error("{0}")]
 113    Io(#[from] std::io::Error),
 114    #[error("{0}")]
 115    Websocket(#[from] async_tungstenite::tungstenite::http::Error),
 116}
 117
 118impl From<WebsocketError> for EstablishConnectionError {
 119    fn from(error: WebsocketError) -> Self {
 120        if let WebsocketError::Http(response) = &error {
 121            match response.status() {
 122                StatusCode::UNAUTHORIZED => return EstablishConnectionError::Unauthorized,
 123                StatusCode::UPGRADE_REQUIRED => return EstablishConnectionError::UpgradeRequired,
 124                _ => {}
 125            }
 126        }
 127        EstablishConnectionError::Other(error.into())
 128    }
 129}
 130
 131impl EstablishConnectionError {
 132    pub fn other(error: impl Into<anyhow::Error> + Send + Sync) -> Self {
 133        Self::Other(error.into())
 134    }
 135}
 136
 137#[derive(Copy, Clone, Debug, Eq, PartialEq)]
 138pub enum Status {
 139    SignedOut,
 140    UpgradeRequired,
 141    Authenticating,
 142    Connecting,
 143    ConnectionError,
 144    Connected { connection_id: ConnectionId },
 145    ConnectionLost,
 146    Reauthenticating,
 147    Reconnecting,
 148    ReconnectionError { next_reconnection: Instant },
 149}
 150
 151impl Status {
 152    pub fn is_connected(&self) -> bool {
 153        matches!(self, Self::Connected { .. })
 154    }
 155}
 156
 157struct ClientState {
 158    credentials: Option<Credentials>,
 159    status: (watch::Sender<Status>, watch::Receiver<Status>),
 160    entity_id_extractors: HashMap<TypeId, fn(&dyn AnyTypedEnvelope) -> u64>,
 161    _reconnect_task: Option<Task<()>>,
 162    reconnect_interval: Duration,
 163    entities_by_type_and_remote_id: HashMap<(TypeId, u64), AnyWeakEntityHandle>,
 164    models_by_message_type: HashMap<TypeId, AnyWeakModelHandle>,
 165    entity_types_by_message_type: HashMap<TypeId, TypeId>,
 166    #[allow(clippy::type_complexity)]
 167    message_handlers: HashMap<
 168        TypeId,
 169        Arc<
 170            dyn Send
 171                + Sync
 172                + Fn(
 173                    AnyEntityHandle,
 174                    Box<dyn AnyTypedEnvelope>,
 175                    &Arc<Client>,
 176                    AsyncAppContext,
 177                ) -> LocalBoxFuture<'static, Result<()>>,
 178        >,
 179    >,
 180}
 181
 182enum AnyWeakEntityHandle {
 183    Model(AnyWeakModelHandle),
 184    View(AnyWeakViewHandle),
 185}
 186
 187enum AnyEntityHandle {
 188    Model(AnyModelHandle),
 189    View(AnyViewHandle),
 190}
 191
 192#[derive(Clone, Debug)]
 193pub struct Credentials {
 194    pub user_id: u64,
 195    pub access_token: String,
 196}
 197
 198impl Default for ClientState {
 199    fn default() -> Self {
 200        Self {
 201            credentials: None,
 202            status: watch::channel_with(Status::SignedOut),
 203            entity_id_extractors: Default::default(),
 204            _reconnect_task: None,
 205            reconnect_interval: Duration::from_secs(5),
 206            models_by_message_type: Default::default(),
 207            entities_by_type_and_remote_id: Default::default(),
 208            entity_types_by_message_type: Default::default(),
 209            message_handlers: Default::default(),
 210        }
 211    }
 212}
 213
 214pub enum Subscription {
 215    Entity {
 216        client: Weak<Client>,
 217        id: (TypeId, u64),
 218    },
 219    Message {
 220        client: Weak<Client>,
 221        id: TypeId,
 222    },
 223}
 224
 225impl Drop for Subscription {
 226    fn drop(&mut self) {
 227        match self {
 228            Subscription::Entity { client, id } => {
 229                if let Some(client) = client.upgrade() {
 230                    let mut state = client.state.write();
 231                    let _ = state.entities_by_type_and_remote_id.remove(id);
 232                }
 233            }
 234            Subscription::Message { client, id } => {
 235                if let Some(client) = client.upgrade() {
 236                    let mut state = client.state.write();
 237                    let _ = state.entity_types_by_message_type.remove(id);
 238                    let _ = state.message_handlers.remove(id);
 239                }
 240            }
 241        }
 242    }
 243}
 244
 245impl Client {
 246    pub fn new(http: Arc<dyn HttpClient>, cx: &AppContext) -> Arc<Self> {
 247        Arc::new(Self {
 248            id: 0,
 249            peer: Peer::new(),
 250            telemetry: Telemetry::new(http.clone(), cx),
 251            http,
 252            state: Default::default(),
 253
 254            #[cfg(any(test, feature = "test-support"))]
 255            authenticate: Default::default(),
 256            #[cfg(any(test, feature = "test-support"))]
 257            establish_connection: Default::default(),
 258        })
 259    }
 260
 261    pub fn id(&self) -> usize {
 262        self.id
 263    }
 264
 265    pub fn http_client(&self) -> Arc<dyn HttpClient> {
 266        self.http.clone()
 267    }
 268
 269    #[cfg(any(test, feature = "test-support"))]
 270    pub fn set_id(&mut self, id: usize) -> &Self {
 271        self.id = id;
 272        self
 273    }
 274
 275    #[cfg(any(test, feature = "test-support"))]
 276    pub fn tear_down(&self) {
 277        let mut state = self.state.write();
 278        state._reconnect_task.take();
 279        state.message_handlers.clear();
 280        state.models_by_message_type.clear();
 281        state.entities_by_type_and_remote_id.clear();
 282        state.entity_id_extractors.clear();
 283        self.peer.reset();
 284    }
 285
 286    #[cfg(any(test, feature = "test-support"))]
 287    pub fn override_authenticate<F>(&self, authenticate: F) -> &Self
 288    where
 289        F: 'static + Send + Sync + Fn(&AsyncAppContext) -> Task<Result<Credentials>>,
 290    {
 291        *self.authenticate.write() = Some(Box::new(authenticate));
 292        self
 293    }
 294
 295    #[cfg(any(test, feature = "test-support"))]
 296    pub fn override_establish_connection<F>(&self, connect: F) -> &Self
 297    where
 298        F: 'static
 299            + Send
 300            + Sync
 301            + Fn(&Credentials, &AsyncAppContext) -> Task<Result<Connection, EstablishConnectionError>>,
 302    {
 303        *self.establish_connection.write() = Some(Box::new(connect));
 304        self
 305    }
 306
 307    pub fn user_id(&self) -> Option<u64> {
 308        self.state
 309            .read()
 310            .credentials
 311            .as_ref()
 312            .map(|credentials| credentials.user_id)
 313    }
 314
 315    pub fn status(&self) -> watch::Receiver<Status> {
 316        self.state.read().status.1.clone()
 317    }
 318
 319    fn set_status(self: &Arc<Self>, status: Status, cx: &AsyncAppContext) {
 320        log::info!("set status on client {}: {:?}", self.id, status);
 321        let mut state = self.state.write();
 322        *state.status.0.borrow_mut() = status;
 323
 324        match status {
 325            Status::Connected { .. } => {
 326                state._reconnect_task = None;
 327            }
 328            Status::ConnectionLost => {
 329                let this = self.clone();
 330                let reconnect_interval = state.reconnect_interval;
 331                state._reconnect_task = Some(cx.spawn(|cx| async move {
 332                    let mut rng = StdRng::from_entropy();
 333                    let mut delay = Duration::from_millis(100);
 334                    while let Err(error) = this.authenticate_and_connect(true, &cx).await {
 335                        log::error!("failed to connect {}", error);
 336                        if matches!(*this.status().borrow(), Status::ConnectionError) {
 337                            this.set_status(
 338                                Status::ReconnectionError {
 339                                    next_reconnection: Instant::now() + delay,
 340                                },
 341                                &cx,
 342                            );
 343                            cx.background().timer(delay).await;
 344                            delay = delay
 345                                .mul_f32(rng.gen_range(1.0..=2.0))
 346                                .min(reconnect_interval);
 347                        } else {
 348                            break;
 349                        }
 350                    }
 351                }));
 352            }
 353            Status::SignedOut | Status::UpgradeRequired => {
 354                self.telemetry.set_authenticated_user_info(None, false);
 355                state._reconnect_task.take();
 356            }
 357            _ => {}
 358        }
 359    }
 360
 361    pub fn add_view_for_remote_entity<T: View>(
 362        self: &Arc<Self>,
 363        remote_id: u64,
 364        cx: &mut ViewContext<T>,
 365    ) -> Subscription {
 366        let id = (TypeId::of::<T>(), remote_id);
 367        self.state
 368            .write()
 369            .entities_by_type_and_remote_id
 370            .insert(id, AnyWeakEntityHandle::View(cx.weak_handle().into()));
 371        Subscription::Entity {
 372            client: Arc::downgrade(self),
 373            id,
 374        }
 375    }
 376
 377    pub fn add_model_for_remote_entity<T: Entity>(
 378        self: &Arc<Self>,
 379        remote_id: u64,
 380        cx: &mut ModelContext<T>,
 381    ) -> Subscription {
 382        let id = (TypeId::of::<T>(), remote_id);
 383        self.state
 384            .write()
 385            .entities_by_type_and_remote_id
 386            .insert(id, AnyWeakEntityHandle::Model(cx.weak_handle().into()));
 387        Subscription::Entity {
 388            client: Arc::downgrade(self),
 389            id,
 390        }
 391    }
 392
 393    pub fn add_message_handler<M, E, H, F>(
 394        self: &Arc<Self>,
 395        model: ModelHandle<E>,
 396        handler: H,
 397    ) -> Subscription
 398    where
 399        M: EnvelopedMessage,
 400        E: Entity,
 401        H: 'static
 402            + Send
 403            + Sync
 404            + Fn(ModelHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
 405        F: 'static + Future<Output = Result<()>>,
 406    {
 407        let message_type_id = TypeId::of::<M>();
 408
 409        let mut state = self.state.write();
 410        state
 411            .models_by_message_type
 412            .insert(message_type_id, model.downgrade().into());
 413
 414        let prev_handler = state.message_handlers.insert(
 415            message_type_id,
 416            Arc::new(move |handle, envelope, client, cx| {
 417                let handle = if let AnyEntityHandle::Model(handle) = handle {
 418                    handle
 419                } else {
 420                    unreachable!();
 421                };
 422                let model = handle.downcast::<E>().unwrap();
 423                let envelope = envelope.into_any().downcast::<TypedEnvelope<M>>().unwrap();
 424                handler(model, *envelope, client.clone(), cx).boxed_local()
 425            }),
 426        );
 427        if prev_handler.is_some() {
 428            panic!("registered handler for the same message twice");
 429        }
 430
 431        Subscription::Message {
 432            client: Arc::downgrade(self),
 433            id: message_type_id,
 434        }
 435    }
 436
 437    pub fn add_request_handler<M, E, H, F>(
 438        self: &Arc<Self>,
 439        model: ModelHandle<E>,
 440        handler: H,
 441    ) -> Subscription
 442    where
 443        M: RequestMessage,
 444        E: Entity,
 445        H: 'static
 446            + Send
 447            + Sync
 448            + Fn(ModelHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
 449        F: 'static + Future<Output = Result<M::Response>>,
 450    {
 451        self.add_message_handler(model, move |handle, envelope, this, cx| {
 452            Self::respond_to_request(
 453                envelope.receipt(),
 454                handler(handle, envelope, this.clone(), cx),
 455                this,
 456            )
 457        })
 458    }
 459
 460    pub fn add_view_message_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
 461    where
 462        M: EntityMessage,
 463        E: View,
 464        H: 'static
 465            + Send
 466            + Sync
 467            + Fn(ViewHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
 468        F: 'static + Future<Output = Result<()>>,
 469    {
 470        self.add_entity_message_handler::<M, E, _, _>(move |handle, message, client, cx| {
 471            if let AnyEntityHandle::View(handle) = handle {
 472                handler(handle.downcast::<E>().unwrap(), message, client, cx)
 473            } else {
 474                unreachable!();
 475            }
 476        })
 477    }
 478
 479    pub fn add_model_message_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
 480    where
 481        M: EntityMessage,
 482        E: Entity,
 483        H: 'static
 484            + Send
 485            + Sync
 486            + Fn(ModelHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
 487        F: 'static + Future<Output = Result<()>>,
 488    {
 489        self.add_entity_message_handler::<M, E, _, _>(move |handle, message, client, cx| {
 490            if let AnyEntityHandle::Model(handle) = handle {
 491                handler(handle.downcast::<E>().unwrap(), message, client, cx)
 492            } else {
 493                unreachable!();
 494            }
 495        })
 496    }
 497
 498    fn add_entity_message_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
 499    where
 500        M: EntityMessage,
 501        E: Entity,
 502        H: 'static
 503            + Send
 504            + Sync
 505            + Fn(AnyEntityHandle, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
 506        F: 'static + Future<Output = Result<()>>,
 507    {
 508        let model_type_id = TypeId::of::<E>();
 509        let message_type_id = TypeId::of::<M>();
 510
 511        let mut state = self.state.write();
 512        state
 513            .entity_types_by_message_type
 514            .insert(message_type_id, model_type_id);
 515        state
 516            .entity_id_extractors
 517            .entry(message_type_id)
 518            .or_insert_with(|| {
 519                |envelope| {
 520                    envelope
 521                        .as_any()
 522                        .downcast_ref::<TypedEnvelope<M>>()
 523                        .unwrap()
 524                        .payload
 525                        .remote_entity_id()
 526                }
 527            });
 528        let prev_handler = state.message_handlers.insert(
 529            message_type_id,
 530            Arc::new(move |handle, envelope, client, cx| {
 531                let envelope = envelope.into_any().downcast::<TypedEnvelope<M>>().unwrap();
 532                handler(handle, *envelope, client.clone(), cx).boxed_local()
 533            }),
 534        );
 535        if prev_handler.is_some() {
 536            panic!("registered handler for the same message twice");
 537        }
 538    }
 539
 540    pub fn add_model_request_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
 541    where
 542        M: EntityMessage + RequestMessage,
 543        E: Entity,
 544        H: 'static
 545            + Send
 546            + Sync
 547            + Fn(ModelHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
 548        F: 'static + Future<Output = Result<M::Response>>,
 549    {
 550        self.add_model_message_handler(move |entity, envelope, client, cx| {
 551            Self::respond_to_request::<M, _>(
 552                envelope.receipt(),
 553                handler(entity, envelope, client.clone(), cx),
 554                client,
 555            )
 556        })
 557    }
 558
 559    pub fn add_view_request_handler<M, E, H, F>(self: &Arc<Self>, handler: H)
 560    where
 561        M: EntityMessage + RequestMessage,
 562        E: View,
 563        H: 'static
 564            + Send
 565            + Sync
 566            + Fn(ViewHandle<E>, TypedEnvelope<M>, Arc<Self>, AsyncAppContext) -> F,
 567        F: 'static + Future<Output = Result<M::Response>>,
 568    {
 569        self.add_view_message_handler(move |entity, envelope, client, cx| {
 570            Self::respond_to_request::<M, _>(
 571                envelope.receipt(),
 572                handler(entity, envelope, client.clone(), cx),
 573                client,
 574            )
 575        })
 576    }
 577
 578    async fn respond_to_request<T: RequestMessage, F: Future<Output = Result<T::Response>>>(
 579        receipt: Receipt<T>,
 580        response: F,
 581        client: Arc<Self>,
 582    ) -> Result<()> {
 583        match response.await {
 584            Ok(response) => {
 585                client.respond(receipt, response)?;
 586                Ok(())
 587            }
 588            Err(error) => {
 589                client.respond_with_error(
 590                    receipt,
 591                    proto::Error {
 592                        message: format!("{:?}", error),
 593                    },
 594                )?;
 595                Err(error)
 596            }
 597        }
 598    }
 599
 600    pub fn has_keychain_credentials(&self, cx: &AsyncAppContext) -> bool {
 601        read_credentials_from_keychain(cx).is_some()
 602    }
 603
 604    #[async_recursion(?Send)]
 605    pub async fn authenticate_and_connect(
 606        self: &Arc<Self>,
 607        try_keychain: bool,
 608        cx: &AsyncAppContext,
 609    ) -> anyhow::Result<()> {
 610        let was_disconnected = match *self.status().borrow() {
 611            Status::SignedOut => true,
 612            Status::ConnectionError
 613            | Status::ConnectionLost
 614            | Status::Authenticating { .. }
 615            | Status::Reauthenticating { .. }
 616            | Status::ReconnectionError { .. } => false,
 617            Status::Connected { .. } | Status::Connecting { .. } | Status::Reconnecting { .. } => {
 618                return Ok(())
 619            }
 620            Status::UpgradeRequired => return Err(EstablishConnectionError::UpgradeRequired)?,
 621        };
 622
 623        if was_disconnected {
 624            self.set_status(Status::Authenticating, cx);
 625        } else {
 626            self.set_status(Status::Reauthenticating, cx)
 627        }
 628
 629        let mut read_from_keychain = false;
 630        let mut credentials = self.state.read().credentials.clone();
 631        if credentials.is_none() && try_keychain {
 632            credentials = read_credentials_from_keychain(cx);
 633            read_from_keychain = credentials.is_some();
 634            if read_from_keychain {
 635                self.report_event("read credentials from keychain", Default::default());
 636            }
 637        }
 638        if credentials.is_none() {
 639            let mut status_rx = self.status();
 640            let _ = status_rx.next().await;
 641            futures::select_biased! {
 642                authenticate = self.authenticate(cx).fuse() => {
 643                    match authenticate {
 644                        Ok(creds) => credentials = Some(creds),
 645                        Err(err) => {
 646                            self.set_status(Status::ConnectionError, cx);
 647                            return Err(err);
 648                        }
 649                    }
 650                }
 651                _ = status_rx.next().fuse() => {
 652                    return Err(anyhow!("authentication canceled"));
 653                }
 654            }
 655        }
 656        let credentials = credentials.unwrap();
 657
 658        if was_disconnected {
 659            self.set_status(Status::Connecting, cx);
 660        } else {
 661            self.set_status(Status::Reconnecting, cx);
 662        }
 663
 664        match self.establish_connection(&credentials, cx).await {
 665            Ok(conn) => {
 666                self.state.write().credentials = Some(credentials.clone());
 667                if !read_from_keychain && IMPERSONATE_LOGIN.is_none() {
 668                    write_credentials_to_keychain(&credentials, cx).log_err();
 669                }
 670                self.set_connection(conn, cx).await;
 671                Ok(())
 672            }
 673            Err(EstablishConnectionError::Unauthorized) => {
 674                self.state.write().credentials.take();
 675                if read_from_keychain {
 676                    cx.platform().delete_credentials(&ZED_SERVER_URL).log_err();
 677                    self.set_status(Status::SignedOut, cx);
 678                    self.authenticate_and_connect(false, cx).await
 679                } else {
 680                    self.set_status(Status::ConnectionError, cx);
 681                    Err(EstablishConnectionError::Unauthorized)?
 682                }
 683            }
 684            Err(EstablishConnectionError::UpgradeRequired) => {
 685                self.set_status(Status::UpgradeRequired, cx);
 686                Err(EstablishConnectionError::UpgradeRequired)?
 687            }
 688            Err(error) => {
 689                self.set_status(Status::ConnectionError, cx);
 690                Err(error)?
 691            }
 692        }
 693    }
 694
 695    async fn set_connection(self: &Arc<Self>, conn: Connection, cx: &AsyncAppContext) {
 696        let executor = cx.background();
 697        log::info!("add connection to peer");
 698        let (connection_id, handle_io, mut incoming) = self
 699            .peer
 700            .add_connection(conn, move |duration| executor.timer(duration))
 701            .await;
 702        log::info!("set status to connected {}", connection_id);
 703        self.set_status(Status::Connected { connection_id }, cx);
 704        cx.foreground()
 705            .spawn({
 706                let cx = cx.clone();
 707                let this = self.clone();
 708                async move {
 709                    let mut message_id = 0_usize;
 710                    while let Some(message) = incoming.next().await {
 711                        let mut state = this.state.write();
 712                        message_id += 1;
 713                        let type_name = message.payload_type_name();
 714                        let payload_type_id = message.payload_type_id();
 715                        let sender_id = message.original_sender_id().map(|id| id.0);
 716
 717                        let model = state
 718                            .models_by_message_type
 719                            .get(&payload_type_id)
 720                            .and_then(|model| model.upgrade(&cx))
 721                            .map(AnyEntityHandle::Model)
 722                            .or_else(|| {
 723                                let entity_type_id =
 724                                    *state.entity_types_by_message_type.get(&payload_type_id)?;
 725                                let entity_id = state
 726                                    .entity_id_extractors
 727                                    .get(&message.payload_type_id())
 728                                    .map(|extract_entity_id| {
 729                                        (extract_entity_id)(message.as_ref())
 730                                    })?;
 731
 732                                let entity = state
 733                                    .entities_by_type_and_remote_id
 734                                    .get(&(entity_type_id, entity_id))?;
 735                                if let Some(entity) = entity.upgrade(&cx) {
 736                                    Some(entity)
 737                                } else {
 738                                    state
 739                                        .entities_by_type_and_remote_id
 740                                        .remove(&(entity_type_id, entity_id));
 741                                    None
 742                                }
 743                            });
 744
 745                        let model = if let Some(model) = model {
 746                            model
 747                        } else {
 748                            log::info!("unhandled message {}", type_name);
 749                            continue;
 750                        };
 751
 752                        if let Some(handler) = state.message_handlers.get(&payload_type_id).cloned()
 753                        {
 754                            drop(state); // Avoid deadlocks if the handler interacts with rpc::Client
 755                            let future = handler(model, message, &this, cx.clone());
 756
 757                            let client_id = this.id;
 758                            log::debug!(
 759                                "rpc message received. client_id:{}, message_id:{}, sender_id:{:?}, type:{}",
 760                                client_id,
 761                                message_id,
 762                                sender_id,
 763                                type_name
 764                            );
 765                            cx.foreground()
 766                                .spawn(async move {
 767                                    match future.await {
 768                                        Ok(()) => {
 769                                            log::debug!(
 770                                                "rpc message handled. client_id:{}, message_id:{}, sender_id:{:?}, type:{}",
 771                                                client_id,
 772                                                message_id,
 773                                                sender_id,
 774                                                type_name
 775                                            );
 776                                        }
 777                                        Err(error) => {
 778                                            log::error!(
 779                                                "error handling message. client_id:{}, message_id:{}, sender_id:{:?}, type:{}, error:{:?}",
 780                                                client_id,
 781                                                message_id,
 782                                                sender_id,
 783                                                type_name,
 784                                                error
 785                                            );
 786                                        }
 787                                    }
 788                                })
 789                                .detach();
 790                        } else {
 791                            log::info!("unhandled message {}", type_name);
 792                        }
 793
 794                        // Don't starve the main thread when receiving lots of messages at once.
 795                        smol::future::yield_now().await;
 796                    }
 797                }
 798            })
 799            .detach();
 800
 801        let handle_io = cx.background().spawn(handle_io);
 802        let this = self.clone();
 803        let cx = cx.clone();
 804        cx.foreground()
 805            .spawn(async move {
 806                match handle_io.await {
 807                    Ok(()) => {
 808                        if *this.status().borrow() == (Status::Connected { connection_id }) {
 809                            this.set_status(Status::SignedOut, &cx);
 810                        }
 811                    }
 812                    Err(err) => {
 813                        log::error!("connection error: {:?}", err);
 814                        this.set_status(Status::ConnectionLost, &cx);
 815                    }
 816                }
 817            })
 818            .detach();
 819    }
 820
 821    fn authenticate(self: &Arc<Self>, cx: &AsyncAppContext) -> Task<Result<Credentials>> {
 822        #[cfg(any(test, feature = "test-support"))]
 823        if let Some(callback) = self.authenticate.read().as_ref() {
 824            return callback(cx);
 825        }
 826
 827        self.authenticate_with_browser(cx)
 828    }
 829
 830    fn establish_connection(
 831        self: &Arc<Self>,
 832        credentials: &Credentials,
 833        cx: &AsyncAppContext,
 834    ) -> Task<Result<Connection, EstablishConnectionError>> {
 835        #[cfg(any(test, feature = "test-support"))]
 836        if let Some(callback) = self.establish_connection.read().as_ref() {
 837            return callback(credentials, cx);
 838        }
 839
 840        self.establish_websocket_connection(credentials, cx)
 841    }
 842
 843    fn establish_websocket_connection(
 844        self: &Arc<Self>,
 845        credentials: &Credentials,
 846        cx: &AsyncAppContext,
 847    ) -> Task<Result<Connection, EstablishConnectionError>> {
 848        let request = Request::builder()
 849            .header(
 850                "Authorization",
 851                format!("{} {}", credentials.user_id, credentials.access_token),
 852            )
 853            .header("x-zed-protocol-version", rpc::PROTOCOL_VERSION);
 854
 855        let http = self.http.clone();
 856        cx.background().spawn(async move {
 857            let mut rpc_url = format!("{}/rpc", *ZED_SERVER_URL);
 858            let rpc_response = http.get(&rpc_url, Default::default(), false).await?;
 859            if rpc_response.status().is_redirection() {
 860                rpc_url = rpc_response
 861                    .headers()
 862                    .get("Location")
 863                    .ok_or_else(|| anyhow!("missing location header in /rpc response"))?
 864                    .to_str()
 865                    .map_err(EstablishConnectionError::other)?
 866                    .to_string();
 867            }
 868            // Until we switch the zed.dev domain to point to the new Next.js app, there
 869            // will be no redirect required, and the app will connect directly to
 870            // wss://zed.dev/rpc.
 871            else if rpc_response.status() != StatusCode::UPGRADE_REQUIRED {
 872                Err(anyhow!(
 873                    "unexpected /rpc response status {}",
 874                    rpc_response.status()
 875                ))?
 876            }
 877
 878            let mut rpc_url = Url::parse(&rpc_url).context("invalid rpc url")?;
 879            let rpc_host = rpc_url
 880                .host_str()
 881                .zip(rpc_url.port_or_known_default())
 882                .ok_or_else(|| anyhow!("missing host in rpc url"))?;
 883            let stream = smol::net::TcpStream::connect(rpc_host).await?;
 884
 885            log::info!("connected to rpc endpoint {}", rpc_url);
 886
 887            match rpc_url.scheme() {
 888                "https" => {
 889                    rpc_url.set_scheme("wss").unwrap();
 890                    let request = request.uri(rpc_url.as_str()).body(())?;
 891                    let (stream, _) =
 892                        async_tungstenite::async_tls::client_async_tls(request, stream).await?;
 893                    Ok(Connection::new(
 894                        stream
 895                            .map_err(|error| anyhow!(error))
 896                            .sink_map_err(|error| anyhow!(error)),
 897                    ))
 898                }
 899                "http" => {
 900                    rpc_url.set_scheme("ws").unwrap();
 901                    let request = request.uri(rpc_url.as_str()).body(())?;
 902                    let (stream, _) = async_tungstenite::client_async(request, stream).await?;
 903                    Ok(Connection::new(
 904                        stream
 905                            .map_err(|error| anyhow!(error))
 906                            .sink_map_err(|error| anyhow!(error)),
 907                    ))
 908                }
 909                _ => Err(anyhow!("invalid rpc url: {}", rpc_url))?,
 910            }
 911        })
 912    }
 913
 914    pub fn authenticate_with_browser(
 915        self: &Arc<Self>,
 916        cx: &AsyncAppContext,
 917    ) -> Task<Result<Credentials>> {
 918        let platform = cx.platform();
 919        let executor = cx.background();
 920        let telemetry = self.telemetry.clone();
 921        executor.clone().spawn(async move {
 922            // Generate a pair of asymmetric encryption keys. The public key will be used by the
 923            // zed server to encrypt the user's access token, so that it can'be intercepted by
 924            // any other app running on the user's device.
 925            let (public_key, private_key) =
 926                rpc::auth::keypair().expect("failed to generate keypair for auth");
 927            let public_key_string =
 928                String::try_from(public_key).expect("failed to serialize public key for auth");
 929
 930            // Start an HTTP server to receive the redirect from Zed's sign-in page.
 931            let server = tiny_http::Server::http("127.0.0.1:0").expect("failed to find open port");
 932            let port = server.server_addr().port();
 933
 934            // Open the Zed sign-in page in the user's browser, with query parameters that indicate
 935            // that the user is signing in from a Zed app running on the same device.
 936            let mut url = format!(
 937                "{}/native_app_signin?native_app_port={}&native_app_public_key={}",
 938                *ZED_SERVER_URL, port, public_key_string
 939            );
 940
 941            if let Some(impersonate_login) = IMPERSONATE_LOGIN.as_ref() {
 942                log::info!("impersonating user @{}", impersonate_login);
 943                write!(&mut url, "&impersonate={}", impersonate_login).unwrap();
 944            }
 945
 946            platform.open_url(&url);
 947
 948            // Receive the HTTP request from the user's browser. Retrieve the user id and encrypted
 949            // access token from the query params.
 950            //
 951            // TODO - Avoid ever starting more than one HTTP server. Maybe switch to using a
 952            // custom URL scheme instead of this local HTTP server.
 953            let (user_id, access_token) = executor
 954                .spawn(async move {
 955                    for _ in 0..100 {
 956                        if let Some(req) = server.recv_timeout(Duration::from_secs(1))? {
 957                            let path = req.url();
 958                            let mut user_id = None;
 959                            let mut access_token = None;
 960                            let url = Url::parse(&format!("http://example.com{}", path))
 961                                .context("failed to parse login notification url")?;
 962                            for (key, value) in url.query_pairs() {
 963                                if key == "access_token" {
 964                                    access_token = Some(value.to_string());
 965                                } else if key == "user_id" {
 966                                    user_id = Some(value.to_string());
 967                                }
 968                            }
 969
 970                            let post_auth_url =
 971                                format!("{}/native_app_signin_succeeded", *ZED_SERVER_URL);
 972                            req.respond(
 973                                tiny_http::Response::empty(302).with_header(
 974                                    tiny_http::Header::from_bytes(
 975                                        &b"Location"[..],
 976                                        post_auth_url.as_bytes(),
 977                                    )
 978                                    .unwrap(),
 979                                ),
 980                            )
 981                            .context("failed to respond to login http request")?;
 982                            return Ok((
 983                                user_id.ok_or_else(|| anyhow!("missing user_id parameter"))?,
 984                                access_token
 985                                    .ok_or_else(|| anyhow!("missing access_token parameter"))?,
 986                            ));
 987                        }
 988                    }
 989
 990                    Err(anyhow!("didn't receive login redirect"))
 991                })
 992                .await?;
 993
 994            let access_token = private_key
 995                .decrypt_string(&access_token)
 996                .context("failed to decrypt access token")?;
 997            platform.activate(true);
 998
 999            telemetry.report_event("authenticate with browser", Default::default());
1000
1001            Ok(Credentials {
1002                user_id: user_id.parse()?,
1003                access_token,
1004            })
1005        })
1006    }
1007
1008    pub fn disconnect(self: &Arc<Self>, cx: &AsyncAppContext) -> Result<()> {
1009        let conn_id = self.connection_id()?;
1010        self.peer.disconnect(conn_id);
1011        self.set_status(Status::SignedOut, cx);
1012        Ok(())
1013    }
1014
1015    fn connection_id(&self) -> Result<ConnectionId> {
1016        if let Status::Connected { connection_id, .. } = *self.status().borrow() {
1017            Ok(connection_id)
1018        } else {
1019            Err(anyhow!("not connected"))
1020        }
1021    }
1022
1023    pub fn send<T: EnvelopedMessage>(&self, message: T) -> Result<()> {
1024        log::debug!("rpc send. client_id:{}, name:{}", self.id, T::NAME);
1025        self.peer.send(self.connection_id()?, message)
1026    }
1027
1028    pub fn request<T: RequestMessage>(
1029        &self,
1030        request: T,
1031    ) -> impl Future<Output = Result<T::Response>> {
1032        let client_id = self.id;
1033        log::debug!(
1034            "rpc request start. client_id:{}. name:{}",
1035            client_id,
1036            T::NAME
1037        );
1038        let response = self
1039            .connection_id()
1040            .map(|conn_id| self.peer.request(conn_id, request));
1041        async move {
1042            let response = response?.await;
1043            log::debug!(
1044                "rpc request finish. client_id:{}. name:{}",
1045                client_id,
1046                T::NAME
1047            );
1048            response
1049        }
1050    }
1051
1052    fn respond<T: RequestMessage>(&self, receipt: Receipt<T>, response: T::Response) -> Result<()> {
1053        log::debug!("rpc respond. client_id:{}. name:{}", self.id, T::NAME);
1054        self.peer.respond(receipt, response)
1055    }
1056
1057    fn respond_with_error<T: RequestMessage>(
1058        &self,
1059        receipt: Receipt<T>,
1060        error: proto::Error,
1061    ) -> Result<()> {
1062        log::debug!("rpc respond. client_id:{}. name:{}", self.id, T::NAME);
1063        self.peer.respond_with_error(receipt, error)
1064    }
1065
1066    pub fn start_telemetry(&self, db: Arc<Db>) {
1067        self.telemetry.start(db);
1068    }
1069
1070    pub fn report_event(&self, kind: &str, properties: Value) {
1071        self.telemetry.report_event(kind, properties)
1072    }
1073
1074    pub fn telemetry_log_file_path(&self) -> Option<PathBuf> {
1075        self.telemetry.log_file_path()
1076    }
1077}
1078
1079impl AnyWeakEntityHandle {
1080    fn upgrade(&self, cx: &AsyncAppContext) -> Option<AnyEntityHandle> {
1081        match self {
1082            AnyWeakEntityHandle::Model(handle) => handle.upgrade(cx).map(AnyEntityHandle::Model),
1083            AnyWeakEntityHandle::View(handle) => handle.upgrade(cx).map(AnyEntityHandle::View),
1084        }
1085    }
1086}
1087
1088fn read_credentials_from_keychain(cx: &AsyncAppContext) -> Option<Credentials> {
1089    if IMPERSONATE_LOGIN.is_some() {
1090        return None;
1091    }
1092
1093    let (user_id, access_token) = cx
1094        .platform()
1095        .read_credentials(&ZED_SERVER_URL)
1096        .log_err()
1097        .flatten()?;
1098    Some(Credentials {
1099        user_id: user_id.parse().ok()?,
1100        access_token: String::from_utf8(access_token).ok()?,
1101    })
1102}
1103
1104fn write_credentials_to_keychain(credentials: &Credentials, cx: &AsyncAppContext) -> Result<()> {
1105    cx.platform().write_credentials(
1106        &ZED_SERVER_URL,
1107        &credentials.user_id.to_string(),
1108        credentials.access_token.as_bytes(),
1109    )
1110}
1111
1112const WORKTREE_URL_PREFIX: &str = "zed://worktrees/";
1113
1114pub fn encode_worktree_url(id: u64, access_token: &str) -> String {
1115    format!("{}{}/{}", WORKTREE_URL_PREFIX, id, access_token)
1116}
1117
1118pub fn decode_worktree_url(url: &str) -> Option<(u64, String)> {
1119    let path = url.trim().strip_prefix(WORKTREE_URL_PREFIX)?;
1120    let mut parts = path.split('/');
1121    let id = parts.next()?.parse::<u64>().ok()?;
1122    let access_token = parts.next()?;
1123    if access_token.is_empty() {
1124        return None;
1125    }
1126    Some((id, access_token.to_string()))
1127}
1128
1129#[cfg(test)]
1130mod tests {
1131    use super::*;
1132    use crate::test::{FakeHttpClient, FakeServer};
1133    use gpui::{executor::Deterministic, TestAppContext};
1134    use parking_lot::Mutex;
1135    use std::future;
1136
1137    #[gpui::test(iterations = 10)]
1138    async fn test_reconnection(cx: &mut TestAppContext) {
1139        cx.foreground().forbid_parking();
1140
1141        let user_id = 5;
1142        let client = cx.update(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
1143        let server = FakeServer::for_client(user_id, &client, cx).await;
1144        let mut status = client.status();
1145        assert!(matches!(
1146            status.next().await,
1147            Some(Status::Connected { .. })
1148        ));
1149        assert_eq!(server.auth_count(), 1);
1150
1151        server.forbid_connections();
1152        server.disconnect();
1153        while !matches!(status.next().await, Some(Status::ReconnectionError { .. })) {}
1154
1155        server.allow_connections();
1156        cx.foreground().advance_clock(Duration::from_secs(10));
1157        while !matches!(status.next().await, Some(Status::Connected { .. })) {}
1158        assert_eq!(server.auth_count(), 1); // Client reused the cached credentials when reconnecting
1159
1160        server.forbid_connections();
1161        server.disconnect();
1162        while !matches!(status.next().await, Some(Status::ReconnectionError { .. })) {}
1163
1164        // Clear cached credentials after authentication fails
1165        server.roll_access_token();
1166        server.allow_connections();
1167        cx.foreground().advance_clock(Duration::from_secs(10));
1168        while !matches!(status.next().await, Some(Status::Connected { .. })) {}
1169        assert_eq!(server.auth_count(), 2); // Client re-authenticated due to an invalid token
1170    }
1171
1172    #[gpui::test(iterations = 10)]
1173    async fn test_authenticating_more_than_once(
1174        cx: &mut TestAppContext,
1175        deterministic: Arc<Deterministic>,
1176    ) {
1177        cx.foreground().forbid_parking();
1178
1179        let auth_count = Arc::new(Mutex::new(0));
1180        let dropped_auth_count = Arc::new(Mutex::new(0));
1181        let client = cx.update(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
1182        client.override_authenticate({
1183            let auth_count = auth_count.clone();
1184            let dropped_auth_count = dropped_auth_count.clone();
1185            move |cx| {
1186                let auth_count = auth_count.clone();
1187                let dropped_auth_count = dropped_auth_count.clone();
1188                cx.foreground().spawn(async move {
1189                    *auth_count.lock() += 1;
1190                    let _drop = util::defer(move || *dropped_auth_count.lock() += 1);
1191                    future::pending::<()>().await;
1192                    unreachable!()
1193                })
1194            }
1195        });
1196
1197        let _authenticate = cx.spawn(|cx| {
1198            let client = client.clone();
1199            async move { client.authenticate_and_connect(false, &cx).await }
1200        });
1201        deterministic.run_until_parked();
1202        assert_eq!(*auth_count.lock(), 1);
1203        assert_eq!(*dropped_auth_count.lock(), 0);
1204
1205        let _authenticate = cx.spawn(|cx| {
1206            let client = client.clone();
1207            async move { client.authenticate_and_connect(false, &cx).await }
1208        });
1209        deterministic.run_until_parked();
1210        assert_eq!(*auth_count.lock(), 2);
1211        assert_eq!(*dropped_auth_count.lock(), 1);
1212    }
1213
1214    #[test]
1215    fn test_encode_and_decode_worktree_url() {
1216        let url = encode_worktree_url(5, "deadbeef");
1217        assert_eq!(decode_worktree_url(&url), Some((5, "deadbeef".to_string())));
1218        assert_eq!(
1219            decode_worktree_url(&format!("\n {}\t", url)),
1220            Some((5, "deadbeef".to_string()))
1221        );
1222        assert_eq!(decode_worktree_url("not://the-right-format"), None);
1223    }
1224
1225    #[gpui::test]
1226    async fn test_subscribing_to_entity(cx: &mut TestAppContext) {
1227        cx.foreground().forbid_parking();
1228
1229        let user_id = 5;
1230        let client = cx.update(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
1231        let server = FakeServer::for_client(user_id, &client, cx).await;
1232
1233        let (done_tx1, mut done_rx1) = smol::channel::unbounded();
1234        let (done_tx2, mut done_rx2) = smol::channel::unbounded();
1235        client.add_model_message_handler(
1236            move |model: ModelHandle<Model>, _: TypedEnvelope<proto::JoinProject>, _, cx| {
1237                match model.read_with(&cx, |model, _| model.id) {
1238                    1 => done_tx1.try_send(()).unwrap(),
1239                    2 => done_tx2.try_send(()).unwrap(),
1240                    _ => unreachable!(),
1241                }
1242                async { Ok(()) }
1243            },
1244        );
1245        let model1 = cx.add_model(|_| Model {
1246            id: 1,
1247            subscription: None,
1248        });
1249        let model2 = cx.add_model(|_| Model {
1250            id: 2,
1251            subscription: None,
1252        });
1253        let model3 = cx.add_model(|_| Model {
1254            id: 3,
1255            subscription: None,
1256        });
1257
1258        let _subscription1 = model1.update(cx, |_, cx| client.add_model_for_remote_entity(1, cx));
1259        let _subscription2 = model2.update(cx, |_, cx| client.add_model_for_remote_entity(2, cx));
1260        // Ensure dropping a subscription for the same entity type still allows receiving of
1261        // messages for other entity IDs of the same type.
1262        let subscription3 = model3.update(cx, |_, cx| client.add_model_for_remote_entity(3, cx));
1263        drop(subscription3);
1264
1265        server.send(proto::JoinProject { project_id: 1 });
1266        server.send(proto::JoinProject { project_id: 2 });
1267        done_rx1.next().await.unwrap();
1268        done_rx2.next().await.unwrap();
1269    }
1270
1271    #[gpui::test]
1272    async fn test_subscribing_after_dropping_subscription(cx: &mut TestAppContext) {
1273        cx.foreground().forbid_parking();
1274
1275        let user_id = 5;
1276        let client = cx.update(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
1277        let server = FakeServer::for_client(user_id, &client, cx).await;
1278
1279        let model = cx.add_model(|_| Model::default());
1280        let (done_tx1, _done_rx1) = smol::channel::unbounded();
1281        let (done_tx2, mut done_rx2) = smol::channel::unbounded();
1282        let subscription1 = client.add_message_handler(
1283            model.clone(),
1284            move |_, _: TypedEnvelope<proto::Ping>, _, _| {
1285                done_tx1.try_send(()).unwrap();
1286                async { Ok(()) }
1287            },
1288        );
1289        drop(subscription1);
1290        let _subscription2 =
1291            client.add_message_handler(model, move |_, _: TypedEnvelope<proto::Ping>, _, _| {
1292                done_tx2.try_send(()).unwrap();
1293                async { Ok(()) }
1294            });
1295        server.send(proto::Ping {});
1296        done_rx2.next().await.unwrap();
1297    }
1298
1299    #[gpui::test]
1300    async fn test_dropping_subscription_in_handler(cx: &mut TestAppContext) {
1301        cx.foreground().forbid_parking();
1302
1303        let user_id = 5;
1304        let client = cx.update(|cx| Client::new(FakeHttpClient::with_404_response(), cx));
1305        let server = FakeServer::for_client(user_id, &client, cx).await;
1306
1307        let model = cx.add_model(|_| Model::default());
1308        let (done_tx, mut done_rx) = smol::channel::unbounded();
1309        let subscription = client.add_message_handler(
1310            model.clone(),
1311            move |model, _: TypedEnvelope<proto::Ping>, _, mut cx| {
1312                model.update(&mut cx, |model, _| model.subscription.take());
1313                done_tx.try_send(()).unwrap();
1314                async { Ok(()) }
1315            },
1316        );
1317        model.update(cx, |model, _| {
1318            model.subscription = Some(subscription);
1319        });
1320        server.send(proto::Ping {});
1321        done_rx.next().await.unwrap();
1322    }
1323
1324    #[derive(Default)]
1325    struct Model {
1326        id: usize,
1327        subscription: Option<Subscription>,
1328    }
1329
1330    impl Entity for Model {
1331        type Event = ();
1332    }
1333}