electron/atom/browser/api/lib/navigation-controller.coffee

128 lines
3.6 KiB
CoffeeScript
Raw Normal View History

{ipcMain} = require 'electron'
2016-01-12 02:03:02 +00:00
### The history operation in renderer is redirected to browser. ###
ipcMain.on 'ATOM_SHELL_NAVIGATION_CONTROLLER', (event, method, args...) ->
event.sender[method] args...
ipcMain.on 'ATOM_SHELL_SYNC_NAVIGATION_CONTROLLER', (event, method, args...) ->
2015-05-19 17:11:03 +00:00
event.returnValue = event.sender[method] args...
2016-01-12 02:03:02 +00:00
###
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.
This helps us avoid Chromium's various optimizations so we can ensure renderer
process is restarted everytime.
###
2015-04-26 13:28:30 +00:00
class NavigationController
constructor: (@webContents) ->
2015-05-19 17:11:03 +00:00
@clearHistory()
2015-04-26 13:28:30 +00:00
2016-01-12 02:03:02 +00:00
### webContents may have already navigated to a page. ###
2015-11-13 08:03:40 +00:00
if @webContents._getURL()
@currentIndex++
2015-11-13 08:03:40 +00:00
@history.push @webContents._getURL()
@webContents.on 'navigation-entry-commited', (event, url, inPage, replaceEntry) =>
if @inPageIndex > -1 and not inPage
2016-01-12 02:03:02 +00:00
### Navigated to a new page, clear in-page mark. ###
@inPageIndex = -1
else if @inPageIndex is -1 and inPage
2016-01-12 02:03:02 +00:00
### Started in-page navigations. ###
@inPageIndex = @currentIndex
2016-01-12 02:03:02 +00:00
if @pendingIndex >= 0
### Go to index. ###
@currentIndex = @pendingIndex
@pendingIndex = -1
@history[@currentIndex] = url
2016-01-12 02:03:02 +00:00
else if replaceEntry
### Non-user initialized navigation. ###
@history[@currentIndex] = url
2016-01-12 02:03:02 +00:00
else
### Normal navigation. Clear history. ###
@history = @history.slice 0, @currentIndex + 1
currentEntry = @history[@currentIndex]
if currentEntry?.url isnt url
2015-04-26 13:28:30 +00:00
@currentIndex++
@history.push url
2015-04-26 13:28:30 +00:00
2015-11-13 08:03:40 +00:00
loadURL: (url, options={}) ->
2015-04-26 13:28:30 +00:00
@pendingIndex = -1
2015-11-13 08:03:40 +00:00
@webContents._loadURL url, options
@webContents.emit 'load-url', url, options
2015-04-26 13:28:30 +00:00
2015-11-13 08:03:40 +00:00
getURL: ->
2015-04-26 13:28:30 +00:00
if @currentIndex is -1
''
else
@history[@currentIndex]
2015-04-26 13:28:30 +00:00
stop: ->
@pendingIndex = -1
@webContents._stop()
reload: ->
@pendingIndex = @currentIndex
2015-11-13 08:03:40 +00:00
@webContents._loadURL @getURL(), {}
2015-04-26 13:28:30 +00:00
reloadIgnoringCache: ->
2015-11-13 20:55:23 +00:00
@pendingIndex = @currentIndex
@webContents._loadURL @getURL(), {extraHeaders: "pragma: no-cache\n"}
2015-04-26 13:28:30 +00:00
canGoBack: ->
@getActiveIndex() > 0
2015-04-26 13:28:30 +00:00
canGoForward: ->
@getActiveIndex() < @history.length - 1
2015-04-26 13:28:30 +00:00
canGoToIndex: (index) ->
index >=0 and index < @history.length
canGoToOffset: (offset) ->
@canGoToIndex @currentIndex + offset
2015-05-19 17:11:03 +00:00
clearHistory: ->
@history = []
@currentIndex = -1
@pendingIndex = -1
@inPageIndex = -1
2015-04-26 13:28:30 +00:00
goBack: ->
return unless @canGoBack()
@pendingIndex = @getActiveIndex() - 1
if @inPageIndex > -1 and @pendingIndex >= @inPageIndex
@webContents._goBack()
else
2015-11-13 08:03:40 +00:00
@webContents._loadURL @history[@pendingIndex], {}
2015-04-26 13:28:30 +00:00
goForward: ->
return unless @canGoForward()
@pendingIndex = @getActiveIndex() + 1
if @inPageIndex > -1 and @pendingIndex >= @inPageIndex
@webContents._goForward()
else
2015-11-13 08:03:40 +00:00
@webContents._loadURL @history[@pendingIndex], {}
2015-04-26 13:28:30 +00:00
goToIndex: (index) ->
return unless @canGoToIndex index
@pendingIndex = index
2015-11-13 08:03:40 +00:00
@webContents._loadURL @history[@pendingIndex], {}
2015-04-26 13:28:30 +00:00
goToOffset: (offset) ->
return unless @canGoToOffset offset
2015-05-11 08:44:01 +00:00
pendingIndex = @currentIndex + offset
if @inPageIndex > -1 and pendingIndex >= @inPageIndex
@pendingIndex = pendingIndex
@webContents._goToOffset offset
else
@goToIndex pendingIndex
2015-04-26 13:28:30 +00:00
getActiveIndex: ->
if @pendingIndex is -1 then @currentIndex else @pendingIndex
2015-05-19 17:11:03 +00:00
length: ->
@history.length
2015-04-26 13:28:30 +00:00
module.exports = NavigationController