1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * Thin wrapper for VolumeManager. This should be an interface proxy to talk
7 * to VolumeManager. This class also filters Drive related data/events if
8 * driveEnabled is set to false.
10 * @param {VolumeManagerWrapper.DriveEnabledStatus} driveEnabled DRIVE_ENABLED
11 * if drive should be available. DRIVE_DISABLED if drive related
12 * data/events should be hidden.
13 * @param {DOMWindow} opt_backgroundPage Window object of the background
14 * page. If this is specified, the class skips to get background page.
15 * TOOD(hirono): Let all clients of the class pass the background page and
16 * make the argument not optional.
18 * @extends {cr.EventTarget}
20 function VolumeManagerWrapper(driveEnabled, opt_backgroundPage) {
21 cr.EventTarget.call(this);
23 this.driveEnabled_ = driveEnabled;
24 this.volumeInfoList = new cr.ui.ArrayDataModel([]);
26 this.volumeManager_ = null;
27 this.pendingTasks_ = [];
28 this.onEventBound_ = this.onEvent_.bind(this);
29 this.onVolumeInfoListUpdatedBound_ =
30 this.onVolumeInfoListUpdated_.bind(this);
32 this.disposed_ = false;
34 // Start initialize the VolumeManager.
35 var queue = new AsyncUtil.Queue();
37 if (opt_backgroundPage) {
38 this.backgroundPage_ = opt_backgroundPage;
40 queue.run(function(callNextStep) {
41 chrome.runtime.getBackgroundPage(function(backgroundPage) {
42 this.backgroundPage_ = backgroundPage;
48 queue.run(function(callNextStep) {
49 this.backgroundPage_.VolumeManager.getInstance(function(volumeManager) {
50 this.onReady_(volumeManager);
57 * If the drive is enabled on the wrapper.
60 VolumeManagerWrapper.DriveEnabledStatus = {
66 * Extends cr.EventTarget.
68 VolumeManagerWrapper.prototype.__proto__ = cr.EventTarget.prototype;
71 * Called when the VolumeManager gets ready for post initialization.
72 * @param {VolumeManager} volumeManager The initialized VolumeManager instance.
75 VolumeManagerWrapper.prototype.onReady_ = function(volumeManager) {
79 this.volumeManager_ = volumeManager;
81 // Subscribe to VolumeManager.
82 this.volumeManager_.addEventListener(
83 'drive-connection-changed', this.onEventBound_);
84 this.volumeManager_.addEventListener(
85 'externally-unmounted', this.onEventBound_);
87 // Cache volumeInfoList.
88 var volumeInfoList = [];
89 for (var i = 0; i < this.volumeManager_.volumeInfoList.length; i++) {
90 var volumeInfo = this.volumeManager_.volumeInfoList.item(i);
91 // TODO(hidehiko): Filter mounted volumes located on Drive File System.
92 if (!this.driveEnabled_ && volumeInfo.volumeType === util.VolumeType.DRIVE)
94 volumeInfoList.push(volumeInfo);
96 this.volumeInfoList.splice.apply(
98 [0, this.volumeInfoList.length].concat(volumeInfoList));
100 // Subscribe to VolumeInfoList.
101 // In VolumeInfoList, we only use 'splice' event.
102 this.volumeManager_.volumeInfoList.addEventListener(
103 'splice', this.onVolumeInfoListUpdatedBound_);
105 // Run pending tasks.
106 var pendingTasks = this.pendingTasks_;
107 this.pendingTasks_ = null;
108 for (var i = 0; i < pendingTasks.length; i++)
113 * Disposes the instance. After the invocation of this method, any other
114 * method should not be called.
116 VolumeManagerWrapper.prototype.dispose = function() {
117 this.disposed_ = true;
119 if (!this.volumeManager_)
121 this.volumeManager_.removeEventListener(
122 'drive-connection-changed', this.onEventBound_);
123 this.volumeManager_.removeEventListener(
124 'externally-unmounted', this.onEventBound_);
125 this.volumeManager_.volumeInfoList.removeEventListener(
126 'splice', this.onVolumeInfoListUpdatedBound_);
130 * Called on events sent from VolumeManager. This has responsibility to
131 * re-dispatch the event to the listeners.
132 * @param {Event} event Event object sent from VolumeManager.
135 VolumeManagerWrapper.prototype.onEvent_ = function(event) {
136 if (!this.driveEnabled_) {
137 // If the drive is disabled, ignore all drive related events.
138 if (event.type === 'drive-connection-changed' ||
139 (event.type === 'externally-unmounted' &&
140 event.volumeInfo.volumeType === util.VolumeType.DRIVE))
144 this.dispatchEvent(event);
148 * Called on events of modifying VolumeInfoList.
149 * @param {Event} event Event object sent from VolumeInfoList.
152 VolumeManagerWrapper.prototype.onVolumeInfoListUpdated_ = function(event) {
153 if (this.driveEnabled_) {
154 // Apply the splice as is.
155 this.volumeInfoList.splice.apply(
157 [event.index, event.removed.length].concat(event.added));
159 // Filters drive related volumes.
160 var index = event.index;
161 for (var i = 0; i < event.index; i++) {
162 if (this.volumeManager_.volumeInfoList.item(i).volumeType ===
163 util.VolumeType.DRIVE)
167 var numRemovedVolumes = 0;
168 for (var i = 0; i < event.removed.length; i++) {
169 if (event.removed[i].volumeType !== util.VolumeType.DRIVE)
173 var addedVolumes = [];
174 for (var i = 0; i < event.added.length; i++) {
175 var volumeInfo = event.added[i];
176 if (volumeInfo.volumeType !== util.VolumeType.DRIVE)
177 addedVolumes.push(volumeInfo);
180 this.volumeInfoList.splice.apply(
182 [index, numRemovedVolumes].concat(addedVolumes));
187 * Ensures the VolumeManager is initialized, and then invokes callback.
188 * If the VolumeManager is already initialized, callback will be called
190 * @param {function()} callback Called on initialization completion.
192 VolumeManagerWrapper.prototype.ensureInitialized = function(callback) {
193 if (this.pendingTasks_) {
194 this.pendingTasks_.push(this.ensureInitialized.bind(this, callback));
202 * @return {util.DriveConnectionType} Current drive connection state.
204 VolumeManagerWrapper.prototype.getDriveConnectionState = function() {
205 if (!this.driveEnabled_ || !this.volumeManager_) {
207 type: util.DriveConnectionType.OFFLINE,
208 reason: util.DriveConnectionReason.NO_SERVICE
212 return this.volumeManager_.getDriveConnectionState();
216 * Obtains a volume info containing the passed entry.
217 * @param {Entry} entry Entry on the volume to be returned.
218 * @return {VolumeInfo} The VolumeInfo instance or null if not found.
220 VolumeManagerWrapper.prototype.getVolumeInfo = function(entry) {
221 return this.filterDisabledDriveVolume_(
222 this.volumeManager_ && this.volumeManager_.getVolumeInfo(entry));
226 * Obtains a volume information of the current profile.
227 * @param {util.VolumeType} volumeType Volume type.
228 * @return {VolumeInfo} Found volume info.
230 VolumeManagerWrapper.prototype.getCurrentProfileVolumeInfo =
231 function(volumeType) {
232 return this.filterDisabledDriveVolume_(
233 this.volumeManager_ &&
234 this.volumeManager_.getCurrentProfileVolumeInfo(volumeType));
238 * Obtains the default display root entry.
239 * @param {function(Entry)} callback Callback passed the default display root.
241 VolumeManagerWrapper.prototype.getDefaultDisplayRoot =
243 this.ensureInitialized(function() {
244 var defaultVolume = this.getCurrentProfileVolumeInfo(
245 util.VolumeType.DOWNLOADS);
246 defaultVolume.resolveDisplayRoot(callback, function() {
247 // defaultVolume is DOWNLOADS and resolveDisplayRoot should succeed.
249 'Unexpectedly failed to obtain the default display root.');
255 * Obtains location information from an entry.
257 * @param {Entry} entry File or directory entry.
258 * @return {EntryLocation} Location information.
260 VolumeManagerWrapper.prototype.getLocationInfo = function(entry) {
262 this.volumeManager_ && this.volumeManager_.getLocationInfo(entry);
265 if (!this.filterDisabledDriveVolume_(locationInfo.volumeInfo))
271 * Requests to mount the archive file.
272 * @param {string} fileUrl The path to the archive file to be mounted.
273 * @param {function(VolumeInfo)} successCallback Called with the VolumeInfo
275 * @param {function(util.VolumeError)} errorCallback Called when an error
278 VolumeManagerWrapper.prototype.mountArchive = function(
279 fileUrl, successCallback, errorCallback) {
280 if (this.pendingTasks_) {
281 this.pendingTasks_.push(
282 this.mountArchive.bind(this, fileUrl, successCallback, errorCallback));
286 this.volumeManager_.mountArchive(fileUrl, successCallback, errorCallback);
290 * Requests unmount the specified volume.
291 * @param {!VolumeInfo} volumeInfo Volume to be unmounted.
292 * @param {function()} successCallback Called on success.
293 * @param {function(util.VolumeError)} errorCallback Called when an error
296 VolumeManagerWrapper.prototype.unmount = function(
297 volumeInfo, successCallback, errorCallback) {
298 if (this.pendingTasks_) {
299 this.pendingTasks_.push(
300 this.unmount.bind(this, volumeInfo, successCallback, errorCallback));
304 this.volumeManager_.unmount(volumeInfo, successCallback, errorCallback);
308 * Filters volume info by referring driveEnabled.
310 * @param {VolumeInfo} volumeInfo Volume info.
311 * @return {VolumeInfo} Null if the drive is disabled and the given volume is
312 * drive. Otherwise just returns the volume.
315 VolumeManagerWrapper.prototype.filterDisabledDriveVolume_ =
316 function(volumeInfo) {
317 var isDrive = volumeInfo && volumeInfo.volumeType === util.VolumeType.DRIVE;
318 return this.driveEnabled_ || !isDrive ? volumeInfo : null;