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

Side by Side Diff: chrome/browser/resources/file_manager/js/background.js

Issue 18024002: Add the new window item to the Files.app's context menu on the launcher. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments + fixed. Created 7 years, 5 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
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 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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. Recreates if already exists.
511 541 * @param {Object} strings Hash array of strings.
512 function addExecuteHandler() { 542 */
513 chrome.fileBrowserHandler.onExecute.addListener(executeFileBrowserTask); 543 function initContextMenu(strings) {
544 try {
545 chrome.contextMenus.remove('new-window');
546 } catch (ignore) {
547 // There is no way to detect if the context menu is already added, therefore
548 // try to recreate it every time.
549 }
550 chrome.contextMenus.create({
551 id: 'new-window',
552 contexts: ['launcher'],
553 title: strings['NEW_WINDOW_BUTTON_LABEL']
554 });
514 } 555 }
515 556
516 addExecuteHandler(); 557 /**
558 * Initializes the background page of Files.app.
559 */
560 function initApp() {
561 // Initialize handlers.
562 chrome.fileBrowserHandler.onExecute.addListener(onExecute);
563 chrome.app.runtime.onLaunched.addListener(onLaunched);
564 chrome.app.runtime.onRestarted.addListener(onRestarted);
565 chrome.contextMenus.onClicked.addListener(onContextMenuClicked);
566
567 // Fetch strings and initialize the context menu.
568 queue.run(function(callback) {
569 chrome.fileBrowserPrivate.getStrings(function(strings) {
570 initContextMenu(strings);
571 chrome.storage.local.set({strings: strings}, callback);
572 });
573 });
574 }
575
576 // Initialize Files.app.
577 initApp();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698