| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('extensions', function() { | 5 cr.define('extensions', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * Clear all the content of a given element. | |
| 10 * @param {HTMLElement} element The element to be cleared. | |
| 11 */ | |
| 12 function clearElement(element) { | |
| 13 while (element.firstChild) | |
| 14 element.removeChild(element.firstChild); | |
| 15 } | |
| 16 | |
| 17 /** | |
| 18 * Get the url relative to the main extension url. If the url is | 9 * Get the url relative to the main extension url. If the url is |
| 19 * unassociated with the extension, this will be the full url. | 10 * unassociated with the extension, this will be the full url. |
| 20 * @param {string} url The url to make relative. | 11 * @param {string} url The url to make relative. |
| 21 * @param {string} extensionUrl The url for the extension resources, in the | 12 * @param {string} extensionUrl The url for the extension resources, in the |
| 22 * form "chrome-etxension://<extension_id>/". | 13 * form "chrome-etxension://<extension_id>/". |
| 23 * @return {string} The url relative to the host. | 14 * @return {string} The url relative to the host. |
| 24 */ | 15 */ |
| 25 function getRelativeUrl(url, extensionUrl) { | 16 function getRelativeUrl(url, extensionUrl) { |
| 26 return url.substring(0, extensionUrl.length) == extensionUrl ? | 17 return url.substring(0, extensionUrl.length) == extensionUrl ? |
| 27 url.substring(extensionUrl.length) : url; | 18 url.substring(extensionUrl.length) : url; |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 this.initStackTrace_(); | 132 this.initStackTrace_(); |
| 142 }, | 133 }, |
| 143 | 134 |
| 144 /** | 135 /** |
| 145 * Wipe content associated with a specific error. | 136 * Wipe content associated with a specific error. |
| 146 */ | 137 */ |
| 147 clearError: function() { | 138 clearError: function() { |
| 148 this.error_ = undefined; | 139 this.error_ = undefined; |
| 149 this.extensionUrl_ = undefined; | 140 this.extensionUrl_ = undefined; |
| 150 this.currentFrameNode_ = undefined; | 141 this.currentFrameNode_ = undefined; |
| 151 clearElement(this.stackTrace_); | 142 extensions.ExtensionUtil.clearElement(this.stackTrace_); |
| 152 this.stackTrace_.hidden = true; | 143 this.stackTrace_.hidden = true; |
| 153 }, | 144 }, |
| 154 | 145 |
| 155 /** | 146 /** |
| 156 * Makes |frame| active and deactivates the previously active frame (if | 147 * Makes |frame| active and deactivates the previously active frame (if |
| 157 * there was one). | 148 * there was one). |
| 158 * @param {HTMLElement} frame The frame to activate. | 149 * @param {HTMLElement} frame The frame to activate. |
| 159 * @private | 150 * @private |
| 160 */ | 151 */ |
| 161 setActiveFrame_: function(frameNode) { | 152 setActiveFrame_: function(frameNode) { |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 this.overlayDiv_ = $('extension-error-overlay'); | 364 this.overlayDiv_ = $('extension-error-overlay'); |
| 374 | 365 |
| 375 /** | 366 /** |
| 376 * The portion of the overlay which shows the code relating to the error | 367 * The portion of the overlay which shows the code relating to the error |
| 377 * and the corresponding line numbers. | 368 * and the corresponding line numbers. |
| 378 * @type {HTMLElement} | 369 * @type {HTMLElement} |
| 379 * @private | 370 * @private |
| 380 */ | 371 */ |
| 381 this.codeDiv_ = $('extension-error-overlay-code'); | 372 this.codeDiv_ = $('extension-error-overlay-code'); |
| 382 | 373 |
| 383 // Also initialize two properties of codeDiv for the section for the pure | |
| 384 // file content and the section for the line numbers. | |
| 385 this.codeDiv_.sourceDiv = | |
| 386 this.codeDiv_.querySelector('#extension-error-overlay-source'); | |
| 387 this.codeDiv_.linesDiv = | |
| 388 this.codeDiv_.querySelector('#extension-error-overlay-line-numbers'); | |
| 389 | |
| 390 /** | 374 /** |
| 391 * The function to show or hide the ExtensionErrorOverlay. | 375 * The function to show or hide the ExtensionErrorOverlay. |
| 392 * @type {function} | 376 * @type {function} |
| 393 * @param {boolean} isVisible Whether the overlay should be visible. | 377 * @param {boolean} isVisible Whether the overlay should be visible. |
| 394 */ | 378 */ |
| 395 this.setVisible = function(isVisible) { | 379 this.setVisible = function(isVisible) { |
| 396 showOverlay(isVisible ? this.overlayDiv_ : null); | 380 showOverlay(isVisible ? this.overlayDiv_ : null); |
| 397 }; | 381 }; |
| 398 | 382 |
| 399 /** | 383 /** |
| (...skipping 16 matching lines...) Expand all Loading... |
| 416 handleDismiss_: function(e) { | 400 handleDismiss_: function(e) { |
| 417 this.setVisible(false); | 401 this.setVisible(false); |
| 418 | 402 |
| 419 // There's a chance that the overlay receives multiple dismiss events; in | 403 // There's a chance that the overlay receives multiple dismiss events; in |
| 420 // this case, handle it gracefully and return (since all necessary work | 404 // this case, handle it gracefully and return (since all necessary work |
| 421 // will already have been done). | 405 // will already have been done). |
| 422 if (!this.error_) | 406 if (!this.error_) |
| 423 return; | 407 return; |
| 424 | 408 |
| 425 // Remove all previous content. | 409 // Remove all previous content. |
| 426 clearElement(this.codeDiv_.sourceDiv); | 410 extensions.ExtensionUtil.clearElement(this.codeDiv_); |
| 427 clearElement(this.codeDiv_.linesDiv); | |
| 428 | 411 |
| 429 this.openDevtoolsButton_.hidden = true; | 412 this.openDevtoolsButton_.hidden = true; |
| 430 | 413 |
| 431 if (this.error_.type == ExtensionErrorOverlay.RUNTIME_ERROR_TYPE_) { | 414 if (this.error_.type == ExtensionErrorOverlay.RUNTIME_ERROR_TYPE_) { |
| 432 this.overlayDiv_.querySelector('.content-area').removeChild( | 415 this.overlayDiv_.querySelector('.content-area').removeChild( |
| 433 this.runtimeErrorContent_); | 416 this.runtimeErrorContent_); |
| 434 this.runtimeErrorContent_.clearError(); | 417 this.runtimeErrorContent_.clearError(); |
| 435 } | 418 } |
| 436 | 419 |
| 437 this.error_ = undefined; | 420 this.error_ = undefined; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 * Set the code to be displayed in the code portion of the overlay. | 467 * Set the code to be displayed in the code portion of the overlay. |
| 485 * @see ExtensionErrorOverlay.requestFileSourceResponse(). | 468 * @see ExtensionErrorOverlay.requestFileSourceResponse(). |
| 486 * @param {?Object} code The code to be displayed. If |code| is null, then | 469 * @param {?Object} code The code to be displayed. If |code| is null, then |
| 487 * a "Could not display code" message will be displayed instead. | 470 * a "Could not display code" message will be displayed instead. |
| 488 */ | 471 */ |
| 489 setCode: function(code) { | 472 setCode: function(code) { |
| 490 document.querySelector( | 473 document.querySelector( |
| 491 '#extension-error-overlay .extension-error-overlay-title'). | 474 '#extension-error-overlay .extension-error-overlay-title'). |
| 492 textContent = code.title; | 475 textContent = code.title; |
| 493 | 476 |
| 494 // Remove all previous content. This should be done on close, but, just in | 477 extensions.ExtensionUtil.populateCodeDiv( |
| 495 // case we crashed, do it again. | 478 this.codeDiv_, |
| 496 clearElement(this.codeDiv_.sourceDiv); | 479 code, |
| 497 clearElement(this.codeDiv_.linesDiv); | 480 loadTimeData.getString('extensionErrorOverlayNoCodeToDisplay')); |
| 498 | |
| 499 // If there's no code, then display an appropriate message. | |
| 500 if (!code) { | |
| 501 var span = document.createElement('span'); | |
| 502 span.textContent = | |
| 503 loadTimeData.getString('extensionErrorOverlayNoCodeToDisplay'); | |
| 504 this.codeDiv_.sourceDiv.appendChild(span); | |
| 505 return; | |
| 506 } | |
| 507 | |
| 508 var lineCount = 0; | |
| 509 var createSpan = function(source, isHighlighted) { | |
| 510 lineCount += source.split('\n').length - 1; | |
| 511 var span = document.createElement('span'); | |
| 512 span.className = isHighlighted ? 'highlighted-source' : 'normal-source'; | |
| 513 span.textContent = source; | |
| 514 return span; | |
| 515 }; | |
| 516 | |
| 517 if (code.beforeHighlight) { | |
| 518 this.codeDiv_.sourceDiv.appendChild( | |
| 519 createSpan(code.beforeHighlight, false)); | |
| 520 } | |
| 521 | |
| 522 if (code.highlight) { | |
| 523 var highlightSpan = createSpan(code.highlight, true); | |
| 524 highlightSpan.title = code.message; | |
| 525 this.codeDiv_.sourceDiv.appendChild(highlightSpan); | |
| 526 } | |
| 527 | |
| 528 if (code.afterHighlight) { | |
| 529 this.codeDiv_.sourceDiv.appendChild( | |
| 530 createSpan(code.afterHighlight, false)); | |
| 531 } | |
| 532 | |
| 533 // Make the line numbers. This should be the number of line breaks + 1 | |
| 534 // (the last line doesn't break, but should still be numbered). | |
| 535 var content = ''; | |
| 536 for (var i = 1; i < lineCount + 1; ++i) | |
| 537 content += i + '\n'; | |
| 538 var span = document.createElement('span'); | |
| 539 span.textContent = content; | |
| 540 this.codeDiv_.linesDiv.appendChild(span); | |
| 541 }, | 481 }, |
| 542 }; | 482 }; |
| 543 | 483 |
| 544 /** | 484 /** |
| 545 * Called by the ExtensionErrorHandler responding to the request for a file's | 485 * Called by the ExtensionErrorHandler responding to the request for a file's |
| 546 * source. Populate the content area of the overlay and display the overlay. | 486 * source. Populate the content area of the overlay and display the overlay. |
| 547 * @param {Object?} result An object with four strings - the title, | 487 * @param {Object?} result An object with four strings - the title, |
| 548 * beforeHighlight, afterHighlight, and highlight. The three 'highlight' | 488 * beforeHighlight, afterHighlight, and highlight. The three 'highlight' |
| 549 * strings represent three portions of the file's content to display - the | 489 * strings represent three portions of the file's content to display - the |
| 550 * portion which is most relevant and should be emphasized (highlight), | 490 * portion which is most relevant and should be emphasized (highlight), |
| 551 * and the parts both before and after this portion. These may be empty. | 491 * and the parts both before and after this portion. These may be empty. |
| 552 */ | 492 */ |
| 553 ExtensionErrorOverlay.requestFileSourceResponse = function(result) { | 493 ExtensionErrorOverlay.requestFileSourceResponse = function(result) { |
| 554 var overlay = extensions.ExtensionErrorOverlay.getInstance(); | 494 var overlay = extensions.ExtensionErrorOverlay.getInstance(); |
| 555 overlay.setCode(result); | 495 overlay.setCode(result); |
| 556 overlay.setVisible(true); | 496 overlay.setVisible(true); |
| 557 }; | 497 }; |
| 558 | 498 |
| 559 // Export | 499 // Export |
| 560 return { | 500 return { |
| 561 ExtensionErrorOverlay: ExtensionErrorOverlay | 501 ExtensionErrorOverlay: ExtensionErrorOverlay |
| 562 }; | 502 }; |
| 563 }); | 503 }); |
| OLD | NEW |