1<!DOCTYPE html>
2<!--
3Copyright (c) 2013 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7<link rel="import" href="/tracing/model/async_slice.html">
8
9<script>
10'use strict';
11
12tr.exportTo('tr.e.net', function() {
13  var AsyncSlice = tr.model.AsyncSlice;
14
15  function NetAsyncSlice() {
16    AsyncSlice.apply(this, arguments);
17    this.url_ = undefined;
18    this.byteCount_ = undefined;
19    // Boolean variables indicating whether we have computed corresponding
20    // fields. Computing these fields needs iteration through all sub-slices and
21    // so recomputation will be costly.
22    this.isTitleComputed_ = false;
23    this.isUrlComputed_ = false;
24  }
25
26  NetAsyncSlice.prototype = {
27    __proto__: AsyncSlice.prototype,
28
29    get viewSubGroupTitle() {
30      return 'NetLog';
31    },
32
33    get title() {
34      if (this.isTitleComputed_ || !this.isTopLevel)
35        return this.title_;
36
37      if (this.url !== undefined && this.url.length > 0) {
38        // Set the title so we do not have to recompute when it is redrawn.
39        this.title_ = this.url;
40      } else if (this.args !== undefined &&
41                 this.args.source_type !== undefined) {
42        // We do not have a URL, use the source type as the title.
43        this.title_ = this.args.source_type;
44      }
45      this.isTitleComputed_ = true;
46      return this.title_;
47    },
48
49    set title(title) {
50      this.title_ = title;
51    },
52
53    // A recursive helper function that gets the url param of a slice or its
54    // nested subslices if there is one.
55    get url() {
56      if (this.isUrlComputed_)
57        return this.url_;
58      if (this.args !== undefined && this.args.params !== undefined &&
59          this.args.params.url !== undefined) {
60        this.url_ = this.args.params.url;
61      } else if (this.subSlices !== undefined && this.subSlices.length > 0) {
62        for (var i = 0; i < this.subSlices.length && ! this.url_; i++) {
63          if (this.subSlices[i].url !== undefined)
64            this.url_ = this.subSlices[i].url;
65        }
66      }
67      this.isUrlComputed_ = true;
68      return this.url_;
69    },
70
71    get byteCount() {
72      if (this.byteCount_ !== undefined)
73        return this.byteCount_;
74
75      this.byteCount_ = 0;
76      if ((this.originalTitle === 'URL_REQUEST_JOB_FILTERED_BYTES_READ' ||
77           this.originalTitle === 'URL_REQUEST_JOB_BYTES_READ') &&
78           this.args !== undefined && this.args.params !== undefined &&
79           this.args.params.byte_count !== undefined) {
80        this.byteCount_ = this.args.params.byte_count;
81      }
82      for (var i = 0; i < this.subSlices.length; i++) {
83        this.byteCount_ += this.subSlices[i].byteCount;
84      }
85      return this.byteCount_;
86    }
87  };
88
89  AsyncSlice.register(
90    NetAsyncSlice,
91    {
92      categoryParts: ['netlog', 'disabled-by-default-netlog']
93    });
94
95  return {
96    NetAsyncSlice: NetAsyncSlice
97  };
98});
99</script>
100