2013-08-14 22:43:35 +00:00
|
|
|
## Synopsis
|
|
|
|
|
2013-08-29 14:37:51 +00:00
|
|
|
It's common that the developers want to use modules in browsers from the
|
|
|
|
renderer, like closing current window, opening file dialogs, etc. Instead of
|
|
|
|
writing IPC code for every operation you want to do, atom-shell provides the
|
|
|
|
`remote` module to let you do RPC call just like using normal javascript
|
|
|
|
objects.
|
2013-08-14 22:43:35 +00:00
|
|
|
|
|
|
|
An example of creating a window in renderer:
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
var remote = require('remote');
|
|
|
|
var BrowserWindow = remote.require('browser-window');
|
|
|
|
var win = new BrowserWindow({ width: 800, height: 600 });
|
|
|
|
win.loadUrl('https://github.com');
|
|
|
|
```
|
|
|
|
|
|
|
|
## Lifetime of remote objects
|
|
|
|
|
2013-08-29 14:37:51 +00:00
|
|
|
Every object returned by `remote` module represents an object in browser (e.g.
|
|
|
|
a remote object), so when you call methods of an object, or call a returned
|
|
|
|
function, or even create a object with the returned constructor, you are
|
|
|
|
indeed making a synchronous RPC call. And when the renderer releases the last
|
|
|
|
reference to the remote object, the browser would release the corresponding
|
|
|
|
reference too.
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2013-08-29 14:37:51 +00:00
|
|
|
This also means that, if the renderer keeps a reference to an object in
|
|
|
|
browser, the object would never be released. So be careful to never leak the
|
|
|
|
remote objects.
|
2013-08-14 22:43:35 +00:00
|
|
|
|
|
|
|
## Passing callbacks
|
|
|
|
|
2013-08-29 14:37:51 +00:00
|
|
|
Many APIs in browser accepts callbacks, so the `remote` module also supports
|
|
|
|
passing callbacks when calling remote functions, and the callbacks passed
|
|
|
|
would become remote functions in the browser.
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2013-08-29 14:37:51 +00:00
|
|
|
But in order to avoid possible dead locks, the callbacks passed to browser
|
|
|
|
would be called asynchronously in browser, so you should never expect the
|
|
|
|
browser to get the return value of the passed callback.
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2013-08-29 14:37:51 +00:00
|
|
|
Another thing is the lifetime of the remote callbacks in browser, it might be
|
|
|
|
very tempting to do things like following:
|
2013-08-14 22:43:35 +00:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
var remote = require('remote');
|
|
|
|
remote.getCurrentWindow().on('close', function() {
|
|
|
|
// blabla...
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2013-08-29 14:37:51 +00:00
|
|
|
Yes it will work correctly, but when you reload the window, the callback you
|
|
|
|
setup on the object in browser will not be erased, resources are leaked and
|
|
|
|
there is no magic in javascript to release a referenced object.
|
2013-08-14 22:43:35 +00:00
|
|
|
|
2013-08-29 14:37:51 +00:00
|
|
|
So if you really need to keep a reference of callbacks in browser, you should
|
|
|
|
write the callback in browser and send messages to renderer. And also make use
|
|
|
|
of DOM's events like `unload` and `beforeunload`, they will work perfectly.
|
2013-08-14 22:43:35 +00:00
|
|
|
|
|
|
|
## remote.require(module)
|
|
|
|
|
|
|
|
* `module` String
|
|
|
|
|
|
|
|
Return a module in browser.
|
|
|
|
|
|
|
|
## remote.getCurrentWindow()
|
|
|
|
|
|
|
|
Return the `BrowserWindow` object that represents current window.
|
|
|
|
|
2013-08-29 14:37:51 +00:00
|
|
|
`Note:` it doesn't return the `window` object which represents the global
|
|
|
|
scope, instead it returns an instance of the `BrowserWindow` class which is
|
|
|
|
created with `browser-window` module in browser.
|
2013-08-14 22:43:35 +00:00
|
|
|
|
|
|
|
## remote.getGlobal(name)
|
|
|
|
|
|
|
|
* `name` String
|
|
|
|
|
|
|
|
Return the `global[name]` value in browser.
|
|
|
|
|
|
|
|
## remote.process
|
|
|
|
|
2013-08-29 14:37:51 +00:00
|
|
|
Getter to return the `process` object in browser, this is the same with
|
|
|
|
`remote.getGlobal('process')` but gets cached.
|