1 /* See license.txt for terms of usage */
  2 
  3 define([
  4     "firebug/lib/xpcom",
  5     "firebug/lib/json",
  6     "firebug/lib/string",
  7 ],
  8 function(Xpcom, Json) {
  9 
 10 // ********************************************************************************************* //
 11 // Constants
 12 
 13 const ioService = Xpcom.CCSV("@mozilla.org/network/io-service;1", "nsIIOService");
 14 
 15 // ********************************************************************************************* //
 16 // Cookie object
 17 
 18 /**
 19  * @class Represents a cookie object that is created as a representation of
 20  * nsICookie component in the browser.
 21  */
 22 function Cookie(cookie, action)
 23 {
 24     this.cookie = cookie;
 25     this.action = action; 
 26     this.rawHost = makeStrippedHost(cookie.host);
 27 }
 28 
 29 Cookie.prototype =
 30 /** @lends Cookie */
 31 {
 32     cookie: null,
 33     action: null,
 34 
 35     toString: function(noDomain)
 36     {
 37         var expires = this.cookie.expires ? new Date(this.cookie.expires * 1000) : null;
 38         return this.cookie.name + "=" + this.cookie.rawValue +
 39             (expires ? "; expires=" + expires.toGMTString() : "") +
 40             ((this.cookie.path) ? "; path=" + this.cookie.path : "; path=/") +
 41             (noDomain ? "" : ((this.cookie.host) ? "; domain=" + this.cookie.host : "")) +
 42             ((this.cookie.isSecure) ? "; Secure" : "") + 
 43             ((this.cookie.isHttpOnly) ? "; HttpOnly" : "");
 44     },
 45 
 46     toJSON: function()
 47     {
 48         return JSON.stringify({
 49             name: this.cookie.name,
 50             value: this.cookie.rawValue,
 51             expires: (this.cookie.expires ? this.cookie.expires : 0),
 52             path: (this.cookie.path ? this.cookie.path : "/"),
 53             host: this.cookie.host,
 54             isHttpOnly: (this.cookie.isHttpOnly),
 55             isSecure: (this.cookie.isSecure)
 56         });
 57     },
 58 
 59     toText: function()
 60     {
 61         return this.cookie.host + "\t" +
 62             new String(this.cookie.isDomain).toUpperCase() + "\t" +
 63             this.cookie.path + "\t" +
 64             new String(this.cookie.isSecure).toUpperCase() + "\t" +
 65             this.cookie.expires + "\t" +
 66             this.cookie.name + "\t" +
 67             this.cookie.rawValue + "\r\n";
 68     },
 69 
 70     getJsonValue: function()
 71     {
 72         if (this.json)
 73             return this.json;
 74 
 75         var jsonString = new String(this.cookie.value);
 76         if (jsonString.indexOf("{") != 0)
 77             return null;
 78 
 79         var currentURI = Firebug.chrome.getCurrentURI();
 80         var jsonObject = Json.parseJSONString(jsonString, currentURI.spec);
 81         if (typeof (jsonObject) != "object")
 82             return null;
 83 
 84         if (FBTrace.DBG_COOKIES)
 85             FBTrace.sysout("cookies.getJsonValue for: " + this.cookie.name, jsonObject);
 86 
 87         return (this.json = jsonObject);
 88     },
 89 
 90     getXmlValue: function()
 91     {
 92         if (this.xml)
 93             return this.xml;
 94 
 95         try
 96         {
 97             var value = this.cookie.value;
 98 
 99             // Simple test if the source is XML (to avoid errors in the Firefox Error console)
100             if (value.indexOf("<") != 0)
101                 return null; 
102 
103             var parser = Xpcom.CCIN("@mozilla.org/xmlextras/domparser;1", "nsIDOMParser");
104             var doc = parser.parseFromString(value, "text/xml");
105             var docElem = doc.documentElement;
106 
107             if (FBTrace.DBG_COOKIES)
108                 FBTrace.sysout("cookies.getXmlValue for: " + this.cookie.name);
109 
110             // Error handling
111             var nsURI = "http://www.mozilla.org/newlayout/xml/parsererror.xml";
112             if (docElem.namespaceURI == nsURI && docElem.nodeName == "parsererror")
113                 return null; 
114 
115             return (this.xml = docElem);
116         }
117         catch (e)
118         {
119             if (FBTrace.DBG_ERRORS)
120                 FBTrace.sysout("cookies.getXmlValue ERROR " + this.cookie.name, e);
121         }
122 
123         return null;
124     },
125 
126     getURI: function()
127     {
128         try
129         {
130             var host = this.cookie.host;
131             var path = this.cookie.path;
132 
133             var httpProtocol = this.cookie.isSecure ? "https://" : "http://";
134             var uri = httpProtocol + host + (path ? path : "");
135             return ioService.newURI(uri, null, null);
136         }
137         catch (exc)
138         {
139             if (FBTrace.DBG_ERRORS || FBTrace.DBG_COOKIES)
140                 FBTrace.sysout("cookies.getURI FAILS for " + this.cookie.name);
141         }
142 
143         return null;
144     },
145 
146     getSize: function()
147     {
148         return this.cookie.name.length + this.cookie.value.length;
149     },
150 
151     getRawSize: function()
152     {
153         return this.cookie.name.length + this.cookie.rawValue.length
154     },
155 };
156 
157 // ********************************************************************************************* //
158 // Helpers
159 
160 // xxxHonza: duplicated in CookieUtils since cycle dep
161 function makeStrippedHost(aHost)
162 {
163     if (!aHost)
164         return aHost;
165 
166     var formattedHost = aHost.charAt(0) == "." ? aHost.substring(1, aHost.length) : aHost;
167     return formattedHost.substring(0, 4) == "www." ? formattedHost.substring(4, formattedHost.length) : formattedHost;
168 }
169 
170 // ********************************************************************************************* //
171 
172 return Cookie;
173 
174 // ********************************************************************************************* //
175 });
176 
177