summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/mre/file_handle.html
blob: 85c1dd18abf52fb6a5ae847db36af4a0fe8a2476 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!DOCTYPE html>
<!--
Copyright 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/tracing/base/guid.html">
<link rel="import" href="/tracing/base/utils.html">
<link rel="import" href="/tracing/base/xhr.html">
<link rel="import" href="/tracing/extras/full_config.html">
<link rel="import" href="/tracing/importer/import.html">
<link rel="import" href="/tracing/model/model.html">

<script>
'use strict';

tr.exportTo('tr.mre', function() {
  function FileHandle(canonicalUrl) {
    this.canonicalUrl_ = canonicalUrl;
  }

  FileHandle.prototype = {
    get canonicalUrl() { return this.canonicalUrl_; },

    asDict: function() {
      var d = {
        canonical_url: this.canonicalUrl_
      };

      this._asDictInto(d);
      if (d.type === undefined)
        throw new Error('_asDictInto must set type field');
    },

    load: function() {
      throw new Error('Not implemented');
    }
  };

  FileHandle.fromDict = function(handleDict) {
    if (handleDict.type === 'url')
      return URLFileHandle.fromDict(handleDict);
    if (handleDict.type === 'in-memory')
      return InMemoryFileHandle.fromDict(handleDict);

    throw new Error('Not implemented: fromDict for ' + handleDict.type);
  };


  function URLFileHandle(canonicalUrl, urlToLoad) {
    // TODO(eakuefner): assert startswith file://
    FileHandle.call(this, canonicalUrl);
    this.urlToLoad = urlToLoad;
  }

  URLFileHandle.prototype = {
    __proto__: FileHandle.prototype,

    _asDictInto: function(handleDict) {
      handleDict.urlToLoad = this.urlToLoad;
      handleDict.type = 'url';
    },

    load: function() {
      try {
        return tr.b.getSync(this.urlToLoad);
      } catch (ex) {
        var err = new Error('Could not open ' + this.urlToLoad);
        err.name = 'FileLoadingError';
        throw err;
      }
    }
  };


  URLFileHandle.fromDict = function(handleDict) {
    return new URLFileHandle(handleDict.canonical_url, handleDict.url_to_load);
  };

  function InMemoryFileHandle(fileData, canonicalUrl) {
    FileHandle.call(this, canonicalUrl);
    this.fileData = fileData;
  }

  InMemoryFileHandle.prototype = {
    __proto__: FileHandle.prototype,

    _asDictInto: function(handleDict) {
      handleDict.data = this.fileData;
      handleDict.type = 'in-memory';
    },

    load: function() {
      return this.fileData;
    }
  };

  InMemoryFileHandle.fromDict = function(handleDict) {
    return new InMemoryFileHandle(
        handleDict.data, handleDict.canonical_url);
  };

  return {
    FileHandle: FileHandle,
    URLFileHandle: URLFileHandle,
    InMemoryFileHandle: InMemoryFileHandle,
  };
});
</script>