FileContentView.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2012 Google 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 are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @extends {WebInspector.View}
  33. * @param {WebInspector.FileSystemModel.File} file
  34. */
  35. WebInspector.FileContentView = function(file)
  36. {
  37. WebInspector.View.call(this);
  38. this._innerView = /** @type {?WebInspector.View} */ (null);
  39. this._file = file;
  40. this._content = null;
  41. }
  42. WebInspector.FileContentView.prototype = {
  43. wasShown: function()
  44. {
  45. if (!this._innerView) {
  46. if (this._file.isTextFile)
  47. this._innerView = new WebInspector.EmptyView("");
  48. else
  49. this._innerView = new WebInspector.EmptyView(WebInspector.UIString("Binary File"));
  50. this.refresh();
  51. }
  52. this._innerView.show(this.element);
  53. },
  54. /**
  55. * @param {number} errorCode
  56. * @param {FileSystemAgent.Metadata} metadata
  57. */
  58. _metadataReceived: function(errorCode, metadata)
  59. {
  60. if (errorCode || !metadata)
  61. return;
  62. if (this._content) {
  63. if (!this._content.updateMetadata(metadata))
  64. return;
  65. var sourceFrame = /** @type {WebInspector.SourceFrame} */ (this._innerView);
  66. this._content.requestContent(sourceFrame.setContent.bind(sourceFrame));
  67. } else {
  68. this._innerView.detach();
  69. this._content = new WebInspector.FileContentView.FileContentProvider(this._file, metadata);
  70. this._innerView = new WebInspector.SourceFrame(this._content);
  71. this._innerView.show(this.element);
  72. }
  73. },
  74. refresh: function()
  75. {
  76. if (!this._innerView)
  77. return;
  78. if (this._file.isTextFile)
  79. this._file.requestMetadata(this._metadataReceived.bind(this));
  80. },
  81. __proto__: WebInspector.View.prototype
  82. }
  83. /**
  84. * @constructor
  85. * @implements {WebInspector.ContentProvider}
  86. * @param {WebInspector.FileSystemModel.File} file
  87. * @param {FileSystemAgent.Metadata} metadata
  88. */
  89. WebInspector.FileContentView.FileContentProvider = function(file, metadata)
  90. {
  91. this._file = file;
  92. this._metadata = metadata;
  93. }
  94. WebInspector.FileContentView.FileContentProvider.prototype = {
  95. /**
  96. * @return {string}
  97. */
  98. contentURL: function()
  99. {
  100. return this._file.url;
  101. },
  102. /**
  103. * @return {WebInspector.ResourceType}
  104. */
  105. contentType: function()
  106. {
  107. return this._file.resourceType;
  108. },
  109. /**
  110. * @param {function(?string, boolean, string)} callback
  111. */
  112. requestContent: function(callback)
  113. {
  114. var size = /** @type {number} */ (this._metadata.size);
  115. this._file.requestFileContent(true, 0, size, this._charset || "", this._fileContentReceived.bind(this, callback));
  116. },
  117. /**
  118. * @param {function(?string, boolean, string)} callback
  119. * @param {number} errorCode
  120. * @param {string=} content
  121. * @param {boolean=} base64Encoded
  122. * @param {string=} charset
  123. */
  124. _fileContentReceived: function(callback, errorCode, content, base64Encoded, charset)
  125. {
  126. if (errorCode || !content) {
  127. callback(null, false, "");
  128. return;
  129. }
  130. this._charset = charset;
  131. callback(content, false, this.contentType().canonicalMimeType());
  132. },
  133. /**
  134. * @param {string} query
  135. * @param {boolean} caseSensitive
  136. * @param {boolean} isRegex
  137. * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
  138. */
  139. searchInContent: function(query, caseSensitive, isRegex, callback)
  140. {
  141. setTimeout(callback.bind(null, []), 0);
  142. },
  143. /**
  144. * @param {FileSystemAgent.Metadata} metadata
  145. * @return {boolean}
  146. */
  147. updateMetadata: function(metadata)
  148. {
  149. if (this._metadata.modificationTime >= metadata.modificationTime)
  150. return false;
  151. this._metadata = metadata.modificationTime;
  152. return true;
  153. }
  154. }