Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / tracing / importer / zip_importer.js
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 'use strict';
6
7 /**
8  * @fileoverview ZipImporter inflates zip compressed data and passes it along
9  * to an actual importer.
10  */
11 tvcm.requireRawScript('jszip.js');
12 tvcm.requireRawScript('jszip-load.js');
13 tvcm.requireRawScript('jszip-inflate.js');
14
15 tvcm.require('tracing.importer.importer');
16 tvcm.require('tracing.importer.gzip_importer');
17 tvcm.require('tracing.trace_model');
18
19 tvcm.exportTo('tracing.importer', function() {
20   var Importer = tracing.importer.Importer;
21
22   function ZipImporter(model, eventData) {
23     if (eventData instanceof ArrayBuffer)
24       eventData = new Uint8Array(eventData);
25     this.model_ = model;
26     this.eventData_ = eventData;
27   }
28
29   /**
30    * @param {eventData} string Possibly zip compressed data.
31    * @return {boolean} Whether eventData looks like zip compressed data.
32    */
33   ZipImporter.canImport = function(eventData) {
34     var header;
35     if (eventData instanceof ArrayBuffer)
36       header = new Uint8Array(eventData.slice(0, 2));
37     else if (typeof(eventData) === 'string' || eventData instanceof String)
38       header = [eventData.charCodeAt(0), eventData.charCodeAt(1)];
39     else
40       return false;
41     return header[0] === 'P'.charCodeAt(0) && header[1] === 'K'.charCodeAt(0);
42   };
43
44   ZipImporter.prototype = {
45     __proto__: Importer.prototype,
46
47     extractSubtraces: function() {
48       var zip = new JSZip(this.eventData_);
49       var subtraces = [];
50       for (var idx in zip.files)
51         subtraces.push(zip.files[idx].asText());
52       return subtraces;
53     }
54   };
55
56   tracing.TraceModel.registerImporter(ZipImporter);
57
58   return {
59     ZipImporter: ZipImporter
60   };
61 });