OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 * This class provides a "bridge" for communicating between the javascript and | 6 * This class provides a "bridge" for communicating between the javascript and |
7 * the browser. | 7 * the browser. |
8 */ | 8 */ |
9 var BrowserBridge = (function() { | 9 var BrowserBridge = (function() { |
10 'use strict'; | 10 'use strict'; |
11 | 11 |
12 /** | 12 /** |
13 * Delay in milliseconds between updates of certain browser information. | 13 * Delay in milliseconds between updates of certain browser information. |
14 */ | 14 */ |
15 var POLL_INTERVAL_MS = 5000; | 15 var POLL_INTERVAL_MS = 5000; |
16 | 16 |
17 /** | 17 /** |
18 * @constructor | 18 * @constructor |
19 */ | 19 */ |
20 function BrowserBridge() { | 20 function BrowserBridge() { |
21 assertFirstConstructorCall(BrowserBridge); | 21 assertFirstConstructorCall(BrowserBridge); |
22 | 22 |
23 // List of observers for various bits of browser state. | 23 // List of observers for various bits of browser state. |
24 this.connectionTestsObservers_ = []; | 24 this.connectionTestsObservers_ = []; |
25 this.hstsObservers_ = []; | 25 this.hstsObservers_ = []; |
26 this.httpThrottlingObservers_ = []; | 26 this.httpThrottlingObservers_ = []; |
27 this.constantsObservers_ = []; | 27 this.constantsObservers_ = []; |
| 28 this.crosONCFileParseObservers_ = []; |
28 | 29 |
29 this.pollableDataHelpers_ = {}; | 30 this.pollableDataHelpers_ = {}; |
30 this.pollableDataHelpers_.proxySettings = | 31 this.pollableDataHelpers_.proxySettings = |
31 new PollableDataHelper('onProxySettingsChanged', | 32 new PollableDataHelper('onProxySettingsChanged', |
32 this.sendGetProxySettings.bind(this)); | 33 this.sendGetProxySettings.bind(this)); |
33 this.pollableDataHelpers_.badProxies = | 34 this.pollableDataHelpers_.badProxies = |
34 new PollableDataHelper('onBadProxiesChanged', | 35 new PollableDataHelper('onBadProxiesChanged', |
35 this.sendGetBadProxies.bind(this)); | 36 this.sendGetBadProxies.bind(this)); |
36 this.pollableDataHelpers_.httpCacheInfo = | 37 this.pollableDataHelpers_.httpCacheInfo = |
37 new PollableDataHelper('onHttpCacheInfoChanged', | 38 new PollableDataHelper('onHttpCacheInfoChanged', |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 }, | 218 }, |
218 | 219 |
219 refreshSystemLogs: function() { | 220 refreshSystemLogs: function() { |
220 this.send('refreshSystemLogs'); | 221 this.send('refreshSystemLogs'); |
221 }, | 222 }, |
222 | 223 |
223 getSystemLog: function(log_key, cellId) { | 224 getSystemLog: function(log_key, cellId) { |
224 this.send('getSystemLog', [log_key, cellId]); | 225 this.send('getSystemLog', [log_key, cellId]); |
225 }, | 226 }, |
226 | 227 |
| 228 importONCFile: function(fileContent, passcode) { |
| 229 this.send('importONCFile', [fileContent, passcode]); |
| 230 }, |
| 231 |
227 //-------------------------------------------------------------------------- | 232 //-------------------------------------------------------------------------- |
228 // Messages received from the browser. | 233 // Messages received from the browser. |
229 //-------------------------------------------------------------------------- | 234 //-------------------------------------------------------------------------- |
230 | 235 |
231 receive: function(command, params) { | 236 receive: function(command, params) { |
232 // Does nothing if disabled. | 237 // Does nothing if disabled. |
233 if (this.disabled_) | 238 if (this.disabled_) |
234 return; | 239 return; |
235 this[command](params); | 240 this[command](params); |
236 }, | 241 }, |
237 | 242 |
238 receivedConstants: function(constants) { | 243 receivedConstants: function(constants) { |
239 for (var i = 0; i < this.constantsObservers_.length; ++i) | 244 for (var i = 0; i < this.constantsObservers_.length; i++) |
240 this.constantsObservers_[i].onReceivedConstants(constants); | 245 this.constantsObservers_[i].onReceivedConstants(constants); |
241 }, | 246 }, |
242 | 247 |
243 receivedLogEntries: function(logEntries) { | 248 receivedLogEntries: function(logEntries) { |
244 this.sourceTracker.onReceivedLogEntries(logEntries); | 249 this.sourceTracker.onReceivedLogEntries(logEntries); |
245 }, | 250 }, |
246 | 251 |
247 receivedProxySettings: function(proxySettings) { | 252 receivedProxySettings: function(proxySettings) { |
248 this.pollableDataHelpers_.proxySettings.update(proxySettings); | 253 this.pollableDataHelpers_.proxySettings.update(proxySettings); |
249 }, | 254 }, |
(...skipping 26 matching lines...) Expand all Loading... |
276 | 281 |
277 receivedServiceProviders: function(serviceProviders) { | 282 receivedServiceProviders: function(serviceProviders) { |
278 this.pollableDataHelpers_.serviceProviders.update(serviceProviders); | 283 this.pollableDataHelpers_.serviceProviders.update(serviceProviders); |
279 }, | 284 }, |
280 | 285 |
281 receivedPassiveLogEntries: function(entries) { | 286 receivedPassiveLogEntries: function(entries) { |
282 this.sourceTracker.onReceivedPassiveLogEntries(entries); | 287 this.sourceTracker.onReceivedPassiveLogEntries(entries); |
283 }, | 288 }, |
284 | 289 |
285 receivedStartConnectionTestSuite: function() { | 290 receivedStartConnectionTestSuite: function() { |
286 for (var i = 0; i < this.connectionTestsObservers_.length; ++i) | 291 for (var i = 0; i < this.connectionTestsObservers_.length; i++) |
287 this.connectionTestsObservers_[i].onStartedConnectionTestSuite(); | 292 this.connectionTestsObservers_[i].onStartedConnectionTestSuite(); |
288 }, | 293 }, |
289 | 294 |
290 receivedStartConnectionTestExperiment: function(experiment) { | 295 receivedStartConnectionTestExperiment: function(experiment) { |
291 for (var i = 0; i < this.connectionTestsObservers_.length; ++i) { | 296 for (var i = 0; i < this.connectionTestsObservers_.length; i++) { |
292 this.connectionTestsObservers_[i].onStartedConnectionTestExperiment( | 297 this.connectionTestsObservers_[i].onStartedConnectionTestExperiment( |
293 experiment); | 298 experiment); |
294 } | 299 } |
295 }, | 300 }, |
296 | 301 |
297 receivedCompletedConnectionTestExperiment: function(info) { | 302 receivedCompletedConnectionTestExperiment: function(info) { |
298 for (var i = 0; i < this.connectionTestsObservers_.length; ++i) { | 303 for (var i = 0; i < this.connectionTestsObservers_.length; i++) { |
299 this.connectionTestsObservers_[i].onCompletedConnectionTestExperiment( | 304 this.connectionTestsObservers_[i].onCompletedConnectionTestExperiment( |
300 info.experiment, info.result); | 305 info.experiment, info.result); |
301 } | 306 } |
302 }, | 307 }, |
303 | 308 |
304 receivedCompletedConnectionTestSuite: function() { | 309 receivedCompletedConnectionTestSuite: function() { |
305 for (var i = 0; i < this.connectionTestsObservers_.length; ++i) | 310 for (var i = 0; i < this.connectionTestsObservers_.length; i++) |
306 this.connectionTestsObservers_[i].onCompletedConnectionTestSuite(); | 311 this.connectionTestsObservers_[i].onCompletedConnectionTestSuite(); |
307 }, | 312 }, |
308 | 313 |
309 receivedHSTSResult: function(info) { | 314 receivedHSTSResult: function(info) { |
310 for (var i = 0; i < this.hstsObservers_.length; ++i) | 315 for (var i = 0; i < this.hstsObservers_.length; i++) |
311 this.hstsObservers_[i].onHSTSQueryResult(info); | 316 this.hstsObservers_[i].onHSTSQueryResult(info); |
312 }, | 317 }, |
313 | 318 |
| 319 receivedONCFileParse: function(status) { |
| 320 for (var i = 0; i < this.crosONCFileParseObservers_.length; i++) |
| 321 this.crosONCFileParseObservers_[i].onONCFileParse(status); |
| 322 }, |
| 323 |
314 receivedHttpCacheInfo: function(info) { | 324 receivedHttpCacheInfo: function(info) { |
315 this.pollableDataHelpers_.httpCacheInfo.update(info); | 325 this.pollableDataHelpers_.httpCacheInfo.update(info); |
316 }, | 326 }, |
317 | 327 |
318 receivedHttpThrottlingEnabledPrefChanged: function(enabled) { | 328 receivedHttpThrottlingEnabledPrefChanged: function(enabled) { |
319 for (var i = 0; i < this.httpThrottlingObservers_.length; ++i) { | 329 for (var i = 0; i < this.httpThrottlingObservers_.length; i++) { |
320 this.httpThrottlingObservers_[i].onHttpThrottlingEnabledPrefChanged( | 330 this.httpThrottlingObservers_[i].onHttpThrottlingEnabledPrefChanged( |
321 enabled); | 331 enabled); |
322 } | 332 } |
323 }, | 333 }, |
324 | 334 |
325 receivedPrerenderInfo: function(prerenderInfo) { | 335 receivedPrerenderInfo: function(prerenderInfo) { |
326 this.pollableDataHelpers_.prerenderInfo.update(prerenderInfo); | 336 this.pollableDataHelpers_.prerenderInfo.update(prerenderInfo); |
327 }, | 337 }, |
328 | 338 |
329 //-------------------------------------------------------------------------- | 339 //-------------------------------------------------------------------------- |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 * Adds a listener for the results of HSTS (HTTPS Strict Transport Security) | 488 * Adds a listener for the results of HSTS (HTTPS Strict Transport Security) |
479 * queries. The observer will be called back with: | 489 * queries. The observer will be called back with: |
480 * | 490 * |
481 * observer.onHSTSQueryResult(result); | 491 * observer.onHSTSQueryResult(result); |
482 */ | 492 */ |
483 addHSTSObserver: function(observer) { | 493 addHSTSObserver: function(observer) { |
484 this.hstsObservers_.push(observer); | 494 this.hstsObservers_.push(observer); |
485 }, | 495 }, |
486 | 496 |
487 /** | 497 /** |
| 498 * Adds a listener for ONC file parse status. The observer will be called |
| 499 * back with: |
| 500 * |
| 501 * observer.onONCFileParse(status); |
| 502 */ |
| 503 addCrosONCFileParseObserver: function(observer) { |
| 504 this.crosONCFileParseObservers_.push(observer); |
| 505 }, |
| 506 |
| 507 /** |
488 * Adds a listener for HTTP throttling-related events. |observer| will be | 508 * Adds a listener for HTTP throttling-related events. |observer| will be |
489 * called back when HTTP throttling is enabled/disabled, through: | 509 * called back when HTTP throttling is enabled/disabled, through: |
490 * | 510 * |
491 * observer.onHttpThrottlingEnabledPrefChanged(enabled); | 511 * observer.onHttpThrottlingEnabledPrefChanged(enabled); |
492 */ | 512 */ |
493 addHttpThrottlingObserver: function(observer) { | 513 addHttpThrottlingObserver: function(observer) { |
494 this.httpThrottlingObservers_.push(observer); | 514 this.httpThrottlingObservers_.push(observer); |
495 }, | 515 }, |
496 | 516 |
497 /** | 517 /** |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 this.startUpdate = startUpdateFunction; | 571 this.startUpdate = startUpdateFunction; |
552 this.observerInfos_ = []; | 572 this.observerInfos_ = []; |
553 } | 573 } |
554 | 574 |
555 PollableDataHelper.prototype = { | 575 PollableDataHelper.prototype = { |
556 getObserverMethodName: function() { | 576 getObserverMethodName: function() { |
557 return this.observerMethodName_; | 577 return this.observerMethodName_; |
558 }, | 578 }, |
559 | 579 |
560 isObserver: function(object) { | 580 isObserver: function(object) { |
561 for (var i = 0; i < this.observerInfos_.length; ++i) { | 581 for (var i = 0; i < this.observerInfos_.length; i++) { |
562 if (this.observerInfos_[i].observer === object) | 582 if (this.observerInfos_[i].observer === object) |
563 return true; | 583 return true; |
564 } | 584 } |
565 return false; | 585 return false; |
566 }, | 586 }, |
567 | 587 |
568 /** | 588 /** |
569 * If |ignoreWhenUnchanged| is true, we won't send data again until it | 589 * If |ignoreWhenUnchanged| is true, we won't send data again until it |
570 * changes. | 590 * changes. |
571 */ | 591 */ |
572 addObserver: function(observer, ignoreWhenUnchanged) { | 592 addObserver: function(observer, ignoreWhenUnchanged) { |
573 this.observerInfos_.push(new ObserverInfo(observer, ignoreWhenUnchanged)); | 593 this.observerInfos_.push(new ObserverInfo(observer, ignoreWhenUnchanged)); |
574 }, | 594 }, |
575 | 595 |
576 removeObserver: function(observer) { | 596 removeObserver: function(observer) { |
577 for (var i = 0; i < this.observerInfos_.length; ++i) { | 597 for (var i = 0; i < this.observerInfos_.length; i++) { |
578 if (this.observerInfos_[i].observer === observer) { | 598 if (this.observerInfos_[i].observer === observer) { |
579 this.observerInfos_.splice(i, 1); | 599 this.observerInfos_.splice(i, 1); |
580 return; | 600 return; |
581 } | 601 } |
582 } | 602 } |
583 }, | 603 }, |
584 | 604 |
585 /** | 605 /** |
586 * Helper function to handle calling all the observers, but ONLY if the data | 606 * Helper function to handle calling all the observers, but ONLY if the data |
587 * has actually changed since last time or the observer has yet to receive | 607 * has actually changed since last time or the observer has yet to receive |
588 * any data. This is used for data we received from browser on an update | 608 * any data. This is used for data we received from browser on an update |
589 * loop. | 609 * loop. |
590 */ | 610 */ |
591 update: function(data) { | 611 update: function(data) { |
592 var prevData = this.currentData_; | 612 var prevData = this.currentData_; |
593 var changed = false; | 613 var changed = false; |
594 | 614 |
595 // If the data hasn't changed since last time, will only need to notify | 615 // If the data hasn't changed since last time, will only need to notify |
596 // observers that have not yet received any data. | 616 // observers that have not yet received any data. |
597 if (!prevData || JSON.stringify(prevData) != JSON.stringify(data)) { | 617 if (!prevData || JSON.stringify(prevData) != JSON.stringify(data)) { |
598 changed = true; | 618 changed = true; |
599 this.currentData_ = data; | 619 this.currentData_ = data; |
600 } | 620 } |
601 | 621 |
602 // Notify the observers of the change, as needed. | 622 // Notify the observers of the change, as needed. |
603 for (var i = 0; i < this.observerInfos_.length; ++i) { | 623 for (var i = 0; i < this.observerInfos_.length; i++) { |
604 var observerInfo = this.observerInfos_[i]; | 624 var observerInfo = this.observerInfos_[i]; |
605 if (changed || !observerInfo.hasReceivedData || | 625 if (changed || !observerInfo.hasReceivedData || |
606 !observerInfo.ignoreWhenUnchanged) { | 626 !observerInfo.ignoreWhenUnchanged) { |
607 observerInfo.observer[this.observerMethodName_](this.currentData_); | 627 observerInfo.observer[this.observerMethodName_](this.currentData_); |
608 observerInfo.hasReceivedData = true; | 628 observerInfo.hasReceivedData = true; |
609 } | 629 } |
610 } | 630 } |
611 }, | 631 }, |
612 | 632 |
613 /** | 633 /** |
614 * Returns true if one of the observers actively wants the data | 634 * Returns true if one of the observers actively wants the data |
615 * (i.e. is visible). | 635 * (i.e. is visible). |
616 */ | 636 */ |
617 hasActiveObserver: function() { | 637 hasActiveObserver: function() { |
618 for (var i = 0; i < this.observerInfos_.length; ++i) { | 638 for (var i = 0; i < this.observerInfos_.length; i++) { |
619 if (this.observerInfos_[i].observer.isActive()) | 639 if (this.observerInfos_[i].observer.isActive()) |
620 return true; | 640 return true; |
621 } | 641 } |
622 return false; | 642 return false; |
623 } | 643 } |
624 }; | 644 }; |
625 | 645 |
626 /** | 646 /** |
627 * This is a helper class used by PollableDataHelper, to keep track of | 647 * This is a helper class used by PollableDataHelper, to keep track of |
628 * each observer and whether or not it has received any data. The | 648 * each observer and whether or not it has received any data. The |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 helper.removeObserver(this); | 688 helper.removeObserver(this); |
669 --this.observingCount_; | 689 --this.observingCount_; |
670 this.updatedData_[name] = data; | 690 this.updatedData_[name] = data; |
671 if (this.observingCount_ == 0) | 691 if (this.observingCount_ == 0) |
672 this.callback_(this.updatedData_); | 692 this.callback_(this.updatedData_); |
673 } | 693 } |
674 }; | 694 }; |
675 | 695 |
676 return BrowserBridge; | 696 return BrowserBridge; |
677 })(); | 697 })(); |
OLD | NEW |