Home | History | Annotate | Download | only in tracing_examples
      1 <!DOCTYPE html>
      2 <html>
      3 <!--
      4 Copyright (c) 2011 The Chromium Authors. All rights reserved.
      5 Use of this source code is governed by a BSD-style license that can be
      6 found in the LICENSE file.
      7 -->
      8 <head>
      9 <title>Simple Embedded Viewer</title>
     10 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     11 
     12 <link rel="import" href="/tracing/base/timing.html">
     13 <link rel="import" href="/tracing/base/xhr.html">
     14 <link rel="import" href="/tracing/importer/import.html">
     15 <link rel="import" href="/tracing/ui/extras/full_config.html">
     16 <link rel="import" href="/tracing/ui/timeline_view.html">
     17 
     18 <style>
     19   html,
     20   body {
     21     height: 100%;
     22   }
     23 
     24   body {
     25     -webkit-flex-direction: column;
     26     display: -webkit-flex;
     27     margin: 0;
     28     padding: 0;
     29   }
     30 
     31   body > tr-ui-timeline-view {
     32     -webkit-flex: 1 1 auto;
     33     min-height: 0;
     34   }
     35   body > tr-ui-timeline-view:focus {
     36     outline: none;
     37   }
     38 </style>
     39 </head>
     40 <body>
     41   <tr-ui-timeline-view>
     42     <track-view-container id='track_view_container'></track-view-container>
     43   </tr-ui-timeline-view>
     44 
     45   <script>
     46   'use strict';
     47 
     48   var Timing = tr.b.Timing;
     49   var timelineViewEl;
     50   var selectEl;
     51 
     52   function loadTraces(filenames, onTracesLoaded) {
     53     var loadTracesMakedTimer = Timing.mark('TraceImport', 'loadTraces');
     54     var traces = [];
     55     for (var i = 0; i < filenames.length; i++) {
     56       traces.push(undefined);
     57     }
     58     var numTracesPending = filenames.length;
     59 
     60     filenames.forEach(function(filename, i) {
     61       getAsync(filename, function(trace) {
     62         traces[i] = trace;
     63         numTracesPending--;
     64         if (numTracesPending == 0) {
     65           loadTracesMakedTimer.end();
     66           onTracesLoaded(filenames, traces);
     67         }
     68       });
     69     });
     70   }
     71 
     72 
     73   function getAsync(url, cb) {
     74     return tr.b.getAsync(url).then(cb);
     75   }
     76 
     77   function createViewFromTraces(filenames, traces) {
     78     var createViewFromTracesTimer = Timing.mark(
     79         'TraceImport', 'createViewFromTraces');
     80     var m = new tr.Model();
     81 
     82     var trackDetailedModelStatsEl =
     83         tr.b.findDeepElementMatching(document.body,
     84                                      '#track-detailed-model-stats');
     85     var importOptions = new tr.importer.ImportOptions();
     86     importOptions.trackDetailedModelStats = trackDetailedModelStatsEl.checked;
     87     var i = new tr.importer.Import(m, importOptions);
     88     var p = i.importTracesWithProgressDialog(traces);
     89 
     90     p.then(
     91       function() {
     92         timelineViewEl.model = m;
     93         timelineViewEl.updateDocumentFavicon();
     94         timelineViewEl.globalMode = true;
     95         timelineViewEl.viewTitle = '';
     96         createViewFromTracesTimer.end();
     97       },
     98       function(err) {
     99         var overlay = new tr.ui.b.Overlay();
    100         overlay.textContent = tr.b.normalizeException(err).message;
    101         overlay.title = 'Import error';
    102         overlay.visible = true;
    103         createViewFromTracesTimer.end();
    104       });
    105   }
    106 
    107   function onSelectionChange() {
    108     window.location.hash = '#' + selectEl[selectEl.selectedIndex].value;
    109   }
    110 
    111   function onHashChange() {
    112     var file = window.location.hash.substr(1);
    113     if (selectEl[selectEl.selectedIndex].value != file) {
    114       for (var i = 0; i < selectEl.children.length; i++) {
    115         if (selectEl.children[i].value === file) {
    116           selectEl.selectedIndex = i;
    117           break;
    118         }
    119       }
    120     }
    121     reload();
    122   }
    123 
    124   function cleanFilename(file) {
    125     var m = /\/tracing\/test_data\/(.+)/.exec(file);
    126     var rest = m[1];
    127 
    128     function upcase(letter) {
    129       return ' ' + letter.toUpperCase();
    130     }
    131 
    132     return rest.replace(/_/g, ' ')
    133                .replace(/\.[^\.]*$/, '')
    134                .replace(/ ([a-z])/g, upcase)
    135                .replace(/^[a-z]/, upcase);
    136   }
    137 
    138   function reload() {
    139     loadTraces([window.location.hash.substr(1)], createViewFromTraces);
    140   }
    141 
    142   window.addEventListener('hashchange', onHashChange);
    143 
    144   function onLoad() {
    145     var onLoadTimer = Timing.mark('TraceImport', 'onLoad');
    146     timelineViewEl = document.querySelector('tr-ui-timeline-view');
    147     timelineViewEl.globalMode = true;
    148 
    149 
    150     selectEl = document.createElement('select');
    151     timelineViewEl.leftControls.appendChild(selectEl);
    152 
    153     getAsync('/tracing/test_data/__file_list__', function(data) {
    154       var files = JSON.parse(data);
    155       for (var i = 0; i < files.length; ++i) {
    156         var opt = document.createElement('option');
    157         opt.value = files[i];
    158         opt.textContent = cleanFilename(files[i]);
    159         selectEl.appendChild(opt);
    160       }
    161       selectEl.selectedIndex = 0;
    162       selectEl.onchange = onSelectionChange;
    163 
    164       if (!window.location.hash) {
    165         // This will trigger an onHashChange so no need to reload directly.
    166         window.location.hash = '#' + selectEl[selectEl.selectedIndex].value;
    167       } else {
    168         onHashChange();
    169       }
    170     }).then(onLoadTimer.end.call(this));
    171 
    172     var trackDetailedModelStatsEl = tr.ui.b.createCheckBox(
    173         this, 'trackDetailedModelStats',
    174         'traceViewer.trackDetailedModelStats', false,
    175         'Detailed file size stats',
    176         onHashChange);
    177     trackDetailedModelStatsEl.id = 'track-detailed-model-stats';
    178     timelineViewEl.leftControls.appendChild(trackDetailedModelStatsEl);
    179   }
    180 
    181   window.addEventListener('load', onLoad);
    182   </script>
    183 </body>
    184 </html>
    185