extensions/wtf-injector-chrome/wtf-injector.js
function fetchOptions() {
  /**
   * Name of the cookie that contains the options for the injection.
   * The data is just a blob GUID that is used to construct a URL to the blob
   * exposed by the extension.
   * @const
   * @type {string}
   */
  var WTF_OPTIONS_COOKIE = 'wtf';

  /**
   * Cookie used for instrumentation options.
   * @const
   * @type {string}
   */
  var WTF_INSTRUMENTATION_COOKIE = 'wtfi';

  // Check for the injection cookie.
  var optionsUuid = null;
  var instrumentationOptions = null;
  var cookies = document.cookie.split('; ');
  for (var n = 0; n < cookies.length; n++) {
    if (cookies[n].lastIndexOf(WTF_OPTIONS_COOKIE + '=') == 0) {
      optionsUuid = cookies[n].substr(cookies[n].indexOf('=') + 1);
    }
    if (cookies[n].lastIndexOf(WTF_INSTRUMENTATION_COOKIE + '=') == 0) {
      instrumentationOptions = cookies[n].substr(cookies[n].indexOf('=') + 1);
    }
  }
  if (!optionsUuid && !instrumentationOptions) {
    return null;
  }

  // If we have an instrumentation cookie we use that and go into
  // instrumentation mode.
  if (instrumentationOptions) {
    instrumentationOptions = JSON.parse(instrumentationOptions);
    instrumentationOptions['__instrumented__'] = true;
    return instrumentationOptions;
  }

  // Fetch the options from the extension.
  // This is complicated by a regression in Chrome that prevents the blob trick
  // from working in certain versions. We try that first (as it's the best) and
  // if it fails we fallback to a nasty HTTP header trick.
  // https://code.google.com/p/chromium/issues/detail?id=295829

  // blob:chrome-extension%3A//[extension id]/[options uuid]
  var blobUrl = 'blob:' +
      chrome.extension.getURL(optionsUuid).replace(':', '%3A');
  var headerUrl =
      'http://tracing-framework.appspot.com/tab-options/' + optionsUuid;

  try {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', blobUrl, false);
    xhr.send(null);
    if (xhr.status != 200) {
      log('Failed to load WTF injection options:',
          blobUrl,
          xhr.status, xhr.statusText);
      return null;
    }
    return JSON.parse(xhr.responseText);
  } catch(e) {
    log('Failed to parse WTF injection options (falling back to headers)... ' +
        'See https://code.google.com/p/chromium/issues/detail?id=295829');

    // Try the headers.
    try {
      var xhr = new XMLHttpRequest();
      xhr.open('GET', headerUrl, false);
      xhr.send(null);
      var optionsData = xhr.getResponseHeader('X-WTF-Options');
      if (!optionsData) {
        log('Failed to load WTF injection options from header:' + headerUrl);
        log('Using defaults for settings :(');
        return {
          'wtf.injector': true,
          'wtf.injector.failed': true,
          'wtf.trace.session.bufferSize': 6 * 1024 * 1024,
          'wtf.trace.session.maximumMemoryUsage': 512 * 1024 * 1024,
          'wtf.trace.provider.chromeDebug.present': true,
          'wtf.trace.provider.chromeDebug.tracing': false
        };
      }
      return JSON.parse(optionsData);
    } catch(e) {
      log('Really failed to fetch WTF injection options, aborting', e);

      // Try again!
      window.setTimeout(function() {
        window.location.reload();
      }, 100);

      return null;
    }
  }

  return null;
}
function fetchOptions() {
  /**
   * Name of the cookie that contains the options for the injection.
   * The data is just a blob GUID that is used to construct a URL to the blob
   * exposed by the extension.
   * @const
   * @type {string}
   */
  var WTF_OPTIONS_COOKIE = 'wtf';

  /**
   * Cookie used for instrumentation options.
   * @const
   * @type {string}
   */
  var WTF_INSTRUMENTATION_COOKIE = 'wtfi';

  // Check for the injection cookie.
  var optionsUuid = null;
  var instrumentationOptions = null;
  var cookies = document.cookie.split('; ');
  for (var n = 0; n < cookies.length; n++) {
    if (cookies[n].lastIndexOf(WTF_OPTIONS_COOKIE + '=') == 0) {
      optionsUuid = cookies[n].substr(cookies[n].indexOf('=') + 1);
    }
    if (cookies[n].lastIndexOf(WTF_INSTRUMENTATION_COOKIE + '=') == 0) {
      instrumentationOptions = cookies[n].substr(cookies[n].indexOf('=') + 1);
    }
  }
  if (!optionsUuid && !instrumentationOptions) {
    return null;
  }

  // If we have an instrumentation cookie we use that and go into
  // instrumentation mode.
  if (instrumentationOptions) {
    instrumentationOptions = JSON.parse(instrumentationOptions);
    instrumentationOptions['__instrumented__'] = true;
    return instrumentationOptions;
  }

  // Fetch the options from the extension.
  // This is complicated by a regression in Chrome that prevents the blob trick
  // from working in certain versions. We try that first (as it's the best) and
  // if it fails we fallback to a nasty HTTP header trick.
  // https://code.google.com/p/chromium/issues/detail?id=295829

  // blob:chrome-extension%3A//[extension id]/[options uuid]
  var blobUrl = 'blob:' +
      chrome.extension.getURL(optionsUuid).replace(':', '%3A');
  var headerUrl =
      'http://tracing-framework.appspot.com/tab-options/' + optionsUuid;

  try {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', blobUrl, false);
    xhr.send(null);
    if (xhr.status != 200) {
      log('Failed to load WTF injection options:',
          blobUrl,
          xhr.status, xhr.statusText);
      return null;
    }
    return JSON.parse(xhr.responseText);
  } catch(e) {
    log('Failed to parse WTF injection options (falling back to headers)... ' +
        'See https://code.google.com/p/chromium/issues/detail?id=295829');

    // Try the headers.
    try {
      var xhr = new XMLHttpRequest();
      xhr.open('GET', headerUrl, false);
      xhr.send(null);
      var optionsData = xhr.getResponseHeader('X-WTF-Options');
      if (!optionsData) {
        log('Failed to load WTF injection options from header:' + headerUrl);
        log('Using defaults for settings :(');
        return {
          'wtf.injector': true,
          'wtf.injector.failed': true,
          'wtf.trace.session.bufferSize': 6 * 1024 * 1024,
          'wtf.trace.session.maximumMemoryUsage': 512 * 1024 * 1024,
          'wtf.trace.provider.chromeDebug.present': true,
          'wtf.trace.provider.chromeDebug.tracing': false
        };
      }
      return JSON.parse(optionsData);
    } catch(e) {
      log('Really failed to fetch WTF injection options, aborting', e);

      // Try again!
      window.setTimeout(function() {
        window.location.reload();
      }, 100);

      return null;
    }
  }

  return null;
}
function fetchOptions ( ) { /**
   * Name of the cookie that contains the options for the injection.
   * The data is just a blob GUID that is used to construct a URL to the blob
   * exposed by the extension.
   * @const
   * @type {string}
   */ var WTF_OPTIONS_COOKIE = 'wtf' ; /**
   * Cookie used for instrumentation options.
   * @const
   * @type {string}
   */ var WTF_INSTRUMENTATION_COOKIE = 'wtfi' ; // Check for the injection cookie. var optionsUuid = null ; var instrumentationOptions = null ; var cookies = document . cookie . split ( '; ' ) ; for ( var n = 0 ; n < cookies . length ; n ++ ) { if ( cookies [ n ] . lastIndexOf ( WTF_OPTIONS_COOKIE + '=' ) == 0 ) { optionsUuid = cookies [ n ] . substr ( cookies [ n ] . indexOf ( '=' ) + 1 ) ; } if ( cookies [ n ] . lastIndexOf ( WTF_INSTRUMENTATION_COOKIE + '=' ) == 0 ) { instrumentationOptions = cookies [ n ] . substr ( cookies [ n ] . indexOf ( '=' ) + 1 ) ; } } if ( ! optionsUuid && ! instrumentationOptions ) { return null ; } // If we have an instrumentation cookie we use that and go into // instrumentation mode. if ( instrumentationOptions ) { instrumentationOptions = JSON . parse ( instrumentationOptions ) ; instrumentationOptions [ '__instrumented__' ] = true ; return instrumentationOptions ; } // Fetch the options from the extension. // This is complicated by a regression in Chrome that prevents the blob trick // from working in certain versions. We try that first (as it's the best) and // if it fails we fallback to a nasty HTTP header trick. // https://code.google.com/p/chromium/issues/detail?id=295829 // blob:chrome-extension%3A//[extension id]/[options uuid] var blobUrl = 'blob:' + chrome . extension . getURL ( optionsUuid ) . replace ( ':' , '%3A' ) ; var headerUrl = 'http://tracing-framework.appspot.com/tab-options/' + optionsUuid ; try { var xhr = new XMLHttpRequest ( ) ; xhr . open ( 'GET' , blobUrl , false ) ; xhr . send ( null ) ; if ( xhr . status != 200 ) { log ( 'Failed to load WTF injection options:' , blobUrl , xhr . status , xhr . statusText ) ; return null ; } return JSON . parse ( xhr . responseText ) ; } catch ( e ) { log ( 'Failed to parse WTF injection options (falling back to headers)... ' + 'See https://code.google.com/p/chromium/issues/detail?id=295829' ) ; // Try the headers. try { var xhr = new XMLHttpRequest ( ) ; xhr . open ( 'GET' , headerUrl , false ) ; xhr . send ( null ) ; var optionsData = xhr . getResponseHeader ( 'X-WTF-Options' ) ; if ( ! optionsData ) { log ( 'Failed to load WTF injection options from header:' + headerUrl ) ; log ( 'Using defaults for settings :(' ) ; return { 'wtf.injector' : true , 'wtf.injector.failed' : true , 'wtf.trace.session.bufferSize' : 6 * 1024 * 1024 , 'wtf.trace.session.maximumMemoryUsage' : 512 * 1024 * 1024 , 'wtf.trace.provider.chromeDebug.present' : true , 'wtf.trace.provider.chromeDebug.tracing' : false } ; } return JSON . parse ( optionsData ) ; } catch ( e ) { log ( 'Really failed to fetch WTF injection options, aborting' , e ) ; // Try again! window . setTimeout ( function ( ) { window . location . reload ( ) ; } , 100 ) ; return null ; } } return null ; }
Fetches the options from the extension background page.
This will only return a value if the options were injected in a cookie, and
if no options are returned it means the injection is not active.
@return {!Object} Options object.
Fetches the options from the extension background page . This will only return a value if the options were injected in a cookie and if no options are returned it means the injection is not active .