crowser 0.1.1

Create "desktop apps" using user-installed browsers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*! # Crowser

Create "desktop apps" using user-installed browsers.

## Main features
* ~1MB binary size with minimal dependencies (except for the browser, of course!)
* Multi-platform. Supports Windows, macOS, and Linux.
* Support for both local and remote websites.
* Maximizes performance of whatever browser is chosen, and uses an entirely separate browser profile.

**NOTE**: This library will basically forever and always be intended for low-stakes or experimental use. It's really hard to guarantee working functionality and consistency across a bunch of different browsers, so please keep that in mind!

## Browser support

All browser support comes with a few small caveats. You may notice small inconsistencies between, say, your app running in Firefox and in Chrome. Many of these are also not properly tested, so if you find any issues, feel free to [submit an issue](https://github.com/SpikeHD/Crowser/issues/choose)!

* Chrome/Chromium (stable, beta, dev, canary)
* Edge (stable, beta, dev, canary)
* Firefox (stable, beta, dev, nightly)
* Floorp
* Thorium
* Brave
* Vivaldi
* Librewolf
* Waterfox
* Mercury

# Usage

More examples can be found in the [examples](./examples) directory. Try them with `cargo run --example <example>`!

## Displaying a remote website
```rust
use crowser::{error::CrowserError, RemoteConfig, Window};

fn main() -> Result<(), CrowserError> {
  // Profile directories are specified by you, so put it wherever makes sense!
  let mut profile_dir = PathBuf::from("/path/to/your/app/profiles");

  let config = RemoteConfig {
    url: "https://example.com".to_string(),
  };

  let mut window = Window::new(config, None, profile_dir)?;

  // Make sure the profile is brand-new before launch
  window.clean_profile()?;

  // This will spawn the window and block until it is closed
  window.create()?;

  Ok(())
}
```

## Embedding a local website
```rust
use crowser::{error::CrowserError, include_dir, LocalConfig, Window};

fn main() -> Result<(), CrowserError> {
  let mut profile_dir = PathBuf::from("/path/to/your/app/profiles");

  // include_dir is a re-export of the include_dir crate
  let dir = include_dir::include_dir!("/path/to/your/app/dist");

  // To be safe, we'll try to find an open port between 9000 and 9999
  for port in 9000..9999 {
    let config = LocalConfig {
      port,
      directory: dir.clone(),
    };

    let mut window = Window::new(config, None, profile_dir.clone())?;

    window.clean_profile()?;

    // Since we're looping, we'll break when we successfully create the window. This will
    // actually block the thread until the window is closed.
    match window.create() {
      Ok(_) => {
        println!("Window created on port {}", port);
        break;
      }
      Err(e) => {
        println!("Error creating window on port {}: {:?}", port, e);
      }
    }
  }

  Ok(())
}
```

# How does it work?

On a high level, Crowser works by first detecting browser installations on the user's system (using known paths and ~~registry keys~~). Then, depending on the browser chosen, it will make some specific changes to the browser's CLI arguments,
profile directory, or both. For example, for Firefox there is a `user.js` file in all profiles that can control much of the browser's default behavior. In Chromium-based browsers, there are a stupid amount of command-line arguments that can be
used to control the browser's behavior ([check out this huge list!](https://peter.sh/experiments/chromium-command-line-switches/)).

IPC is facilitated through the [Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/). To keep the binary size small, the implementation is custom and therefore a little scuffed, but developers do not have to
care about it anyways!
*/

use std::{
  path::PathBuf,
  sync::{atomic::AtomicBool, Arc, Mutex},
};

use browser::{get_browser_path, Browser, BrowserKind};
use error::CrowserError;
use include_dir::Dir;
use shared_child::SharedChild;

pub mod browser;
mod cdp;
pub mod error;
mod ipc;
mod util;
mod webserver;

// Re-export the include_dir macro
pub use include_dir;
use webserver::{Webserver, WebserverMessage};

/// Firefox/Gecko-specific configuration options. These have no effect if the window is not a Firefox window.
#[derive(Debug)]
pub struct FirefoxConfig {
  pub custom_css: Option<String>,
}

/// Chromium-specific configuration options. These have no effect if the window is not a Chromium window.
#[derive(Debug)]
pub struct ChromiumConfig {
  pub extensions: Vec<PathBuf>,
}

#[derive(Debug, Clone)]
pub enum ContentConfig {
  Local(LocalConfig),
  Remote(RemoteConfig),
}

/// Configuration for a local (i.e bundled) website/web app
#[derive(Debug, Clone)]
pub struct LocalConfig {
  pub port: u16,
  pub directory: Dir<'static>,
}

/// Configuration for a remote (i.e hosted) website/web app
#[derive(Debug, Clone)]
pub struct RemoteConfig {
  pub url: String,
}

/// This is so the Window::new() can just be provided a LocalConfig or RemoteConfig
/// and it will automatically create the correct ContentConfig
pub trait IntoContentConfig {
  fn into_content_config(self) -> ContentConfig;
}

impl IntoContentConfig for LocalConfig {
  fn into_content_config(self) -> ContentConfig {
    ContentConfig::Local(self)
  }
}

impl IntoContentConfig for RemoteConfig {
  fn into_content_config(self) -> ContentConfig {
    ContentConfig::Remote(self)
  }
}

/// The main Window, representing a browser window
///
/// This struct is used to create and manage a browser window.
/// It contains all configuration, controls, etc. needed to control the window.
///
/// # Example
/// ```rust
/// let mut win = Window::new(RemoteConfig {
///   url: "https://example.com".to_string(),
/// }, None, PathBuf::from("/path/to/your/app/profiles"))?;
///
/// // By default, the window will be created with the "best" browser, but you can also specify a browser, or even construct a custom one!
/// win.set_browser(Browser {
///   name: "my_browser ",
///   kind: BrowserKind::Chromium,
///   win: BrowserWindowsConfig {
///     paths: vec![PathBuf::from("/path/to/my/browser/executable")],
///     registry_keys: vec![],
///   },
///   unix: vec!["my_browser", "my_browser-browser"],
///   mac: vec![PathBuf::from("/Applications/My Browser.app/Contents/MacOS/my_browser")],
/// })?;
///
/// // This will block the thread until the window is closed
/// win.create()?;
/// ```
#[derive(Debug)]
pub struct Window {
  created: bool,

  config: ContentConfig,
  browser: Browser,

  profile_directory: PathBuf,
  process_handle: Option<SharedChild>,

  // Window properties
  width: u32,
  height: u32,

  initialization_script: String,

  disable_hardware_acceleration: bool,

  firefox_config: Option<FirefoxConfig>,
  chromium_config: Option<ChromiumConfig>,

  ipc: Arc<Mutex<Option<ipc::BrowserIpc>>>,
}

impl Window {
  /// Create a new window with the specified browser engine (if any) and profile directory.
  pub fn new(
    config: impl IntoContentConfig,
    engine: Option<BrowserKind>,
    profile_directory: PathBuf,
  ) -> Result<Self, CrowserError> {
    let browser = match browser::get_best_browser(engine) {
      Some(browser) => browser,
      None => {
        return Err(CrowserError::NoBrowser(
          "No compatible browsers on system!".to_string(),
        ))
      }
    };

    Ok(Self {
      profile_directory,

      process_handle: None,

      created: false,

      config: config.into_content_config(),
      browser,

      width: 800,
      height: 600,

      initialization_script: "".to_string(),

      disable_hardware_acceleration: false,

      firefox_config: None,
      chromium_config: None,

      ipc: Arc::new(Mutex::new(None)),
    })
  }

  /// Set the remote URL for the window, if it is configured to be remote.
  pub fn set_url(&mut self, url: impl AsRef<str>) -> Result<(), CrowserError> {
    match &mut self.config {
      ContentConfig::Remote(remote) => {
        remote.url = url.as_ref().to_string();
      }
      _ => {
        return Err(CrowserError::DoAfterCreate(
          "Cannot set URL after window is created".to_string(),
        ))
      }
    }

    // TODO If we are already created, we need to send the signal to the window to change the URL
    Ok(())
  }

  /// Manually set the browser to use for the window.
  pub fn set_browser(&mut self, browser: Browser) -> Result<(), CrowserError> {
    if self.created {
      return Err(CrowserError::DoAfterCreate(
        "Cannot set browser after window is created".to_string(),
      ));
    }

    self.browser = browser;

    Ok(())
  }

  /// Set the window size
  pub fn set_size(&mut self, width: u32, height: u32) {
    self.width = width;
    self.height = height;
  }

  /// Set the initialization script for the window.
  /// This script will be run when the window is created or the contents are reloaded.
  pub fn set_initialization_script(&mut self, script: impl AsRef<str>) -> Result<(), CrowserError> {
    if self.created {
      return Err(CrowserError::DoAfterCreate(
        "Initialization script will have no effect if window is already created".to_string(),
      ));
    }

    self.initialization_script = script.as_ref().to_string();

    Ok(())
  }

  /// Get a clone of the IPC handler, so you can use it in threads without bringing along the whole Window struct.
  pub fn get_ipc(&self) -> Arc<Mutex<Option<ipc::BrowserIpc>>> {
    self.ipc.clone()
  }

  /// Disable hardware acceleration in the browser window.
  pub fn disable_hardware_acceleration(&mut self) -> Result<(), CrowserError> {
    if self.created {
      return Err(CrowserError::DoAfterCreate(
        "Changing hardware acceleration will have no effect if window is already created"
          .to_string(),
      ));
    }

    self.disable_hardware_acceleration = true;

    Ok(())
  }

  /// Set Firefox-specific configuration options. This will have no effect if the window is not a Firefox window.
  pub fn set_firefox_config(&mut self, config: FirefoxConfig) -> Result<(), CrowserError> {
    if self.created {
      return Err(CrowserError::DoAfterCreate(
        "Changing Firefox-specific configuration will have no effect if window is already created"
          .to_string(),
      ));
    }

    self.firefox_config = Some(config);

    Ok(())
  }

  /// Set Chromium-specific configuration options. This will have no effect if the window is not a Chromium window.
  pub fn set_chromium_config(&mut self, config: ChromiumConfig) -> Result<(), CrowserError> {
    if self.created {
      return Err(CrowserError::DoAfterCreate(
        "Changing Chromium-specific configuration will have no effect if window is already created"
          .to_string(),
      ));
    }

    self.chromium_config = Some(config);

    Ok(())
  }

  /// Create the window after you have provided all the necessary configuration.
  pub fn create(&mut self) -> Result<(), CrowserError> {
    self.created = true;

    let t_config = self.config.clone();
    let (w_tx, w_rx) = std::sync::mpsc::channel::<WebserverMessage>();
    let webserver_thread = std::thread::spawn(move || {
      if let ContentConfig::Local(config) = t_config {
        let webserver = Webserver::new(config.port, config.directory);

        if let Ok(webserver) = webserver {
          loop {
            // Small delay to prevent a tight loop
            std::thread::sleep(std::time::Duration::from_millis(1));

            if let Ok(WebserverMessage::Kill) = w_rx.try_recv() {
              break;
            }

            match webserver.poll_request() {
              Ok(_) => {}
              Err(err) => {
                eprintln!("Webserver error: {}", err);
                break;
              }
            };
          }
        }
      }
    });

    let browser_path = get_browser_path(&self.browser);

    if browser_path.is_none() {
      return Err(CrowserError::NoBrowser(
        "No compatible browsers on system! I don't know how you got this far...".to_string(),
      ));
    }

    let browser_path = browser_path.unwrap();

    // TODO this needs to provide CLI options and crap
    let mut cmd: std::process::Command = std::process::Command::new(browser_path);
    let mut args = match self.browser.kind {
      BrowserKind::Chromium => browser::chromium::generate_cli_options(self),
      BrowserKind::Gecko => browser::firefox::generate_cli_options(self),
      _ => {
        vec![]
      }
    };
    let remote_debugging_port = util::port::get_available_port();

    args.push("--remote-debugging-port=".to_string() + &remote_debugging_port.to_string());

    cmd.args(args);

    match self.browser.kind {
      BrowserKind::Chromium => browser::chromium::write_extra_profile_files(self)?,
      BrowserKind::Gecko => browser::firefox::write_extra_profile_files(self)?,
      _ => {}
    }

    let process = cmd.spawn()?;
    let terminated = Arc::new(AtomicBool::new(false));

    self.process_handle = Some(SharedChild::new(process)?);

    // Now that the process is running, we can start attempting to connect to it with IPC
    let ipc = ipc::BrowserIpc::new(remote_debugging_port)?;
    self.ipc.lock().unwrap().replace(ipc);

    for signal in &[signal_hook::consts::SIGINT, signal_hook::consts::SIGTERM] {
      let terminated = terminated.clone();
      signal_hook::flag::register(*signal, terminated)?;
    }

    // TODO create like an event handler and stuff
    loop {
      std::thread::sleep(std::time::Duration::from_secs(1));

      if terminated.load(std::sync::atomic::Ordering::Relaxed) {
        // Kill the process
        if let Some(child) = self.process_handle.as_ref() {
          child.kill()?;
        }

        match w_tx.send(WebserverMessage::Kill) {
          Ok(_) => {}
          Err(_) => {
            // TODO This likely means the thread is already dead
          }
        }

        webserver_thread.join()?;
        break;
      }

      // if the process is dead, break
      if let Some(child) = self.process_handle.as_ref() {
        if child.try_wait()?.is_some() {
          match w_tx.send(WebserverMessage::Kill) {
            Ok(_) => {}
            Err(_) => {
              // TODO This likely means the thread is already dead
            }
          }

          webserver_thread.join()?;
          break;
        }
      } else {
        break;
      }
    }

    // If we have broken out of the loop, the window is closed
    self.created = false;

    Ok(())
  }

  /// Force kill the window. The death of the window will be detected and kill the webserver, if running a local configuration.
  pub fn kill(&mut self) -> Result<(), CrowserError> {
    if !self.created {
      return Err(CrowserError::DoBeforeCreate(
        "Cannot kill window before it is created".to_string(),
      ));
    }

    if let Some(child) = self.process_handle.as_ref() {
      child.kill()?;
    }

    Ok(())
  }

  /// Wipe the profile directory for the window. This will remove all user data, settings, etc. for the window.
  pub fn clear_profile(&mut self) -> Result<(), CrowserError> {
    if self.created {
      return Err(CrowserError::DoAfterCreate(
        "Cannot reset profile after window is created".to_string(),
      ));
    }

    std::fs::remove_dir_all(&self.profile_directory)?;

    Ok(())
  }
}