Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 var stringData = new Promise(function(fulfill) { | 7 /** |
| 8 chrome.fileBrowserPrivate.getStrings(function(stringData) { | 8 * @param {Object.<string, string>} stringData String data. |
| 9 loadTimeData.data = stringData; | 9 * @param {VolumeManager} volumeManager Volume manager. |
| 10 fulfill(stringData); | 10 */ |
| 11 function BackgroundComponents(stringData, volumeManager) { | |
| 12 /** | |
| 13 * String data. | |
| 14 * @type {Object.<string, string>} | |
| 15 */ | |
| 16 this.stringData = stringData; | |
| 17 | |
| 18 /** | |
| 19 * Volume manager. | |
| 20 * @type {VolumeManager} | |
| 21 */ | |
| 22 this.volumeManager = volumeManager; | |
| 23 | |
| 24 Object.freeze(this); | |
| 25 } | |
| 26 | |
| 27 /** | |
| 28 * Loads background component. | |
| 29 * @return {Promise} Promise fulfilled with BackgroundComponents. | |
| 30 */ | |
| 31 BackgroundComponents.load = function() { | |
| 32 var stringDataPromise = new Promise(function(fulfill) { | |
| 33 chrome.fileBrowserPrivate.getStrings(function(stringData) { | |
| 34 loadTimeData.data = stringData; | |
| 35 fulfill(stringData); | |
| 36 }); | |
| 11 }); | 37 }); |
| 12 }); | |
| 13 | 38 |
| 14 // VolumeManager should be obtained after stringData initialized. | 39 // VolumeManager should be obtained after stringData initialized. |
| 15 var volumeManager = stringData.then(function() { | 40 var volumeManagerPromise = stringDataPromise.then(function() { |
| 16 return new Promise(function(fulfill) { | 41 return new Promise(function(fulfill) { |
| 17 VolumeManager.getInstance(fulfill); | 42 VolumeManager.getInstance(fulfill); |
| 43 }); | |
| 18 }); | 44 }); |
| 19 }); | |
| 20 | 45 |
| 21 var backgroundComponent = Promise.all([stringData, volumeManager]). | 46 return Promise.all([stringDataPromise, volumeManagerPromise]).then( |
| 22 then(function(args) { | 47 function(args) { |
| 23 return { | 48 return new BackgroundComponents(args[0], args[1]); |
| 24 stringData: args[0], | 49 }); |
| 25 volumeManager: args[1] | 50 }; |
| 26 }; | 51 |
| 52 /** | |
| 53 * Promise to be fulfilled with singleton instance of background components. | |
| 54 * @type {Promise} | |
| 55 */ | |
| 56 var backgroundComponentPromise = BackgroundComponents.load(); | |
|
mtomasz
2014/05/16 03:29:21
nit: backgroundComponentPromise -> backgroundCompo
hirono
2014/05/16 04:12:48
Done.
| |
| 57 | |
| 58 /** | |
| 59 * Resolves file system names and obtains entries. | |
| 60 * @param {Array.<FileEntry>} entries Names of isolated file system. | |
| 61 * @return {Promise} Promise to be fulfilled with an entry array. | |
| 62 */ | |
| 63 function resolveEntries(entries) { | |
| 64 return new Promise(function(fulfill, reject) { | |
| 65 chrome.fileBrowserPrivate.resolveIsolatedEntries(entries, | |
| 66 function(externalEntries) { | |
| 67 if (!chrome.runtime.lastError) | |
| 68 fulfill(externalEntries); | |
| 69 else | |
| 70 reject(chrome.runtime.lastError); | |
| 27 }); | 71 }); |
| 72 }); | |
| 73 } | |
| 74 | |
| 75 /** | |
| 76 * Obtains child entries. | |
| 77 * @param {DirectoryEntry} entry Directory entry. | |
| 78 * @return {Promise} Promise to be fulfilled with child entries. | |
| 79 */ | |
| 80 function getChildren(entry) { | |
| 81 var reader = entry.createReader(); | |
| 82 var readEntries = function() { | |
| 83 return new Promise(reader.readEntries.bind(reader)).then(function(entries) { | |
| 84 if (entries.length === 0) | |
| 85 return []; | |
| 86 return readEntries().then(function(nextEntries) { | |
| 87 return entries.concat(nextEntries); | |
| 88 }); | |
| 89 }); | |
| 90 }; | |
| 91 return readEntries(); | |
| 92 } | |
| 28 | 93 |
| 29 chrome.app.runtime.onLaunched.addListener(function(launchData) { | 94 chrome.app.runtime.onLaunched.addListener(function(launchData) { |
| 30 // Skip if files are not selected. | 95 // Skip if files are not selected. |
| 31 if (!launchData || !launchData.items || launchData.items.length == 0) | 96 if (!launchData || !launchData.items || launchData.items.length == 0) |
| 32 return; | 97 return; |
| 33 | 98 |
| 99 // Obtains entries in non-isolated file systems. | |
| 100 // The entries in launchData is stored in the isolated file system. | |
|
mtomasz
2014/05/16 03:29:21
nit: is -> are
hirono
2014/05/16 04:12:48
Done.
| |
| 101 // We need to map the isolated entries to the normal entries to retrieve their | |
| 102 // parent directory. | |
| 103 var isolatedEntries = launchData.items.map(function(item) { | |
| 104 return item.entry; | |
| 105 }); | |
| 106 var selectedEntriesPromise = backgroundComponentPromise.then(function() { | |
| 107 return resolveEntries(isolatedEntries); | |
| 108 }); | |
| 109 | |
| 110 // If only 1 entry is selected, retrieve entries in the same directory. | |
| 111 // Otherwise, just use the selectedEntries as an entry set. | |
| 112 var allEntriesPromise = selectedEntriesPromise.then(function(entries) { | |
| 113 if (entries.length === 1) { | |
| 114 var parent = new Promise(entries[0].getParent.bind(entries[0])); | |
| 115 return parent.then(getChildren).then(function(entries) { | |
| 116 return entries.filter(FileType.isImageOrVideo); | |
| 117 }); | |
| 118 } else { | |
| 119 return entries; | |
| 120 } | |
| 121 }); | |
| 122 | |
| 123 // Store the selected and all entries to the launchData. | |
| 124 launchData.entriesPromise = Promise.all([selectedEntriesPromise, | |
| 125 allEntriesPromise]).then( | |
| 126 function(args) { | |
| 127 return Object.freeze({ | |
| 128 selectedEntries: args[0], | |
| 129 allEntries: args[1] | |
| 130 }); | |
| 131 }); | |
| 132 | |
| 34 // Open application window. | 133 // Open application window. |
| 35 chrome.app.window.create( | 134 chrome.app.window.create( |
| 36 'gallery.html', | 135 'gallery.html', |
| 37 { | 136 { |
| 38 id: 'video', | 137 id: 'gallery', |
| 39 minWidth: 160, | 138 minWidth: 160, |
| 40 minHeight: 100, | 139 minHeight: 100, |
| 41 frame: 'none' | 140 frame: 'none' |
| 42 }, | 141 }, |
| 43 function(appWindow) { | 142 function(appWindow) { |
| 44 appWindow.contentWindow.launchData = launchData; | 143 appWindow.contentWindow.launchData = launchData; |
| 45 appWindow.contentWindow.backgroundComponent = backgroundComponent; | 144 appWindow.contentWindow.backgroundComponentPromise = |
| 145 backgroundComponentPromise; | |
| 46 }); | 146 }); |
| 47 }); | 147 }); |
| OLD | NEW |