CookieItemsView.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2009 Apple Inc. All rights reserved.
  3. * Copyright (C) 2009 Joseph Pecoraro
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /**
  30. * @constructor
  31. * @extends {WebInspector.View}
  32. */
  33. WebInspector.CookieItemsView = function(treeElement, cookieDomain)
  34. {
  35. WebInspector.View.call(this);
  36. this.element.addStyleClass("storage-view");
  37. this._deleteButton = new WebInspector.StatusBarButton(WebInspector.UIString("Delete"), "delete-storage-status-bar-item");
  38. this._deleteButton.visible = false;
  39. this._deleteButton.addEventListener("click", this._deleteButtonClicked, this);
  40. this._clearButton = new WebInspector.StatusBarButton(WebInspector.UIString("Clear"), "clear-storage-status-bar-item");
  41. this._clearButton.visible = false;
  42. this._clearButton.addEventListener("click", this._clearButtonClicked, this);
  43. this._refreshButton = new WebInspector.StatusBarButton(WebInspector.UIString("Refresh"), "refresh-storage-status-bar-item");
  44. this._refreshButton.addEventListener("click", this._refreshButtonClicked, this);
  45. this._treeElement = treeElement;
  46. this._cookieDomain = cookieDomain;
  47. this._emptyView = new WebInspector.EmptyView(WebInspector.UIString("This site has no cookies."));
  48. this._emptyView.show(this.element);
  49. this.element.addEventListener("contextmenu", this._contextMenu.bind(this), true);
  50. }
  51. WebInspector.CookieItemsView.prototype = {
  52. get statusBarItems()
  53. {
  54. return [this._refreshButton.element, this._clearButton.element, this._deleteButton.element];
  55. },
  56. wasShown: function()
  57. {
  58. this._update();
  59. },
  60. willHide: function()
  61. {
  62. this._deleteButton.visible = false;
  63. },
  64. _update: function()
  65. {
  66. WebInspector.Cookies.getCookiesAsync(this._updateWithCookies.bind(this));
  67. },
  68. /**
  69. * @param {Array.<WebInspector.Cookie>} allCookies
  70. */
  71. _updateWithCookies: function(allCookies)
  72. {
  73. this._cookies = this._filterCookiesForDomain(allCookies);
  74. if (!this._cookies.length) {
  75. // Nothing to show.
  76. this._emptyView.show(this.element);
  77. this._clearButton.visible = false;
  78. this._deleteButton.visible = false;
  79. if (this._cookiesTable)
  80. this._cookiesTable.detach();
  81. return;
  82. }
  83. if (!this._cookiesTable)
  84. this._cookiesTable = new WebInspector.CookiesTable(false, this._update.bind(this), this._showDeleteButton.bind(this));
  85. this._cookiesTable.setCookies(this._cookies);
  86. this._emptyView.detach();
  87. this._cookiesTable.show(this.element);
  88. this._treeElement.subtitle = String.sprintf(WebInspector.UIString("%d cookies (%s)"), this._cookies.length,
  89. Number.bytesToString(this._totalSize));
  90. this._clearButton.visible = true;
  91. this._deleteButton.visible = !!this._cookiesTable.selectedCookie();
  92. },
  93. /**
  94. * @param {Array.<WebInspector.Cookie>} allCookies
  95. */
  96. _filterCookiesForDomain: function(allCookies)
  97. {
  98. var cookies = [];
  99. var resourceURLsForDocumentURL = [];
  100. this._totalSize = 0;
  101. function populateResourcesForDocuments(resource)
  102. {
  103. var url = resource.documentURL.asParsedURL();
  104. if (url && url.host == this._cookieDomain)
  105. resourceURLsForDocumentURL.push(resource.url);
  106. }
  107. WebInspector.forAllResources(populateResourcesForDocuments.bind(this));
  108. for (var i = 0; i < allCookies.length; ++i) {
  109. var pushed = false;
  110. var size = allCookies[i].size();
  111. for (var j = 0; j < resourceURLsForDocumentURL.length; ++j) {
  112. var resourceURL = resourceURLsForDocumentURL[j];
  113. if (WebInspector.Cookies.cookieMatchesResourceURL(allCookies[i], resourceURL)) {
  114. this._totalSize += size;
  115. if (!pushed) {
  116. pushed = true;
  117. cookies.push(allCookies[i]);
  118. }
  119. }
  120. }
  121. }
  122. return cookies;
  123. },
  124. clear: function()
  125. {
  126. this._cookiesTable.clear();
  127. this._update();
  128. },
  129. _clearButtonClicked: function()
  130. {
  131. this.clear();
  132. },
  133. _showDeleteButton: function()
  134. {
  135. this._deleteButton.visible = true;
  136. },
  137. _deleteButtonClicked: function()
  138. {
  139. var selectedCookie = this._cookiesTable.selectedCookie();
  140. if (selectedCookie) {
  141. selectedCookie.remove();
  142. this._update();
  143. }
  144. },
  145. _refreshButtonClicked: function(event)
  146. {
  147. this._update();
  148. },
  149. _contextMenu: function(event)
  150. {
  151. if (!this._cookies.length) {
  152. var contextMenu = new WebInspector.ContextMenu(event);
  153. contextMenu.appendItem(WebInspector.UIString("Refresh"), this._update.bind(this));
  154. contextMenu.show();
  155. }
  156. },
  157. __proto__: WebInspector.View.prototype
  158. }