1// Copyright (c) 2012 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
7base.exportTo('tracing', function() {
8
9  function filterSliceArray(filter, slices) {
10    if (filter === undefined)
11      return slices;
12
13    var matched = [];
14    for (var i = 0; i < slices.length; ++i) {
15      if (filter.matchSlice(slices[i]))
16        matched.push(slices[i]);
17    }
18    return matched;
19  }
20
21  function filterCounterArray(filter, counters) {
22    if (filter === undefined)
23      return counters;
24
25    var matched = [];
26    for (var i = 0; i < counters.length; ++i) {
27      if (filter.matchCounter(counters[i]))
28        matched.push(counters[i]);
29    }
30    return matched;
31  }
32
33  /**
34   * @constructor The generic base class for filtering a TraceModel based on
35   * various rules. The base class returns true for everything.
36   */
37  function Filter() {
38  }
39
40  Filter.prototype = {
41    __proto__: Object.prototype,
42
43    matchCounter: function(counter) {
44      return true;
45    },
46
47    matchCpu: function(cpu) {
48      return true;
49    },
50
51    matchProcess: function(process) {
52      return true;
53    },
54
55    matchSlice: function(slice) {
56      return true;
57    },
58
59    matchThread: function(thread) {
60      return true;
61    }
62  };
63
64  /**
65   * @constructor A filter that matches objects by their name case insensitive.
66   * .findAllObjectsMatchingFilter
67   */
68  function TitleFilter(text) {
69    Filter.call(this);
70    this.text_ = text.toLowerCase();
71  }
72  TitleFilter.prototype = {
73    __proto__: Filter.prototype,
74
75    matchCounter: function(counter) {
76      if (this.text_.length === 0)
77        return false;
78      if (counter.name === undefined)
79        return false;
80      return counter.name.toLowerCase().indexOf(this.text_) !== -1;
81    },
82
83    matchSlice: function(slice) {
84      if (this.text_.length === 0)
85        return false;
86      if (slice.title === undefined)
87        return false;
88      return slice.title.toLowerCase().indexOf(this.text_) !== -1;
89    }
90  };
91
92  /**
93   * @constructor A filter that filters objects by their category.
94   * Objects match if they are NOT in the list of categories
95   * @param {Array<string>=} opt_categories Categories to blacklist.
96   */
97  function CategoryFilter(opt_categories) {
98    Filter.call(this);
99    this.categories_ = {};
100    var cats = opt_categories || [];
101    for (var i = 0; i < cats.length; i++)
102      this.addCategory(cats[i]);
103  }
104  CategoryFilter.prototype = {
105    __proto__: Filter.prototype,
106
107    addCategory: function(cat) {
108      this.categories_[cat] = true;
109    },
110
111    matchCounter: function(counter) {
112      if (!counter.category)
113        return true;
114      return !this.categories_[counter.category];
115    },
116
117    matchSlice: function(slice) {
118      if (!slice.category)
119        return true;
120      return !this.categories_[slice.category];
121    }
122  };
123
124  return {
125    filterCounterArray: filterCounterArray,
126    filterSliceArray: filterSliceArray,
127    Filter: Filter,
128    TitleFilter: TitleFilter,
129    CategoryFilter: CategoryFilter
130  };
131});
132