Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / tracing / analysis / analysis_view.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 Displays an analysis of the selection.
9  */
10 tvcm.requireStylesheet('tracing.analysis.analysis_view');
11
12 tvcm.require('tracing.analysis.analysis_results');
13 tvcm.require('tracing.analysis.analyze_selection');
14 tvcm.require('tracing.analysis.default_object_view');
15 tvcm.require('tracing.analysis.object_instance_view');
16 tvcm.require('tracing.analysis.object_snapshot_view');
17 tvcm.require('tracing.analysis.slice_view');
18 tvcm.require('tracing.analysis.util');
19 tvcm.require('tvcm.ui');
20 tvcm.require('tvcm.guid');
21 tvcm.exportTo('tracing.analysis', function() {
22
23   var AnalysisView = tvcm.ui.define('div');
24
25   AnalysisView.prototype = {
26     __proto__: HTMLDivElement.prototype,
27
28     decorate: function() {
29       this.className = 'analysis-view';
30
31       this.currentView_ = undefined;
32       this.currentSelection_ = undefined;
33       this.selections_ = [];
34       this.guid_ = tvcm.GUID.allocate();
35
36       window.addEventListener('popstate', this.onPopState.bind(this));
37     },
38
39     changeViewType: function(viewType) {
40       if (this.currentView_ instanceof viewType)
41         return;
42       this.textContent = '';
43       try {
44         this.currentView_ = new viewType();
45         this.appendChild(this.currentView_);
46       } catch (e) {
47         this.currentView_ = undefined;
48         throw e;
49       }
50
51       this.updateClassList_();
52     },
53     updateClassList_: function() {
54       if (this.currentView_ instanceof tracing.analysis.AnalysisResults)
55         this.classList.remove('viewing-old-style-analysis');
56       else
57         this.classList.add('viewing-old-style-analysis');
58
59       if (this.currentView_ &&
60           this.currentView_.requiresTallView) {
61         this.classList.add('tall-mode');
62       } else {
63         this.classList.remove('tall-mode');
64       }
65     },
66
67     get currentView() {
68       return this.currentView_;
69     },
70
71     get selection() {
72       return this.currentSelection_;
73     },
74
75     set selection(selection) {
76       this.selections_.push(selection);
77
78       var state = {
79         view_guid: this.guid_,
80         selection_guid: selection.guid
81       };
82       window.history.pushState(state);
83
84       this.processSelection(selection);
85     },
86
87     clearSelectionHistory: function() {
88       this.selections_ = [];
89     },
90
91     onPopState: function(event) {
92       if ((event.state === null) ||
93           (event.state.view_guid !== this.guid_))
94         return;
95
96       var idx;
97       for (idx = 0; idx < this.selections_.length; ++idx) {
98         if (this.selections_[idx].guid === event.state.selection_guid)
99           break;
100       }
101
102       if (idx >= this.selections_.length)
103         return;
104
105       this.processSelection(this.selections_[idx]);
106       event.stopPropagation();
107     },
108
109     processSelection: function(selection) {
110       var eventsByType = selection.getEventsOrganizedByType();
111       if (selection.length == 1 &&
112           eventsByType.counterSamples.length == 0) {
113         if (this.tryToProcessSelectionUsingCustomView(selection[0]))
114           return;
115       }
116
117       this.changeViewType(tracing.analysis.AnalysisResults);
118
119       this.currentView.clear();
120       this.currentSelection_ = selection;
121       tracing.analysis.analyzeEventsByType(this.currentView, eventsByType);
122     },
123
124     tryToProcessSelectionUsingCustomView: function(event) {
125       var obj;
126       var typeName;
127       var viewBaseType;
128       var defaultViewType;
129       var viewProperty;
130       if (event instanceof tracing.trace_model.ObjectSnapshot) {
131         typeName = event.objectInstance.typeName;
132         viewBaseType = tracing.analysis.ObjectSnapshotView;
133         defaultViewType = tracing.analysis.DefaultObjectSnapshotView;
134       } else if (event instanceof tracing.trace_model.ObjectInstance) {
135         typeName = event.typeName;
136         viewBaseType = tracing.analysis.ObjectInstanceView;
137         defaultViewType = tracing.analysis.DefaultObjectInstanceView;
138       } else if (event instanceof tracing.trace_model.Slice) {
139         typeName = event.analysisTypeName;
140         viewBaseType = tracing.analysis.SliceView;
141         defaultViewType = undefined;
142       } else {
143         return false;
144       }
145
146       var customViewInfo = viewBaseType.getViewInfo(typeName);
147
148       var viewType = customViewInfo ?
149           customViewInfo.constructor : defaultViewType;
150
151       // Some view types don't have default views. In those cases, we fall
152       // back to the standard analysis sytem.
153       if (!viewType)
154         return false;
155
156       this.changeViewType(viewType);
157       this.currentView.modelEvent = event;
158       return true;
159     }
160   };
161
162   return {
163     AnalysisView: AnalysisView
164   };
165 });