nw_sys/
app.rs

1//!
2//! App bindings providing application control, access to command-line arguments,
3//! working directory, paths for the browser data folders, cache control, origin control,
4//! global keyboard hotkey registration, proxy control, underlaying browser control and
5//! `package.json` manifest access.
6//!
7
8use crate::result::Result;
9use crate::shortcut::Shortcut;
10use js_sys::{Array, Function, Object, RegExp};
11use wasm_bindgen::prelude::*;
12
13#[wasm_bindgen]
14extern "C" {
15
16    #[wasm_bindgen(js_namespace=nw, js_name = App)]
17    type NwApp;
18
19    /// (Internal) Get the filtered command line arguments when starting the app.
20    /// In NW.js, some command line arguments are used by NW.js,
21    /// which should not be interested of your app. App.argv will filter out
22    /// those arguments and return the ones left. You can get filtered patterns
23    /// from [app::filtered_argv](self::filtered_argv) and the full arguments from [app::full_argv](self::full_argv).
24    ///
25    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appargv)
26    ///
27    #[wasm_bindgen(getter, static_method_of=NwApp, js_namespace=nw, js_class=App, js_name = argv)]
28    pub fn argv_impl() -> Array;
29
30    /// (Internal) Get all the command line arguments when starting the app.
31    /// The return values contains the arguments used by NW.js,
32    /// such as --nwapp, --remote-debugging-port etc.
33    ///
34    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appfullargv)
35    #[wasm_bindgen(getter, static_method_of=NwApp, js_namespace=nw, js_class=App, js_name = fullArgv)]
36    pub fn full_argv_impl() -> Array;
37
38    /// (Internal) Get a list of patterns of filtered command line arguments used by App.argv.
39    /// By default, following patterns are used to filter the arguments:
40    /// ```
41    /// [
42    ///     /^--url=/,
43    ///     /^--remote-debugging-port=/,
44    ///     /^--renderer-cmd-prefix=/,
45    ///     /^--nwapp=/
46    /// ]
47    /// ```
48    ///
49    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appfilteredargv)
50    #[wasm_bindgen(getter, static_method_of=NwApp, js_namespace=nw, js_class=App, js_name = filteredArgv)]
51    pub fn filtered_argv_impl() -> Array;
52
53    #[wasm_bindgen(getter, static_method_of=NwApp, js_namespace=nw, js_class=App, js_name = startPath)]
54    fn start_path() -> String;
55
56    #[wasm_bindgen(getter, static_method_of=NwApp, js_namespace=nw, js_class=App, js_name = dataPath)]
57    fn data_path() -> String;
58
59    #[wasm_bindgen(getter, static_method_of=NwApp, js_namespace=nw, js_class=App, js_name = manifest)]
60    fn manifest() -> Object;
61
62    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = clearCache)]
63    /// Clear the HTTP cache in memory and the one on disk.
64    /// This method call is synchronized.
65    ///
66    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appclearcache)
67    ///
68    pub fn clear_cache();
69
70    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = clearAppCache)]
71    /// Mark the Application cache group specified by `manifest_url` obsolete.
72    /// This method call is synchronized.
73    ///
74    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appclearappcachemanifest_url)
75    ///
76    pub fn clear_app_cache(manifest_url: &str);
77
78    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = closeAllWindows)]
79    /// Send the close event to all windows of current app,
80    /// if no window is blocking the close event, then the app will quit after
81    /// all windows have done shutdown. Use this method to quit an app
82    /// will give windows a chance to save data.
83    ///
84    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appcloseallwindows)
85    ///
86    pub fn close_all_windows();
87
88    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = crashBrowser)]
89    /// Crashes the browser process to test
90    /// the [Crash dump](https://docs.nwjs.io/en/latest/For%20Developers/Understanding%20Crash%20Dump/) feature.
91    ///
92    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appcrashbrowser)
93    ///
94    pub fn crash_browser();
95
96    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = crashRenderer)]
97    /// Crashes the renderer process to test
98    /// the [Crash dump](https://docs.nwjs.io/en/latest/For%20Developers/Understanding%20Crash%20Dump/) feature.
99    ///
100    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appcrashrenderer)
101    ///
102    pub fn crash_renderer();
103
104    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = enableComponent)]
105    /// Experimental
106    ///
107    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appenablecomponentcomponent-callback)
108    ///
109    pub fn enable_component(component: &str, callback: &Function);
110
111    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = getProxyForURL)]
112    /// Query the proxy to be used for loading `url` in DOM.
113    /// The return value is in the same format used in
114    /// PAC (e.g. "DIRECT", "PROXY localhost:8080").
115    ///
116    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appgetproxyforurlurl)
117    ///
118    fn get_proxy_for_url(url: &str) -> String;
119
120    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = setProxyConfig)]
121    /// Set the proxy config which the web engine will be used to request
122    /// network resources or PAC url to detect proxy automatically.
123    ///
124    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appsetproxyconfigconfig-pac_url)
125    ///
126    pub fn set_proxy_config(config: &str, pac_url: &str);
127
128    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = quit)]
129    /// Quit current app.
130    /// This method will not send `close` event to windows and app will
131    /// just quit quietly.
132    ///
133    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appquit)
134    ///
135    pub fn quit();
136
137    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = setCrashDumpDir)]
138    /// Deprecated: Set the directory where the minidump
139    /// file will be saved on crash. For more information,
140    /// see [Crash dump](https://docs.nwjs.io/en/latest/For%20Developers/Understanding%20Crash%20Dump/).
141    ///
142    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appsetcrashdumpdirdir)
143    ///
144    pub fn set_crash_dump_dir(dir: &str);
145
146    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = addOriginAccessWhitelistEntry)]
147    /// Add an entry to the whitelist used for controlling cross-origin access.
148    /// Suppose you want to allow HTTP redirecting from github.com to
149    /// the page of your app, use something like this:
150    ///
151    /// ```
152    /// nw_sys::app::add_origin_access_whitelist_entry(
153    ///     "http://github.com/",
154    ///     "chrome-extension",
155    ///     "domain.com",
156    ///     true
157    /// );
158    /// ```
159    /// Use [nw_sys::app::remove_origin_access_whitelist_entry()](self::remove_origin_access_whitelist_entry) with exactly the
160    /// same arguments to do the contrary.
161    ///
162    /// - `source_origin`: The source origin. e.g. http://github.com/
163    /// - `destination_protocol`: The destination protocol where the `source_origin` can access to. e.g. `app`
164    /// - `destination_host`: The destination host where the `source_origin` can access to. e.g. `myapp`
165    /// - `allow_destination_subdomains`: If set to true, the `source_origin` is allowed to access subdomains of destinations.
166    ///
167    ///
168    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appaddoriginaccesswhitelistentrysourceorigin-destinationprotocol-destinationhost-allowdestinationsubdomains)
169    ///
170    pub fn add_origin_access_whitelist_entry(
171        source_origin: &str,
172        destination_protocol: &str,
173        destination_host: &str,
174        allow_destination_subdomains: bool,
175    );
176
177    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = removeOriginAccessWhitelistEntry)]
178    /// Remove an entry from the whitelist used for controlling cross-origin access.
179    /// See [nw_sys::app::add_origin_access_whitelist_entry()](self::add_origin_access_whitelist_entry).
180    ///
181    ///
182    /// - `source_origin`: The source origin. e.g. http://github.com/
183    /// - `destination_protocol`: The destination protocol where the `source_origin` can access to. e.g. `app`
184    /// - `destination_host`: The destination host where the `source_origin` can access to. e.g. `myapp`
185    /// - `allow_destination_subdomains`: If set to true, the `source_origin` is allowed to access subdomains of destinations.
186    ///
187    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appremoveoriginaccesswhitelistentrysourceorigin-destinationprotocol-destinationhost-allowdestinationsubdomains)
188    ///
189    pub fn remove_origin_access_whitelist_entry(
190        source_origin: &str,
191        destination_protocol: &str,
192        destination_host: &str,
193        allow_destination_subdomains: bool,
194    );
195
196    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = registerGlobalHotKey)]
197    /// Register a global keyboard shortcut (also known as system-wide hot key)
198    /// to the system.
199    ///
200    /// See [Shortcut](crate::shortcut) for more information.
201    ///
202    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appregisterglobalhotkeyshortcut)
203    ///
204    pub fn register_global_hot_key(shortcut: &Shortcut);
205
206    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = unregisterGlobalHotKey)]
207    /// Unregisters a global keyboard shortcut.
208    ///
209    /// See [Shortcut](crate::shortcut) for more information.
210    ///
211    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appunregisterglobalhotkeyshortcut)
212    ///
213    pub fn unregister_global_hot_key(shortcut: &Shortcut);
214
215    #[wasm_bindgen(js_namespace=["nw", "App"], js_name = updateComponent)]
216    /// Experimental
217    ///
218    /// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appupdatecomponentcomponent-callback)
219    ///
220    pub fn update_component(component: &str, callback: &Function);
221
222    // Event: open(args:String)
223    // args: the full command line of the program.
224    // Emitted when users opened a file with your app.
225
226    // Event: reopen
227    // This is a Mac specific feature.
228    // This event is sent when the user clicks the dock icon for an
229    // already running application.
230
231}
232
233fn build_argv_str(argv: Array) -> Result<Vec<String>> {
234    let mut list = Vec::new();
235    for index in 0..argv.length() {
236        if let Some(v) = argv.get(index).as_string() {
237            list.push(v);
238        }
239    }
240
241    Ok(list)
242}
243
244fn build_argv_filters(argv: Array) -> Result<Vec<RegExp>> {
245    let mut list = Vec::new();
246    for index in 0..argv.length() {
247        let a = argv.get(index);
248        let v = RegExp::from(a);
249        list.push(v);
250        /*
251        match argv.get(index).as_string(){
252            Some(v)=>{
253                list.push(v);
254            }
255            None=>{}
256        }
257        */
258    }
259
260    Ok(list)
261}
262
263/// Get the filtered command line arguments when starting the app.
264/// In NW.js, some command line arguments are used by NW.js,
265/// which should not be interested of your app. App.argv will filter out
266/// those arguments and return the ones left. You can get filtered patterns
267/// from [app::filtered_argv](self::filtered_argv) and the
268/// full arguments from [app::full_argv](self::full_argv).
269///
270/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appargv)
271///
272pub fn argv() -> Result<Vec<String>> {
273    let list = build_argv_str(NwApp::argv_impl())?;
274    Ok(list)
275}
276
277/// Get all the command line arguments when starting the app.
278/// The return values contains the arguments used by NW.js,
279/// such as --nwapp, --remote-debugging-port etc.
280///
281/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appfullargv)
282pub fn full_argv() -> Result<Vec<String>> {
283    let list = build_argv_str(NwApp::full_argv_impl())?;
284    Ok(list)
285}
286
287/// Get a list of patterns of filtered command line arguments used by [app::argv()](self::argv).
288/// By default, following patterns are used to filter the arguments:
289/// ```
290/// [
291///     /^--url=/,
292///     /^--remote-debugging-port=/,
293///     /^--renderer-cmd-prefix=/,
294///     /^--nwapp=/
295/// ]
296/// ```
297///
298///
299/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appfilteredargv)
300pub fn filtered_argv() -> Result<Vec<RegExp>> {
301    let list = build_argv_filters(NwApp::filtered_argv_impl())?;
302    Ok(list)
303}
304
305/// Get the directory where the application starts.
306/// The application will change the current directory to where
307/// the package files reside after start.
308///
309/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appstartpath)
310///
311///
312pub fn start_path() -> String {
313    NwApp::start_path()
314}
315
316/// Get the application’s data path in user’s directory.
317///
318/// - Windows: `%LOCALAPPDATA%/<name>`
319/// - Linux: `~/.config/<name>`
320/// - OS X: `~/Library/Application Support/<name>/Default`
321/// - `<name>` is the name field in the `package.json` manifest.
322///
323/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appdatapath)
324///
325pub fn data_path() -> String {
326    NwApp::data_path()
327}
328
329/// Get the JSON object of the manifest file.
330///
331/// ⧉ [NWJS Documentation](https://docs.nwjs.io/en/latest/References/App/#appmanifest)
332///
333pub fn manifest() -> Object {
334    NwApp::manifest()
335}
336
337pub fn folder() -> String {
338    js_sys::Reflect::get(&crate::global::global(), &JsValue::from_str("__dirname"))
339        .unwrap()
340        .as_string()
341        .expect("unable to get global.__dirname")
342}