• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2011 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 * Namespace for utility functions.
7 */
8var util = {
9  /**
10   * Returns a function that console.log's its arguments, prefixed by |msg|.
11   *
12   * @param {string} msg The message prefix to use in the log.
13   * @param {function(*)} opt_callback A function to invoke after logging.
14   */
15  flog: function(msg, opt_callback) {
16    return function() {
17      var ary = Array.apply(null, arguments);
18      console.log(msg + ': ' + ary.join(', '));
19      if (opt_callback)
20        opt_callback.call(arguments);
21    };
22  },
23
24  /**
25   * Returns a function that throws an exception that includes its arguments
26   * prefixed by |msg|.
27   *
28   * @param {string} msg The message prefix to use in the exception.
29   */
30  ferr: function(msg) {
31    return function() {
32      var ary = Array.apply(null, arguments);
33      throw new Error(msg + ': ' + ary.join(', '));
34    };
35  },
36
37  /**
38   * Install a sensible toString() on the FileError object.
39   *
40   * FileError.prototype.code is a numeric code describing the cause of the
41   * error.  The FileError constructor has a named property for each possible
42   * error code, but provides no way to map the code to the named property.
43   * This toString() implementation fixes that.
44   */
45  installFileErrorToString: function() {
46    FileError.prototype.toString = function() {
47      return '[object FileError: ' + util.getFileErrorMnemonic(this.code) + ']';
48    }
49  },
50
51  getFileErrorMnemonic: function(code) {
52    for (var key in FileError) {
53      if (key.search(/_ERR$/) != -1 && FileError[key] == code)
54        return key;
55    }
56
57    return code;
58  },
59
60  /**
61   * Utility function to invoke callback once for each entry in dirEntry.
62   *
63   * @param {DirectoryEntry} dirEntry The directory entry to enumerate.
64   * @param {function(Entry)} callback The function to invoke for each entry in
65   *   dirEntry.
66   */
67  forEachDirEntry: function(dirEntry, callback) {
68    var reader;
69
70    function onReadSome(results) {
71      if (results.length == 0)
72        return callback(null);
73
74      for (var i = 0; i < results.length; i++)
75        callback(results[i]);
76
77      reader.readEntries(onReadSome);
78    };
79
80    reader = dirEntry.createReader();
81    reader.readEntries(onReadSome);
82  },
83
84  /**
85   * Utility function to resolve multiple directories with a single call.
86   *
87   * The successCallback will be invoked once for each directory object
88   * found.  The errorCallback will be invoked once for each
89   * path that could not be resolved.
90   *
91   * The successCallback is invoked with a null entry when all paths have
92   * been processed.
93   *
94   * @param {DirEntry} dirEntry The base directory.
95   * @param {Object} params The parameters to pass to the underlying
96   *     getDirectory calls.
97   * @param {Array<string>} paths The list of directories to resolve.
98   * @param {function(!DirEntry)} successCallback The function to invoke for
99   *     each DirEntry found.  Also invoked once with null at the end of the
100   *     process.
101   * @param {function(string, FileError)} errorCallback The function to invoke
102   *     for each path that cannot be resolved.
103   */
104  getDirectories: function(dirEntry, params, paths, successCallback,
105                           errorCallback) {
106
107    // Copy the params array, since we're going to destroy it.
108    params = [].slice.call(params);
109
110    function onComplete() {
111      successCallback(null);
112    }
113
114    function getNextDirectory() {
115      var path = paths.shift();
116      if (!path)
117        return onComplete();
118
119      dirEntry.getDirectory(
120        path, params,
121        function(entry) {
122          successCallback(entry);
123          getNextDirectory();
124        },
125        function(err) {
126          errorCallback(path, err);
127          getNextDirectory();
128        });
129    }
130
131    getNextDirectory();
132  },
133
134};
135