OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 var fileCopyManagerWrapper = null; |
| 6 |
| 7 /** |
| 8 * While FileCopyManager is run in the background page, this class is used to |
| 9 * communicate with it. |
| 10 * @constructor |
| 11 * @param {DirectoryEntry} root Root directory entry. |
| 12 */ |
| 13 function FileCopyManagerWrapper(root) { |
| 14 this.root_ = root; |
| 15 } |
| 16 |
| 17 /** |
| 18 * Extending cr.EventTarget. |
| 19 */ |
| 20 FileCopyManagerWrapper.prototype.__proto__ = cr.EventTarget.prototype; |
| 21 |
| 22 /** |
| 23 * Create a new instance or get existing instance of FCMW. |
| 24 * @param {DirectoryEntry} root Root directory entry. |
| 25 * @return {FileCopyManagerWrapper} A FileCopyManagerWrapper instance. |
| 26 */ |
| 27 FileCopyManagerWrapper.getInstance = function(root) { |
| 28 if (fileCopyManagerWrapper === null) { |
| 29 fileCopyManagerWrapper = new FileCopyManagerWrapper(root); |
| 30 } |
| 31 return fileCopyManagerWrapper; |
| 32 }; |
| 33 |
| 34 /** |
| 35 * @private |
| 36 * @return {FileCopyManager?} Copy manager instance in the background page or |
| 37 * NULL if background page is not loaded. |
| 38 */ |
| 39 FileCopyManagerWrapper.prototype.getCopyManagerSync_ = function() { |
| 40 var bg = chrome.extension.getBackgroundPage(); |
| 41 if (!bg) |
| 42 return null; |
| 43 return bg.FileCopyManager.getInstance(this.root_); |
| 44 }; |
| 45 |
| 46 /** |
| 47 * Load background page and call callback with copy manager as an argument. |
| 48 * @private |
| 49 * @param {Function} callback Function with FileCopyManager as a parameter. |
| 50 */ |
| 51 FileCopyManagerWrapper.prototype.getCopyManagerAsync_ = function(callback) { |
| 52 chrome.runtime.getBackgroundPage( |
| 53 function(bg) { |
| 54 callback(bg.FileCopyManager.getInstance(this.root_)); |
| 55 }.bind(this)); |
| 56 }; |
| 57 |
| 58 /** |
| 59 * Called be FileCopyManager to raise an event in this instance of FileManager. |
| 60 * @param {string} eventName Event name. |
| 61 * @param {Object} eventArgs Arbitratry field written to event object. |
| 62 */ |
| 63 FileCopyManagerWrapper.prototype.onEvent = function(eventName, eventArgs) { |
| 64 var event = new cr.Event(eventName); |
| 65 for (var arg in eventArgs) |
| 66 if (eventArgs.hasOwnProperty(arg)) |
| 67 event[arg] = eventArgs[arg]; |
| 68 |
| 69 this.dispatchEvent(event); |
| 70 }; |
| 71 |
| 72 /** |
| 73 * Get the overall progress data of all queued copy tasks. |
| 74 * @return {Object} An object containing the following parameters: |
| 75 * percentage - The percentage (0-1) of finished items. |
| 76 * pendingItems - The number of pending/unfinished items. |
| 77 */ |
| 78 FileCopyManagerWrapper.prototype.getProgress = function() { |
| 79 var cm = this.getCopyManagerSync_(); |
| 80 if (cm) |
| 81 return cm.getProgress(); |
| 82 |
| 83 return {percentage: NaN, pendingItems: 0}; |
| 84 }; |
| 85 |
| 86 /** |
| 87 * @return {Object} Status object. |
| 88 */ |
| 89 FileCopyManagerWrapper.prototype.getStatus = function() { |
| 90 var cm = this.getCopyManagerSync_(); |
| 91 if (cm) |
| 92 return cm.getStatus(); |
| 93 |
| 94 return { |
| 95 pendingItems: 0, |
| 96 pendingFiles: 0, |
| 97 pendingDirectories: 0, |
| 98 pendingBytes: 0, |
| 99 |
| 100 completedItems: 0, |
| 101 completedFiles: 0, |
| 102 completedDirectories: 0, |
| 103 completedBytes: 0, |
| 104 |
| 105 totalItems: 0, |
| 106 totalFiles: 0, |
| 107 totalDirectories: 0, |
| 108 totalBytes: 0 |
| 109 }; |
| 110 }; |
| 111 |
| 112 /** |
| 113 * Request that the current copy queue be abandoned. |
| 114 * @param {Function} opt_callback On cancel. |
| 115 */ |
| 116 FileCopyManagerWrapper.prototype.requestCancel = function(opt_callback) { |
| 117 this.getCopyManagerAsync_( |
| 118 function(cm) { |
| 119 cm.requestCancel(opt_callback); |
| 120 }); |
| 121 }; |
| 122 |
| 123 /** |
| 124 * Convert string in clipboard to entries and kick off pasting. |
| 125 * @param {Object} clipboard Clipboard contents. |
| 126 * @param {string} targetPath Target path. |
| 127 * @param {boolean} targetOnGData If target is on GDrive. |
| 128 */ |
| 129 FileCopyManagerWrapper.prototype.paste = function(clipboard, targetPath, |
| 130 targetOnGData) { |
| 131 this.getCopyManagerAsync_( |
| 132 function(cm) { |
| 133 cm.paste(clipboard, targetPath, targetOnGData); |
| 134 }); |
| 135 }; |
| 136 |
OLD | NEW |