Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1539)

Unified Diff: chrome/browser/resources/file_manager/js/background.js

Issue 15817002: Remove races in window creation in Files.app. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/file_manager/js/test_util.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/file_manager/js/background.js
diff --git a/chrome/browser/resources/file_manager/js/background.js b/chrome/browser/resources/file_manager/js/background.js
index 455385e6f419d9f47871b7100ebc4645b95c347c..10fac00b71f63496139f5aae4600833d612459f7 100644
--- a/chrome/browser/resources/file_manager/js/background.js
+++ b/chrome/browser/resources/file_manager/js/background.js
@@ -282,69 +282,82 @@ function createFileManagerOptions() {
* @param {Object=} opt_appState App state.
* @param {number=} opt_id Window id.
* @param {LaunchType=} opt_type Launch type. Default: ALWAYS_CREATE.
- * @return {string} The window's App ID.
+ * @param {function(string)=} opt_callback Completion callback with the App ID.
*/
-function launchFileManager(opt_appState, opt_id, opt_type) {
+function launchFileManager(opt_appState, opt_id, opt_type, opt_callback) {
var type = opt_type || LaunchType.ALWAYS_CREATE;
- // Check if there is already a window with the same path. If so, then
- // reuse it instead of opening a new one.
- if (type == LaunchType.FOCUS_SAME_OR_CREATE ||
- type == LaunchType.FOCUS_ANY_OR_CREATE) {
- if (opt_appState && opt_appState.defaultPath) {
- for (var key in appWindows) {
- var contentWindow = appWindows[key].contentWindow;
- if (contentWindow.appState &&
- opt_appState.defaultPath == contentWindow.appState.defaultPath) {
- appWindows[key].focus();
- return key;
+ // Wait until all windows are created.
+ queue.run(function(onTaskCompleted) {
+ // Check if there is already a window with the same path. If so, then
+ // reuse it instead of opening a new one.
+ if (type == LaunchType.FOCUS_SAME_OR_CREATE ||
+ type == LaunchType.FOCUS_ANY_OR_CREATE) {
+ if (opt_appState && opt_appState.defaultPath) {
+ for (var key in appWindows) {
+ var contentWindow = appWindows[key].contentWindow;
+ if (contentWindow.appState &&
+ opt_appState.defaultPath == contentWindow.appState.defaultPath) {
+ appWindows[key].focus();
+ if (opt_callback)
+ opt_callback(key);
+ onTaskCompleted();
+ return;
+ }
}
}
}
- }
- // Focus any window if none is focused. Try restored first.
- if (type == LaunchType.FOCUS_ANY_OR_CREATE) {
- // If there is already a focused window, then finish.
- for (var key in appWindows) {
- // The isFocused() method should always be available, but in case
- // Files.app's failed on some error, wrap it with try catch.
- try {
- if (appWindows[key].contentWindow.isFocused())
- return key;
- } catch (e) {
- console.error(e.message);
+ // Focus any window if none is focused. Try restored first.
+ if (type == LaunchType.FOCUS_ANY_OR_CREATE) {
+ // If there is already a focused window, then finish.
+ for (var key in appWindows) {
+ // The isFocused() method should always be available, but in case
+ // Files.app's failed on some error, wrap it with try catch.
+ try {
+ if (appWindows[key].contentWindow.isFocused())
+ return key;
+ } catch (e) {
+ console.error(e.message);
+ }
}
- }
- // Try to focus the first non-minimized window.
- for (var key in appWindows) {
- if (!appWindows[key].isMinimized()) {
+ // Try to focus the first non-minimized window.
+ for (var key in appWindows) {
+ if (!appWindows[key].isMinimized()) {
+ appWindows[key].focus();
+ if (opt_callback)
+ opt_callback(key);
+ onTaskCompleted();
+ return;
+ }
+ }
+ // Restore and focus any window.
+ for (var key in appWindows) {
appWindows[key].focus();
- return key;
+ if (opt_callback)
+ opt_callback(key);
+ onTaskCompleted();
+ return;
}
}
- // Restore and focus any window.
- for (var key in appWindows) {
- appWindows[key].focus();
- return key;
- }
- }
- // Create a new instance in case of ALWAYS_CREATE type, or as a fallback
- // for other types.
-
- var id = opt_id || nextFileManagerWindowID;
- nextFileManagerWindowID = Math.max(nextFileManagerWindowID, id + 1);
- var appId = FILES_ID_PREFIX + id;
-
- var appWindow = new AppWindowWrapper(
- queue,
- util.platform.newUI() ? 'main_new_ui.html' : 'main.html',
- appId,
- createFileManagerOptions);
- appWindow.enqueueLaunch(opt_appState || {});
-
- return appId;
+ // Create a new instance in case of ALWAYS_CREATE type, or as a fallback
+ // for other types.
+
+ var id = opt_id || nextFileManagerWindowID;
+ nextFileManagerWindowID = Math.max(nextFileManagerWindowID, id + 1);
+ var appId = FILES_ID_PREFIX + id;
+
+ var appWindow = new AppWindowWrapper(
+ queue,
+ util.platform.newUI() ? 'main_new_ui.html' : 'main.html',
+ appId,
+ createFileManagerOptions);
+ appWindow.enqueueLaunch(opt_appState || {});
+ if (opt_callback)
+ opt_callback(appId);
+ onTaskCompleted();
+ });
}
/**
« no previous file with comments | « no previous file | chrome/browser/resources/file_manager/js/test_util.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698