1 /* See license.txt for terms of usage */ 2 3 define([ 4 "firebug/lib/object", 5 "firebug/lib/xpcom", 6 "firebug/lib/locale", 7 "firebug/lib/options", 8 ], 9 function(Obj, Xpcom, Locale, Options) { 10 11 // ********************************************************************************************* // 12 // Constants 13 14 var Cc = Components.classes; 15 var Ci = Components.interfaces; 16 17 var permissionManager = Xpcom.CCSV("@mozilla.org/permissionmanager;1", "nsIPermissionManager"); 18 19 // Firefox Preferences 20 const networkPrefDomain = "network.cookie"; 21 const cookieBehaviorPref = "cookieBehavior"; 22 const cookieLifeTimePref = "lifetimePolicy"; 23 24 const permOptions = 25 { 26 "default-session": ["cookies.default.session", false], 27 "default-third-party-session": ["cookies.default.thirdPartySession", false], 28 "default-third-party": ["cookies.default.thirdParty", false], 29 "default-allow": ["cookies.default.allow", false], 30 "default-deny": ["cookies.default.deny", false], 31 "default-warn": ["cookies.default.warn", false], 32 "host-allow-session": ["cookies.host.session", true], 33 "host-allow": ["cookies.host.accept", true], 34 "host-deny": ["cookies.host.reject", true] 35 }; 36 37 // ********************************************************************************************* // 38 // Cookie Permissions 39 40 /** 41 * @class This class is responsible for managing cookie permisssions. 42 */ 43 var CookiePermissions = Obj.extend(Object, 44 /** @lends CookiePermissions */ 45 { 46 onCommand: function(event, context, location) 47 { 48 var menu = event.target; 49 this.setPermission(context, menu.value, location); 50 }, 51 52 onTooltipShowing: function(tooltip, context) 53 { 54 if (tooltip.fcEnabled) 55 { 56 var host = context.window.location.host; 57 tooltip.label = Locale.$STRF("cookies.perm.manage.tooltip", [host]); 58 } 59 60 return tooltip.fcEnabled; 61 }, 62 63 onPopupShowing: function(menu, context) 64 { 65 var permTooltip = Firebug.chrome.$("fcPermTooltip"); 66 permTooltip.fcEnabled = false; 67 68 var items = menu.getElementsByTagName("menuitem"); 69 var location = context.browser.currentURI; 70 71 var value = this.getPermission(location); 72 var defaultValue = (value.indexOf("default") == 0) ? value : this.getDefaultPref(); 73 74 items[0].value = defaultValue; 75 76 for (var i=0; i<items.length; i++) 77 { 78 var option = items[i].value; 79 if (option == value) 80 items[i].setAttribute("checked", "true"); 81 items[i].label = this.getLabel(option, location); 82 } 83 84 return true; 85 }, 86 87 onPopupHiding: function(menu, context) 88 { 89 var permTooltip = Firebug.chrome.$("fcPermTooltip"); 90 permTooltip.fcEnabled = true; 91 return true; 92 }, 93 94 getContextMenuItems: function(cookie, target, context) 95 { 96 if (context.browser.currentURI.host == cookie.cookie.host) 97 return null; 98 99 var location = cookie.getURI(); 100 var value = this.getPermission(location); 101 var defaultValue = (value.indexOf("default") == 0) ? value : this.getDefaultPref(); 102 103 var items = []; 104 items.push("-"); 105 106 var menu = Firebug.chrome.$("fcPermMenuPopup"); 107 menu.childNodes[0].value = defaultValue; 108 for (var i=0; i<menu.childNodes.length; i++) 109 { 110 var item = menu.childNodes[i]; 111 var option = item.value; 112 113 items.push({ 114 label: this.getLabel(option, location), 115 type: "radio", 116 checked: (option == value), 117 nol10n: true, 118 command: Obj.bindFixed(this.onCommand, this, {target: item}, context, location), 119 }); 120 } 121 122 return items; 123 }, 124 125 getPermission: function(location) 126 { 127 switch (permissionManager.testPermission(location, "cookie")) 128 { 129 case Ci.nsIPermissionManager.ALLOW_ACTION: 130 return "host-allow"; 131 case Ci.nsIPermissionManager.DENY_ACTION: 132 return "host-deny"; 133 case Ci.nsICookiePermission.ACCESS_SESSION: 134 return "host-allow-session"; 135 default: 136 return this.getDefaultPref(); 137 } 138 }, 139 140 setPermission: function(context, option, location) 141 { 142 var location = location ? location : context.browser.currentURI; 143 permissionManager.remove(location.host, "cookie"); 144 switch(option) 145 { 146 case "host-allow-session": 147 permissionManager.add(location, "cookie", Ci.nsICookiePermission.ACCESS_SESSION); 148 break; 149 case "host-allow": 150 permissionManager.add(location, "cookie", permissionManager.ALLOW_ACTION); 151 break; 152 case "host-deny": 153 permissionManager.add(location, "cookie", permissionManager.DENY_ACTION); 154 155 case "default-deny": 156 if (Options.get("cookies.clearWhenDeny")) 157 Firebug.CookieModule.onRemoveAllFromHost(context, location.host); 158 break; 159 } 160 161 this.updatePermButton(context); 162 }, 163 164 updatePermButton: function(context, chrome) 165 { 166 if (!chrome) 167 chrome = context.chrome; 168 169 // This is called through TabWatcher.iterateContexts and 170 // "this" isn't passed along 171 var location = context.browser.currentURI; 172 var value = this.getPermission(location); 173 174 var button = Firebug.chrome.$("fcPerm"); 175 button.label = this.getLabel(value, location); 176 button.removeAttribute("disabled"); 177 button.setAttribute("value", value); 178 }, 179 180 getLabel: function (option, location) 181 { 182 var optionInfo = permOptions[option]; 183 if (!optionInfo) 184 return null; 185 186 if (optionInfo[1]) 187 return Locale.$STRF(optionInfo[0], [location.host]); 188 189 return Locale.$STR(optionInfo[0]); 190 }, 191 192 getDefaultPref: function() 193 { 194 var behavior = Options.getPref(networkPrefDomain, cookieBehaviorPref); 195 if (typeof(behavior) == "undefined") 196 behavior = 0; 197 198 if (behavior == 2) 199 return "default-deny"; 200 201 switch (Options.getPref(networkPrefDomain, cookieLifeTimePref)) 202 { 203 case 1: 204 return "default-warn"; 205 case 2: 206 return (behavior == 0) ? "default-third-party-session" : 207 "default-session"; 208 } 209 210 switch (behavior) 211 { 212 case 0: 213 return "default-third-party"; 214 case 1: 215 return "default-allow"; 216 } 217 218 return null; 219 } 220 }); 221 222 // ********************************************************************************************* // 223 // Registration 224 225 return CookiePermissions; 226 227 // ********************************************************************************************* // 228 }); 229 230