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 CSPERMMANAGER_CONTRACTID
= '@mozilla.org/CSPermManager;1';
31 const CSPERMMANAGER_CID
= Components
.ID('{4b0a4425-e9d8-441c-a34b-1867d8ca2d3b}');
32 const CSPERMMANAGER_IID
= Components
.interfaces
.nsICSPermManager
;
33 const CSPERMMANAGER_SERVICENAME
= 'CSPermManager';
35 var nsCSPermManager
= {
44 if (this._DB
&& 'close' in this._DB
) {
49 add: function(uri
,type
,permission
) {
50 if (!this._DB
) this.openDatabaseConnection();
53 var host
= (uri
.host
) ? uri
.host
: 'scheme:file';
54 var found
= this.testPermission(uri
,type
);
55 this._DB
.beginTransaction();
59 this._DB
.executeSimpleSQL("INSERT INTO cookies VALUES('"+host
+"','"+permission
+"')");
61 } else if (found
!= permission
) {
63 this._DB
.executeSimpleSQL("REPLACE INTO cookies VALUES('"+host
+"','"+permission
+"')");
67 this._DB
.commitTransaction();
71 remove: function(host
,type
) {
72 if (!this._DB
) this.openDatabaseConnection();
75 this._DB
.beginTransaction();
78 this._DB
.executeSimpleSQL("DELETE FROM cookies WHERE host = '"+host
+"'");
81 this._DB
.commitTransaction();
85 removeAll: function() {
86 if (!this._DB
) this.openDatabaseConnection();
89 this._DB
.beginTransaction();
92 this._DB
.executeSimpleSQL("DELETE FROM cookies");
95 this._DB
.commitTransaction();
99 testPermission: function(uri
,type
) {
100 if (!this._DB
) this.openDatabaseConnection();
101 if (!this._DB
) return false;
104 var host
= (uri
.host
) ? uri
.host
: 'scheme:file';
105 this._DB
.beginTransaction();
107 var stmt
= this._DB
.createStatement("SELECT * FROM cookies WHERE host = '"+host
+"'");
108 while (stmt
.executeStep()) {
109 if (stmt
.getString(1)) found
= stmt
.getString(1);
114 //if not found then test for base domain
116 var cs
= this.getCS();
117 var base
= cs
.removeSub(host
);
119 var stmt
= this._DB
.createStatement("SELECT * FROM cookies WHERE host = '"+base
+"'");
120 while (stmt
.executeStep()) {
121 if (stmt
.getString(1)) found
= stmt
.getString(1);
128 this._DB
.commitTransaction();
132 getAllPermissions: function() {
133 if (!this._DB
) this.openDatabaseConnection();
134 if (!this._DB
) return '';
137 this._DB
.beginTransaction();
139 var stmt
= this._DB
.createStatement("SELECT * FROM cookies");
140 while (stmt
.executeStep()) {
141 if (stmt
.getString(0) && stmt
.getString(1)) {
142 entries
.push(stmt
.getString(0) + '|' + stmt
.getString(1));
147 this._DB
.commitTransaction();
148 return entries
.join(' ');
151 copyCSDBToProfile: function() {
152 //get nsIFile for profile folder
153 var profile
= this.getProfile();
154 profile
.append('cshostperm.sqlite');
156 //get location of jar file
157 var chromeuri
= this.getURI('chrome://cookiesafe/content/sqlite/cshostperm.sqlite');
158 var jaruri
= this.getJarURI(chromeuri
);
160 //the uri of the base jar file is needed here so query nsIJARURI
161 jaruri
.QueryInterface(Components
.interfaces
.nsIJARURI
);
162 var jarfile
= this.urlToFile(jaruri
.JARFile
.spec
);
164 var zipReader
= this.getZipReaderForFile(jarfile
);
168 var entries
= zipReader
.findEntries('\*cshostperm.sqlite\$');
169 if ('init' in zipReader
) {
170 while (entries
.hasMoreElements()) {
171 entry
= entries
.getNext();
172 entry
.QueryInterface(Components
.interfaces
.nsIZipEntry
);
173 zipReader
.extract(entry
.name
,profile
);
177 while (entries
.hasMore()) {
178 entry
= entries
.getNext();
179 zipReader
.extract(entry
,profile
);
190 getURI: function(url
) {
191 return Components
.classes
["@mozilla.org/network/io-service;1"].
192 getService(Components
.interfaces
.nsIIOService
).
193 newURI(url
,null,null);
196 getJarURI: function(uri
) {
197 return Components
.classes
['@mozilla.org/chrome/chrome-registry;1'].
198 getService(Components
.interfaces
.nsIChromeRegistry
).
199 convertChromeURL(uri
);
202 urlToFile: function(url
) {
203 return Components
.classes
['@mozilla.org/network/protocol;1?name=file'].
204 createInstance(Components
.interfaces
.nsIFileProtocolHandler
).
205 getFileFromURLSpec(url
);
208 getZipReaderForFile: function(zipFile
) {
210 var zipReader
= Components
.classes
["@mozilla.org/libjar/zip-reader;1"].
211 createInstance(Components
.interfaces
.nsIZipReader
);
213 if ('init' in zipReader
) {
214 zipReader
.init(zipFile
);
217 zipReader
.open(zipFile
);
226 getProfile: function() {
227 return Components
.classes
["@mozilla.org/file/directory_service;1"].
228 getService(Components
.interfaces
.nsIProperties
).
229 get("ProfD", Components
.interfaces
.nsIFile
);
232 getDBService: function() {
233 return Components
.classes
["@mozilla.org/storage/service;1"].
234 getService(Components
.interfaces
.mozIStorageService
);
237 openDatabaseConnection: function() {
238 var dbfile
= this.getProfile();
239 dbfile
.append("cshostperm.sqlite");
240 if (!dbfile
.exists()) {
241 this.copyCSDBToProfile();
242 if (!dbfile
.exists()) return false;
245 var dbService
= this.getDBService();
246 this._DB
= dbService
.openDatabase(dbfile
);
248 if (!this._DB
.tableExists("cookies")) {
249 this._DB
.createTable("cookies", "host STRING PRIMARY KEY, type INTEGER");
255 return Components
.classes
['@mozilla.org/CookieSafe;1'].
256 createInstance(Components
.interfaces
.nsICookieSafe
);
259 getCSHiddenMenuItems: function() {
260 return Components
.classes
['@mozilla.org/CSHiddenMenuItems;1'].
261 createInstance(Components
.interfaces
.nsICSHiddenMenuItems
);
264 getCSLast10Hosts: function() {
265 return Components
.classes
['@mozilla.org/CSLast10Hosts;1'].
266 getService(Components
.interfaces
.nsICSLast10Hosts
);
269 getCSTempExceptions: function() {
270 return Components
.classes
['@mozilla.org/CSTempExceptions;1'].
271 getService(Components
.interfaces
.nsICSTempExceptions
);
274 QueryInterface: function(iid
) {
275 if (!iid
.equals(Components
.interfaces
.nsISupports
) &&
276 !iid
.equals(CSPERMMANAGER_IID
))
277 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
282 var nsCSPermManagerModule
= {
284 registerSelf: function(compMgr
, fileSpec
, location
, type
) {
285 compMgr
= compMgr
.QueryInterface(Components
.interfaces
.nsIComponentRegistrar
);
286 compMgr
.registerFactoryLocation(CSPERMMANAGER_CID
,
287 CSPERMMANAGER_SERVICENAME
,
288 CSPERMMANAGER_CONTRACTID
,
289 fileSpec
,location
,type
);
292 unregisterSelf: function (compMgr
, fileSpec
, location
) {
293 compMgr
= compMgr
.QueryInterface(Components
.interfaces
.nsIComponentRegistrar
);
294 compMgr
.unregisterFactoryLocation(CSPERMMANAGER_CID
,fileSpec
);
297 getClassObject: function(compMgr
, cid
, iid
) {
298 if (!cid
.equals(CSPERMMANAGER_CID
))
299 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
300 if (!iid
.equals(Components
.interfaces
.nsIFactory
))
301 throw Components
.results
.NS_ERROR_NOT_IMPLEMENTED
;
302 return this.nsCSPermManagerFactory
;
305 canUnload: function(compMgr
) {
309 nsCSPermManagerFactory
: {
311 createInstance: function(outer
, iid
) {
313 throw Components
.results
.NS_ERROR_NO_AGGREGATION
;
314 if (!iid
.equals(CSPERMMANAGER_IID
) &&
315 !iid
.equals(Components
.interfaces
.nsISupports
))
316 throw Components
.results
.NS_ERROR_INVALID_ARG
;
317 return nsCSPermManager
;
322 function NSGetModule(comMgr
, fileSpec
) { return nsCSPermManagerModule
; }