forked from catapult-project/catapult
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser_bridge.js
791 lines (685 loc) · 25.7 KB
/
browser_bridge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Populated by constants from the browser. Used only by this file.
var NetInfoSources = null;
/**
* This class provides a "bridge" for communicating between the javascript and
* the browser.
*/
var BrowserBridge = (function() {
'use strict';
/**
* Delay in milliseconds between updates of certain browser information.
*/
var POLL_INTERVAL_MS = 5000;
/**
* @constructor
*/
function BrowserBridge() {
assertFirstConstructorCall(BrowserBridge);
// List of observers for various bits of browser state.
this.hstsObservers_ = [];
this.expectCTObservers_ = [];
this.constantsObservers_ = [];
this.crosONCFileParseObservers_ = [];
this.storeDebugLogsObservers_ = [];
this.setNetworkDebugModeObservers_ = [];
// Unprocessed data received before the constants. This serves to protect
// against passing along data before having information on how to interpret
// it.
this.earlyReceivedData_ = [];
this.pollableDataHelpers_ = {};
// Add PollableDataHelpers for NetInfoSources, which retrieve information
// directly from the network stack.
this.addNetInfoPollableDataHelper(
'proxySettings', 'onProxySettingsChanged');
this.addNetInfoPollableDataHelper('badProxies', 'onBadProxiesChanged');
this.addNetInfoPollableDataHelper(
'hostResolverInfo', 'onHostResolverInfoChanged');
this.addNetInfoPollableDataHelper(
'socketPoolInfo', 'onSocketPoolInfoChanged');
this.addNetInfoPollableDataHelper(
'spdySessionInfo', 'onSpdySessionInfoChanged');
this.addNetInfoPollableDataHelper('spdyStatus', 'onSpdyStatusChanged');
this.addNetInfoPollableDataHelper(
'altSvcMappings', 'onAltSvcMappingsChanged');
this.addNetInfoPollableDataHelper('quicInfo', 'onQuicInfoChanged');
this.addNetInfoPollableDataHelper(
'reportingInfo', 'onReportingInfoChanged');
this.addNetInfoPollableDataHelper(
'httpCacheInfo', 'onHttpCacheInfoChanged');
// Add other PollableDataHelpers.
this.pollableDataHelpers_.sessionNetworkStats = new PollableDataHelper(
'onSessionNetworkStatsChanged',
this.sendGetSessionNetworkStats.bind(this));
this.pollableDataHelpers_.historicNetworkStats = new PollableDataHelper(
'onHistoricNetworkStatsChanged',
this.sendGetHistoricNetworkStats.bind(this));
if (cr.isWindows) {
this.pollableDataHelpers_.serviceProviders = new PollableDataHelper(
'onServiceProvidersChanged', this.sendGetServiceProviders.bind(this));
}
this.pollableDataHelpers_.prerenderInfo = new PollableDataHelper(
'onPrerenderInfoChanged', this.sendGetPrerenderInfo.bind(this));
this.pollableDataHelpers_.extensionInfo = new PollableDataHelper(
'onExtensionInfoChanged', this.sendGetExtensionInfo.bind(this));
this.pollableDataHelpers_.dataReductionProxyInfo = new PollableDataHelper(
'onDataReductionProxyInfoChanged',
this.sendGetDataReductionProxyInfo.bind(this));
// Setting this to true will cause messages from the browser to be ignored,
// and no messages will be sent to the browser, either. Intended for use
// when viewing log files.
this.disabled_ = false;
// Interval id returned by window.setInterval for polling timer.
this.pollIntervalId_ = null;
}
cr.addSingletonGetter(BrowserBridge);
BrowserBridge.prototype = {
//--------------------------------------------------------------------------
// Messages sent to the browser
//--------------------------------------------------------------------------
/**
* Wraps |chrome.send|. Doesn't send anything when disabled.
*/
send: function(value1, value2) {
if (console && console.warn) {
console.warn('TODO: Called deprecated BrowserBridge.send');
}
return;
/*if (!this.disabled_) {
if (arguments.length == 1) {
chrome.send(value1);
} else if (arguments.length == 2) {
chrome.send(value1, value2);
} else {
throw 'Unsupported number of arguments.';
}
}*/
},
sendReady: function() {
this.send('notifyReady');
this.setPollInterval(POLL_INTERVAL_MS);
},
/**
* Some of the data we are interested is not currently exposed as a
* stream. This starts polling those with active observers (visible
* views) every |intervalMs|. Subsequent calls override previous calls
* to this function. If |intervalMs| is 0, stops polling.
*/
setPollInterval: function(intervalMs) {
if (this.pollIntervalId_ !== null) {
window.clearInterval(this.pollIntervalId_);
this.pollIntervalId_ = null;
}
if (intervalMs > 0) {
this.pollIntervalId_ = window.setInterval(
this.checkForUpdatedInfo.bind(this, false), intervalMs);
}
},
sendGetNetInfo: function(netInfoSource) {
// If don't have constants yet, don't do anything yet.
if (NetInfoSources)
this.send('getNetInfo', [NetInfoSources[netInfoSource]]);
},
sendReloadProxySettings: function() {
this.send('reloadProxySettings');
},
sendClearBadProxies: function() {
this.send('clearBadProxies');
},
sendClearHostResolverCache: function() {
this.send('clearHostResolverCache');
},
sendClearBrowserCache: function() {
this.send('clearBrowserCache');
},
sendClearAllCache: function() {
this.sendClearHostResolverCache();
this.sendClearBrowserCache();
},
sendHSTSQuery: function(domain) {
this.send('hstsQuery', [domain]);
},
sendHSTSAdd: function(domain, sts_include_subdomains) {
this.send('hstsAdd', [domain, sts_include_subdomains]);
},
sendDomainSecurityPolicyDelete: function(domain) {
this.send('domainSecurityPolicyDelete', [domain]);
},
sendExpectCTQuery: function(domain) {
this.send('expectCTQuery', [domain]);
},
sendExpectCTAdd: function(domain, report_uri, enforce) {
this.send('expectCTAdd', [domain, report_uri, enforce]);
},
sendExpectCTTestReport: function(report_uri) {
this.send('expectCTTestReport', [report_uri]);
},
sendGetSessionNetworkStats: function() {
this.send('getSessionNetworkStats');
},
sendGetHistoricNetworkStats: function() {
this.send('getHistoricNetworkStats');
},
sendCloseIdleSockets: function() {
this.send('closeIdleSockets');
},
sendFlushSocketPools: function() {
this.send('flushSocketPools');
},
sendGetServiceProviders: function() {
this.send('getServiceProviders');
},
sendGetPrerenderInfo: function() {
this.send('getPrerenderInfo');
},
sendGetExtensionInfo: function() {
this.send('getExtensionInfo');
},
sendGetDataReductionProxyInfo: function() {
this.send('getDataReductionProxyInfo');
},
setCaptureMode: function(captureMode) {
this.send('setCaptureMode', ['' + captureMode]);
},
importONCFile: function(fileContent, passcode) {
this.send('importONCFile', [fileContent, passcode]);
},
storeDebugLogs: function() {
this.send('storeDebugLogs');
},
setNetworkDebugMode: function(subsystem) {
this.send('setNetworkDebugMode', [subsystem]);
},
//--------------------------------------------------------------------------
// Messages received from the browser.
//--------------------------------------------------------------------------
receive: function(command, params) {
// Does nothing if disabled.
if (this.disabled_)
return;
// If no constants have been received, and params does not contain the
// constants, delay handling the data.
if (Constants == null && command != 'receivedConstants') {
this.earlyReceivedData_.push({command: command, params: params});
return;
}
this[command](params);
// Handle any data that was received early in the order it was received,
// once the constants have been processed.
if (this.earlyReceivedData_ != null) {
for (var i = 0; i < this.earlyReceivedData_.length; i++) {
var command = this.earlyReceivedData_[i];
this[command.command](command.params);
}
this.earlyReceivedData_ = null;
}
},
receivedConstants: function(constants) {
NetInfoSources = constants.netInfoSources;
for (var i = 0; i < this.constantsObservers_.length; i++)
this.constantsObservers_[i].onReceivedConstants(constants);
// May have been waiting for the constants to be received before getting
// information for the currently displayed tab.
this.checkForUpdatedInfo();
},
receivedLogEntries: function(logEntries) {
EventsTracker.getInstance().addLogEntries(logEntries);
},
receivedNetInfo: function(netInfo) {
// Dispatch |netInfo| to the various PollableDataHelpers listening to
// each field it contains.
//
// Currently information is only received from one source at a time, but
// the API does allow for data from more that one to be requested at once.
for (var source in netInfo)
this.pollableDataHelpers_[source].update(netInfo[source]);
},
receivedSessionNetworkStats: function(sessionNetworkStats) {
this.pollableDataHelpers_.sessionNetworkStats.update(sessionNetworkStats);
},
receivedHistoricNetworkStats: function(historicNetworkStats) {
this.pollableDataHelpers_.historicNetworkStats.update(
historicNetworkStats);
},
receivedServiceProviders: function(serviceProviders) {
this.pollableDataHelpers_.serviceProviders.update(serviceProviders);
},
receivedHSTSResult: function(info) {
for (var i = 0; i < this.hstsObservers_.length; i++)
this.hstsObservers_[i].onHSTSQueryResult(info);
},
receivedExpectCTResult: function(info) {
for (var i = 0; i < this.expectCTObservers_.length; i++)
this.expectCTObservers_[i].onExpectCTQueryResult(info);
},
receivedExpectCTTestReportResult: function(result) {
for (var i = 0; i < this.expectCTObservers_.length; i++)
this.expectCTObservers_[i].onExpectCTTestReportResult(result);
},
receivedONCFileParse: function(error) {
for (var i = 0; i < this.crosONCFileParseObservers_.length; i++)
this.crosONCFileParseObservers_[i].onONCFileParse(error);
},
receivedStoreDebugLogs: function(status) {
for (var i = 0; i < this.storeDebugLogsObservers_.length; i++)
this.storeDebugLogsObservers_[i].onStoreDebugLogs(status);
},
receivedSetNetworkDebugMode: function(status) {
for (var i = 0; i < this.setNetworkDebugModeObservers_.length; i++)
this.setNetworkDebugModeObservers_[i].onSetNetworkDebugMode(status);
},
receivedPrerenderInfo: function(prerenderInfo) {
this.pollableDataHelpers_.prerenderInfo.update(prerenderInfo);
},
receivedExtensionInfo: function(extensionInfo) {
this.pollableDataHelpers_.extensionInfo.update(extensionInfo);
},
receivedDataReductionProxyInfo: function(dataReductionProxyInfo) {
this.pollableDataHelpers_.dataReductionProxyInfo.update(
dataReductionProxyInfo);
},
//--------------------------------------------------------------------------
/**
* Prevents receiving/sending events to/from the browser.
*/
disable: function() {
this.disabled_ = true;
this.setPollInterval(0);
},
/**
* Returns true if the BrowserBridge has been disabled.
*/
isDisabled: function() {
return this.disabled_;
},
/**
* Adds a listener of the proxy settings. |observer| will be called back
* when data is received, through:
*
* observer.onProxySettingsChanged(proxySettings)
*
* |proxySettings| is a dictionary with (up to) two properties:
*
* "original" -- The settings that chrome was configured to use
* (i.e. system settings.)
* "effective" -- The "effective" proxy settings that chrome is using.
* (decides between the manual/automatic modes of the
* fetched settings).
*
* Each of these two configurations is formatted as a string, and may be
* omitted if not yet initialized.
*
* If |ignoreWhenUnchanged| is true, data is only sent when it changes.
* If it's false, data is sent whenever it's received from the browser.
*/
addProxySettingsObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.proxySettings.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener of the proxy settings. |observer| will be called back
* when data is received, through:
*
* observer.onBadProxiesChanged(badProxies)
*
* |badProxies| is an array, where each entry has the property:
* badProxies[i].proxy_uri: String identify the proxy.
* badProxies[i].bad_until: The time when the proxy stops being considered
* bad. Note the time is in time ticks.
*/
addBadProxiesObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.badProxies.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener of the host resolver info. |observer| will be called back
* when data is received, through:
*
* observer.onHostResolverInfoChanged(hostResolverInfo)
*/
addHostResolverInfoObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.hostResolverInfo.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener of the socket pool. |observer| will be called back
* when data is received, through:
*
* observer.onSocketPoolInfoChanged(socketPoolInfo)
*/
addSocketPoolInfoObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.socketPoolInfo.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener of the network session. |observer| will be called back
* when data is received, through:
*
* observer.onSessionNetworkStatsChanged(sessionNetworkStats)
*/
addSessionNetworkStatsObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.sessionNetworkStats.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener of persistent network session data. |observer| will be
* called back when data is received, through:
*
* observer.onHistoricNetworkStatsChanged(historicNetworkStats)
*/
addHistoricNetworkStatsObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.historicNetworkStats.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener of the QUIC info. |observer| will be called back
* when data is received, through:
*
* observer.onQuicInfoChanged(quicInfo)
*/
addQuicInfoObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.quicInfo.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener of the Reporting info. |observer| will be called back
* when data is received, through:
*
* observer.onReportingInfoChanged(reportingInfo)
*/
addReportingInfoObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.reportingInfo.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener of the SPDY info. |observer| will be called back
* when data is received, through:
*
* observer.onSpdySessionInfoChanged(spdySessionInfo)
*/
addSpdySessionInfoObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.spdySessionInfo.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener of the SPDY status. |observer| will be called back
* when data is received, through:
*
* observer.onSpdyStatusChanged(spdyStatus)
*/
addSpdyStatusObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.spdyStatus.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener of the altSvcMappings. |observer| will be
* called back when data is received, through:
*
* observer.onAltSvcMappingsChanged(altSvcMappings)
*/
addAltSvcMappingsObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.altSvcMappings.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener of the service providers info. |observer| will be called
* back when data is received, through:
*
* observer.onServiceProvidersChanged(serviceProviders)
*
* Will do nothing if on a platform other than Windows, as service providers
* are only present on Windows.
*/
addServiceProvidersObserver: function(observer, ignoreWhenUnchanged) {
if (this.pollableDataHelpers_.serviceProviders) {
this.pollableDataHelpers_.serviceProviders.addObserver(
observer, ignoreWhenUnchanged);
}
},
/**
* Adds a listener for the http cache info results.
* The observer will be called back with:
*
* observer.onHttpCacheInfoChanged(info);
*/
addHttpCacheInfoObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.httpCacheInfo.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener for the results of HSTS (HTTPS Strict Transport Security)
* queries. The observer will be called back with:
*
* observer.onHSTSQueryResult(result);
*/
addHSTSObserver: function(observer) {
this.hstsObservers_.push(observer);
},
/**
* Adds a listener for the results of Expect-CT queries. The observer will
* be called back with:
*
* observer.onExpectCTQueryResult(result);
*/
addExpectCTObserver: function(observer) {
this.expectCTObservers_.push(observer);
},
/**
* Adds a listener for ONC file parse status. The observer will be called
* back with:
*
* observer.onONCFileParse(error);
*/
addCrosONCFileParseObserver: function(observer) {
this.crosONCFileParseObservers_.push(observer);
},
/**
* Adds a listener for storing log file status. The observer will be called
* back with:
*
* observer.onStoreDebugLogs(status);
*/
addStoreDebugLogsObserver: function(observer) {
this.storeDebugLogsObservers_.push(observer);
},
/**
* Adds a listener for network debugging mode status. The observer
* will be called back with:
*
* observer.onSetNetworkDebugMode(status);
*/
addSetNetworkDebugModeObserver: function(observer) {
this.setNetworkDebugModeObservers_.push(observer);
},
/**
* Adds a listener for the received constants event. |observer| will be
* called back when the constants are received, through:
*
* observer.onReceivedConstants(constants);
*/
addConstantsObserver: function(observer) {
this.constantsObservers_.push(observer);
},
/**
* Adds a listener for updated prerender info events
* |observer| will be called back with:
*
* observer.onPrerenderInfoChanged(prerenderInfo);
*/
addPrerenderInfoObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.prerenderInfo.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener of extension information. |observer| will be called
* back when data is received, through:
*
* observer.onExtensionInfoChanged(extensionInfo)
*/
addExtensionInfoObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.extensionInfo.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* Adds a listener of the data reduction proxy info. |observer| will be
* called back when data is received, through:
*
* observer.onDataReductionProxyInfoChanged(dataReductionProxyInfo)
*/
addDataReductionProxyInfoObserver: function(observer, ignoreWhenUnchanged) {
this.pollableDataHelpers_.dataReductionProxyInfo.addObserver(
observer, ignoreWhenUnchanged);
},
/**
* If |force| is true, calls all startUpdate functions. Otherwise, just
* runs updates with active observers.
*/
checkForUpdatedInfo: function(force) {
for (var name in this.pollableDataHelpers_) {
var helper = this.pollableDataHelpers_[name];
if (force || helper.hasActiveObserver())
helper.startUpdate();
}
},
/**
* Calls all startUpdate functions and, if |callback| is non-null,
* calls it with the results of all updates.
*/
updateAllInfo: function(callback) {
if (callback)
new UpdateAllObserver(callback, this.pollableDataHelpers_);
this.checkForUpdatedInfo(true);
},
/**
* Adds a PollableDataHelper that listens to the specified NetInfoSource.
*/
addNetInfoPollableDataHelper: function(sourceName, observerMethodName) {
this.pollableDataHelpers_[sourceName] = new PollableDataHelper(
observerMethodName, this.sendGetNetInfo.bind(this, sourceName));
},
};
/**
* This is a helper class used by BrowserBridge, to keep track of:
* - the list of observers interested in some piece of data.
* - the last known value of that piece of data.
* - the name of the callback method to invoke on observers.
* - the update function.
* @constructor
*/
function PollableDataHelper(observerMethodName, startUpdateFunction) {
this.observerMethodName_ = observerMethodName;
this.startUpdate = startUpdateFunction;
this.observerInfos_ = [];
}
PollableDataHelper.prototype = {
getObserverMethodName: function() {
return this.observerMethodName_;
},
isObserver: function(object) {
for (var i = 0; i < this.observerInfos_.length; i++) {
if (this.observerInfos_[i].observer === object)
return true;
}
return false;
},
/**
* If |ignoreWhenUnchanged| is true, we won't send data again until it
* changes.
*/
addObserver: function(observer, ignoreWhenUnchanged) {
this.observerInfos_.push(new ObserverInfo(observer, ignoreWhenUnchanged));
},
removeObserver: function(observer) {
for (var i = 0; i < this.observerInfos_.length; i++) {
if (this.observerInfos_[i].observer === observer) {
this.observerInfos_.splice(i, 1);
return;
}
}
},
/**
* Helper function to handle calling all the observers, but ONLY if the data
* has actually changed since last time or the observer has yet to receive
* any data. This is used for data we received from browser on an update
* loop.
*/
update: function(data) {
var prevData = this.currentData_;
var changed = false;
// If the data hasn't changed since last time, will only need to notify
// observers that have not yet received any data.
if (!prevData || JSON.stringify(prevData) != JSON.stringify(data)) {
changed = true;
this.currentData_ = data;
}
// Notify the observers of the change, as needed.
for (var i = 0; i < this.observerInfos_.length; i++) {
var observerInfo = this.observerInfos_[i];
if (changed || !observerInfo.hasReceivedData ||
!observerInfo.ignoreWhenUnchanged) {
observerInfo.observer[this.observerMethodName_](this.currentData_);
observerInfo.hasReceivedData = true;
}
}
},
/**
* Returns true if one of the observers actively wants the data
* (i.e. is visible).
*/
hasActiveObserver: function() {
for (var i = 0; i < this.observerInfos_.length; i++) {
if (this.observerInfos_[i].observer.isActive())
return true;
}
return false;
}
};
/**
* This is a helper class used by PollableDataHelper, to keep track of
* each observer and whether or not it has received any data. The
* latter is used to make sure that new observers get sent data on the
* update following their creation.
* @constructor
*/
function ObserverInfo(observer, ignoreWhenUnchanged) {
this.observer = observer;
this.hasReceivedData = false;
this.ignoreWhenUnchanged = ignoreWhenUnchanged;
}
/**
* This is a helper class used by BrowserBridge to send data to
* a callback once data from all polls has been received.
*
* It works by keeping track of how many polling functions have
* yet to receive data, and recording the data as it it received.
*
* @constructor
*/
function UpdateAllObserver(callback, pollableDataHelpers) {
this.callback_ = callback;
this.observingCount_ = 0;
this.updatedData_ = {};
for (var name in pollableDataHelpers) {
++this.observingCount_;
var helper = pollableDataHelpers[name];
helper.addObserver(this);
this[helper.getObserverMethodName()] =
this.onDataReceived_.bind(this, helper, name);
}
}
UpdateAllObserver.prototype = {
isActive: function() {
return true;
},
onDataReceived_: function(helper, name, data) {
helper.removeObserver(this);
--this.observingCount_;
this.updatedData_[name] = data;
if (this.observingCount_ == 0)
this.callback_(this.updatedData_);
}
};
return BrowserBridge;
})();