1 /***************************************************************************
3 Description: Control cookie permissions.
5 Homepage: http://addons.mozilla.org
7 Copyright (C) 2007 Ron Beckman
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to:
22 Free Software Foundation, Inc.
27 ***************************************************************************/
30 const COOKIESAFE_CONTRACTID
= '@mozilla.org/CookieSafe;1';
31 const COOKIESAFE_CID
= Components
.ID('{d3b60080-fd35-4507-a5a2-70c3560b3874}');
32 const COOKIESAFE_IID
= Components
.interfaces
.nsICookieSafe
;
33 const COOKIESAFE_SERVICENAME
= 'CookieSafe';
37 removeSub: function(host
) {
38 if (!host
|| host
.indexOf('.')==-1) return host
;
40 //remove port number if found
41 host
= host
.replace(/\:.*$/g,'');
43 var testip
= host
.replace(/\./g,'');
44 if (!isNaN(testip
)) return host
;
46 var domain
= host
.split('.');
47 if (domain
.length
<3) return domain
.join('.');
50 var sld
= domain
[domain
.length
-2];
51 var ext
= domain
[domain
.length
-1];
52 if ((sld
in this && this[sld
].indexOf(' '+ext
+' ')!=-1) ||
53 (sld
=='co' || sld
=='com' || sld
=='org')) {
54 return domain
[domain
.length
-3]+'.'+domain
[domain
.length
-2]+'.'+domain
[domain
.length
-1];
56 return domain
[domain
.length
-2]+'.'+domain
[domain
.length
-1];
59 return domain
[domain
.length
-2]+'.'+domain
[domain
.length
-1];
63 formatCookieString: function(ckstr
,uri
) {
64 var url
= uri
.QueryInterface(Components
.interfaces
.nsIURL
);
66 //setup cookie object to appear as nsICookie
78 var time
= dt
.getTime();
80 ckstr
= ckstr
.replace(/\; /g,'\;');
81 var values
= ckstr
.split('\;');
83 for (var i
=0; i
<values
.length
; ++i
) {
84 if (values
[i
].substr(0,6).toLowerCase()=='domain') {
85 cookie
.host
= values
[i
].substr(values
[i
].indexOf('=')+1);
87 if (values
[i
].substr(0,4).toLowerCase()=='path') {
88 cookie
.path
= values
[i
].substr(values
[i
].indexOf('=')+1);
90 if (values
[i
].substr(0,7).toLowerCase()=='expires') {
91 var expStr
= values
[i
].substr(values
[i
].indexOf('=')+1);
92 expStr
= expStr
.replace(/\-/g,' ');
93 var exp
= parseInt(Date
.parse(expStr
));
94 cookie
.expires
= (exp
< time
) ? 0 : exp
/ 1000;
96 if (values
[i
].substr(0,6).toLowerCase()=='secure') {
97 cookie
.isSecure
= true;
101 cookie
.name
= values
[0].substr(0,values
[0].indexOf('='));
102 cookie
.value
= values
[0].substr(values
[0].indexOf('=')+1);
103 if (cookie
.host
.charAt(0)=='.') cookie
.isDomain
= true;
108 /** BEGIN ORGANIZATIONAL SECOND LEVEL DOMAINS **/
110 ac
: ' ac at be cn id il in jp kr nz th uk za ',
136 co
: ' ac at bw ck cr id il im in je jp ke kr ls ma nz th ug uk uz ve vi za zm zw ',
137 com
: ' af ag ar au bd bh bn bo br bz cn co cu do ec eg et fj fr gi gt hk jm kh ly mm mt mx my na nf ng ni np om pa pe ph pk pl pr py qa ro ru sa sb sg sv tj tr tw ua uy vc vn ',
142 edu
: ' ar au cn hk mm mx pl tr za ',
160 gov
: ' ar br cn ec il in mm mx sg tr uk za ',
196 mil
: ' br ec nz pl tr za ',
201 net
: ' ar au br cn ec hk id il in mm mx nz pl ru sg th tr tw za ',
206 nom
: ' br pl ro za ',
214 or
: ' ac at jp kr th ',
215 org
: ' ar au br cn ec hk il in mm mx nz pl ro ru sg tr tw uk za ',
245 tm
: ' fr mc pl ro za ',
266 /** END ORGANIZATIONAL SECOND LEVEL DOMAINS **/
268 QueryInterface: function(iid
) {
269 if (!iid
.equals(Components
.interfaces
.nsISupports
) &&
270 !iid
.equals(COOKIESAFE_IID
))
271 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
276 var nsCookieSafeModule
= {
278 registerSelf: function(compMgr
, fileSpec
, location
, type
) {
279 compMgr
= compMgr
.QueryInterface(Components
.interfaces
.nsIComponentRegistrar
);
280 compMgr
.registerFactoryLocation(COOKIESAFE_CID
,
281 COOKIESAFE_SERVICENAME
,
282 COOKIESAFE_CONTRACTID
,
283 fileSpec
,location
,type
);
286 unregisterSelf: function (compMgr
, fileSpec
, location
) {
287 compMgr
= compMgr
.QueryInterface(Components
.interfaces
.nsIComponentRegistrar
);
288 compMgr
.unregisterFactoryLocation(COOKIESAFE_CID
,fileSpec
);
291 getClassObject: function(compMgr
, cid
, iid
) {
292 if (!cid
.equals(COOKIESAFE_CID
))
293 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
294 if (!iid
.equals(Components
.interfaces
.nsIFactory
))
295 throw Components
.results
.NS_ERROR_NOT_IMPLEMENTED
;
296 return this.nsCookieSafeFactory
;
299 canUnload: function(compMgr
) {
303 nsCookieSafeFactory
: {
305 createInstance: function(outer
, iid
) {
307 throw Components
.results
.NS_ERROR_NO_AGGREGATION
;
308 if (!iid
.equals(COOKIESAFE_IID
) &&
309 !iid
.equals(Components
.interfaces
.nsISupports
))
310 throw Components
.results
.NS_ERROR_INVALID_ARG
;
316 function NSGetModule(comMgr
, fileSpec
) { return nsCookieSafeModule
; }