ipc.on 'ATOM_SHELL_NAVIGATION_CONTROLLER', (event, method, args...) ->
event.sender[method] args...
+ipc.on 'ATOM_SHELL_SYNC_NAVIGATION_CONTROLLER', (event, method, args...) ->
+ event.returnValue = event.sender[method] args...
+
# JavaScript implementation of Chromium's NavigationController.
# Instead of relying on Chromium for history control, we compeletely do history
# control on user land, and only rely on WebContents.loadUrl for navigation.
# process is restarted everytime.
class NavigationController
constructor: (@webContents) ->
- @history = []
- @currentIndex = -1
- @pendingIndex = -1
- @inPageIndex = -1
+ @clearHistory()
@webContents.on 'navigation-entry-commited', (event, url, inPage, replaceEntry) =>
if @inPageIndex > -1 and not inPage
canGoToOffset: (offset) ->
@canGoToIndex @currentIndex + offset
+ clearHistory: ->
+ @history = []
+ @currentIndex = -1
+ @pendingIndex = -1
+ @inPageIndex = -1
+
goBack: ->
return unless @canGoBack()
@pendingIndex = @getActiveIndex() - 1
getActiveIndex: ->
if @pendingIndex is -1 then @currentIndex else @pendingIndex
+ length: ->
+ @history.length
+
module.exports = NavigationController
# Forward history operations to browser.
sendHistoryOperation = (args...) ->
ipc.send 'ATOM_SHELL_NAVIGATION_CONTROLLER', args...
+
+getHistoryOperation = (args...) ->
+ ipc.sendSync 'ATOM_SHELL_SYNC_NAVIGATION_CONTROLLER', args...
+
window.history.back = -> sendHistoryOperation 'goBack'
window.history.forward = -> sendHistoryOperation 'goForward'
window.history.go = (offset) -> sendHistoryOperation 'goToOffset', offset
+Object.defineProperty window.history, 'length',
+ get: ->
+ getHistoryOperation 'length'