};
/**
- * Locate the file referred to by path, creating directories or the file
- * itself if necessary.
- * @param {DirEntry} root The root entry.
- * @param {string} path The file path.
- * @param {function(FileEntry)} successCallback The callback.
- * @param {function(FileError)} errorCallback The callback.
- */
-util.getOrCreateFile = function(root, path, successCallback, errorCallback) {
- var dirname = null;
- var basename = null;
-
- var onDirFound = function(dirEntry) {
- dirEntry.getFile(basename, { create: true },
- successCallback, errorCallback);
- };
-
- var i = path.lastIndexOf('/');
- if (i > -1) {
- dirname = path.substr(0, i);
- basename = path.substr(i + 1);
- } else {
- basename = path;
- }
-
- if (!dirname) {
- onDirFound(root);
- return;
- }
-
- util.getOrCreateDirectory(root, dirname, onDirFound, errorCallback);
-};
-
-/**
- * Locate the directory referred to by path, creating directories along the
- * way.
- * @param {DirEntry} root The root entry.
- * @param {string} path The directory path.
- * @param {function(FileEntry)} successCallback The callback.
- * @param {function(FileError)} errorCallback The callback.
- */
-util.getOrCreateDirectory = function(root, path, successCallback,
- errorCallback) {
- var names = path.split('/');
-
- var getOrCreateNextName = function(dir) {
- if (!names.length)
- return successCallback(dir);
-
- var name;
- do {
- name = names.shift();
- } while (!name || name == '.');
-
- dir.getDirectory(name, { create: true }, getOrCreateNextName,
- errorCallback);
- };
-
- getOrCreateNextName(root);
-};
-
-/**
* Renames the entry to newName.
* @param {Entry} entry The entry to be renamed.
* @param {string} newName The new name.
};
/**
- * Update the app state.
+ * Updates the app state.
*
- * @param {string} path Path to be put in the address bar after the hash.
- * If null the hash is left unchanged.
- * @param {string|Object=} opt_param Search parameter. Used directly if string,
- * stringified if object. If omitted the search query is left unchanged.
- */
-util.updateAppState = function(path, opt_param) {
+ * @param {string} currentDirectoryURL Currently opened directory as an URL.
+ * If null the value is left unchanged.
+ * @param {string} selectionURL Currently selected entry as an URL. If null the
+ * value is left unchanged.
+ * @param {string|Object=} opt_param Additional parameters, to be stored. If
+ * null, then left unchanged.
+ */
+util.updateAppState = function(currentDirectoryURL, selectionURL, opt_param) {
window.appState = window.appState || {};
- if (typeof opt_param == 'string')
- window.appState.params = {};
- else if (typeof opt_param == 'object')
+ if (opt_param !== undefined && opt_param !== null)
window.appState.params = opt_param;
- if (path)
- window.appState.defaultPath = path;
+ if (currentDirectoryURL !== null)
+ window.appState.currentDirectoryURL = currentDirectoryURL;
+ if (selectionURL !== null)
+ window.appState.selectionURL = selectionURL;
util.saveAppState();
- return;
};
/**
- * Return a translated string.
+ * Returns a translated string.
*
* Wrapper function to make dealing with translated strings more concise.
* Equivalent to loadTimeData.getString(id).
}
/**
- * Return a translated string with arguments replaced.
+ * Returns a translated string with arguments replaced.
*
* Wrapper function to make dealing with translated strings more concise.
* Equivalent to loadTimeData.getStringF(id, ...).
};
/**
- * Views files in the browser.
- *
- * @param {Array.<string>} urls URLs of files to view.
- * @param {function(bool)} callback Callback notifying success or not.
- */
-util.viewFilesInBrowser = function(urls, callback) {
- var taskId = chrome.runtime.id + '|file|view-in-browser';
- chrome.fileBrowserPrivate.executeTask(taskId, urls, callback);
-};
-
-/**
* Checks if the child entry is a descendant of another entry. If the entries
* point to the same file or directory, then returns false.
*
* Converts array of URLs to an array of corresponding Entries.
*
* @param {Array.<string>} urls Input array of URLs.
- * @param {function(Array.<Entry>)} callback Completion callback with array of
- * Entries.
+ * @param {function(Array.<Entry>, Array.<URL>)} callback Completion callback
+ * with array of success Entries and failure URLs.
*/
util.URLsToEntries = function(urls, callback) {
var result = [];
+ var failureUrl = [];
AsyncUtil.forEach(
urls,
function(forEachCallback, url) {
}, function() {
// Not an error. Possibly, the file is not accessible anymore.
console.warn('Failed to resolve the file with url: ' + url + '.');
+ failureUrl.push(url);
forEachCallback();
});
},
- function() {
- callback(result);
- });
+ callback.bind(null, result, failureUrl));
+};
+
+/**
+ * Returns whether the window is teleported or not.
+ * @param {DOMWindow} window Window.
+ * @return {Promise.<boolean>} Whether the window is teleported or not.
+ */
+util.isTeleported = function(window) {
+ return new Promise(function(onFulfilled) {
+ window.chrome.fileBrowserPrivate.getProfiles(function(profiles,
+ currentId,
+ displayedId) {
+ onFullfilled(currentId !== displayedId);
+ });
+ });
+};
+
+/**
+ * Sets up and shows the alert to inform a user the task is opened in the
+ * desktop of the running profile.
+ *
+ * TODO(hirono): Move the function from the util namespace.
+ * @param {cr.ui.AlertDialog} alertDialog Alert dialog to be shown.
+ * @param {Array.<Entry>} entries List of opened entries.
+ */
+util.showOpenInOtherDesktopAlert = function(alertDialog, entries) {
+ if (!entries.length)
+ return;
+ chrome.fileBrowserPrivate.getProfiles(function(profiles,
+ currentId,
+ displayedId) {
+ // Find strings.
+ var displayName;
+ for (var i = 0; i < profiles.length; i++) {
+ if (profiles[i].profileId === currentId) {
+ displayName = profiles[i].displayName;
+ break;
+ }
+ }
+ if (!displayName) {
+ console.warn('Display name is not found.');
+ return;
+ }
+
+ var title = entries.size > 1 ?
+ entries[0].name + '\u2026' /* ellipsis */ : entries[0].name;
+ var message = strf(entries.size > 1 ?
+ 'OPEN_IN_OTHER_DESKTOP_MESSAGE_PLURAL' :
+ 'OPEN_IN_OTHER_DESKTOP_MESSAGE',
+ displayName,
+ currentId);
+
+ // Show the dialog.
+ alertDialog.showWithTitle(title, message);
+ }.bind(this));
};
/**