UISourceCode.js 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /*
  2. * Copyright (C) 2011 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.Object}
  33. * @implements {WebInspector.ContentProvider}
  34. * @param {WebInspector.Project} project
  35. * @param {string} parentPath
  36. * @param {string} name
  37. * @param {string} url
  38. * @param {WebInspector.ResourceType} contentType
  39. * @param {boolean} isEditable
  40. */
  41. WebInspector.UISourceCode = function(project, parentPath, name, originURL, url, contentType, isEditable)
  42. {
  43. this._project = project;
  44. this._parentPath = parentPath;
  45. this._name = name;
  46. this._originURL = originURL;
  47. this._url = url;
  48. this._contentType = contentType;
  49. this._isEditable = isEditable;
  50. /** @type {!Array.<function(?string,boolean,string)>} */
  51. this._requestContentCallbacks = [];
  52. /** @type {!Set.<!WebInspector.LiveLocation>} */
  53. this._liveLocations = new Set();
  54. /** @type {!Array.<WebInspector.PresentationConsoleMessage>} */
  55. this._consoleMessages = [];
  56. /** @type {!Array.<WebInspector.Revision>} */
  57. this.history = [];
  58. if (this.isEditable() && this._url)
  59. this._restoreRevisionHistory();
  60. this._formatterMapping = new WebInspector.IdentityFormatterSourceMapping();
  61. }
  62. WebInspector.UISourceCode.Events = {
  63. FormattedChanged: "FormattedChanged",
  64. WorkingCopyChanged: "WorkingCopyChanged",
  65. WorkingCopyCommitted: "WorkingCopyCommitted",
  66. TitleChanged: "TitleChanged",
  67. SavedStateUpdated: "SavedStateUpdated",
  68. ConsoleMessageAdded: "ConsoleMessageAdded",
  69. ConsoleMessageRemoved: "ConsoleMessageRemoved",
  70. ConsoleMessagesCleared: "ConsoleMessagesCleared",
  71. SourceMappingChanged: "SourceMappingChanged",
  72. }
  73. WebInspector.UISourceCode.prototype = {
  74. /**
  75. * @return {string}
  76. */
  77. get url()
  78. {
  79. return this._url;
  80. },
  81. /**
  82. * @return {string}
  83. */
  84. name: function()
  85. {
  86. return this._name;
  87. },
  88. /**
  89. * @return {string}
  90. */
  91. parentPath: function()
  92. {
  93. return this._parentPath;
  94. },
  95. /**
  96. * @return {string}
  97. */
  98. path: function()
  99. {
  100. return this._parentPath ? this._parentPath + "/" + this._name : this._name;
  101. },
  102. /**
  103. * @return {string}
  104. */
  105. fullDisplayName: function()
  106. {
  107. return this._project.displayName() + "/" + (this._parentPath ? this._parentPath + "/" : "") + this.displayName(true);
  108. },
  109. /**
  110. * @param {boolean=} skipTrim
  111. * @return {string}
  112. */
  113. displayName: function(skipTrim)
  114. {
  115. var displayName = this.name() || WebInspector.UIString("(index)");
  116. return skipTrim ? displayName : displayName.trimEnd(100);
  117. },
  118. /**
  119. * @return {string}
  120. */
  121. uri: function()
  122. {
  123. var path = this.path();
  124. if (!this._project.id())
  125. return path;
  126. if (!path)
  127. return this._project.id();
  128. return this._project.id() + "/" + path;
  129. },
  130. /**
  131. * @return {string}
  132. */
  133. originURL: function()
  134. {
  135. return this._originURL;
  136. },
  137. /**
  138. * @return {boolean}
  139. */
  140. canRename: function()
  141. {
  142. return this._project.canRename();
  143. },
  144. /**
  145. * @param {string} newName
  146. * @param {function(boolean)} callback
  147. */
  148. rename: function(newName, callback)
  149. {
  150. this._project.rename(this, newName, innerCallback.bind(this));
  151. /**
  152. * @param {boolean} success
  153. * @param {string=} newName
  154. */
  155. function innerCallback(success, newName)
  156. {
  157. if (success)
  158. this._updateName(newName);
  159. callback(success);
  160. }
  161. },
  162. /**
  163. * @param {string} name
  164. */
  165. _updateName: function(name)
  166. {
  167. var oldURI = this.uri();
  168. this._name = name;
  169. // FIXME: why?
  170. this._url = name;
  171. this._originURL = name;
  172. this.dispatchEventToListeners(WebInspector.UISourceCode.Events.TitleChanged, oldURI);
  173. },
  174. /**
  175. * @return {string}
  176. */
  177. contentURL: function()
  178. {
  179. return this.originURL();
  180. },
  181. /**
  182. * @return {WebInspector.ResourceType}
  183. */
  184. contentType: function()
  185. {
  186. return this._contentType;
  187. },
  188. /**
  189. * @return {WebInspector.ScriptFile}
  190. */
  191. scriptFile: function()
  192. {
  193. return this._scriptFile;
  194. },
  195. /**
  196. * @param {WebInspector.ScriptFile} scriptFile
  197. */
  198. setScriptFile: function(scriptFile)
  199. {
  200. this._scriptFile = scriptFile;
  201. },
  202. /**
  203. * @return {WebInspector.StyleFile}
  204. */
  205. styleFile: function()
  206. {
  207. return this._styleFile;
  208. },
  209. /**
  210. * @param {WebInspector.StyleFile} styleFile
  211. */
  212. setStyleFile: function(styleFile)
  213. {
  214. this._styleFile = styleFile;
  215. },
  216. /**
  217. * @return {WebInspector.Project}
  218. */
  219. project: function()
  220. {
  221. return this._project;
  222. },
  223. /**
  224. * @param {function(?Date, ?number)} callback
  225. */
  226. requestMetadata: function(callback)
  227. {
  228. this._project.requestMetadata(this, callback);
  229. },
  230. /**
  231. * @param {function(?string,boolean,string)} callback
  232. */
  233. requestContent: function(callback)
  234. {
  235. if (this._content || this._contentLoaded) {
  236. callback(this._content, false, this._mimeType);
  237. return;
  238. }
  239. this._requestContentCallbacks.push(callback);
  240. if (this._requestContentCallbacks.length === 1)
  241. this._project.requestFileContent(this, this._fireContentAvailable.bind(this));
  242. },
  243. /**
  244. * @param {function()=} callback
  245. */
  246. checkContentUpdated: function(callback)
  247. {
  248. if (!this._project.canSetFileContent())
  249. return;
  250. if (this._checkingContent)
  251. return;
  252. this._checkingContent = true;
  253. this._project.requestFileContent(this, contentLoaded.bind(this));
  254. function contentLoaded(updatedContent)
  255. {
  256. if (updatedContent === null) {
  257. var workingCopy = this.workingCopy();
  258. this._commitContent("", false);
  259. this.setWorkingCopy(workingCopy);
  260. delete this._checkingContent;
  261. if (callback)
  262. callback();
  263. return;
  264. }
  265. if (typeof this._lastAcceptedContent === "string" && this._lastAcceptedContent === updatedContent) {
  266. delete this._checkingContent;
  267. if (callback)
  268. callback();
  269. return;
  270. }
  271. if (this._content === updatedContent) {
  272. delete this._lastAcceptedContent;
  273. delete this._checkingContent;
  274. if (callback)
  275. callback();
  276. return;
  277. }
  278. if (!this.isDirty()) {
  279. this._commitContent(updatedContent, false);
  280. delete this._checkingContent;
  281. if (callback)
  282. callback();
  283. return;
  284. }
  285. var shouldUpdate = window.confirm(WebInspector.UIString("This file was changed externally. Would you like to reload it?"));
  286. if (shouldUpdate)
  287. this._commitContent(updatedContent, false);
  288. else
  289. this._lastAcceptedContent = updatedContent;
  290. delete this._checkingContent;
  291. if (callback)
  292. callback();
  293. }
  294. },
  295. /**
  296. * @param {function(?string,boolean,string)} callback
  297. */
  298. requestOriginalContent: function(callback)
  299. {
  300. this._project.requestFileContent(this, callback);
  301. },
  302. /**
  303. * @param {string} content
  304. * @param {boolean} shouldSetContentInProject
  305. */
  306. _commitContent: function(content, shouldSetContentInProject)
  307. {
  308. delete this._lastAcceptedContent;
  309. this._content = content;
  310. this._contentLoaded = true;
  311. var lastRevision = this.history.length ? this.history[this.history.length - 1] : null;
  312. if (!lastRevision || lastRevision._content !== this._content) {
  313. var revision = new WebInspector.Revision(this, this._content, new Date());
  314. this.history.push(revision);
  315. revision._persist();
  316. }
  317. this._innerResetWorkingCopy();
  318. this._hasCommittedChanges = true;
  319. this.dispatchEventToListeners(WebInspector.UISourceCode.Events.WorkingCopyCommitted);
  320. if (this._url && WebInspector.fileManager.isURLSaved(this._url))
  321. this._saveURLWithFileManager(false, this._content);
  322. if (shouldSetContentInProject)
  323. this._project.setFileContent(this, this._content, function() { });
  324. },
  325. /**
  326. * @param {boolean} forceSaveAs
  327. */
  328. _saveURLWithFileManager: function(forceSaveAs, content)
  329. {
  330. WebInspector.fileManager.save(this._url, content, forceSaveAs, callback.bind(this));
  331. WebInspector.fileManager.close(this._url);
  332. function callback()
  333. {
  334. this._savedWithFileManager = true;
  335. this.dispatchEventToListeners(WebInspector.UISourceCode.Events.SavedStateUpdated);
  336. }
  337. },
  338. /**
  339. * @param {boolean} forceSaveAs
  340. */
  341. saveToFileSystem: function(forceSaveAs)
  342. {
  343. if (this.isDirty()) {
  344. this._saveURLWithFileManager(forceSaveAs, this.workingCopy());
  345. this.commitWorkingCopy(function() { });
  346. return;
  347. }
  348. this.requestContent(this._saveURLWithFileManager.bind(this, forceSaveAs));
  349. },
  350. /**
  351. * @return {boolean}
  352. */
  353. hasUnsavedCommittedChanges: function()
  354. {
  355. var mayHavePersistingExtensions = WebInspector.extensionServer.hasSubscribers(WebInspector.extensionAPI.Events.ResourceContentCommitted);
  356. if (this._savedWithFileManager || this.project().canSetFileContent() || mayHavePersistingExtensions)
  357. return false;
  358. return !!this._hasCommittedChanges;
  359. },
  360. /**
  361. * @param {string} content
  362. */
  363. addRevision: function(content)
  364. {
  365. this._commitContent(content, true);
  366. },
  367. _restoreRevisionHistory: function()
  368. {
  369. if (!window.localStorage)
  370. return;
  371. var registry = WebInspector.Revision._revisionHistoryRegistry();
  372. var historyItems = registry[this.url];
  373. if (!historyItems)
  374. return;
  375. function filterOutStale(historyItem)
  376. {
  377. // FIXME: Main frame might not have been loaded yet when uiSourceCodes for snippets are created.
  378. if (!WebInspector.resourceTreeModel.mainFrame)
  379. return false;
  380. return historyItem.loaderId === WebInspector.resourceTreeModel.mainFrame.loaderId;
  381. }
  382. historyItems = historyItems.filter(filterOutStale);
  383. if (!historyItems.length)
  384. return;
  385. for (var i = 0; i < historyItems.length; ++i) {
  386. var content = window.localStorage[historyItems[i].key];
  387. var timestamp = new Date(historyItems[i].timestamp);
  388. var revision = new WebInspector.Revision(this, content, timestamp);
  389. this.history.push(revision);
  390. }
  391. this._content = this.history[this.history.length - 1].content;
  392. this._hasCommittedChanges = true;
  393. this._contentLoaded = true;
  394. this._mimeType = this.canonicalMimeType();
  395. },
  396. _clearRevisionHistory: function()
  397. {
  398. if (!window.localStorage)
  399. return;
  400. var registry = WebInspector.Revision._revisionHistoryRegistry();
  401. var historyItems = registry[this.url];
  402. for (var i = 0; historyItems && i < historyItems.length; ++i)
  403. delete window.localStorage[historyItems[i].key];
  404. delete registry[this.url];
  405. window.localStorage["revision-history"] = JSON.stringify(registry);
  406. },
  407. revertToOriginal: function()
  408. {
  409. /**
  410. * @this {WebInspector.UISourceCode}
  411. * @param {?string} content
  412. * @param {boolean} contentEncoded
  413. * @param {string} mimeType
  414. */
  415. function callback(content, contentEncoded, mimeType)
  416. {
  417. if (typeof content !== "string")
  418. return;
  419. this.addRevision(content);
  420. }
  421. this.requestOriginalContent(callback.bind(this));
  422. WebInspector.notifications.dispatchEventToListeners(WebInspector.UserMetrics.UserAction, {
  423. action: WebInspector.UserMetrics.UserActionNames.ApplyOriginalContent,
  424. url: this.url
  425. });
  426. },
  427. /**
  428. * @param {function(WebInspector.UISourceCode)} callback
  429. */
  430. revertAndClearHistory: function(callback)
  431. {
  432. /**
  433. * @this {WebInspector.UISourceCode}
  434. * @param {?string} content
  435. * @param {boolean} contentEncoded
  436. * @param {string} mimeType
  437. */
  438. function revert(content, contentEncoded, mimeType)
  439. {
  440. if (typeof content !== "string")
  441. return;
  442. this.addRevision(content);
  443. this._clearRevisionHistory();
  444. this.history = [];
  445. callback(this);
  446. }
  447. this.requestOriginalContent(revert.bind(this));
  448. WebInspector.notifications.dispatchEventToListeners(WebInspector.UserMetrics.UserAction, {
  449. action: WebInspector.UserMetrics.UserActionNames.RevertRevision,
  450. url: this.url
  451. });
  452. },
  453. /**
  454. * @return {boolean}
  455. */
  456. isEditable: function()
  457. {
  458. return this._isEditable;
  459. },
  460. /**
  461. * @return {string}
  462. */
  463. workingCopy: function()
  464. {
  465. if (this._workingCopyGetter) {
  466. this._workingCopy = this._workingCopyGetter();
  467. delete this._workingCopyGetter;
  468. }
  469. if (this.isDirty())
  470. return this._workingCopy;
  471. return this._content;
  472. },
  473. resetWorkingCopy: function()
  474. {
  475. this._innerResetWorkingCopy();
  476. this.dispatchEventToListeners(WebInspector.UISourceCode.Events.WorkingCopyChanged);
  477. },
  478. _innerResetWorkingCopy: function()
  479. {
  480. delete this._workingCopy;
  481. delete this._workingCopyGetter;
  482. },
  483. /**
  484. * @param {string} newWorkingCopy
  485. */
  486. setWorkingCopy: function(newWorkingCopy)
  487. {
  488. if (!this._mimeType)
  489. this._mimeType = this.canonicalMimeType();
  490. this._workingCopy = newWorkingCopy;
  491. delete this._workingCopyGetter;
  492. this.dispatchEventToListeners(WebInspector.UISourceCode.Events.WorkingCopyChanged);
  493. },
  494. setWorkingCopyGetter: function(workingCopyGetter)
  495. {
  496. this._workingCopyGetter = workingCopyGetter;
  497. this.dispatchEventToListeners(WebInspector.UISourceCode.Events.WorkingCopyChanged);
  498. },
  499. removeWorkingCopyGetter: function()
  500. {
  501. if (!this._workingCopyGetter)
  502. return;
  503. this._workingCopy = this._workingCopyGetter();
  504. delete this._workingCopyGetter;
  505. },
  506. /**
  507. * @param {function(?string)} callback
  508. */
  509. commitWorkingCopy: function(callback)
  510. {
  511. if (!this.isDirty()) {
  512. callback(null);
  513. return;
  514. }
  515. this._commitContent(this.workingCopy(), true);
  516. callback(null);
  517. WebInspector.notifications.dispatchEventToListeners(WebInspector.UserMetrics.UserAction, {
  518. action: WebInspector.UserMetrics.UserActionNames.FileSaved,
  519. url: this.url
  520. });
  521. },
  522. /**
  523. * @return {boolean}
  524. */
  525. isDirty: function()
  526. {
  527. return typeof this._workingCopy !== "undefined" || typeof this._workingCopyGetter !== "undefined";
  528. },
  529. /**
  530. * @return {string}
  531. */
  532. mimeType: function()
  533. {
  534. return this._mimeType;
  535. },
  536. /**
  537. * @return {string}
  538. */
  539. canonicalMimeType: function()
  540. {
  541. return this.contentType().canonicalMimeType() || this._mimeType;
  542. },
  543. /**
  544. * @return {?string}
  545. */
  546. content: function()
  547. {
  548. return this._content;
  549. },
  550. /**
  551. * @param {string} query
  552. * @param {boolean} caseSensitive
  553. * @param {boolean} isRegex
  554. * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
  555. */
  556. searchInContent: function(query, caseSensitive, isRegex, callback)
  557. {
  558. var content = this.content();
  559. if (content) {
  560. var provider = new WebInspector.StaticContentProvider(this.contentType(), content);
  561. provider.searchInContent(query, caseSensitive, isRegex, callback);
  562. return;
  563. }
  564. this._project.searchInFileContent(this, query, caseSensitive, isRegex, callback);
  565. },
  566. /**
  567. * @param {?string} content
  568. * @param {boolean} contentEncoded
  569. * @param {string} mimeType
  570. */
  571. _fireContentAvailable: function(content, contentEncoded, mimeType)
  572. {
  573. this._contentLoaded = true;
  574. this._mimeType = mimeType;
  575. this._content = content;
  576. var callbacks = this._requestContentCallbacks.slice();
  577. this._requestContentCallbacks = [];
  578. for (var i = 0; i < callbacks.length; ++i)
  579. callbacks[i](content, contentEncoded, mimeType);
  580. if (this._formatOnLoad) {
  581. delete this._formatOnLoad;
  582. this.setFormatted(true);
  583. }
  584. },
  585. /**
  586. * @return {boolean}
  587. */
  588. contentLoaded: function()
  589. {
  590. return this._contentLoaded;
  591. },
  592. /**
  593. * @param {number} lineNumber
  594. * @param {number} columnNumber
  595. * @return {WebInspector.RawLocation}
  596. */
  597. uiLocationToRawLocation: function(lineNumber, columnNumber)
  598. {
  599. if (!this._sourceMapping)
  600. return null;
  601. var location = this._formatterMapping.formattedToOriginal(lineNumber, columnNumber);
  602. return this._sourceMapping.uiLocationToRawLocation(this, location[0], location[1]);
  603. },
  604. /**
  605. * @param {!WebInspector.LiveLocation} liveLocation
  606. */
  607. addLiveLocation: function(liveLocation)
  608. {
  609. this._liveLocations.add(liveLocation);
  610. },
  611. /**
  612. * @param {!WebInspector.LiveLocation} liveLocation
  613. */
  614. removeLiveLocation: function(liveLocation)
  615. {
  616. this._liveLocations.remove(liveLocation);
  617. },
  618. updateLiveLocations: function()
  619. {
  620. var items = this._liveLocations.items();
  621. for (var i = 0; i < items.length; ++i)
  622. items[i].update();
  623. },
  624. /**
  625. * @param {WebInspector.UILocation} uiLocation
  626. */
  627. overrideLocation: function(uiLocation)
  628. {
  629. var location = this._formatterMapping.originalToFormatted(uiLocation.lineNumber, uiLocation.columnNumber);
  630. uiLocation.lineNumber = location[0];
  631. uiLocation.columnNumber = location[1];
  632. return uiLocation;
  633. },
  634. /**
  635. * @return {Array.<WebInspector.PresentationConsoleMessage>}
  636. */
  637. consoleMessages: function()
  638. {
  639. return this._consoleMessages;
  640. },
  641. /**
  642. * @param {WebInspector.PresentationConsoleMessage} message
  643. */
  644. consoleMessageAdded: function(message)
  645. {
  646. this._consoleMessages.push(message);
  647. this.dispatchEventToListeners(WebInspector.UISourceCode.Events.ConsoleMessageAdded, message);
  648. },
  649. /**
  650. * @param {WebInspector.PresentationConsoleMessage} message
  651. */
  652. consoleMessageRemoved: function(message)
  653. {
  654. this._consoleMessages.remove(message);
  655. this.dispatchEventToListeners(WebInspector.UISourceCode.Events.ConsoleMessageRemoved, message);
  656. },
  657. consoleMessagesCleared: function()
  658. {
  659. this._consoleMessages = [];
  660. this.dispatchEventToListeners(WebInspector.UISourceCode.Events.ConsoleMessagesCleared);
  661. },
  662. /**
  663. * @return {boolean}
  664. */
  665. formatted: function()
  666. {
  667. return !!this._formatted;
  668. },
  669. /**
  670. * @param {boolean} formatted
  671. */
  672. setFormatted: function(formatted)
  673. {
  674. if (!this.contentLoaded()) {
  675. this._formatOnLoad = formatted;
  676. return;
  677. }
  678. if (this._formatted === formatted)
  679. return;
  680. if (this.isDirty())
  681. return;
  682. this._formatted = formatted;
  683. // Re-request content
  684. this._contentLoaded = false;
  685. this._content = false;
  686. WebInspector.UISourceCode.prototype.requestContent.call(this, didGetContent.bind(this));
  687. /**
  688. * @this {WebInspector.UISourceCode}
  689. * @param {?string} content
  690. * @param {boolean} contentEncoded
  691. * @param {string} mimeType
  692. */
  693. function didGetContent(content, contentEncoded, mimeType)
  694. {
  695. var formatter;
  696. if (!formatted)
  697. formatter = new WebInspector.IdentityFormatter();
  698. else
  699. formatter = WebInspector.Formatter.createFormatter(this.contentType());
  700. formatter.formatContent(mimeType, content || "", formattedChanged.bind(this));
  701. /**
  702. * @this {WebInspector.UISourceCode}
  703. * @param {string} content
  704. * @param {WebInspector.FormatterSourceMapping} formatterMapping
  705. */
  706. function formattedChanged(content, formatterMapping)
  707. {
  708. this._content = content;
  709. this._innerResetWorkingCopy();
  710. this._formatterMapping = formatterMapping;
  711. this.dispatchEventToListeners(WebInspector.UISourceCode.Events.FormattedChanged, {content: content});
  712. this.updateLiveLocations();
  713. }
  714. }
  715. },
  716. /**
  717. * @return {WebInspector.Formatter} formatter
  718. */
  719. createFormatter: function()
  720. {
  721. // overridden by subclasses.
  722. return null;
  723. },
  724. /**
  725. * @param {WebInspector.SourceMapping} sourceMapping
  726. */
  727. setSourceMapping: function(sourceMapping)
  728. {
  729. var wasIdentity = this._sourceMapping ? this._sourceMapping.isIdentity() : true;
  730. this._sourceMapping = sourceMapping;
  731. var data = {}
  732. data.isIdentity = sourceMapping ? sourceMapping.isIdentity() : true;
  733. data.identityHasChanged = data.isIdentity !== wasIdentity;
  734. this.dispatchEventToListeners(WebInspector.UISourceCode.Events.SourceMappingChanged, data);
  735. },
  736. __proto__: WebInspector.Object.prototype
  737. }
  738. /**
  739. * @constructor
  740. * @param {WebInspector.UISourceCode} uiSourceCode
  741. * @param {number} lineNumber
  742. * @param {number} columnNumber
  743. */
  744. WebInspector.UILocation = function(uiSourceCode, lineNumber, columnNumber)
  745. {
  746. this.uiSourceCode = uiSourceCode;
  747. this.lineNumber = lineNumber;
  748. this.columnNumber = columnNumber;
  749. }
  750. WebInspector.UILocation.prototype = {
  751. /**
  752. * @return {WebInspector.RawLocation}
  753. */
  754. uiLocationToRawLocation: function()
  755. {
  756. return this.uiSourceCode.uiLocationToRawLocation(this.lineNumber, this.columnNumber);
  757. },
  758. /**
  759. * @return {?string}
  760. */
  761. url: function()
  762. {
  763. return this.uiSourceCode.contentURL();
  764. },
  765. /**
  766. * @return {string}
  767. */
  768. linkText: function()
  769. {
  770. var linkText = this.uiSourceCode.displayName();
  771. if (typeof this.lineNumber === "number")
  772. linkText += ":" + (this.lineNumber + 1);
  773. return linkText;
  774. }
  775. }
  776. /**
  777. * @interface
  778. */
  779. WebInspector.RawLocation = function()
  780. {
  781. }
  782. /**
  783. * @constructor
  784. * @param {WebInspector.RawLocation} rawLocation
  785. * @param {function(WebInspector.UILocation):(boolean|undefined)} updateDelegate
  786. */
  787. WebInspector.LiveLocation = function(rawLocation, updateDelegate)
  788. {
  789. this._rawLocation = rawLocation;
  790. this._updateDelegate = updateDelegate;
  791. this._uiSourceCodes = [];
  792. }
  793. WebInspector.LiveLocation.prototype = {
  794. update: function()
  795. {
  796. var uiLocation = this.uiLocation();
  797. if (uiLocation) {
  798. var uiSourceCode = uiLocation.uiSourceCode;
  799. if (this._uiSourceCodes.indexOf(uiSourceCode) === -1) {
  800. uiSourceCode.addLiveLocation(this);
  801. this._uiSourceCodes.push(uiSourceCode);
  802. }
  803. var oneTime = this._updateDelegate(uiLocation);
  804. if (oneTime)
  805. this.dispose();
  806. }
  807. },
  808. /**
  809. * @return {WebInspector.RawLocation}
  810. */
  811. rawLocation: function()
  812. {
  813. return this._rawLocation;
  814. },
  815. /**
  816. * @return {WebInspector.UILocation}
  817. */
  818. uiLocation: function()
  819. {
  820. // Should be overridden by subclasses.
  821. },
  822. dispose: function()
  823. {
  824. for (var i = 0; i < this._uiSourceCodes.length; ++i)
  825. this._uiSourceCodes[i].removeLiveLocation(this);
  826. this._uiSourceCodes = [];
  827. }
  828. }
  829. /**
  830. * @constructor
  831. * @implements {WebInspector.ContentProvider}
  832. * @param {WebInspector.UISourceCode} uiSourceCode
  833. * @param {?string|undefined} content
  834. * @param {Date} timestamp
  835. */
  836. WebInspector.Revision = function(uiSourceCode, content, timestamp)
  837. {
  838. this._uiSourceCode = uiSourceCode;
  839. this._content = content;
  840. this._timestamp = timestamp;
  841. }
  842. WebInspector.Revision._revisionHistoryRegistry = function()
  843. {
  844. if (!WebInspector.Revision._revisionHistoryRegistryObject) {
  845. if (window.localStorage) {
  846. var revisionHistory = window.localStorage["revision-history"];
  847. try {
  848. WebInspector.Revision._revisionHistoryRegistryObject = revisionHistory ? JSON.parse(revisionHistory) : {};
  849. } catch (e) {
  850. WebInspector.Revision._revisionHistoryRegistryObject = {};
  851. }
  852. } else
  853. WebInspector.Revision._revisionHistoryRegistryObject = {};
  854. }
  855. return WebInspector.Revision._revisionHistoryRegistryObject;
  856. }
  857. WebInspector.Revision.filterOutStaleRevisions = function()
  858. {
  859. if (!window.localStorage)
  860. return;
  861. var registry = WebInspector.Revision._revisionHistoryRegistry();
  862. var filteredRegistry = {};
  863. for (var url in registry) {
  864. var historyItems = registry[url];
  865. var filteredHistoryItems = [];
  866. for (var i = 0; historyItems && i < historyItems.length; ++i) {
  867. var historyItem = historyItems[i];
  868. if (historyItem.loaderId === WebInspector.resourceTreeModel.mainFrame.loaderId) {
  869. filteredHistoryItems.push(historyItem);
  870. filteredRegistry[url] = filteredHistoryItems;
  871. } else
  872. delete window.localStorage[historyItem.key];
  873. }
  874. }
  875. WebInspector.Revision._revisionHistoryRegistryObject = filteredRegistry;
  876. function persist()
  877. {
  878. window.localStorage["revision-history"] = JSON.stringify(filteredRegistry);
  879. }
  880. // Schedule async storage.
  881. setTimeout(persist, 0);
  882. }
  883. WebInspector.Revision.prototype = {
  884. /**
  885. * @return {WebInspector.UISourceCode}
  886. */
  887. get uiSourceCode()
  888. {
  889. return this._uiSourceCode;
  890. },
  891. /**
  892. * @return {Date}
  893. */
  894. get timestamp()
  895. {
  896. return this._timestamp;
  897. },
  898. /**
  899. * @return {?string}
  900. */
  901. get content()
  902. {
  903. return this._content || null;
  904. },
  905. revertToThis: function()
  906. {
  907. function revert(content)
  908. {
  909. if (this._uiSourceCode._content !== content)
  910. this._uiSourceCode.addRevision(content);
  911. }
  912. this.requestContent(revert.bind(this));
  913. },
  914. /**
  915. * @return {string}
  916. */
  917. contentURL: function()
  918. {
  919. return this._uiSourceCode.originURL();
  920. },
  921. /**
  922. * @return {WebInspector.ResourceType}
  923. */
  924. contentType: function()
  925. {
  926. return this._uiSourceCode.contentType();
  927. },
  928. /**
  929. * @param {function(?string, boolean, string)} callback
  930. */
  931. requestContent: function(callback)
  932. {
  933. callback(this._content || "", false, this.uiSourceCode.canonicalMimeType());
  934. },
  935. /**
  936. * @param {string} query
  937. * @param {boolean} caseSensitive
  938. * @param {boolean} isRegex
  939. * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
  940. */
  941. searchInContent: function(query, caseSensitive, isRegex, callback)
  942. {
  943. callback([]);
  944. },
  945. _persist: function()
  946. {
  947. if (this._uiSourceCode.project().type() === WebInspector.projectTypes.FileSystem)
  948. return;
  949. if (!window.localStorage)
  950. return;
  951. var url = this.contentURL();
  952. if (!url || url.startsWith("inspector://"))
  953. return;
  954. var loaderId = WebInspector.resourceTreeModel.mainFrame.loaderId;
  955. var timestamp = this.timestamp.getTime();
  956. var key = "revision-history|" + url + "|" + loaderId + "|" + timestamp;
  957. var registry = WebInspector.Revision._revisionHistoryRegistry();
  958. var historyItems = registry[url];
  959. if (!historyItems) {
  960. historyItems = [];
  961. registry[url] = historyItems;
  962. }
  963. historyItems.push({url: url, loaderId: loaderId, timestamp: timestamp, key: key});
  964. function persist()
  965. {
  966. window.localStorage[key] = this._content;
  967. window.localStorage["revision-history"] = JSON.stringify(registry);
  968. }
  969. // Schedule async storage.
  970. setTimeout(persist.bind(this), 0);
  971. }
  972. }