CookieStorageContentView.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. WebInspector.CookieStorageContentView = function(representedObject)
  26. {
  27. WebInspector.ContentView.call(this, representedObject);
  28. this.element.classList.add(WebInspector.CookieStorageContentView.StyleClassName);
  29. this.update();
  30. };
  31. WebInspector.CookieStorageContentView.StyleClassName = "cookie-storage";
  32. WebInspector.CookieStorageContentView.prototype = {
  33. constructor: WebInspector.CookieStorageContentView,
  34. // Public
  35. update: function()
  36. {
  37. function callback(error, cookies, cookiesString)
  38. {
  39. if (error)
  40. return;
  41. this._cookies = this._filterCookies(cookies);
  42. this._rebuildTable();
  43. }
  44. PageAgent.getCookies(callback.bind(this));
  45. },
  46. updateLayout: function()
  47. {
  48. if (this._dataGrid)
  49. this._dataGrid.updateLayout();
  50. },
  51. get scrollableElements()
  52. {
  53. if (!this._dataGrid)
  54. return [];
  55. return [this._dataGrid.scrollContainer];
  56. },
  57. // Private
  58. _rebuildTable: function()
  59. {
  60. // FIXME: If there are no cookies, do we want to show an empty datagrid, or do something like the old
  61. // inspector and show some text saying there are no cookies?
  62. if (!this._dataGrid) {
  63. var columns = { 0: {}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {} };
  64. columns[0].title = WebInspector.UIString("Name");
  65. columns[0].sortable = true;
  66. columns[0].width = "24%";
  67. columns[1].title = WebInspector.UIString("Value");
  68. columns[1].sortable = true;
  69. columns[1].width = "34%";
  70. columns[2].title = WebInspector.UIString("Domain");
  71. columns[2].sortable = true;
  72. columns[2].width = "7%";
  73. columns[3].title = WebInspector.UIString("Path");
  74. columns[3].sortable = true;
  75. columns[3].width = "7%";
  76. columns[4].title = WebInspector.UIString("Expires");
  77. columns[4].sortable = true;
  78. columns[4].width = "7%";
  79. columns[5].title = WebInspector.UIString("Size");
  80. columns[5].aligned = "right";
  81. columns[5].sortable = true;
  82. columns[5].width = "7%";
  83. columns[6].title = WebInspector.UIString("HTTP");
  84. columns[6].aligned = "centered";
  85. columns[6].sortable = true;
  86. columns[6].width = "7%";
  87. columns[7].title = WebInspector.UIString("Secure");
  88. columns[7].aligned = "centered";
  89. columns[7].sortable = true;
  90. columns[7].width = "7%";
  91. this._dataGrid = new WebInspector.DataGrid(columns, null, this._deleteCallback.bind(this));
  92. this._dataGrid.addEventListener(WebInspector.DataGrid.Event.SortChanged, this._rebuildTable, this);
  93. this.element.appendChild(this._dataGrid.element);
  94. this._dataGrid.updateLayout();
  95. }
  96. console.assert(this._dataGrid);
  97. this._dataGrid.removeChildren();
  98. this._sortCookies(this._cookies);
  99. for (var i = 0; i < this._cookies.length; ++i) {
  100. const cookie = this._cookies[i];
  101. var data = {};
  102. data[0] = cookie.name;
  103. data[1] = cookie.value;
  104. data[2] = cookie.domain || "";
  105. data[3] = cookie.path || "";
  106. if (cookie.type === WebInspector.CookieType.Request)
  107. data[4] = "";
  108. else
  109. data[4] = cookie.session ? WebInspector.UIString("Session") : new Date(cookie.expires).toLocaleString();
  110. data[5] = Number.bytesToString(cookie.size);
  111. const checkmark = "\u2713";
  112. data[6] = cookie.httpOnly ? checkmark : "";
  113. data[7] = cookie.secure ? checkmark : "";
  114. var node = new WebInspector.DataGridNode(data);
  115. node.cookie = cookie;
  116. node.selectable = true;
  117. this._dataGrid.appendChild(node);
  118. }
  119. },
  120. _filterCookies: function(cookies)
  121. {
  122. var filteredCookies = [];
  123. var resourcesForDomain = [];
  124. var frames = WebInspector.frameResourceManager.frames;
  125. for (var i = 0; i < frames.length; ++i) {
  126. var resources = frames[i].resources;
  127. for (var j = 0; j < resources.length; ++j) {
  128. var urlComponents = resources[j].urlComponents;
  129. if (urlComponents && urlComponents.host && urlComponents.host === this.representedObject.host)
  130. resourcesForDomain.push(resources[j].url);
  131. }
  132. // The main resource isn't always in the list of resources, make sure to add it to the list of resources
  133. // we get the URLs from.
  134. var mainResourceURLComponents = frames[i].mainResource.urlComponents;
  135. if (mainResourceURLComponents && mainResourceURLComponents.host && mainResourceURLComponents.host == this.representedObject.host)
  136. resourcesForDomain.push(frames[i].mainResource.url);
  137. }
  138. for (var i = 0; i < cookies.length; ++i) {
  139. for (var j = 0; j < resourcesForDomain.length; ++j) {
  140. if (WebInspector.cookieMatchesResourceURL(cookies[i], resourcesForDomain[j])) {
  141. filteredCookies.push(cookies[i]);
  142. break;
  143. }
  144. }
  145. }
  146. return filteredCookies;
  147. },
  148. _sortCookies: function(cookies)
  149. {
  150. var sortDirection = this._dataGrid.sortOrder === "ascending" ? 1 : -1;
  151. function localeCompare(field, cookie1, cookie2)
  152. {
  153. return sortDirection * (cookie1[field] + "").localeCompare(cookie2[field] + "")
  154. }
  155. function numberCompare(field, cookie1, cookie2)
  156. {
  157. return sortDirection * (cookie1[field] - cookie2[field]);
  158. }
  159. function expiresCompare(cookie1, cookie2)
  160. {
  161. if (cookie1.session !== cookie2.session)
  162. return sortDirection * (cookie1.session ? 1 : -1);
  163. if (cookie1.session)
  164. return 0;
  165. return sortDirection * (cookie1.expires - cookie2.expires);
  166. }
  167. var comparator;
  168. switch (parseInt(this._dataGrid.sortColumnIdentifier, 10)) {
  169. case 0: comparator = localeCompare.bind(this, "name"); break;
  170. case 1: comparator = localeCompare.bind(this, "value"); break;
  171. case 2: comparator = localeCompare.bind(this, "domain"); break;
  172. case 3: comparator = localeCompare.bind(this, "path"); break;
  173. case 4: comparator = expiresCompare; break;
  174. case 5: comparator = numberCompare.bind(this, "size"); break;
  175. case 6: comparator = localeCompare.bind(this, "httpOnly"); break;
  176. case 7: comparator = localeCompare.bind(this, "secure"); break;
  177. default: localeCompare.bind(this, "name");
  178. }
  179. cookies.sort(comparator);
  180. },
  181. _deleteCallback: function(node)
  182. {
  183. if (!node || !node.cookie)
  184. return;
  185. var cookie = node.cookie;
  186. var cookieURL = (cookie.secure ? "https://" : "http://") + cookie.domain + cookie.path;
  187. // COMPATIBILITY (iOS 6): PageAgent.deleteCookie used to take 'domain', now takes 'url'. Send both.
  188. PageAgent.deleteCookie.invoke({cookieName: cookie.name, domain: cookie.domain, url: cookieURL});
  189. this.update();
  190. }
  191. };
  192. WebInspector.CookieStorageContentView.prototype.__proto__ = WebInspector.ContentView.prototype;
  193. WebInspector.cookieMatchesResourceURL = function(cookie, resourceURL)
  194. {
  195. var parsedURL = parseURL(resourceURL);
  196. if (!parsedURL || !WebInspector.cookieDomainMatchesResourceDomain(cookie.domain, parsedURL.host))
  197. return false;
  198. return (parsedURL.path.startsWith(cookie.path)
  199. && (!cookie.port || parsedURL.port == cookie.port)
  200. && (!cookie.secure || parsedURL.scheme === "https"));
  201. }
  202. WebInspector.cookieDomainMatchesResourceDomain = function(cookieDomain, resourceDomain)
  203. {
  204. if (cookieDomain.charAt(0) !== '.')
  205. return resourceDomain === cookieDomain;
  206. return !!resourceDomain.match(new RegExp("^([^\\.]+\\.)?" + cookieDomain.substring(1).escapeForRegExp() + "$"), "i");
  207. }
  208. WebInspector.CookieType = {
  209. Request: 0,
  210. Response: 1
  211. };