OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Dictionary of constants (initialized by browser). | 6 * Dictionary of constants (initialized by browser). |
7 */ | 7 */ |
8 var LogEntryType = null; | 8 var LogEntryType = null; |
9 var LogEventType = null; | 9 var LogEventType = null; |
10 var LogEventPhase = null; | 10 var LogEventPhase = null; |
11 var LogSourceType = null; | 11 var LogSourceType = null; |
12 | 12 |
13 /** | 13 /** |
| 14 * Object to communicate between the renderer and the browser. |
| 15 * @type {!BrowserBridge} |
| 16 */ |
| 17 var g_browser = null; |
| 18 |
| 19 /** |
14 * Main entry point. called once the page has loaded. | 20 * Main entry point. called once the page has loaded. |
15 */ | 21 */ |
16 function onLoaded() { | 22 function onLoaded() { |
| 23 g_browser = new BrowserBridge(); |
| 24 |
17 // Create the view which displays requests lists, and lets you select, filter | 25 // Create the view which displays requests lists, and lets you select, filter |
18 // and delete them. | 26 // and delete them. |
19 var requestsView = new RequestsView('requestsListTableBody', | 27 var requestsView = new RequestsView('requestsListTableBody', |
20 'filterInput', | 28 'filterInput', |
21 'filterCount', | 29 'filterCount', |
22 'deleteSelected', | 30 'deleteSelected', |
23 'selectAll', | 31 'selectAll', |
24 | 32 |
25 // IDs for the details view. | 33 // IDs for the details view. |
26 "detailsTabHandles", | 34 "detailsTabHandles", |
27 "detailsLogTab", | 35 "detailsLogTab", |
28 "detailsTimelineTab", | 36 "detailsTimelineTab", |
29 "detailsLogBox", | 37 "detailsLogBox", |
30 "detailsTimelineBox", | 38 "detailsTimelineBox", |
31 | 39 |
32 // IDs for the layout boxes. | 40 // IDs for the layout boxes. |
33 "filterBox", | 41 "filterBox", |
34 "requestsBox", | 42 "requestsBox", |
35 "actionBox", | 43 "actionBox", |
36 "splitterBox"); | 44 "splitterBox"); |
37 | 45 |
| 46 // Create a view which will display info on the proxy setup. |
| 47 var proxyView = new ProxyView("proxyTabContent", |
| 48 "proxyCurrentConfig", |
| 49 "proxyReloadSettings", |
| 50 "badProxiesTableBody", |
| 51 "clearBadProxies"); |
| 52 |
38 // Create a view which lets you tab between the different sub-views. | 53 // Create a view which lets you tab between the different sub-views. |
39 var categoryTabSwitcher = | 54 var categoryTabSwitcher = |
40 new TabSwitcherView(new DivView('categoryTabHandles')); | 55 new TabSwitcherView(new DivView('categoryTabHandles')); |
41 | 56 |
42 // Populate the main tabs. | 57 // Populate the main tabs. |
43 categoryTabSwitcher.addTab('requestsTab', requestsView); | 58 categoryTabSwitcher.addTab('requestsTab', requestsView); |
44 categoryTabSwitcher.addTab('proxyTab', new DivView('proxyTabContent')); | 59 categoryTabSwitcher.addTab('proxyTab', proxyView); |
45 categoryTabSwitcher.addTab('dnsTab', new DivView('dnsTabContent')); | 60 categoryTabSwitcher.addTab('dnsTab', new DivView('dnsTabContent')); |
46 categoryTabSwitcher.addTab('socketsTab', new DivView('socketsTabContent')); | 61 categoryTabSwitcher.addTab('socketsTab', new DivView('socketsTabContent')); |
47 categoryTabSwitcher.addTab('httpCacheTab', | 62 categoryTabSwitcher.addTab('httpCacheTab', |
48 new DivView('httpCacheTabContent')); | 63 new DivView('httpCacheTabContent')); |
49 | 64 |
50 // Select the requests tab as the default. | 65 // Select the requests tab as the default. |
51 categoryTabSwitcher.switchToTab('requestsTab'); | 66 categoryTabSwitcher.switchToTab('requestsTab'); |
52 | 67 |
53 // Make this category tab widget the primary view, that fills the whole page. | 68 // Make this category tab widget the primary view, that fills the whole page. |
54 var windowView = new WindowView(categoryTabSwitcher); | 69 var windowView = new WindowView(categoryTabSwitcher); |
55 | 70 |
56 // Trigger initial layout. | 71 // Trigger initial layout. |
57 windowView.resetGeometry(); | 72 windowView.resetGeometry(); |
58 | 73 |
59 // Tell the browser that we are ready to start receiving log events. | 74 // Tell the browser that we are ready to start receiving log events. |
60 notifyApplicationReady(); | 75 g_browser.sendReady(); |
61 } | 76 } |
62 | 77 |
| 78 /** |
| 79 * This class provides a "bridge" for communicating between the javascript and |
| 80 * the browser. |
| 81 * |
| 82 * @constructor |
| 83 */ |
| 84 function BrowserBridge() { |
| 85 // List of observers for various bits of browser state. |
| 86 this.logObservers_ = []; |
| 87 this.proxySettingsObservers_ = []; |
| 88 this.badProxiesObservers_ = []; |
| 89 |
| 90 // Map from observer method name (i.e. 'onProxySettingsChanged', 'onBadProxies
Changed') |
| 91 // to the previously received data for that type. Used to tell if the data has |
| 92 // actually changed since we last polled it. |
| 93 this.prevPollData_ = {}; |
| 94 } |
| 95 |
| 96 /** |
| 97 * Delay in milliseconds between polling of certain browser information. |
| 98 */ |
| 99 BrowserBridge.POLL_INTERVAL_MS = 5000; |
| 100 |
63 //------------------------------------------------------------------------------ | 101 //------------------------------------------------------------------------------ |
64 // Messages sent to the browser | 102 // Messages sent to the browser |
65 //------------------------------------------------------------------------------ | 103 //------------------------------------------------------------------------------ |
66 | 104 |
67 function notifyApplicationReady() { | 105 BrowserBridge.prototype.sendReady = function() { |
68 chrome.send('notifyReady'); | 106 chrome.send('notifyReady'); |
69 } | 107 |
| 108 // Some of the data we are interested is not currently exposed as a stream, |
| 109 // so we will poll the browser to find out when it changes and then notify |
| 110 // the observers. |
| 111 window.setInterval( |
| 112 this.doPolling_.bind(this), BrowserBridge.POLL_INTERVAL_MS); |
| 113 }; |
| 114 |
| 115 BrowserBridge.prototype.sendGetProxySettings = function() { |
| 116 // The browser will call receivedProxySettings on completion. |
| 117 chrome.send('getProxySettings'); |
| 118 }; |
| 119 |
| 120 BrowserBridge.prototype.sendReloadProxySettings = function() { |
| 121 chrome.send('reloadProxySettings'); |
| 122 }; |
| 123 |
| 124 BrowserBridge.prototype.sendGetBadProxies = function() { |
| 125 // The browser will call receivedBadProxies on completion. |
| 126 chrome.send('getBadProxies'); |
| 127 }; |
| 128 |
| 129 BrowserBridge.prototype.sendClearBadProxies = function() { |
| 130 chrome.send('clearBadProxies'); |
| 131 }; |
70 | 132 |
71 //------------------------------------------------------------------------------ | 133 //------------------------------------------------------------------------------ |
72 // Messages received from the browser | 134 // Messages received from the browser |
73 //------------------------------------------------------------------------------ | 135 //------------------------------------------------------------------------------ |
74 | 136 |
75 function onLogEntryAdded(logEntry) { | 137 BrowserBridge.prototype.receivedLogEntry = function(logEntry) { |
76 LogDataProvider.broadcast(logEntry); | 138 for (var i = 0; i < this.logObservers_.length; ++i) |
77 } | 139 this.logObservers_[i].onLogEntryAdded(logEntry); |
| 140 }; |
78 | 141 |
79 function setLogEventTypeConstants(constantsMap) { | 142 BrowserBridge.prototype.receivedLogEventTypeConstants = function(constantsMap) { |
80 LogEventType = constantsMap; | 143 LogEventType = constantsMap; |
81 } | 144 }; |
82 | 145 |
83 function setLogEventPhaseConstants(constantsMap) { | 146 BrowserBridge.prototype.receivedLogEventPhaseConstants = function(constantsMap)
{ |
84 LogEventPhase = constantsMap; | 147 LogEventPhase = constantsMap; |
85 } | 148 }; |
86 | 149 |
87 function setLogSourceTypeConstants(constantsMap) { | 150 BrowserBridge.prototype.receivedLogSourceTypeConstants = function(constantsMap)
{ |
88 LogSourceType = constantsMap; | 151 LogSourceType = constantsMap; |
89 } | 152 }; |
90 | 153 |
91 function setLogEntryTypeConstants(constantsMap) { | 154 BrowserBridge.prototype.receivedLogEntryTypeConstants = function(constantsMap) { |
92 LogEntryType = constantsMap; | 155 LogEntryType = constantsMap; |
93 } | 156 }; |
| 157 |
| 158 BrowserBridge.prototype.receivedTimeTickOffset = function(timeTickOffset) { |
| 159 this.timeTickOffset_ = timeTickOffset; |
| 160 }; |
| 161 |
| 162 BrowserBridge.prototype.receivedProxySettings = function(proxySettings) { |
| 163 this.dispatchToObserversFromPoll_( |
| 164 this.proxySettingsObservers_, 'onProxySettingsChanged', proxySettings); |
| 165 }; |
| 166 |
| 167 BrowserBridge.prototype.receivedBadProxies = function(badProxies) { |
| 168 this.dispatchToObserversFromPoll_( |
| 169 this.badProxiesObservers_, 'onBadProxiesChanged', badProxies); |
| 170 }; |
94 | 171 |
95 //------------------------------------------------------------------------------ | 172 //------------------------------------------------------------------------------ |
96 // LogDataProvider | |
97 //------------------------------------------------------------------------------ | |
98 | 173 |
99 var LogDataProvider = {} | 174 /** |
100 | 175 * Adds a listener of log entries. |observer| will be called back when new log |
101 LogDataProvider.observers_ = []; | 176 * data arrives, through: |
102 | 177 * |
103 LogDataProvider.broadcast = function(logEntry) { | 178 * observer.onLogEntryAdded(logEntry) |
104 for (var i = 0; i < this.observers_.length; ++i) { | 179 */ |
105 this.observers_[i].onLogEntryAdded(logEntry); | 180 BrowserBridge.prototype.addLogObserver = function(observer) { |
106 } | 181 this.logObservers_.push(observer); |
107 }; | 182 }; |
108 | 183 |
109 LogDataProvider.addObserver = function(observer) { | 184 /** |
110 this.observers_.push(observer); | 185 * Adds a listener of the proxy settings. |observer| will be called back when |
| 186 * data is received, through: |
| 187 * |
| 188 * observer.onProxySettingsChanged(proxySettings) |
| 189 * |
| 190 * |proxySettings| is a formatted string describing the settings. |
| 191 * TODO(eroman): send a dictionary instead. |
| 192 */ |
| 193 BrowserBridge.prototype.addProxySettingsObserver = function(observer) { |
| 194 this.proxySettingsObservers_.push(observer); |
111 }; | 195 }; |
| 196 |
| 197 /** |
| 198 * Adds a listener of the proxy settings. |observer| will be called back when |
| 199 * data is received, through: |
| 200 * |
| 201 * observer.onBadProxiesChanged(badProxies) |
| 202 * |
| 203 * |badProxies| is an array, where each entry has the property: |
| 204 * badProxies[i].proxy_uri: String identify the proxy. |
| 205 * badProxies[i].bad_until: The time when the proxy stops being considered |
| 206 * bad. Note the time is in time ticks. |
| 207 */ |
| 208 BrowserBridge.prototype.addBadProxiesObsever = function(observer) { |
| 209 this.badProxiesObservers_.push(observer); |
| 210 }; |
| 211 |
| 212 /** |
| 213 * The browser gives us times in terms of "time ticks" in milliseconds. |
| 214 * This function converts the tick count to a Date() object. |
| 215 * |
| 216 * @param {String} timeTicks. |
| 217 * @returns {Date} The time that |timeTicks| represents. |
| 218 */ |
| 219 BrowserBridge.prototype.convertTimeTicksToDate = function(timeTicks) { |
| 220 // Note that the subtraction by 0 is to cast to a number (probably a float |
| 221 // since the numbers are big). |
| 222 var timeStampMs = (this.timeTickOffset_ - 0) + (timeTicks - 0); |
| 223 var d = new Date(); |
| 224 d.setTime(timeStampMs); |
| 225 return d; |
| 226 }; |
| 227 |
| 228 BrowserBridge.prototype.doPolling_ = function() { |
| 229 this.sendGetProxySettings(); |
| 230 this.sendGetBadProxies(); |
| 231 }; |
| 232 |
| 233 /** |
| 234 * Helper function to handle calling all the observers, but ONLY if the data has |
| 235 * actually changed since last time. This is used for data we received from |
| 236 * browser on a poll loop. |
| 237 */ |
| 238 BrowserBridge.prototype.dispatchToObserversFromPoll_ = function( |
| 239 observerList, method, data) { |
| 240 var prevData = this.prevPollData_[method]; |
| 241 |
| 242 // If the data hasn't changed since last time, no need to notify observers. |
| 243 if (prevData && JSON.stringify(prevData) == JSON.stringify(data)) |
| 244 return; |
| 245 |
| 246 this.prevPollData_[method] = data; |
| 247 |
| 248 // Ok, notify the observers of the change. |
| 249 for (var i = 0; i < observerList.length; ++i) |
| 250 observerList[i][method](data); |
| 251 }; |
OLD | NEW |