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

Side by Side 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: Cleaned up. 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 /** 7 /**
8 * Map of all currently open app window. The key is an app id. 8 * Map of all currently open app window. The key is an app id.
9 */ 9 */
10 var appWindows = {}; 10 var appWindows = {};
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 minHeight: 240, 275 minHeight: 240,
276 frame: util.platform.newUI() ? 'none' : 'chrome', 276 frame: util.platform.newUI() ? 'none' : 'chrome',
277 transparentBackground: true 277 transparentBackground: true
278 }; 278 };
279 } 279 }
280 280
281 /** 281 /**
282 * @param {Object=} opt_appState App state. 282 * @param {Object=} opt_appState App state.
283 * @param {number=} opt_id Window id. 283 * @param {number=} opt_id Window id.
284 * @param {LaunchType=} opt_type Launch type. Default: ALWAYS_CREATE. 284 * @param {LaunchType=} opt_type Launch type. Default: ALWAYS_CREATE.
285 * @return {string} The window's App ID.
286 */ 285 */
287 function launchFileManager(opt_appState, opt_id, opt_type) { 286 function launchFileManager(opt_appState, opt_id, opt_type) {
288 var type = opt_type || LaunchType.ALWAYS_CREATE; 287 var type = opt_type || LaunchType.ALWAYS_CREATE;
289 288
290 // Check if there is already a window with the same path. If so, then 289 // Wait until all windows are created.
291 // reuse it instead of opening a new one. 290 queue.run(function(callback) {
292 if (type == LaunchType.FOCUS_SAME_OR_CREATE || 291 // Check if there is already a window with the same path. If so, then
293 type == LaunchType.FOCUS_ANY_OR_CREATE) { 292 // reuse it instead of opening a new one.
294 if (opt_appState && opt_appState.defaultPath) { 293 if (type == LaunchType.FOCUS_SAME_OR_CREATE ||
295 for (var key in appWindows) { 294 type == LaunchType.FOCUS_ANY_OR_CREATE) {
296 var contentWindow = appWindows[key].contentWindow; 295 if (opt_appState && opt_appState.defaultPath) {
297 if (contentWindow.appState && 296 for (var key in appWindows) {
298 opt_appState.defaultPath == contentWindow.appState.defaultPath) { 297 var contentWindow = appWindows[key].contentWindow;
299 appWindows[key].focus(); 298 if (contentWindow.appState &&
300 return key; 299 opt_appState.defaultPath == contentWindow.appState.defaultPath) {
300 appWindows[key].focus();
301 callback();
302 return;
303 }
301 } 304 }
302 } 305 }
303 } 306 }
304 }
305 307
306 // Focus any window if none is focused. Try restored first. 308 // Focus any window if none is focused. Try restored first.
307 if (type == LaunchType.FOCUS_ANY_OR_CREATE) { 309 if (type == LaunchType.FOCUS_ANY_OR_CREATE) {
308 // If there is already a focused window, then finish. 310 // If there is already a focused window, then finish.
309 for (var key in appWindows) { 311 for (var key in appWindows) {
310 // The isFocused() method should always be available, but in case 312 // The isFocused() method should always be available, but in case
311 // Files.app's failed on some error, wrap it with try catch. 313 // Files.app's failed on some error, wrap it with try catch.
312 try { 314 try {
313 if (appWindows[key].contentWindow.isFocused()) 315 if (appWindows[key].contentWindow.isFocused())
314 return key; 316 return key;
315 } catch (e) { 317 } catch (e) {
316 console.error(e.message); 318 console.error(e.message);
319 }
320 }
321 // Try to focus the first non-minimized window.
322 for (var key in appWindows) {
323 if (!appWindows[key].isMinimized()) {
324 appWindows[key].focus();
325 callback();
326 return;
327 }
328 }
329 // Restore and focus any window.
330 for (var key in appWindows) {
331 appWindows[key].focus();
332 callback();
333 return;
317 } 334 }
318 } 335 }
319 // Try to focus the first non-minimized window.
320 for (var key in appWindows) {
321 if (!appWindows[key].isMinimized()) {
322 appWindows[key].focus();
323 return key;
324 }
325 }
326 // Restore and focus any window.
327 for (var key in appWindows) {
328 appWindows[key].focus();
329 return key;
330 }
331 }
332 336
333 // Create a new instance in case of ALWAYS_CREATE type, or as a fallback 337 // Create a new instance in case of ALWAYS_CREATE type, or as a fallback
334 // for other types. 338 // for other types.
335 339
336 var id = opt_id || nextFileManagerWindowID; 340 var id = opt_id || nextFileManagerWindowID;
337 nextFileManagerWindowID = Math.max(nextFileManagerWindowID, id + 1); 341 nextFileManagerWindowID = Math.max(nextFileManagerWindowID, id + 1);
338 var appId = FILES_ID_PREFIX + id; 342 var appId = FILES_ID_PREFIX + id;
339 343
340 var appWindow = new AppWindowWrapper( 344 var appWindow = new AppWindowWrapper(
341 queue, 345 queue,
342 util.platform.newUI() ? 'main_new_ui.html' : 'main.html', 346 util.platform.newUI() ? 'main_new_ui.html' : 'main.html',
343 appId, 347 appId,
344 createFileManagerOptions); 348 createFileManagerOptions);
345 appWindow.enqueueLaunch(opt_appState || {}); 349 appWindow.enqueueLaunch(opt_appState || {});
346 350 callback();
347 return appId; 351 });
yoshiki 2013/05/23 06:49:21 In test_util.js, the retual value is used. Could y
mtomasz 2013/05/23 07:17:30 Done.
348 } 352 }
349 353
350 /** 354 /**
351 * Relaunch file manager windows based on the persisted state. 355 * Relaunch file manager windows based on the persisted state.
352 */ 356 */
353 function reopenFileManagers() { 357 function reopenFileManagers() {
354 chrome.storage.local.get(function(items) { 358 chrome.storage.local.get(function(items) {
355 for (var key in items) { 359 for (var key in items) {
356 if (items.hasOwnProperty(key)) { 360 if (items.hasOwnProperty(key)) {
357 var match = key.match(FILES_ID_PATTERN); 361 var match = key.match(FILES_ID_PATTERN);
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 } 494 }
491 495
492 chrome.app.runtime.onLaunched.addListener(launch); 496 chrome.app.runtime.onLaunched.addListener(launch);
493 chrome.app.runtime.onRestarted.addListener(restart); 497 chrome.app.runtime.onRestarted.addListener(restart);
494 498
495 function addExecuteHandler() { 499 function addExecuteHandler() {
496 chrome.fileBrowserHandler.onExecute.addListener(executeFileBrowserTask); 500 chrome.fileBrowserHandler.onExecute.addListener(executeFileBrowserTask);
497 } 501 }
498 502
499 addExecuteHandler(); 503 addExecuteHandler();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698