OLD | NEW |
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 Loading... |
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. | 285 * @param {function(string)=} opt_callback Completion callback with the App ID. |
286 */ | 286 */ |
287 function launchFileManager(opt_appState, opt_id, opt_type) { | 287 function launchFileManager(opt_appState, opt_id, opt_type, opt_callback) { |
288 var type = opt_type || LaunchType.ALWAYS_CREATE; | 288 var type = opt_type || LaunchType.ALWAYS_CREATE; |
289 | 289 |
290 // Check if there is already a window with the same path. If so, then | 290 // Wait until all windows are created. |
291 // reuse it instead of opening a new one. | 291 queue.run(function(onTaskCompleted) { |
292 if (type == LaunchType.FOCUS_SAME_OR_CREATE || | 292 // Check if there is already a window with the same path. If so, then |
293 type == LaunchType.FOCUS_ANY_OR_CREATE) { | 293 // reuse it instead of opening a new one. |
294 if (opt_appState && opt_appState.defaultPath) { | 294 if (type == LaunchType.FOCUS_SAME_OR_CREATE || |
295 for (var key in appWindows) { | 295 type == LaunchType.FOCUS_ANY_OR_CREATE) { |
296 var contentWindow = appWindows[key].contentWindow; | 296 if (opt_appState && opt_appState.defaultPath) { |
297 if (contentWindow.appState && | 297 for (var key in appWindows) { |
298 opt_appState.defaultPath == contentWindow.appState.defaultPath) { | 298 var contentWindow = appWindows[key].contentWindow; |
299 appWindows[key].focus(); | 299 if (contentWindow.appState && |
300 return key; | 300 opt_appState.defaultPath == contentWindow.appState.defaultPath) { |
| 301 appWindows[key].focus(); |
| 302 if (opt_callback) |
| 303 opt_callback(key); |
| 304 onTaskCompleted(); |
| 305 return; |
| 306 } |
301 } | 307 } |
302 } | 308 } |
303 } | 309 } |
304 } | |
305 | 310 |
306 // Focus any window if none is focused. Try restored first. | 311 // Focus any window if none is focused. Try restored first. |
307 if (type == LaunchType.FOCUS_ANY_OR_CREATE) { | 312 if (type == LaunchType.FOCUS_ANY_OR_CREATE) { |
308 // If there is already a focused window, then finish. | 313 // If there is already a focused window, then finish. |
309 for (var key in appWindows) { | 314 for (var key in appWindows) { |
310 // The isFocused() method should always be available, but in case | 315 // The isFocused() method should always be available, but in case |
311 // Files.app's failed on some error, wrap it with try catch. | 316 // Files.app's failed on some error, wrap it with try catch. |
312 try { | 317 try { |
313 if (appWindows[key].contentWindow.isFocused()) | 318 if (appWindows[key].contentWindow.isFocused()) |
314 return key; | 319 return key; |
315 } catch (e) { | 320 } catch (e) { |
316 console.error(e.message); | 321 console.error(e.message); |
| 322 } |
| 323 } |
| 324 // Try to focus the first non-minimized window. |
| 325 for (var key in appWindows) { |
| 326 if (!appWindows[key].isMinimized()) { |
| 327 appWindows[key].focus(); |
| 328 if (opt_callback) |
| 329 opt_callback(key); |
| 330 onTaskCompleted(); |
| 331 return; |
| 332 } |
| 333 } |
| 334 // Restore and focus any window. |
| 335 for (var key in appWindows) { |
| 336 appWindows[key].focus(); |
| 337 if (opt_callback) |
| 338 opt_callback(key); |
| 339 onTaskCompleted(); |
| 340 return; |
317 } | 341 } |
318 } | 342 } |
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 | 343 |
333 // Create a new instance in case of ALWAYS_CREATE type, or as a fallback | 344 // Create a new instance in case of ALWAYS_CREATE type, or as a fallback |
334 // for other types. | 345 // for other types. |
335 | 346 |
336 var id = opt_id || nextFileManagerWindowID; | 347 var id = opt_id || nextFileManagerWindowID; |
337 nextFileManagerWindowID = Math.max(nextFileManagerWindowID, id + 1); | 348 nextFileManagerWindowID = Math.max(nextFileManagerWindowID, id + 1); |
338 var appId = FILES_ID_PREFIX + id; | 349 var appId = FILES_ID_PREFIX + id; |
339 | 350 |
340 var appWindow = new AppWindowWrapper( | 351 var appWindow = new AppWindowWrapper( |
341 queue, | 352 queue, |
342 util.platform.newUI() ? 'main_new_ui.html' : 'main.html', | 353 util.platform.newUI() ? 'main_new_ui.html' : 'main.html', |
343 appId, | 354 appId, |
344 createFileManagerOptions); | 355 createFileManagerOptions); |
345 appWindow.enqueueLaunch(opt_appState || {}); | 356 appWindow.enqueueLaunch(opt_appState || {}); |
346 | 357 if (opt_callback) |
347 return appId; | 358 opt_callback(appId); |
| 359 onTaskCompleted(); |
| 360 }); |
348 } | 361 } |
349 | 362 |
350 /** | 363 /** |
351 * Relaunch file manager windows based on the persisted state. | 364 * Relaunch file manager windows based on the persisted state. |
352 */ | 365 */ |
353 function reopenFileManagers() { | 366 function reopenFileManagers() { |
354 chrome.storage.local.get(function(items) { | 367 chrome.storage.local.get(function(items) { |
355 for (var key in items) { | 368 for (var key in items) { |
356 if (items.hasOwnProperty(key)) { | 369 if (items.hasOwnProperty(key)) { |
357 var match = key.match(FILES_ID_PATTERN); | 370 var match = key.match(FILES_ID_PATTERN); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 } | 503 } |
491 | 504 |
492 chrome.app.runtime.onLaunched.addListener(launch); | 505 chrome.app.runtime.onLaunched.addListener(launch); |
493 chrome.app.runtime.onRestarted.addListener(restart); | 506 chrome.app.runtime.onRestarted.addListener(restart); |
494 | 507 |
495 function addExecuteHandler() { | 508 function addExecuteHandler() { |
496 chrome.fileBrowserHandler.onExecute.addListener(executeFileBrowserTask); | 509 chrome.fileBrowserHandler.onExecute.addListener(executeFileBrowserTask); |
497 } | 510 } |
498 | 511 |
499 addExecuteHandler(); | 512 addExecuteHandler(); |
OLD | NEW |