Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Added redirect notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
futpib committed Feb 1, 2015
1 parent dc8ae44 commit 522dd1c
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/chrome/locale/en-US/policeman.properties
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,8 @@ context_blocked_object_load = Load this object
context_blocked_object_load_all_on_tab = Load all objects on this tab
context_blocked_object_temp_allow_domain_pair_and_load = Load all objects from %1$S (temporary rule)
context_blocked_object_pers_allow_domain_pair_and_load = Always load objects from %1$S (persistent rule)

redirect_notification_popup_message = Would you like to allow redirect to %1$S?
redirect_notification_action_allow_once = Allow once
redirect_notification_action_temp_allow_domain_pair = Temporarily allow page loads from %1$S to %2$S
redirect_notification_action_pers_allow_domain_pair = Always allow page loads from %1$S to %2$S
203 changes: 203 additions & 0 deletions src/chrome/skin/redirect-notification-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/chrome/skin/redirect-notifications.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");

@-moz-document url("chrome://browser/content/browser.xul") {
.popup-notification-icon[popupid="policeman-redirect-notification-popup"] {
list-style-image: url(chrome://policeman/skin/redirect-notification-icon-64.png);
}
}
21 changes: 21 additions & 0 deletions src/lib/blocked-redirects.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@


{ tabs } = require 'tabs'

{ redirectNotifications } = require 'ui/redirect-notifications'


class BlockedRedirectInfo
constructor: (@origin, @destination, @context) ->
@browser = (tabs.getTabById @context._tabId).linkedBrowser

restore: ->
@browser.loadURI @destination.spec

exports.blockedRedirects = blockedRedirects =
process: (origin, destination, context, decision) ->
if decision is false \
and context.hints.redirect \
and context._tabId
redirect = new BlockedRedirectInfo origin, destination, context
redirectNotifications.show redirect
2 changes: 2 additions & 0 deletions src/lib/content-policy.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ catMan = Cc["@mozilla.org/categorymanager;1"].getService Ci.nsICategoryManager
} = require 'request-info'
{ memo } = require 'request-memo'
{ blockedElements } = require 'blocked-elements'
{ blockedRedirects } = require 'blocked-redirects'
{
Handlers
runAsync
Expand Down Expand Up @@ -63,6 +64,7 @@ exports.policy = policy =
try
memo.add origin, dest, ctx, decision
blockedElements.process origin, dest, ctx, decision
blockedRedirects.process origin, dest, ctx, decision
@onRequest.execute origin, dest, ctx, decision
catch e
log.error 'Error processing a request:', e
Expand Down
11 changes: 8 additions & 3 deletions src/lib/tabs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ exports.tabs = tabs =

getTabId: (tab) ->
return tab if typeof tab is 'string'
# not a DOM id or smth, just a unique string
# copypasted from addon-sdk/lib/tabs/utils.js#getTabId
String.split(tab.linkedPanel, 'panel').pop()
# returns DOM id of linked panel
# inspired by addon-sdk/lib/tabs/utils.js#getTabId
return tab.linkedPanel

getTabById: (id) ->
for tab in @list
return tab if id == @getTabId tab
return null

getCurrent: -> windows.getCurrent().gBrowser.tabContainer.tabbox.selectedTab

Expand Down
69 changes: 69 additions & 0 deletions src/lib/ui/redirect-notifications.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@


{ windows } = require 'windows'
{ manager } = require 'ruleset/manager'

{
loadSheet
removeSheet
} = require 'utils'

{ l10n } = require 'l10n'


exports.redirectNotifications = redirectNotifications = new class
_styleURI: 'chrome://policeman/skin/redirect-notifications.css'

constructor: ->
@_addUI w for w in windows.list
windows.onOpen.add @_addUI.bind @
windows.onClose.add @_removeUI.bind @
onShutdown.add => @_removeUI w for w in windows.list

_addUI: (win) ->
loadSheet win, @_styleURI

_removeUI: (win) ->
removeSheet win, @_styleURI

_makeActionDescriptors: (redirect) ->
actions = []
temp = manager.get 'user_temporary'
pers = manager.get 'user_persistent'
if temp
actions.push
label: l10n 'redirect_notification_action_allow_once'
accessKey: 'O'
callback: ->
temp.addClosure (o, d, c) ->
if c.contentType == 'DOCUMENT' \
and d.spec == redirect.destination.spec
temp.revokeClosure this
return true
return null
redirect.restore()
for [rs, rsLbl] in [[temp, 'temp'], [pers, 'pers']]
domains = Object.create null
domains[redirect.destination.host] = yes
domains[redirect.destination.baseDomain] = yes
for dest of domains
actions.push
label: l10n \
"redirect_notification_action_#{rsLbl}_allow_domain_pair",
redirect.origin.host, dest
accessKey: actions.length.toString()
callback: do (rs=rs, dest=dest) -> ->
rs.allow redirect.origin.host, dest, 'DOCUMENT'
redirect.restore()
return actions

show: (redirect) ->
actions = @_makeActionDescriptors redirect
window = redirect.browser.ownerDocument.defaultView
window.PopupNotifications.show \
redirect.browser,
'policeman-redirect-notification-popup',
l10n('redirect_notification_popup_message', redirect.destination.host),
null, # default anchor
actions[0],
actions.slice(1),
6 changes: 6 additions & 0 deletions src/lib/utils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,22 @@ exports.isDead = isDead = (node) ->
return false

exports.loadSheet = (win, styleURI, type=Ci.nsIDOMWindowUtils.AUTHOR_SHEET) ->
styleURI = newURI styleURI if 'string' == typeof styleURI
win.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils)
.loadSheet(styleURI, type)

exports.removeSheet = (win, styleURI, type=Ci.nsIDOMWindowUtils.AUTHOR_SHEET) ->
styleURI = newURI styleURI if 'string' == typeof styleURI
win.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils)
.removeSheet(styleURI, type)


exports.newURI = newURI = (spec, originCharset=null, baseURI=null) ->
return Services.io.newURI spec, originCharset, baseURI


exports.XMLHttpRequest = XMLHttpRequest = Components.Constructor \
"@mozilla.org/xmlextras/xmlhttprequest;1", "nsIXMLHttpRequest"

Expand Down

0 comments on commit 522dd1c

Please sign in to comment.