Update AdBlock Plus patterns.
[tails-test.git] / config / chroot_local-includes / etc / iceweasel / profile / extensions / {00084897-021a-4361-8423-083407a033e0} / components / nsCSPermManager.js
blobaca0f76f87726710ec5f941be3f118f79acfb935
1 /***************************************************************************
2 Name: CS Lite
3 Description: Control cookie permissions.
4 Author: Ron Beckman
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.
23 51 Franklin Street
24 Fifth Floor
25 Boston, MA 02110-1301
26 USA
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 = {
37 _DB: null,
39 UNKNOWN_ACTION: 0,
40 ALLOW_ACTION: 1,
41 DENY_ACTION: 2,
43 closeDB: function() {
44 if (this._DB && 'close' in this._DB) {
45 this._DB.close();
49 add: function(uri,type,permission) {
50 if (!this._DB) this.openDatabaseConnection();
52 if (this._DB) {
53 var host = (uri.host) ? uri.host : 'scheme:file';
54 var found = this.testPermission(uri,type);
55 this._DB.beginTransaction();
57 if (!found) {
58 try {
59 this._DB.executeSimpleSQL("INSERT INTO cookies VALUES('"+host+"','"+permission+"')");
60 } catch(e) { }
61 } else if (found != permission) {
62 try {
63 this._DB.executeSimpleSQL("REPLACE INTO cookies VALUES('"+host+"','"+permission+"')");
64 } catch(e) { }
67 this._DB.commitTransaction();
71 remove: function(host,type) {
72 if (!this._DB) this.openDatabaseConnection();
74 if (this._DB) {
75 this._DB.beginTransaction();
77 try {
78 this._DB.executeSimpleSQL("DELETE FROM cookies WHERE host = '"+host+"'");
79 } catch(e) { }
81 this._DB.commitTransaction();
85 removeAll: function() {
86 if (!this._DB) this.openDatabaseConnection();
88 if (this._DB) {
89 this._DB.beginTransaction();
91 try {
92 this._DB.executeSimpleSQL("DELETE FROM cookies");
93 } catch(e) { }
95 this._DB.commitTransaction();
99 testPermission: function(uri,type) {
100 if (!this._DB) this.openDatabaseConnection();
101 if (!this._DB) return false;
103 var found = 0;
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);
110 break;
112 stmt.reset();
114 //if not found then test for base domain
115 if (found==0) {
116 var cs = this.getCS();
117 var base = cs.removeSub(host);
118 if (base!=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);
122 break;
124 stmt.reset();
128 this._DB.commitTransaction();
129 return found;
132 getAllPermissions: function() {
133 if (!this._DB) this.openDatabaseConnection();
134 if (!this._DB) return '';
136 var entries = [];
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));
145 stmt.reset();
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);
166 try {
167 var entry;
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);
174 break;
176 } else {
177 while (entries.hasMore()) {
178 entry = entries.getNext();
179 zipReader.extract(entry,profile);
180 break;
183 } catch (e) {
184 throw e;
187 zipReader.close();
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) {
209 try {
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);
215 zipReader.open();
216 } else {
217 zipReader.open(zipFile);
219 } catch (e) {
220 zipReader.close();
221 throw e;
223 return zipReader;
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");
251 return false;
254 getCS: function() {
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;
278 return this;
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) {
306 return true;
309 nsCSPermManagerFactory: {
311 createInstance: function(outer, iid) {
312 if (outer != null)
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; }