Home | History | Annotate | Download | only in js
      1 // Copyright (c) 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 /**
      6  * Wallpaper file system quota.
      7  */
      8 /** @const */ var WallpaperQuota = 1024 * 1024 * 100;
      9 
     10 /**
     11  * Wallpaper directories enum.
     12  */
     13 /** @const */ var WallpaperDirNameEnum = {
     14                   ORIGINAL: 'original',
     15                   THUMBNAIL: 'thumbnail'
     16               };
     17 
     18 var wallpaperDirectories = null;
     19 
     20 /**
     21  * Manages custom wallpaper related directories in wallpaper's sandboxed
     22  * FileSystem.
     23  * @constructor
     24  */
     25 function WallpaperDirectories() {
     26   this.wallpaperDirs_ = {};
     27   this.wallpaperDirs_[WallpaperDirNameEnum.ORIGINAL] = null;
     28   this.wallpaperDirs_[WallpaperDirNameEnum.THUMBNAIL] = null;
     29 }
     30 
     31 /**
     32  * Gets WallpaperDirectories instance. In case is hasn't been initialized, a new
     33  * instance is created.
     34  * @return {WallpaperDirectories} A WallpaperDirectories instance.
     35  */
     36 WallpaperDirectories.getInstance = function() {
     37   if (wallpaperDirectories === null)
     38     wallpaperDirectories = new WallpaperDirectories();
     39   return wallpaperDirectories;
     40 };
     41 
     42 WallpaperDirectories.prototype = {
     43   /**
     44    * Returns all custom wallpaper related directory entries.
     45    */
     46   get wallpaperDirs() {
     47     return this.wallpaperDirs_;
     48   },
     49 
     50   /**
     51    * If dirName is not requested, gets the directory entry of dirName and cache
     52    * the result. Calls success callback if success.
     53    * @param {string} dirName The directory name of requested directory entry.
     54    * @param {function(DirectoryEntry):void} success Call success with requested
     55    *     DirectoryEntry.
     56    * @param {function(e):void} failure Call failure when failed to get the
     57    *     requested directory.
     58    */
     59   requestDir: function(dirName, success, failure) {
     60     if (dirName != WallpaperDirNameEnum.ORIGINAL &&
     61         dirName != WallpaperDirNameEnum.THUMBNAIL) {
     62       console.error('Error: Unknow directory name.');
     63       var e = new Error();
     64       e.code = FileError.NOT_FOUND_ERR;
     65       failure(e);
     66       return;
     67     }
     68     var self = this;
     69     window.webkitRequestFileSystem(window.PERSISTENT, WallpaperQuota,
     70                                    function(fs) {
     71       fs.root.getDirectory(dirName, {create: true}, function(dirEntry) {
     72         self.wallpaperDirs_[dirName] = dirEntry;
     73         success(dirEntry);
     74       }, failure);
     75     }, failure);
     76   },
     77 
     78   /**
     79    * Gets DirectoryEntry associated with dirName from cache. If not in cache try
     80    * to request it from FileSystem.
     81    * @param {string} dirName The directory name of requested directory entry.
     82    * @param {function(DirectoryEntry):void} success Call success with requested
     83    *     DirectoryEntry.
     84    * @param {function(e):void} failure Call failure when failed to get the
     85    *     requested directory.
     86    */
     87   getDirectory: function(dirName, success, failure) {
     88     if (this.wallpaperDirs[dirName])
     89       success(this.wallpaperDirs[dirName]);
     90     else
     91       this.requestDir(dirName, success, failure);
     92   }
     93 };
     94