1 <!DOCTYPE html> 2 <!-- 3 Copyright (c) 2014 The Chromium Authors. All rights reserved. 4 Use of this source code is governed by a BSD-style license that can be 5 found in the LICENSE file. 6 --> 7 8 <link rel="import" href="/tracing/importer/importer.html"> 9 <link rel="import" href="/tracing/importer/simple_line_reader.html"> 10 <link rel="import" href="/tracing/base/base64.html"> 11 <link rel="import" href="/tracing/model/model.html"> 12 13 <script> 14 'use strict'; 15 16 tr.exportTo('tr.e.importer', function() { 17 18 function Trace2HTMLImporter(model, events) { 19 this.importPriority = 0; 20 } 21 22 Trace2HTMLImporter.subtraces_ = []; 23 24 function _extractEventsFromHTML(text) { 25 // Clear the array before pushing data to it. 26 Trace2HTMLImporter.subtraces_ = []; 27 28 var r = new tr.importer.SimpleLineReader(text); 29 30 // Try to find viewer-data... 31 while (true) { 32 if (!r.advanceToLineMatching( 33 new RegExp('^<\s*script id="viewer-data" ' + 34 'type="(application\/json|text\/plain)">$'))) 35 break; 36 37 r.beginSavingLines(); 38 if (!r.advanceToLineMatching(/^<\/\s*script>$/)) 39 return failure; 40 41 var raw_events = r.endSavingLinesAndGetResult(); 42 43 // Drop off first and last event as it contains the end script tag. 44 raw_events = raw_events.slice(1, raw_events.length - 1); 45 var data64 = raw_events.join('\n'); 46 var buffer = new ArrayBuffer( 47 tr.b.Base64.getDecodedBufferLength(data64)); 48 var len = tr.b.Base64.DecodeToTypedArray(data64, new DataView(buffer)); 49 Trace2HTMLImporter.subtraces_.push(buffer.slice(0, len)); 50 } 51 } 52 53 function _canImportFromHTML(text) { 54 if (/^<!DOCTYPE html>/.test(text) === false) 55 return false; 56 57 // Try to find viewer-data... 58 _extractEventsFromHTML(text); 59 if (Trace2HTMLImporter.subtraces_.length === 0) 60 return false; 61 return true; 62 } 63 64 Trace2HTMLImporter.canImport = function(events) { 65 return _canImportFromHTML(events); 66 }; 67 68 Trace2HTMLImporter.prototype = { 69 __proto__: tr.importer.Importer.prototype, 70 71 get importerName() { 72 return 'Trace2HTMLImporter'; 73 }, 74 75 isTraceDataContainer: function() { 76 return true; 77 }, 78 79 extractSubtraces: function() { 80 return Trace2HTMLImporter.subtraces_; 81 }, 82 83 importEvents: function() { 84 } 85 }; 86 87 88 tr.importer.Importer.register(Trace2HTMLImporter); 89 90 91 return { 92 Trace2HTMLImporter: Trace2HTMLImporter 93 }; 94 }); 95 </script> 96