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 var fileCopyManagerWrapper = null; | 5 var fileCopyManagerWrapper = null; |
6 | 6 |
7 /** | 7 /** |
8 * While FileCopyManager is run in the background page, this class is used to | 8 * While FileCopyManager is run in the background page, this class is used to |
9 * communicate with it. | 9 * communicate with it. |
10 * @constructor | 10 * @constructor |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 return null; | 42 return null; |
43 return bg.FileCopyManager.getInstance(this.root_); | 43 return bg.FileCopyManager.getInstance(this.root_); |
44 }; | 44 }; |
45 | 45 |
46 /** | 46 /** |
47 * Load background page and call callback with copy manager as an argument. | 47 * Load background page and call callback with copy manager as an argument. |
48 * @private | 48 * @private |
49 * @param {Function} callback Function with FileCopyManager as a parameter. | 49 * @param {Function} callback Function with FileCopyManager as a parameter. |
50 */ | 50 */ |
51 FileCopyManagerWrapper.prototype.getCopyManagerAsync_ = function(callback) { | 51 FileCopyManagerWrapper.prototype.getCopyManagerAsync_ = function(callback) { |
52 chrome.runtime.getBackgroundPage( | 52 var MAX_RETRIES = 10; |
53 function(bg) { | 53 var TIMEOUT = 100; |
54 callback(bg.FileCopyManager.getInstance(this.root_)); | 54 |
55 }.bind(this)); | 55 var root = this.root_; |
| 56 var retries = 0; |
| 57 |
| 58 function tryOnce() { |
| 59 chrome.runtime.getBackgroundPage(function(bg) { |
| 60 if (bg) { |
| 61 callback(bg.FileCopyManager.getInstance(root)); |
| 62 return; |
| 63 } |
| 64 if (++retries < MAX_RETRIES) |
| 65 setTimeout(tryOnce, TIMEOUT); |
| 66 }); |
| 67 } |
| 68 |
| 69 tryOnce(); |
56 }; | 70 }; |
57 | 71 |
58 /** | 72 /** |
59 * Called be FileCopyManager to raise an event in this instance of FileManager. | 73 * Called be FileCopyManager to raise an event in this instance of FileManager. |
60 * @param {string} eventName Event name. | 74 * @param {string} eventName Event name. |
61 * @param {Object} eventArgs Arbitratry field written to event object. | 75 * @param {Object} eventArgs Arbitratry field written to event object. |
62 */ | 76 */ |
63 FileCopyManagerWrapper.prototype.onEvent = function(eventName, eventArgs) { | 77 FileCopyManagerWrapper.prototype.onEvent = function(eventName, eventArgs) { |
64 var event = new cr.Event(eventName); | 78 var event = new cr.Event(eventName); |
65 for (var arg in eventArgs) | 79 for (var arg in eventArgs) |
(...skipping 28 matching lines...) Expand all Loading... |
94 totalBytes: 0, | 108 totalBytes: 0, |
95 | 109 |
96 percentage: NaN, | 110 percentage: NaN, |
97 pendingCopies: 0, | 111 pendingCopies: 0, |
98 pendingMoves: 0, | 112 pendingMoves: 0, |
99 filename: '' // In case pendingItems == 1 | 113 filename: '' // In case pendingItems == 1 |
100 }; | 114 }; |
101 }; | 115 }; |
102 | 116 |
103 /** | 117 /** |
104 * Request that the current copy queue be abandoned. | 118 * Decorates a FileCopyManager method, so it will be executed after initializing |
105 * @param {Function} opt_callback On cancel. | 119 * the FileCopyManager instance in background page. |
| 120 * @param {string} method The method name. |
106 */ | 121 */ |
107 FileCopyManagerWrapper.prototype.requestCancel = function(opt_callback) { | 122 FileCopyManagerWrapper.decorateAsyncMethod = function(method) { |
108 this.getCopyManagerAsync_( | 123 FileCopyManagerWrapper.prototype[method] = function() { |
109 function(cm) { | 124 var args = Array.prototype.slice.call(arguments); |
110 cm.requestCancel(opt_callback); | 125 this.getCopyManagerAsync_(function(cm) { |
111 }); | 126 cm[method].apply(cm, args); |
| 127 }); |
| 128 }; |
112 }; | 129 }; |
113 | 130 |
114 /** | 131 FileCopyManagerWrapper.decorateAsyncMethod('requestCancel'); |
115 * Convert string in clipboard to entries and kick off pasting. | 132 FileCopyManagerWrapper.decorateAsyncMethod('paste'); |
116 * @param {Object} clipboard Clipboard contents. | 133 FileCopyManagerWrapper.decorateAsyncMethod('deleteEntries'); |
117 * @param {string} targetPath Target path. | 134 FileCopyManagerWrapper.decorateAsyncMethod('forceDeleteTask'); |
118 * @param {boolean} targetOnGData If target is on GDrive. | 135 FileCopyManagerWrapper.decorateAsyncMethod('cancelDeleteTask'); |
119 */ | |
120 FileCopyManagerWrapper.prototype.paste = function(clipboard, targetPath, | |
121 targetOnGData) { | |
122 this.getCopyManagerAsync_( | |
123 function(cm) { | |
124 cm.paste(clipboard, targetPath, targetOnGData); | |
125 }); | |
126 }; | |
127 | |
OLD | NEW |