OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 'use strict'; | |
6 | |
7 /** | |
8 * Handler of the background page for the drive sync events. | |
9 * @param {ProgressCenter} progressCenter Progress center to submit the | |
10 * progressing items. | |
11 * @constructor | |
12 */ | |
13 function DriveSyncHandler(progressCenter) { | |
14 /** | |
15 * Progress center to submit the progressng item. | |
16 * @type {ProgressCenter} | |
17 * @private | |
18 */ | |
19 this.progressCenter_ = progressCenter; | |
20 | |
21 /** | |
22 * Counter for progress ID. | |
23 * @type {number} | |
24 * @private | |
25 */ | |
26 this.idCounter_ = 0; | |
27 | |
28 /** | |
29 * Progressing file names. | |
30 * @type {Object.<string, ProgressCenterItem>} Map a file URL and a progress | |
31 * center item. | |
32 * @private | |
33 */ | |
34 this.items_ = {}; | |
35 | |
36 // Register event. | |
mtomasz
2014/01/15 06:38:01
nit: event -> events / the event.
hirono
2014/01/16 05:05:52
Done.
| |
37 chrome.fileBrowserPrivate.onFileTransfersUpdated.addListener( | |
38 this.onFileTransfersUpdated_.bind(this)); | |
39 } | |
40 | |
41 /** | |
42 * Complete event name. | |
mtomasz
2014/01/15 06:38:01
nit: jsdoc missing.
nit: Complete -> Completion
hirono
2014/01/16 05:05:52
Done.
| |
43 */ | |
44 DriveSyncHandler.COMPLETE_EVENT = 'complete'; | |
45 | |
46 /** | |
47 * Progress ID of the drive sync. | |
48 * @type {string} | |
49 * @const | |
50 */ | |
51 DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX = 'drive-sync-'; | |
52 | |
53 DriveSyncHandler.prototype = { | |
54 __proto__: cr.EventTarget.prototype, | |
55 | |
56 /** | |
57 * @return {boolean} Whether the handler is having synching items or not. | |
58 */ | |
59 get synching() { | |
mtomasz
2014/01/15 06:38:01
nit: synching -> syncing
hirono
2014/01/16 05:05:52
Done.
| |
60 for (var url in this.items_) { | |
61 return true; | |
mtomasz
2014/01/15 06:38:01
This looks tricky. Why not if (this.items_) { ...
hirono
2014/01/16 05:05:52
items_ is an object and this is checking if it doe
| |
62 } | |
63 return false; | |
64 } | |
65 }; | |
66 | |
67 /** | |
68 * Handle file transfer updated events. | |
mtomasz
2014/01/15 06:38:01
nit: Handles
hirono
2014/01/16 05:05:52
Done.
| |
69 * @param {Array.<object>} statusList List of drive status. | |
mtomasz
2014/01/15 06:38:01
nit: object -> Object
hirono
2014/01/16 05:05:52
Replaced with FileTransferStatus, which is defined
| |
70 * @private | |
71 */ | |
72 DriveSyncHandler.prototype.onFileTransfersUpdated_ = function(statusList) { | |
73 var completed = false; | |
74 for (var i = 0; i < statusList.length; i++) { | |
75 var status = statusList[i]; | |
76 switch (status.transferState) { | |
77 case 'in_progress': | |
78 case 'started': | |
79 this.updateItem_(status); | |
80 break; | |
81 case 'completed': | |
mtomasz
2014/01/15 06:38:01
Let's be consistent. Please choose one consistentl
hirono
2014/01/16 05:05:52
Done.
| |
82 case 'failed': | |
83 this.removeItem_(status); | |
84 if (!this.synching) | |
mtomasz
2014/01/15 06:38:01
nit: ditto typo
hirono
2014/01/16 05:05:52
Done.
| |
85 this.dispatchEvent(new Event(DriveSyncHandler.COMPLETE_EVENT)); | |
mtomasz
2014/01/15 06:38:01
ditto: -> completion event. 'Complete event' is mi
hirono
2014/01/16 05:05:52
Done.
| |
86 break; | |
87 default: | |
88 throw new Error( | |
89 'Invalid transfer state: ' + status.transferState + '.'); | |
90 } | |
91 } | |
92 }; | |
93 | |
94 /** | |
95 * Updates the item involved with the given status. | |
96 * @param {FileTransferStatus} status Transfer status. | |
97 * @private | |
98 */ | |
99 DriveSyncHandler.prototype.updateItem_ = function(status) { | |
100 var item; | |
101 if (!this.items_[status.fileUrl]) { | |
102 var fileName = status.fileUrl.replace(/.*\//, ''); | |
mtomasz
2014/01/15 06:38:01
Can we avoid working on URLs? Can we convert to En
hirono
2014/01/16 05:05:52
Done.
| |
103 item = new ProgressCenterItem(); | |
104 item.id = DriveSyncHandler.PROGRESS_ITEM_ID_PREFIX + (this.idCounter_++); | |
105 item.type = ProgressItemType.SYNC; | |
106 item.quiet = true; | |
107 item.message = strf('SYNC_FILE_NAME', fileName); | |
108 item.cancelCallback = this.requestCancel_.bind(this, status.fileUrl); | |
109 this.items_[status.fileUrl] = item; | |
110 } else { | |
111 item = this.items_[status.fileUrl]; | |
112 } | |
113 item.progressValue = status.processed || 0; | |
114 item.progressMax = status.total || 1; | |
115 this.progressCenter_.updateItem(item); | |
116 }; | |
117 | |
118 /** | |
119 * Removes the item involved with the given status. | |
120 * @param {FileTransferStatus} status Transfer status. | |
121 * @private | |
122 */ | |
123 DriveSyncHandler.prototype.removeItem_ = function(status) { | |
124 var item = this.items_[status.fileUrl]; | |
125 delete this.items_[status.fileUrl]; | |
126 if (item) { | |
127 item.state = status.transferState === 'completed' ? | |
128 ProgressItemState.COMPLETED : ProgressItemState.CANCELED; | |
129 this.progressCenter_.updateItem(item); | |
130 } | |
131 }; | |
132 | |
133 /** | |
134 * Requests to cancel for the given files' drive sync. | |
135 * @param {string} url URL of the file to be canceled. | |
mtomasz
2014/01/15 06:38:01
Can we work on Entries instead of URLs?
hirono
2014/01/16 05:05:52
Done.
| |
136 * @private | |
137 */ | |
138 DriveSyncHandler.prototype.requestCancel_ = function(url) { | |
139 chrome.fileBrowserPrivate.cancelFileTransfers([url], function() {}); | |
140 }; | |
OLD | NEW |