Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / tracing / trace_model / process.js
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
7 /**
8  * @fileoverview Provides the Process class.
9  */
10 tvcm.require('tracing.trace_model.process_base');
11 tvcm.exportTo('tracing.trace_model', function() {
12
13   var ProcessBase = tracing.trace_model.ProcessBase;
14
15   /**
16    * The Process represents a single userland process in the
17    * trace.
18    * @constructor
19    */
20   function Process(model, pid) {
21     if (model === undefined)
22       throw new Error('model must be provided');
23     if (pid === undefined)
24       throw new Error('pid must be provided');
25     tracing.trace_model.ProcessBase.call(this, model);
26     this.pid = pid;
27     this.name = undefined;
28     this.labels = [];
29     this.instantEvents = [];
30   };
31
32   /**
33    * Comparison between processes that orders by pid.
34    */
35   Process.compare = function(x, y) {
36     var tmp = tracing.trace_model.ProcessBase.compare(x, y);
37     if (tmp)
38       return tmp;
39
40     tmp = tvcm.comparePossiblyUndefinedValues(
41         x.name, y.name,
42         function(x, y) { return x.localeCompare(y); });
43     if (tmp)
44       return tmp;
45
46     tmp = tvcm.compareArrays(x.labels, y.labels,
47         function(x, y) { return x.localeCompare(y); });
48     if (tmp)
49       return tmp;
50
51     return x.pid - y.pid;
52   };
53
54   Process.prototype = {
55     __proto__: tracing.trace_model.ProcessBase.prototype,
56
57     compareTo: function(that) {
58       return Process.compare(this, that);
59     },
60
61     pushInstantEvent: function(instantEvent) {
62       this.instantEvents.push(instantEvent);
63     },
64
65     addLabelIfNeeded: function(labelName) {
66       for (var i = 0; i < this.labels.length; i++) {
67         if (this.labels[i] === labelName)
68           return;
69       }
70       this.labels.push(labelName);
71     },
72
73     get userFriendlyName() {
74       var res;
75       if (this.name)
76         res = this.name + ' (pid ' + this.pid + ')';
77       else
78         res = 'Process ' + this.pid;
79       if (this.labels.length)
80         res += ': ' + this.labels.join(', ');
81       return res;
82     },
83
84     get userFriendlyDetails() {
85       if (this.name)
86         return this.name + ' (pid ' + this.pid + ')';
87       return 'pid: ' + this.pid;
88     },
89
90     getSettingsKey: function() {
91       if (!this.name)
92         return undefined;
93       if (!this.labels.length)
94         return 'processes.' + this.name;
95       return 'processes.' + this.name + '.' + this.labels.join('.');
96     },
97
98     shiftTimestampsForward: function(amount) {
99       for (var id in this.instantEvents)
100         this.instantEvents[id].start += amount;
101
102       tracing.trace_model.ProcessBase.prototype
103           .shiftTimestampsForward.apply(this, arguments);
104     },
105
106     iterateAllEvents: function(callback, opt_this) {
107       this.instantEvents.forEach(callback, opt_this);
108
109       ProcessBase.prototype.iterateAllEvents.call(this, callback, opt_this);
110     }
111   };
112
113   return {
114     Process: Process
115   };
116 });