| 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 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 } catch (e) { | 380 } catch (e) { |
| 381 console.error('Corrupt launch data for ' + id); | 381 console.error('Corrupt launch data for ' + id); |
| 382 } | 382 } |
| 383 } | 383 } |
| 384 } | 384 } |
| 385 } | 385 } |
| 386 }); | 386 }); |
| 387 } | 387 } |
| 388 | 388 |
| 389 /** | 389 /** |
| 390 * Executes a file browser task. |
| 391 * |
| 390 * @param {string} action Task id. | 392 * @param {string} action Task id. |
| 391 * @param {Object} details Details object. | 393 * @param {Object} details Details object. |
| 392 */ | 394 */ |
| 393 function executeFileBrowserTask(action, details) { | 395 function onExecute(action, details) { |
| 394 var urls = details.entries.map(function(e) { return e.toURL() }); | 396 var urls = details.entries.map(function(e) { return e.toURL() }); |
| 395 | 397 |
| 396 switch (action) { | 398 switch (action) { |
| 397 case 'play': | 399 case 'play': |
| 398 launchAudioPlayer({items: urls, position: 0}); | 400 launchAudioPlayer({items: urls, position: 0}); |
| 399 break; | 401 break; |
| 400 | 402 |
| 401 case 'watch': | 403 case 'watch': |
| 402 launchVideoPlayer(urls[0]); | 404 launchVideoPlayer(urls[0]); |
| 403 break; | 405 break; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 | 464 |
| 463 /** | 465 /** |
| 464 * Launch the video player. | 466 * Launch the video player. |
| 465 * @param {string} url Video url. | 467 * @param {string} url Video url. |
| 466 */ | 468 */ |
| 467 function launchVideoPlayer(url) { | 469 function launchVideoPlayer(url) { |
| 468 videoPlayer.enqueueLaunch({url: url}); | 470 videoPlayer.enqueueLaunch({url: url}); |
| 469 } | 471 } |
| 470 | 472 |
| 471 /** | 473 /** |
| 472 * Launch the app. | 474 * Launches the app. |
| 473 */ | 475 */ |
| 474 function launch() { | 476 function onLaunched() { |
| 475 if (nextFileManagerWindowID == 0) { | 477 if (nextFileManagerWindowID == 0) { |
| 476 // The app just launched. Remove window state records that are not needed | 478 // The app just launched. Remove window state records that are not needed |
| 477 // any more. | 479 // any more. |
| 478 chrome.storage.local.get(function(items) { | 480 chrome.storage.local.get(function(items) { |
| 479 for (var key in items) { | 481 for (var key in items) { |
| 480 if (items.hasOwnProperty(key)) { | 482 if (items.hasOwnProperty(key)) { |
| 481 if (key.match(FILES_ID_PATTERN)) | 483 if (key.match(FILES_ID_PATTERN)) |
| 482 chrome.storage.local.remove(key); | 484 chrome.storage.local.remove(key); |
| 483 } | 485 } |
| 484 } | 486 } |
| 485 }); | 487 }); |
| 486 } | 488 } |
| 487 | 489 |
| 488 launchFileManager(); | 490 launchFileManager(); |
| 489 } | 491 } |
| 490 | 492 |
| 491 /** | 493 /** |
| 492 * Restarted the app, restore windows. | 494 * Restarted the app, restore windows. |
| 493 */ | 495 */ |
| 494 function restart() { | 496 function onRestarted() { |
| 495 reopenFileManagers(); | 497 reopenFileManagers(); |
| 496 audioPlayer.reopen(); | 498 audioPlayer.reopen(); |
| 497 videoPlayer.reopen(); | 499 videoPlayer.reopen(); |
| 498 } | 500 } |
| 499 | 501 |
| 500 /** | 502 /** |
| 503 * Handles clicks on a custom item on the launcher context menu. |
| 504 * @param {OnClickData} info Event details. |
| 505 */ |
| 506 function onContextMenuClicked(info) { |
| 507 if (info.menuItemId == 'new-window') { |
| 508 // Find the focused window (if any) and use it's current path for the |
| 509 // new window. If not found, then launch with the default path. |
| 510 for (var key in appWindows) { |
| 511 try { |
| 512 if (appWindows[key].contentWindow.isFocused()) { |
| 513 var appState = { |
| 514 defaultPath: appWindows[key].contentWindow.appState.defaultPath |
| 515 }; |
| 516 launchFileManager(appState); |
| 517 return; |
| 518 } |
| 519 } catch (ignore) { |
| 520 // The isFocused method may not be defined during initialization. |
| 521 // Therefore, wrapped with a try-catch block. |
| 522 } |
| 523 } |
| 524 |
| 525 // Launch with the default path. |
| 526 launchFileManager(); |
| 527 } |
| 528 } |
| 529 |
| 530 /** |
| 501 * Closes the background page, if it is not needed. | 531 * Closes the background page, if it is not needed. |
| 502 */ | 532 */ |
| 503 function maybeCloseBackgroundPage() { | 533 function maybeCloseBackgroundPage() { |
| 504 if (Object.keys(appWindows).length === 0 && | 534 if (Object.keys(appWindows).length === 0 && |
| 505 !FileCopyManager.getInstance().hasQueuedTasks()) | 535 !FileCopyManager.getInstance().hasQueuedTasks()) |
| 506 close(); | 536 close(); |
| 507 } | 537 } |
| 508 | 538 |
| 509 chrome.app.runtime.onLaunched.addListener(launch); | 539 /** |
| 510 chrome.app.runtime.onRestarted.addListener(restart); | 540 * Initializes the context menu. |
| 511 | 541 * @param {Object} strings Hash array of strings. |
| 512 function addExecuteHandler() { | 542 */ |
| 513 chrome.fileBrowserHandler.onExecute.addListener(executeFileBrowserTask); | 543 function initContextMenu(strings) { |
| 544 chrome.contextMenus.create({ |
| 545 id: 'new-window', |
| 546 contexts: ['launcher'], |
| 547 title: strings['NEW_WINDOW_BUTTON_LABEL'] |
| 548 }); |
| 514 } | 549 } |
| 515 | 550 |
| 516 addExecuteHandler(); | 551 /** |
| 552 * Initializes the background page of Files.app. |
| 553 */ |
| 554 function initApp() { |
| 555 // Fetch strings and save in the local storage, before initializing handlers. |
| 556 chrome.fileBrowserPrivate.getStrings(function(strings) { |
| 557 initContextMenu(strings); |
| 558 // Save and initialize handlers. |
| 559 chrome.storage.local.set({strings: strings}, function() { |
| 560 chrome.fileBrowserHandler.onExecute.addListener(onExecute); |
| 561 chrome.app.runtime.onLaunched.addListener(onLaunched); |
| 562 chrome.app.runtime.onRestarted.addListener(onRestarted); |
| 563 chrome.contextMenus.onClicked.addListener(onContextMenuClicked); |
| 564 }); |
| 565 }); |
| 566 } |
| 567 |
| 568 // Initialize Files.app. |
| 569 initApp(); |
| OLD | NEW |