 Chromium Code Reviews
 Chromium Code Reviews Issue 1495873002:
  AudioPlayer: Stop using Object.observe() and Array.observe().  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1495873002:
  AudioPlayer: Stop using Object.observe() and Array.observe().  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| OLD | NEW | 
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 * Overrided metadata worker's path. | 6 * Overrided metadata worker's path. | 
| 7 * @type {string} | 7 * @type {string} | 
| 8 */ | 8 */ | 
| 9 ContentMetadataProvider.WORKER_SCRIPT = '/js/metadata_worker.js'; | 9 ContentMetadataProvider.WORKER_SCRIPT = '/js/metadata_worker.js'; | 
| 10 | 10 | 
| 11 /** | 11 /** | 
| 12 * @param {Element} container Container element. | 12 * @param {Element} container Container element. | 
| 13 * @constructor | 13 * @constructor | 
| 14 */ | 14 */ | 
| 15 function AudioPlayer(container) { | 15 function AudioPlayer(container) { | 
| 16 this.container_ = container; | 16 this.container_ = container; | 
| 17 this.volumeManager_ = new VolumeManagerWrapper( | 17 this.volumeManager_ = new VolumeManagerWrapper( | 
| 18 VolumeManagerWrapper.NonNativeVolumeStatus.ENABLED); | 18 VolumeManagerWrapper.NonNativeVolumeStatus.ENABLED); | 
| 19 this.metadataModel_ = MetadataModel.create(this.volumeManager_); | 19 this.metadataModel_ = MetadataModel.create(this.volumeManager_); | 
| 20 this.selectedEntry_ = null; | 20 this.selectedEntry_ = null; | 
| 21 this.invalidTracks_ = {}; | 21 this.invalidTracks_ = {}; | 
| 22 | |
| 23 this.model_ = new AudioPlayerModel(); | |
| 24 Object.observe(this.model_, function(changes) { | |
| 25 for (var i = 0; i < changes.length; i++) { | |
| 26 var change = changes[i]; | |
| 27 if (change.name == 'expanded' && | |
| 28 (change.type == 'add' || change.type == 'update')) { | |
| 29 this.onModelExpandedChanged(change.oldValue, change.object.expanded); | |
| 30 break; | |
| 31 } | |
| 32 } | |
| 33 }.bind(this)); | |
| 34 | |
| 35 this.entries_ = []; | 22 this.entries_ = []; | 
| 36 this.currentTrackIndex_ = -1; | 23 this.currentTrackIndex_ = -1; | 
| 37 this.playlistGeneration_ = 0; | 24 this.playlistGeneration_ = 0; | 
| 38 | 25 | 
| 39 /** | 26 /** | 
| 40 * Whether if the playlist is expanded or not. This value is changed by | 27 * Whether if the playlist is expanded or not. This value is changed by | 
| 41 * this.syncExpanded(). | 28 * this.syncExpanded(). | 
| 42 * True: expanded, false: collapsed, null: unset. | 29 * True: expanded, false: collapsed, null: unset. | 
| 43 * | 30 * | 
| 44 * @type {?boolean} | 31 * @type {?boolean} | 
| 45 * @private | 32 * @private | 
| 46 */ | 33 */ | 
| 47 this.isExpanded_ = null; // Initial value is null. It'll be set in load(). | 34 this.isExpanded_ = null; // Initial value is null. It'll be set in load(). | 
| 48 | 35 | 
| 49 this.player_ = | 36 this.player_ = | 
| 50 /** @type {AudioPlayerElement} */ (document.querySelector('audio-player')); | 37 /** @type {AudioPlayerElement} */ (document.querySelector('audio-player')); | 
| 51 // TODO(yoshiki): Move tracks into the model. | |
| 52 this.player_.tracks = []; | 38 this.player_.tracks = []; | 
| 53 this.model_.initialize(function() { | 39 | 
| 54 this.player_.model = this.model_; | 40 // Restore the saved state from local storage, and update the local storage | 
| 41 // if the states are changed. | |
| 42 var STORAGE_PREFIX = 'audioplayer-'; | |
| 43 var KEYS_TO_SAVE_STATES = ['shuffle', 'repeat', 'volume', 'expanded']; | |
| 44 var objectsToBeRead = KEYS_TO_SAVE_STATES.map(a => STORAGE_PREFIX + a); | |
| 45 chrome.storage.local.get(objectsToBeRead, function(results) { | |
| 46 for (var storageKey in results) { | |
| 47 // Update the UI, and start listening UI changes to write back the states | |
| 48 // to local storage. | |
| 49 var key = storageKey.substr(STORAGE_PREFIX.length); | |
| 50 this.player_[key] = results[storageKey]; | |
| 51 this.player_.addEventListener( | |
| 
yoshiki
2015/12/07 05:39:21
Is it guaranteed that all requested items are retu
 
fukino
2015/12/07 07:21:01
Oops, thank you for pointing this out!
I had all k
 | |
| 52 key + '-changed', | |
| 53 function(storageKey, event) { | |
| 54 var objectToBeSaved = {}; | |
| 55 objectToBeSaved[storageKey] = event.detail.value; | |
| 56 chrome.storage.local.set(objectToBeSaved); | |
| 57 }.bind(this, storageKey)); | |
| 58 } | |
| 59 }.bind(this)); | |
| 60 | |
| 61 // Update the window size when UI's 'expanded' state is changed. | |
| 62 this.player_.addEventListener('expanded-changed', function(event) { | |
| 63 this.onExpandedChanged_(event.detail.value); | |
| 55 }.bind(this)); | 64 }.bind(this)); | 
| 56 | 65 | 
| 57 // Run asynchronously after an event of model change is delivered. | 66 // Run asynchronously after an event of model change is delivered. | 
| 58 setTimeout(function() { | 67 setTimeout(function() { | 
| 59 this.errorString_ = ''; | 68 this.errorString_ = ''; | 
| 60 this.offlineString_ = ''; | 69 this.offlineString_ = ''; | 
| 61 chrome.fileManagerPrivate.getStrings(function(strings) { | 70 chrome.fileManagerPrivate.getStrings(function(strings) { | 
| 62 container.ownerDocument.title = strings['AUDIO_PLAYER_TITLE']; | 71 container.ownerDocument.title = strings['AUDIO_PLAYER_TITLE']; | 
| 63 this.errorString_ = strings['AUDIO_ERROR']; | 72 this.errorString_ = strings['AUDIO_ERROR']; | 
| 64 this.offlineString_ = strings['AUDIO_OFFLINE']; | 73 this.offlineString_ = strings['AUDIO_OFFLINE']; | 
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 365 /** | 374 /** | 
| 366 * Minimum size of the window in the expanded mode in pixels. | 375 * Minimum size of the window in the expanded mode in pixels. | 
| 367 * @type {number} | 376 * @type {number} | 
| 368 * @const | 377 * @const | 
| 369 */ | 378 */ | 
| 370 AudioPlayer.EXPANDED_MODE_MIN_HEIGHT = AudioPlayer.CONTROLS_HEIGHT + | 379 AudioPlayer.EXPANDED_MODE_MIN_HEIGHT = AudioPlayer.CONTROLS_HEIGHT + | 
| 371 AudioPlayer.TRACK_HEIGHT * 2; | 380 AudioPlayer.TRACK_HEIGHT * 2; | 
| 372 | 381 | 
| 373 /** | 382 /** | 
| 374 * Invoked when the 'expanded' property in the model is changed. | 383 * Invoked when the 'expanded' property in the model is changed. | 
| 375 * @param {boolean} oldValue Old value. | |
| 376 * @param {boolean} newValue New value. | 384 * @param {boolean} newValue New value. | 
| 385 * @private | |
| 377 */ | 386 */ | 
| 378 AudioPlayer.prototype.onModelExpandedChanged = function(oldValue, newValue) { | 387 AudioPlayer.prototype.onExpandedChanged_ = function(newValue) { | 
| 379 if (this.isExpanded_ !== null && | 388 if (this.isExpanded_ !== null && | 
| 380 this.isExpanded_ === newValue) | 389 this.isExpanded_ === newValue) | 
| 381 return; | 390 return; | 
| 382 | 391 | 
| 383 if (this.isExpanded_ && !newValue) | 392 if (this.isExpanded_ && !newValue) | 
| 384 this.lastExpandedHeight_ = window.innerHeight; | 393 this.lastExpandedHeight_ = window.innerHeight; | 
| 385 | 394 | 
| 386 if (this.isExpanded_ !== newValue) { | 395 if (this.isExpanded_ !== newValue) { | 
| 387 this.isExpanded_ = newValue; | 396 this.isExpanded_ = newValue; | 
| 388 this.syncHeight_(); | 397 this.syncHeight_(); | 
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 467 // TODO(yoshiki): Handle error in better way. | 476 // TODO(yoshiki): Handle error in better way. | 
| 468 // TODO(yoshiki): implement artwork (metadata.thumbnail) | 477 // TODO(yoshiki): implement artwork (metadata.thumbnail) | 
| 469 this.title = metadata.mediaTitle || this.getDefaultTitle(); | 478 this.title = metadata.mediaTitle || this.getDefaultTitle(); | 
| 470 this.artist = error || metadata.mediaArtist || this.getDefaultArtist(); | 479 this.artist = error || metadata.mediaArtist || this.getDefaultArtist(); | 
| 471 }; | 480 }; | 
| 472 | 481 | 
| 473 // Starts loading the audio player. | 482 // Starts loading the audio player. | 
| 474 window.addEventListener('DOMContentLoaded', function(e) { | 483 window.addEventListener('DOMContentLoaded', function(e) { | 
| 475 AudioPlayer.load(); | 484 AudioPlayer.load(); | 
| 476 }); | 485 }); | 
| OLD | NEW |