1// Copyright (C) 2018 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {Actions} from '../common/actions'; 16import * as trace_to_text from '../gen/trace_to_text'; 17 18import {globals} from './globals'; 19 20export function ConvertTrace(trace: Blob) { 21 const mod = trace_to_text({ 22 noInitialRun: true, 23 locateFile: (s: string) => s, 24 print: updateStatus, 25 printErr: updateStatus, 26 onRuntimeInitialized: () => { 27 updateStatus('Converting trace'); 28 const outPath = '/trace.json'; 29 mod.callMain(['json', '/fs/trace.proto', outPath]); 30 updateStatus('Trace conversion completed'); 31 const fsNode = mod.FS.lookupPath(outPath).node; 32 const data = fsNode.contents.buffer; 33 const size = fsNode.usedBytes; 34 globals.publish('LegacyTrace', {data, size}, /*transfer=*/[data]); 35 mod.FS.unlink(outPath); 36 }, 37 onAbort: () => { 38 console.log('ABORT'); 39 }, 40 }); 41 mod.FS.mkdir('/fs'); 42 mod.FS.mount( 43 mod.FS.filesystems.WORKERFS, 44 {blobs: [{name: 'trace.proto', data: trace}]}, 45 '/fs'); 46 47 // TODO removeme. 48 (self as {} as {mod: {}}).mod = mod; 49} 50 51function updateStatus(msg: {}) { 52 console.log(msg); 53 globals.dispatch(Actions.updateStatus({ 54 msg: msg.toString(), 55 timestamp: Date.now() / 1000, 56 })); 57} 58