| 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 'use strict'; | |
| 6 | |
| 7 /** | |
| 8 * Responsible for showing following banners in the file list. | |
| 9 * - WelcomeBanner | |
| 10 * - AuthFailBanner | |
| 11 * @param {DirectoryModel} directoryModel The model. | |
| 12 * @param {VolumeManagerWrapper} volumeManager The manager. | |
| 13 * @param {DOMDocument} document HTML document. | |
| 14 * @param {boolean} showOffers True if we should show offer banners. | |
| 15 * @constructor | |
| 16 */ | |
| 17 function FileListBannerController( | |
| 18 directoryModel, volumeManager, document, showOffers) { | |
| 19 this.directoryModel_ = directoryModel; | |
| 20 this.volumeManager_ = volumeManager; | |
| 21 this.document_ = document; | |
| 22 this.showOffers_ = showOffers; | |
| 23 this.driveEnabled_ = false; | |
| 24 | |
| 25 this.initializeWelcomeBanner_(); | |
| 26 this.privateOnDirectoryChangedBound_ = | |
| 27 this.privateOnDirectoryChanged_.bind(this); | |
| 28 | |
| 29 var handler = this.checkSpaceAndMaybeShowWelcomeBanner_.bind(this); | |
| 30 this.directoryModel_.addEventListener('scan-completed', handler); | |
| 31 this.directoryModel_.addEventListener('rescan-completed', handler); | |
| 32 this.directoryModel_.addEventListener('directory-changed', | |
| 33 this.onDirectoryChanged_.bind(this)); | |
| 34 | |
| 35 this.unmountedPanel_ = this.document_.querySelector('#unmounted-panel'); | |
| 36 this.volumeManager_.volumeInfoList.addEventListener( | |
| 37 'splice', this.onVolumeInfoListSplice_.bind(this)); | |
| 38 this.volumeManager_.addEventListener('drive-connection-changed', | |
| 39 this.onDriveConnectionChanged_.bind(this)); | |
| 40 | |
| 41 chrome.storage.onChanged.addListener(this.onStorageChange_.bind(this)); | |
| 42 this.welcomeHeaderCounter_ = WELCOME_HEADER_COUNTER_LIMIT; | |
| 43 this.warningDismissedCounter_ = 0; | |
| 44 chrome.storage.local.get([WELCOME_HEADER_COUNTER_KEY, WARNING_DISMISSED_KEY], | |
| 45 function(values) { | |
| 46 this.welcomeHeaderCounter_ = | |
| 47 parseInt(values[WELCOME_HEADER_COUNTER_KEY]) || 0; | |
| 48 this.warningDismissedCounter_ = | |
| 49 parseInt(values[WARNING_DISMISSED_KEY]) || 0; | |
| 50 }.bind(this)); | |
| 51 | |
| 52 this.authFailedBanner_ = | |
| 53 this.document_.querySelector('#drive-auth-failed-warning'); | |
| 54 var authFailedText = this.authFailedBanner_.querySelector('.drive-text'); | |
| 55 authFailedText.innerHTML = util.htmlUnescape(str('DRIVE_NOT_REACHED')); | |
| 56 authFailedText.querySelector('a').addEventListener('click', function(e) { | |
| 57 chrome.fileBrowserPrivate.logoutUser(); | |
| 58 e.preventDefault(); | |
| 59 }); | |
| 60 this.maybeShowAuthFailBanner_(); | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * FileListBannerController extends cr.EventTarget. | |
| 65 */ | |
| 66 FileListBannerController.prototype.__proto__ = cr.EventTarget.prototype; | |
| 67 | |
| 68 /** | |
| 69 * Key in localStorage to keep number of times the Drive Welcome | |
| 70 * banner has shown. | |
| 71 */ | |
| 72 var WELCOME_HEADER_COUNTER_KEY = 'driveWelcomeHeaderCounter'; | |
| 73 | |
| 74 // If the warning was dismissed before, this key stores the quota value | |
| 75 // (as of the moment of dismissal). | |
| 76 // If the warning was never dismissed or was reset this key stores 0. | |
| 77 var WARNING_DISMISSED_KEY = 'driveSpaceWarningDismissed'; | |
| 78 | |
| 79 /** | |
| 80 * Maximum times Drive Welcome banner could have shown. | |
| 81 */ | |
| 82 var WELCOME_HEADER_COUNTER_LIMIT = 25; | |
| 83 | |
| 84 /** | |
| 85 * Initializes the banner to promote DRIVE. | |
| 86 * This method must be called before any of showing banner functions, and | |
| 87 * also before registering them as callbacks. | |
| 88 * @private | |
| 89 */ | |
| 90 FileListBannerController.prototype.initializeWelcomeBanner_ = function() { | |
| 91 this.usePromoWelcomeBanner_ = (!util.boardIs('x86-mario') && | |
| 92 !util.boardIs('x86-zgb') && | |
| 93 !util.boardIs('x86-alex')); | |
| 94 }; | |
| 95 | |
| 96 /** | |
| 97 * @param {number} value How many times the Drive Welcome header banner | |
| 98 * has shown. | |
| 99 * @private | |
| 100 */ | |
| 101 FileListBannerController.prototype.setWelcomeHeaderCounter_ = function(value) { | |
| 102 var values = {}; | |
| 103 values[WELCOME_HEADER_COUNTER_KEY] = value; | |
| 104 chrome.storage.local.set(values); | |
| 105 }; | |
| 106 | |
| 107 /** | |
| 108 * @param {number} value How many times the low space warning has dismissed. | |
| 109 * @private | |
| 110 */ | |
| 111 FileListBannerController.prototype.setWarningDismissedCounter_ = | |
| 112 function(value) { | |
| 113 var values = {}; | |
| 114 values[WARNING_DISMISSED_KEY] = value; | |
| 115 chrome.storage.local.set(values); | |
| 116 }; | |
| 117 | |
| 118 /** | |
| 119 * chrome.storage.onChanged event handler. | |
| 120 * @param {Object.<string, Object>} changes Changes values. | |
| 121 * @param {string} areaName "local" or "sync". | |
| 122 * @private | |
| 123 */ | |
| 124 FileListBannerController.prototype.onStorageChange_ = function(changes, | |
| 125 areaName) { | |
| 126 if (areaName == 'local' && WELCOME_HEADER_COUNTER_KEY in changes) { | |
| 127 this.welcomeHeaderCounter_ = changes[WELCOME_HEADER_COUNTER_KEY].newValue; | |
| 128 } | |
| 129 if (areaName == 'local' && WARNING_DISMISSED_KEY in changes) { | |
| 130 this.warningDismissedCounter_ = changes[WARNING_DISMISSED_KEY].newValue; | |
| 131 } | |
| 132 }; | |
| 133 | |
| 134 /** | |
| 135 * Invoked when the drive connection status is change in the volume manager. | |
| 136 * @private | |
| 137 */ | |
| 138 FileListBannerController.prototype.onDriveConnectionChanged_ = function() { | |
| 139 this.maybeShowAuthFailBanner_(); | |
| 140 }; | |
| 141 | |
| 142 /** | |
| 143 * @param {string} type 'none'|'page'|'header'. | |
| 144 * @param {string} messageId Resource ID of the message. | |
| 145 * @private | |
| 146 */ | |
| 147 FileListBannerController.prototype.prepareAndShowWelcomeBanner_ = | |
| 148 function(type, messageId) { | |
| 149 this.showWelcomeBanner_(type); | |
| 150 | |
| 151 var container = this.document_.querySelector('.drive-welcome.' + type); | |
| 152 if (container.firstElementChild) | |
| 153 return; // Do not re-create. | |
| 154 | |
| 155 if (!this.document_.querySelector('link[drive-welcome-style]')) { | |
| 156 var style = this.document_.createElement('link'); | |
| 157 style.rel = 'stylesheet'; | |
| 158 style.href = 'css/drive_welcome.css'; | |
| 159 style.setAttribute('drive-welcome-style', ''); | |
| 160 this.document_.head.appendChild(style); | |
| 161 } | |
| 162 | |
| 163 var wrapper = util.createChild(container, 'drive-welcome-wrapper'); | |
| 164 util.createChild(wrapper, 'drive-welcome-icon'); | |
| 165 | |
| 166 var close = util.createChild(wrapper, 'cr-dialog-close'); | |
| 167 close.addEventListener('click', this.closeWelcomeBanner_.bind(this)); | |
| 168 | |
| 169 var message = util.createChild(wrapper, 'drive-welcome-message'); | |
| 170 | |
| 171 var title = util.createChild(message, 'drive-welcome-title'); | |
| 172 | |
| 173 var text = util.createChild(message, 'drive-welcome-text'); | |
| 174 text.innerHTML = str(messageId); | |
| 175 | |
| 176 var links = util.createChild(message, 'drive-welcome-links'); | |
| 177 | |
| 178 var more; | |
| 179 if (this.usePromoWelcomeBanner_) { | |
| 180 var welcomeTitle = str('DRIVE_WELCOME_TITLE_ALTERNATIVE'); | |
| 181 if (util.boardIs('link')) | |
| 182 welcomeTitle = str('DRIVE_WELCOME_TITLE_ALTERNATIVE_1TB'); | |
| 183 title.textContent = welcomeTitle; | |
| 184 more = util.createChild(links, | |
| 185 'drive-welcome-button drive-welcome-start', 'a'); | |
| 186 more.textContent = str('DRIVE_WELCOME_CHECK_ELIGIBILITY'); | |
| 187 more.href = urlConstants.GOOGLE_DRIVE_REDEEM; | |
| 188 } else { | |
| 189 title.textContent = str('DRIVE_WELCOME_TITLE'); | |
| 190 more = util.createChild(links, 'plain-link', 'a'); | |
| 191 more.textContent = str('DRIVE_LEARN_MORE'); | |
| 192 more.href = urlConstants.GOOGLE_DRIVE_FAQ_URL; | |
| 193 } | |
| 194 more.tabIndex = '13'; // See: go/filesapp-tabindex. | |
| 195 more.target = '_blank'; | |
| 196 | |
| 197 var dismiss; | |
| 198 if (this.usePromoWelcomeBanner_) | |
| 199 dismiss = util.createChild(links, 'drive-welcome-button'); | |
| 200 else | |
| 201 dismiss = util.createChild(links, 'plain-link'); | |
| 202 | |
| 203 dismiss.classList.add('drive-welcome-dismiss'); | |
| 204 dismiss.textContent = str('DRIVE_WELCOME_DISMISS'); | |
| 205 dismiss.addEventListener('click', this.closeWelcomeBanner_.bind(this)); | |
| 206 | |
| 207 this.previousDirWasOnDrive_ = false; | |
| 208 }; | |
| 209 | |
| 210 /** | |
| 211 * Show or hide the "Low Google Drive space" warning. | |
| 212 * @param {boolean} show True if the box need to be shown. | |
| 213 * @param {Object} sizeStats Size statistics. Should be defined when showing the | |
| 214 * warning. | |
| 215 * @private | |
| 216 */ | |
| 217 FileListBannerController.prototype.showLowDriveSpaceWarning_ = | |
| 218 function(show, sizeStats) { | |
| 219 var box = this.document_.querySelector('#volume-space-warning'); | |
| 220 | |
| 221 // Avoid showing two banners. | |
| 222 // TODO(kaznacheev): Unify the low space warning and the promo header. | |
| 223 if (show) | |
| 224 this.cleanupWelcomeBanner_(); | |
| 225 | |
| 226 if (box.hidden == !show) | |
| 227 return; | |
| 228 | |
| 229 if (this.warningDismissedCounter_) { | |
| 230 if (this.warningDismissedCounter_ == | |
| 231 sizeStats.totalSize && // Quota had not changed | |
| 232 sizeStats.remainingSize / sizeStats.totalSize < 0.15) { | |
| 233 // Since the last dismissal decision the quota has not changed AND | |
| 234 // the user did not free up significant space. Obey the dismissal. | |
| 235 show = false; | |
| 236 } else { | |
| 237 // Forget the dismissal. Warning will be shown again. | |
| 238 this.setWarningDismissedCounter_(0); | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 box.textContent = ''; | |
| 243 if (show) { | |
| 244 var icon = this.document_.createElement('div'); | |
| 245 icon.className = 'drive-icon'; | |
| 246 box.appendChild(icon); | |
| 247 | |
| 248 var text = this.document_.createElement('div'); | |
| 249 text.className = 'drive-text'; | |
| 250 text.textContent = strf('DRIVE_SPACE_AVAILABLE_LONG', | |
| 251 util.bytesToString(sizeStats.remainingSize)); | |
| 252 box.appendChild(text); | |
| 253 | |
| 254 var link = this.document_.createElement('a'); | |
| 255 link.className = 'plain-link'; | |
| 256 link.textContent = str('DRIVE_BUY_MORE_SPACE_LINK'); | |
| 257 link.href = urlConstants.GOOGLE_DRIVE_BUY_STORAGE; | |
| 258 link.target = '_blank'; | |
| 259 box.appendChild(link); | |
| 260 | |
| 261 var close = this.document_.createElement('div'); | |
| 262 close.className = 'cr-dialog-close'; | |
| 263 box.appendChild(close); | |
| 264 close.addEventListener('click', function(total) { | |
| 265 window.localStorage[WARNING_DISMISSED_KEY] = total; | |
| 266 box.hidden = true; | |
| 267 this.requestRelayout_(100); | |
| 268 }.bind(this, sizeStats.totalSize)); | |
| 269 } | |
| 270 | |
| 271 if (box.hidden != !show) { | |
| 272 box.hidden = !show; | |
| 273 this.requestRelayout_(100); | |
| 274 } | |
| 275 }; | |
| 276 /** | |
| 277 * Closes the Drive Welcome banner. | |
| 278 * @private | |
| 279 */ | |
| 280 FileListBannerController.prototype.closeWelcomeBanner_ = function() { | |
| 281 this.cleanupWelcomeBanner_(); | |
| 282 // Stop showing the welcome banner. | |
| 283 this.setWelcomeHeaderCounter_(WELCOME_HEADER_COUNTER_LIMIT); | |
| 284 }; | |
| 285 | |
| 286 /** | |
| 287 * Shows or hides the welcome banner for drive. | |
| 288 * @private | |
| 289 */ | |
| 290 FileListBannerController.prototype.checkSpaceAndMaybeShowWelcomeBanner_ = | |
| 291 function() { | |
| 292 if (!this.isOnDrive()) { | |
| 293 // We are not on the drive file system. Do not show (close) the welcome | |
| 294 // banner. | |
| 295 this.cleanupWelcomeBanner_(); | |
| 296 this.previousDirWasOnDrive_ = false; | |
| 297 return; | |
| 298 } | |
| 299 | |
| 300 var driveVolume = this.volumeManager_.getVolumeInfo(RootDirectory.DRIVE); | |
| 301 if (this.welcomeHeaderCounter_ >= WELCOME_HEADER_COUNTER_LIMIT || | |
| 302 !driveVolume || driveVolume.error) { | |
| 303 // The banner is already shown enough times or the drive FS is not mounted. | |
| 304 // So, do nothing here. | |
| 305 return; | |
| 306 } | |
| 307 | |
| 308 if (!this.showOffers_) { | |
| 309 // Because it is not necessary to show the offer, set | |
| 310 // |usePromoWelcomeBanner_| false here. Note that it probably should be able | |
| 311 // to do this in the constructor, but there remains non-trivial path, | |
| 312 // which may be causes |usePromoWelcomeBanner_| == true's behavior even | |
| 313 // if |showOffers_| is false. | |
| 314 // TODO(hidehiko): Make sure if it is expected or not, and simplify | |
| 315 // |showOffers_| if possible. | |
| 316 this.usePromoWelcomeBanner_ = false; | |
| 317 } | |
| 318 | |
| 319 // Perform asynchronous tasks in parallel. | |
| 320 var group = new AsyncUtil.Group(); | |
| 321 | |
| 322 // Choose the offer basing on the board name. The default one is 100 GB. | |
| 323 var offerSize = 100; // In GB. | |
| 324 var offerServiceId = 'drive.cros.echo.1'; | |
| 325 | |
| 326 if (util.boardIs('link')) { | |
| 327 offerSize = 1024; // 1 TB. | |
| 328 offerServiceId = 'drive.cros.echo.2'; | |
| 329 } | |
| 330 | |
| 331 // If the offer has been checked, then do not show the promo anymore. | |
| 332 group.add(function(onCompleted) { | |
| 333 chrome.echoPrivate.getOfferInfo(offerServiceId, function(offerInfo) { | |
| 334 // If the offer has not been checked, then an error is raised. | |
| 335 if (!chrome.runtime.lastError) | |
| 336 this.usePromoWelcomeBanner_ = false; | |
| 337 onCompleted(); | |
| 338 }.bind(this)); | |
| 339 }.bind(this)); | |
| 340 | |
| 341 if (this.usePromoWelcomeBanner_) { | |
| 342 // getSizeStats for Drive file system accesses to the server, so we should | |
| 343 // minimize the invocation. | |
| 344 group.add(function(onCompleted) { | |
| 345 chrome.fileBrowserPrivate.getSizeStats( | |
| 346 util.makeFilesystemUrl(this.directoryModel_.getCurrentRootPath()), | |
| 347 function(result) { | |
| 348 if (result && result.totalSize >= offerSize * 1024 * 1024 * 1024) | |
| 349 this.usePromoWelcomeBanner_ = false; | |
| 350 onCompleted(); | |
| 351 }.bind(this)); | |
| 352 }.bind(this)); | |
| 353 } | |
| 354 | |
| 355 group.run(this.maybeShowWelcomeBanner_.bind(this)); | |
| 356 }; | |
| 357 | |
| 358 /** | |
| 359 * Decides which banner should be shown, and show it. This method is designed | |
| 360 * to be called only from checkSpaceAndMaybeShowWelcomeBanner_. | |
| 361 * @private | |
| 362 */ | |
| 363 FileListBannerController.prototype.maybeShowWelcomeBanner_ = function() { | |
| 364 if (this.directoryModel_.getFileList().length == 0 && | |
| 365 this.welcomeHeaderCounter_ == 0) { | |
| 366 // Only show the full page banner if the header banner was never shown. | |
| 367 // Do not increment the counter. | |
| 368 // The timeout below is required because sometimes another | |
| 369 // 'rescan-completed' event arrives shortly with non-empty file list. | |
| 370 setTimeout(function() { | |
| 371 if (this.isOnDrive() && this.welcomeHeaderCounter_ == 0) { | |
| 372 this.prepareAndShowWelcomeBanner_('page', 'DRIVE_WELCOME_TEXT_LONG'); | |
| 373 } | |
| 374 }.bind(this), 2000); | |
| 375 } else { | |
| 376 // We do not want to increment the counter when the user navigates | |
| 377 // between different directories on Drive, but we increment the counter | |
| 378 // once anyway to prevent the full page banner from showing. | |
| 379 if (!this.previousDirWasOnDrive_ || this.welcomeHeaderCounter_ == 0) { | |
| 380 this.setWelcomeHeaderCounter_(this.welcomeHeaderCounter_ + 1); | |
| 381 this.prepareAndShowWelcomeBanner_('header', 'DRIVE_WELCOME_TEXT_SHORT'); | |
| 382 } | |
| 383 } | |
| 384 this.previousDirWasOnDrive_ = true; | |
| 385 }; | |
| 386 | |
| 387 /** | |
| 388 * @return {boolean} True if current directory is on Drive. | |
| 389 */ | |
| 390 FileListBannerController.prototype.isOnDrive = function() { | |
| 391 return this.directoryModel_.getCurrentRootType() === RootType.DRIVE; | |
| 392 }; | |
| 393 | |
| 394 /** | |
| 395 * Shows the Drive Welcome banner. | |
| 396 * @param {string} type 'page'|'head'|'none'. | |
| 397 * @private | |
| 398 */ | |
| 399 FileListBannerController.prototype.showWelcomeBanner_ = function(type) { | |
| 400 var container = this.document_.querySelector('.dialog-container'); | |
| 401 if (container.getAttribute('drive-welcome') != type) { | |
| 402 container.setAttribute('drive-welcome', type); | |
| 403 this.requestRelayout_(200); // Resize only after the animation is done. | |
| 404 } | |
| 405 }; | |
| 406 | |
| 407 /** | |
| 408 * Update the UI when the current directory changes. | |
| 409 * | |
| 410 * @param {Event} event The directory-changed event. | |
| 411 * @private | |
| 412 */ | |
| 413 FileListBannerController.prototype.onDirectoryChanged_ = function(event) { | |
| 414 var root = PathUtil.getTopDirectory(event.newDirEntry.fullPath); | |
| 415 // Show (or hide) the low space warning. | |
| 416 this.maybeShowLowSpaceWarning_(root); | |
| 417 | |
| 418 // Add or remove listener to show low space warning, if necessary. | |
| 419 var isLowSpaceWarningTarget = this.isLowSpaceWarningTarget_(root); | |
| 420 var previousRoot = event.previousDirEntry ? | |
| 421 PathUtil.getTopDirectory(event.previousDirEntry.fullPath) : ''; | |
| 422 if (isLowSpaceWarningTarget !== this.isLowSpaceWarningTarget_(previousRoot)) { | |
| 423 if (isLowSpaceWarningTarget) { | |
| 424 chrome.fileBrowserPrivate.onDirectoryChanged.addListener( | |
| 425 this.privateOnDirectoryChangedBound_); | |
| 426 } else { | |
| 427 chrome.fileBrowserPrivate.onDirectoryChanged.removeListener( | |
| 428 this.privateOnDirectoryChangedBound_); | |
| 429 } | |
| 430 } | |
| 431 | |
| 432 if (!this.isOnDrive()) { | |
| 433 this.cleanupWelcomeBanner_(); | |
| 434 this.authFailedBanner_.hidden = true; | |
| 435 } | |
| 436 | |
| 437 this.updateDriveUnmountedPanel_(); | |
| 438 if (this.isOnDrive()) { | |
| 439 this.unmountedPanel_.classList.remove('retry-enabled'); | |
| 440 this.maybeShowAuthFailBanner_(); | |
| 441 } | |
| 442 }; | |
| 443 | |
| 444 /** | |
| 445 * @param {string} root Root directory to be checked. | |
| 446 * @return {boolean} true if the file system specified by |root| is a target | |
| 447 * to show low space warning. Otherwise false. | |
| 448 * @private | |
| 449 */ | |
| 450 FileListBannerController.prototype.isLowSpaceWarningTarget_ = function(root) { | |
| 451 return (root == RootDirectory.DOWNLOADS || root == RootDirectory.DRIVE); | |
| 452 }; | |
| 453 | |
| 454 /** | |
| 455 * Callback which is invoked when the file system has been changed. | |
| 456 * @param {Object} event chrome.fileBrowserPrivate.onDirectoryChanged event. | |
| 457 * @private | |
| 458 */ | |
| 459 FileListBannerController.prototype.privateOnDirectoryChanged_ = function( | |
| 460 event) { | |
| 461 if (!this.directoryModel_.getCurrentDirEntry()) | |
| 462 return; | |
| 463 | |
| 464 var currentRoot = PathUtil.getTopDirectory( | |
| 465 this.directoryModel_.getCurrentDirPath()); | |
| 466 var eventRoot = PathUtil.getTopDirectory( | |
| 467 util.extractFilePath(event.directoryUrl)); | |
| 468 if (currentRoot == eventRoot) { | |
| 469 // The file system we are currently on is changed. | |
| 470 // So, check the free space. | |
| 471 this.maybeShowLowSpaceWarning_(eventRoot); | |
| 472 } | |
| 473 }; | |
| 474 | |
| 475 /** | |
| 476 * Shows or hides the low space warning. | |
| 477 * @param {string} root Root directory of the file system, which we are | |
| 478 * interested in. | |
| 479 * @private | |
| 480 */ | |
| 481 FileListBannerController.prototype.maybeShowLowSpaceWarning_ = function(root) { | |
| 482 // TODO(kaznacheev): Unify the two low space warning. | |
| 483 var threshold = 0; | |
| 484 if (root === RootDirectory.DOWNLOADS) { | |
| 485 this.showLowDriveSpaceWarning_(false); | |
| 486 threshold = 0.2; | |
| 487 } else if (root === RootDirectory.DRIVE) { | |
| 488 this.showLowDownloadsSpaceWarning_(false); | |
| 489 threshold = 0.1; | |
| 490 } else { | |
| 491 // If the current file system is neither the DOWNLOAD nor the DRIVE, | |
| 492 // just hide the warning. | |
| 493 this.showLowDownloadsSpaceWarning_(false); | |
| 494 this.showLowDriveSpaceWarning_(false); | |
| 495 return; | |
| 496 } | |
| 497 | |
| 498 var self = this; | |
| 499 chrome.fileBrowserPrivate.getSizeStats( | |
| 500 util.makeFilesystemUrl(root), | |
| 501 function(sizeStats) { | |
| 502 var currentRoot = PathUtil.getTopDirectory( | |
| 503 self.directoryModel_.getCurrentDirPath()); | |
| 504 if (root != currentRoot) { | |
| 505 // This happens when the current directory is moved during requesting | |
| 506 // the file system size. Just ignore it. | |
| 507 return; | |
| 508 } | |
| 509 // sizeStats is undefined, if some error occurs. | |
| 510 if (!sizeStats || sizeStats.totalSize == 0) | |
| 511 return; | |
| 512 | |
| 513 var remainingRatio = sizeStats.remainingSize / sizeStats.totalSize; | |
| 514 var isLowDiskSpace = remainingRatio < threshold; | |
| 515 if (root == RootDirectory.DOWNLOADS) | |
| 516 self.showLowDownloadsSpaceWarning_(isLowDiskSpace); | |
| 517 else | |
| 518 self.showLowDriveSpaceWarning_(isLowDiskSpace, sizeStats); | |
| 519 }); | |
| 520 }; | |
| 521 | |
| 522 /** | |
| 523 * removes the Drive Welcome banner. | |
| 524 * @private | |
| 525 */ | |
| 526 FileListBannerController.prototype.cleanupWelcomeBanner_ = function() { | |
| 527 this.showWelcomeBanner_('none'); | |
| 528 }; | |
| 529 | |
| 530 /** | |
| 531 * Notifies the file manager what layout must be recalculated. | |
| 532 * @param {number} delay In milliseconds. | |
| 533 * @private | |
| 534 */ | |
| 535 FileListBannerController.prototype.requestRelayout_ = function(delay) { | |
| 536 var self = this; | |
| 537 setTimeout(function() { | |
| 538 cr.dispatchSimpleEvent(self, 'relayout'); | |
| 539 }, delay); | |
| 540 }; | |
| 541 | |
| 542 /** | |
| 543 * Show or hide the "Low disk space" warning. | |
| 544 * @param {boolean} show True if the box need to be shown. | |
| 545 * @private | |
| 546 */ | |
| 547 FileListBannerController.prototype.showLowDownloadsSpaceWarning_ = | |
| 548 function(show) { | |
| 549 var box = this.document_.querySelector('.downloads-warning'); | |
| 550 | |
| 551 if (box.hidden == !show) return; | |
| 552 | |
| 553 if (show) { | |
| 554 var html = util.htmlUnescape(str('DOWNLOADS_DIRECTORY_WARNING')); | |
| 555 box.innerHTML = html; | |
| 556 var link = box.querySelector('a'); | |
| 557 link.href = urlConstants.DOWNLOADS_FAQ_URL; | |
| 558 link.target = '_blank'; | |
| 559 } else { | |
| 560 box.innerHTML = ''; | |
| 561 } | |
| 562 | |
| 563 box.hidden = !show; | |
| 564 this.requestRelayout_(100); | |
| 565 }; | |
| 566 | |
| 567 /** | |
| 568 * Creates contents for the DRIVE unmounted panel. | |
| 569 * @private | |
| 570 */ | |
| 571 FileListBannerController.prototype.ensureDriveUnmountedPanelInitialized_ = | |
| 572 function() { | |
| 573 var panel = this.unmountedPanel_; | |
| 574 if (panel.firstElementChild) | |
| 575 return; | |
| 576 | |
| 577 var create = function(parent, tag, className, opt_textContent) { | |
| 578 var div = panel.ownerDocument.createElement(tag); | |
| 579 div.className = className; | |
| 580 div.textContent = opt_textContent || ''; | |
| 581 parent.appendChild(div); | |
| 582 return div; | |
| 583 }; | |
| 584 | |
| 585 var loading = create(panel, 'div', 'loading', str('DRIVE_LOADING')); | |
| 586 var spinnerBox = create(loading, 'div', 'spinner-box'); | |
| 587 create(spinnerBox, 'div', 'spinner'); | |
| 588 create(panel, 'div', 'error', str('DRIVE_CANNOT_REACH')); | |
| 589 | |
| 590 var learnMore = create(panel, 'a', 'learn-more plain-link', | |
| 591 str('DRIVE_LEARN_MORE')); | |
| 592 learnMore.href = urlConstants.GOOGLE_DRIVE_ERROR_HELP_URL; | |
| 593 learnMore.target = '_blank'; | |
| 594 }; | |
| 595 | |
| 596 /** | |
| 597 * Called when volume info list is updated. | |
| 598 * @param {Event} event Splice event data on volume info list. | |
| 599 * @private | |
| 600 */ | |
| 601 FileListBannerController.prototype.onVolumeInfoListSplice_ = function(event) { | |
| 602 var isDriveVolume = function(volumeInfo) { | |
| 603 return volumeInfo.volumeType === util.VolumeType.DRIVE; | |
| 604 }; | |
| 605 if (event.removed.some(isDriveVolume) || event.added.some(isDriveVolume)) | |
| 606 this.updateDriveUnmountedPanel_(); | |
| 607 }; | |
| 608 | |
| 609 /** | |
| 610 * Shows the panel when current directory is DRIVE and it's unmounted. | |
| 611 * Hides it otherwise. The panel shows spinner if DRIVE is mounting or | |
| 612 * an error message if it failed. | |
| 613 * @private | |
| 614 */ | |
| 615 FileListBannerController.prototype.updateDriveUnmountedPanel_ = function() { | |
| 616 var node = this.document_.body; | |
| 617 if (this.isOnDrive()) { | |
| 618 var driveVolume = this.volumeManager_.getVolumeInfo(RootDirectory.DRIVE); | |
| 619 if (driveVolume && driveVolume.error) { | |
| 620 this.ensureDriveUnmountedPanelInitialized_(); | |
| 621 this.unmountedPanel_.classList.add('retry-enabled'); | |
| 622 } else { | |
| 623 this.unmountedPanel_.classList.remove('retry-enabled'); | |
| 624 } | |
| 625 node.setAttribute('drive', status); | |
| 626 } else { | |
| 627 node.removeAttribute('drive'); | |
| 628 } | |
| 629 }; | |
| 630 | |
| 631 /** | |
| 632 * Updates the visibility of Drive Connection Warning banner, retrieving the | |
| 633 * current connection information. | |
| 634 * @private | |
| 635 */ | |
| 636 FileListBannerController.prototype.maybeShowAuthFailBanner_ = function() { | |
| 637 var connection = this.volumeManager_.getDriveConnectionState(); | |
| 638 var reasons = connection.reasons; | |
| 639 var showDriveNotReachedMessage = | |
| 640 this.isOnDrive() && | |
| 641 connection.type == util.DriveConnectionType.OFFLINE && | |
| 642 // Show the banner only when authentication fails. Don't show it when the | |
| 643 // drive service is disabled. | |
| 644 reasons.indexOf(util.DriveConnectionReason.NOT_READY) != -1 && | |
| 645 reasons.indexOf(util.DriveConnectionReason.NO_SERVICE) == -1; | |
| 646 this.authFailedBanner_.hidden = !showDriveNotReachedMessage; | |
| 647 }; | |
| OLD | NEW |