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
3 // found in the LICENSE file.
8 * Type of a Files.app's instance launch.
11 var LaunchType = Object.freeze({
13 FOCUS_ANY_OR_CREATE: 1,
14 FOCUS_SAME_OR_CREATE: 2
18 * Root class of the background page.
21 function Background() {
23 * Map of all currently open app windows. The key is an app id.
24 * @type {Object.<string, AppWindow>}
29 * Synchronous queue for asynchronous calls.
30 * @type {AsyncUtil.Queue}
32 this.queue = new AsyncUtil.Queue();
35 * Progress center of the background page.
36 * @type {ProgressCenter}
38 this.progressCenter = new ProgressCenter();
41 * File operation manager.
42 * @type {FileOperationManager}
44 this.fileOperationManager = new FileOperationManager();
47 * Event handler for progress center.
48 * @type {FileOperationHandler}
51 this.fileOperationHandler_ = new FileOperationHandler(this);
54 * Event handler for C++ sides notifications.
55 * @type {DeviceHandler}
58 this.deviceHandler_ = new DeviceHandler();
62 * @type {DriveSyncHandler}
65 this.driveSyncHandler_ = new DriveSyncHandler(this.progressCenter);
66 this.driveSyncHandler_.addEventListener(
67 DriveSyncHandler.COMPLETED_EVENT,
68 function() { this.tryClose(); }.bind(this));
72 * @type {Object.<string, string>}
74 this.stringData = null;
77 * Callback list to be invoked after initialization.
78 * It turns to null after initialization.
80 * @type {Array.<function()>}
83 this.initializeCallbacks_ = [];
86 * Last time when the background page can close.
91 this.lastTimeCanClose_ = null;
96 // Initialize handlers.
97 chrome.fileBrowserHandler.onExecute.addListener(this.onExecute_.bind(this));
98 chrome.app.runtime.onLaunched.addListener(this.onLaunched_.bind(this));
99 chrome.app.runtime.onRestarted.addListener(this.onRestarted_.bind(this));
100 chrome.contextMenus.onClicked.addListener(
101 this.onContextMenuClicked_.bind(this));
103 // Fetch strings and initialize the context menu.
104 this.queue.run(function(callNextStep) {
105 chrome.fileBrowserPrivate.getStrings(function(strings) {
106 // Initialize string assets.
107 this.stringData = strings;
108 loadTimeData.data = strings;
109 this.initContextMenu_();
111 // Invoke initialize callbacks.
112 for (var i = 0; i < this.initializeCallbacks_.length; i++) {
113 this.initializeCallbacks_[i]();
115 this.initializeCallbacks_ = null;
123 * A number of delay milliseconds from the first call of tryClose to the actual
129 Background.CLOSE_DELAY_MS_ = 5000;
132 * Make a key of window geometry preferences for the given initial URL.
133 * @param {string} url Initialize URL that the window has.
134 * @return {string} Key of window geometry preferences.
136 Background.makeGeometryKey = function(url) {
137 return 'windowGeometry' + ':' + url;
141 * Key for getting and storing the last window state (maximized or not).
145 Background.MAXIMIZED_KEY_ = 'isMaximized';
148 * Register callback to be invoked after initialization.
149 * If the initialization is already done, the callback is invoked immediately.
151 * @param {function()} callback Initialize callback to be registered.
153 Background.prototype.ready = function(callback) {
154 if (this.initializeCallbacks_ !== null)
155 this.initializeCallbacks_.push(callback);
161 * Checks the current condition of background page and closes it if possible.
163 Background.prototype.tryClose = function() {
164 // If the file operation is going, the background page cannot close.
165 if (this.fileOperationManager.hasQueuedTasks() ||
166 this.driveSyncHandler_.syncing) {
167 this.lastTimeCanClose_ = null;
171 var views = chrome.extension.getViews();
173 for (var i = 0; i < views.length; i++) {
174 // If the window that is not the background page itself and it is not
175 // closing, the background page cannot close.
176 if (views[i] !== window && !views[i].closing) {
177 this.lastTimeCanClose_ = null;
180 closing = closing || views[i].closing;
183 // If some windows are closing, or the background page can close but could not
184 // 5 seconds ago, We need more time for sure.
186 this.lastTimeCanClose_ === null ||
187 Date.now() - this.lastTimeCanClose_ < Background.CLOSE_DELAY_MS_) {
188 if (this.lastTimeCanClose_ === null)
189 this.lastTimeCanClose_ = Date.now();
190 setTimeout(this.tryClose.bind(this), Background.CLOSE_DELAY_MS_);
194 // Otherwise we can close the background page.
199 * Gets similar windows, it means with the same initial url.
200 * @param {string} url URL that the obtained windows have.
201 * @return {Array.<AppWindow>} List of similar windows.
203 Background.prototype.getSimilarWindows = function(url) {
205 for (var appID in this.appWindows) {
206 if (this.appWindows[appID].contentWindow.appInitialURL === url)
207 result.push(this.appWindows[appID]);
213 * Wrapper for an app window.
215 * Expects the following from the app scripts:
216 * 1. The page load handler should initialize the app using |window.appState|
217 * and call |util.platform.saveAppState|.
218 * 2. Every time the app state changes the app should update |window.appState|
219 * and call |util.platform.saveAppState| .
220 * 3. The app may have |unload| function to persist the app state that does not
221 * fit into |window.appState|.
223 * @param {string} url App window content url.
224 * @param {string} id App window id.
225 * @param {Object} options Options object to create it.
228 function AppWindowWrapper(url, id, options) {
231 // Do deep copy for the template of options to assign customized params later.
232 this.options_ = JSON.parse(JSON.stringify(options));
234 this.appState_ = null;
235 this.openingOrOpened_ = false;
236 this.queue = new AsyncUtil.Queue();
240 AppWindowWrapper.prototype = {
242 * @return {AppWindow} Wrapped application window.
250 * Focuses the window on the specified desktop.
251 * @param {AppWindow} appWindow Application window.
252 * @param {string=} opt_profileId The profiled ID of the target window. If it is
253 * dropped, the window is focused on the current window.
255 AppWindowWrapper.focusOnDesktop = function(appWindow, opt_profileId) {
256 new Promise(function(onFulfilled, onRejected) {
258 onFulfilled(opt_profileId);
260 chrome.fileBrowserPrivate.getProfiles(function(profiles,
263 onFulfilled(currentId);
266 }).then(function(profileId) {
267 appWindow.contentWindow.chrome.fileBrowserPrivate.visitDesktop(
268 profileId, function() {
275 * Shift distance to avoid overlapping windows.
279 AppWindowWrapper.SHIFT_DISTANCE = 40;
282 * Sets the icon of the window.
283 * @param {string} iconPath Path of the icon.
285 AppWindowWrapper.prototype.setIcon = function(iconPath) {
286 this.window_.setIcon(iconPath);
292 * @param {Object} appState App state.
293 * @param {boolean} reopen True if the launching is triggered automatically.
295 * @param {function()=} opt_callback Completion callback.
297 AppWindowWrapper.prototype.launch = function(appState, reopen, opt_callback) {
298 // Check if the window is opened or not.
299 if (this.openingOrOpened_) {
300 console.error('The window is already opened.');
305 this.openingOrOpened_ = true;
307 // Save application state.
308 this.appState_ = appState;
310 // Get similar windows, it means with the same initial url, eg. different
311 // main windows of Files.app.
312 var similarWindows = background.getSimilarWindows(this.url_);
314 // Restore maximized windows, to avoid hiding them to tray, which can be
315 // confusing for users.
316 this.queue.run(function(callback) {
317 for (var index = 0; index < similarWindows.length; index++) {
318 if (similarWindows[index].isMaximized()) {
319 var createWindowAndRemoveListener = function() {
320 similarWindows[index].onRestored.removeListener(
321 createWindowAndRemoveListener);
324 similarWindows[index].onRestored.addListener(
325 createWindowAndRemoveListener);
326 similarWindows[index].restore();
330 // If no maximized windows, then create the window immediately.
334 // Obtains the last geometry and window state (maximized or not).
336 var isMaximized = false;
337 this.queue.run(function(callback) {
338 var boundsKey = Background.makeGeometryKey(this.url_);
339 var maximizedKey = Background.MAXIMIZED_KEY_;
340 chrome.storage.local.get([boundsKey, maximizedKey], function(preferences) {
341 if (!chrome.runtime.lastError) {
342 lastBounds = preferences[boundsKey];
343 isMaximized = preferences[maximizedKey];
349 // Closure creating the window, once all preprocessing tasks are finished.
350 this.queue.run(function(callback) {
351 // Apply the last bounds.
353 this.options_.bounds = lastBounds;
355 this.options_.state = 'maximized';
358 chrome.app.window.create(this.url_, this.options_, function(appWindow) {
359 this.window_ = appWindow;
365 this.queue.run(function(callback) {
366 // If there is another window in the same position, shift the window.
367 var makeBoundsKey = function(bounds) {
368 return bounds.left + '/' + bounds.top;
370 var notAvailablePositions = {};
371 for (var i = 0; i < similarWindows.length; i++) {
372 var key = makeBoundsKey(similarWindows[i].getBounds());
373 notAvailablePositions[key] = true;
375 var candidateBounds = this.window_.getBounds();
377 var key = makeBoundsKey(candidateBounds);
378 if (!notAvailablePositions[key])
380 // Make the position available to avoid an infinite loop.
381 notAvailablePositions[key] = false;
382 var nextLeft = candidateBounds.left + AppWindowWrapper.SHIFT_DISTANCE;
383 var nextRight = nextLeft + candidateBounds.width;
384 candidateBounds.left = nextRight >= screen.availWidth ?
385 nextRight % screen.availWidth : nextLeft;
386 var nextTop = candidateBounds.top + AppWindowWrapper.SHIFT_DISTANCE;
387 var nextBottom = nextTop + candidateBounds.height;
388 candidateBounds.top = nextBottom >= screen.availHeight ?
389 nextBottom % screen.availHeight : nextTop;
391 this.window_.moveTo(candidateBounds.left, candidateBounds.top);
393 // Save the properties.
394 var appWindow = this.window_;
395 background.appWindows[this.id_] = appWindow;
396 var contentWindow = appWindow.contentWindow;
397 contentWindow.appID = this.id_;
398 contentWindow.appState = this.appState_;
399 contentWindow.appReopen = reopen;
400 contentWindow.appInitialURL = this.url_;
402 contentWindow.IN_TEST = true;
404 // Register event listeners.
405 appWindow.onBoundsChanged.addListener(this.onBoundsChanged_.bind(this));
406 appWindow.onClosed.addListener(this.onClosed_.bind(this));
416 * Handles the onClosed extension API event.
419 AppWindowWrapper.prototype.onClosed_ = function() {
420 // Remember the last window state (maximized or normal).
421 var preferences = {};
422 preferences[Background.MAXIMIZED_KEY_] = this.window_.isMaximized();
423 chrome.storage.local.set(preferences);
425 // Unload the window.
426 var appWindow = this.window_;
427 var contentWindow = this.window_.contentWindow;
428 if (contentWindow.unload)
429 contentWindow.unload();
431 this.openingOrOpened_ = false;
433 // Updates preferences.
434 if (contentWindow.saveOnExit) {
435 contentWindow.saveOnExit.forEach(function(entry) {
436 util.AppCache.update(entry.key, entry.value);
439 chrome.storage.local.remove(this.id_); // Forget the persisted state.
441 // Remove the window from the set.
442 delete background.appWindows[this.id_];
444 // If there is no application window, reset window ID.
445 if (!Object.keys(background.appWindows).length)
446 nextFileManagerWindowID = 0;
447 background.tryClose();
451 * Handles onBoundsChanged extension API event.
454 AppWindowWrapper.prototype.onBoundsChanged_ = function() {
455 if (!this.window_.isMaximized()) {
456 var preferences = {};
457 preferences[Background.makeGeometryKey(this.url_)] =
458 this.window_.getBounds();
459 chrome.storage.local.set(preferences);
464 * Wrapper for a singleton app window.
466 * In addition to the AppWindowWrapper requirements the app scripts should
467 * have |reload| method that re-initializes the app based on a changed
470 * @param {string} url App window content url.
471 * @param {Object|function()} options Options object or a function to return it.
474 function SingletonAppWindowWrapper(url, options) {
475 AppWindowWrapper.call(this, url, url, options);
479 * Inherits from AppWindowWrapper.
481 SingletonAppWindowWrapper.prototype = {__proto__: AppWindowWrapper.prototype};
486 * Activates an existing window or creates a new one.
488 * @param {Object} appState App state.
489 * @param {boolean} reopen True if the launching is triggered automatically.
491 * @param {function()=} opt_callback Completion callback.
493 SingletonAppWindowWrapper.prototype.launch =
494 function(appState, reopen, opt_callback) {
495 // If the window is not opened yet, just call the parent method.
496 if (!this.openingOrOpened_) {
497 AppWindowWrapper.prototype.launch.call(
498 this, appState, reopen, opt_callback);
502 // If the window is already opened, reload the window.
503 // The queue is used to wait until the window is opened.
504 this.queue.run(function(nextStep) {
505 this.window_.contentWindow.appState = appState;
506 this.window_.contentWindow.appReopen = reopen;
507 this.window_.contentWindow.reload();
515 * Reopen a window if its state is saved in the local storage.
516 * @param {function()=} opt_callback Completion callback.
518 SingletonAppWindowWrapper.prototype.reopen = function(opt_callback) {
519 chrome.storage.local.get(this.id_, function(items) {
520 var value = items[this.id_];
522 opt_callback && opt_callback();
523 return; // No app state persisted.
527 var appState = JSON.parse(value);
529 console.error('Corrupt launch data for ' + this.id_, value);
530 opt_callback && opt_callback();
533 this.launch(appState, true, opt_callback);
538 * Prefix for the file manager window ID.
540 var FILES_ID_PREFIX = 'files#';
543 * Regexp matching a file manager window ID.
545 var FILES_ID_PATTERN = new RegExp('^' + FILES_ID_PREFIX + '(\\d*)$');
548 * Value of the next file manager window ID.
550 var nextFileManagerWindowID = 0;
553 * File manager window create options.
557 var FILE_MANAGER_WINDOW_CREATE_OPTIONS = Object.freeze({
558 bounds: Object.freeze({
559 left: Math.round(window.screen.availWidth * 0.1),
560 top: Math.round(window.screen.availHeight * 0.1),
561 width: Math.round(window.screen.availWidth * 0.8),
562 height: Math.round(window.screen.availHeight * 0.8)
568 transparentBackground: true
572 * @param {Object=} opt_appState App state.
573 * @param {number=} opt_id Window id.
574 * @param {LaunchType=} opt_type Launch type. Default: ALWAYS_CREATE.
575 * @param {function(string)=} opt_callback Completion callback with the App ID.
577 function launchFileManager(opt_appState, opt_id, opt_type, opt_callback) {
578 var type = opt_type || LaunchType.ALWAYS_CREATE;
580 // Wait until all windows are created.
581 background.queue.run(function(onTaskCompleted) {
582 // Check if there is already a window with the same URL. If so, then
583 // reuse it instead of opening a new one.
584 if (type == LaunchType.FOCUS_SAME_OR_CREATE ||
585 type == LaunchType.FOCUS_ANY_OR_CREATE) {
587 for (var key in background.appWindows) {
588 if (!key.match(FILES_ID_PATTERN))
591 var contentWindow = background.appWindows[key].contentWindow;
592 if (!contentWindow.appState)
595 // Different current directories.
596 if (opt_appState.currentDirectoryURL !==
597 contentWindow.appState.currentDirectoryURL) {
601 // Selection URL specified, and it is different.
602 if (opt_appState.selectionURL &&
603 opt_appState.selectionURL !==
604 contentWindow.appState.selectionURL) {
608 AppWindowWrapper.focusOnDesktop(
609 background.appWindows[key], opt_appState.displayedId);
618 // Focus any window if none is focused. Try restored first.
619 if (type == LaunchType.FOCUS_ANY_OR_CREATE) {
620 // If there is already a focused window, then finish.
621 for (var key in background.appWindows) {
622 if (!key.match(FILES_ID_PATTERN))
625 // The isFocused() method should always be available, but in case
626 // Files.app's failed on some error, wrap it with try catch.
628 if (background.appWindows[key].contentWindow.isFocused()) {
635 console.error(e.message);
638 // Try to focus the first non-minimized window.
639 for (var key in background.appWindows) {
640 if (!key.match(FILES_ID_PATTERN))
643 if (!background.appWindows[key].isMinimized()) {
644 AppWindowWrapper.focusOnDesktop(
645 background.appWindows[key], (opt_appState || {}).displayedId);
652 // Restore and focus any window.
653 for (var key in background.appWindows) {
654 if (!key.match(FILES_ID_PATTERN))
657 AppWindowWrapper.focusOnDesktop(
658 background.appWindows[key], (opt_appState || {}).displayedId);
666 // Create a new instance in case of ALWAYS_CREATE type, or as a fallback
669 var id = opt_id || nextFileManagerWindowID;
670 nextFileManagerWindowID = Math.max(nextFileManagerWindowID, id + 1);
671 var appId = FILES_ID_PREFIX + id;
673 var appWindow = new AppWindowWrapper(
676 FILE_MANAGER_WINDOW_CREATE_OPTIONS);
677 appWindow.launch(opt_appState || {}, false, function() {
678 AppWindowWrapper.focusOnDesktop(
679 appWindow.window_, (opt_appState || {}).displayedId);
688 * Executes a file browser task.
690 * @param {string} action Task id.
691 * @param {Object} details Details object.
694 Background.prototype.onExecute_ = function(action, details) {
695 var urls = details.entries.map(function(e) { return e.toURL(); });
699 launchAudioPlayer({items: urls, position: 0});
703 var launchEnable = null;
704 var queue = new AsyncUtil.Queue();
705 queue.run(function(nextStep) {
706 // If it is not auto-open (triggered by mounting external devices), we
707 // always launch Files.app.
708 if (action != 'auto-open') {
713 // If the disable-default-apps flag is on, Files.app is not opened
714 // automatically on device mount not to obstruct the manual test.
715 chrome.commandLinePrivate.hasSwitch('disable-default-apps',
717 launchEnable = !flag;
721 queue.run(function(nextStep) {
727 // Every other action opens a Files app window.
732 // It is not allowed to call getParent() here, since there may be
733 // no permissions to access it at this stage. Therefore we are passing
734 // the selectionURL only, and the currentDirectory will be resolved
736 selectionURL: details.entries[0].toURL()
738 // For mounted devices just focus any Files.app window. The mounted
739 // volume will appear on the navigation list.
740 var type = action == 'auto-open' ? LaunchType.FOCUS_ANY_OR_CREATE :
741 LaunchType.FOCUS_SAME_OR_CREATE;
742 launchFileManager(appState, /* App ID */ undefined, type, nextStep);
749 * Icon of the audio player.
750 * TODO(yoshiki): Consider providing an exact size icon, instead of relying
751 * on downsampling by ash.
756 var AUDIO_PLAYER_ICON = 'audio_player/icons/audio-player-64.png';
758 // The instance of audio player. Until it's ready, this is null.
759 var audioPlayer = null;
761 // Queue to serializes the initialization, launching and reloading of the audio
762 // player, so races won't happen.
763 var audioPlayerInitializationQueue = new AsyncUtil.Queue();
765 audioPlayerInitializationQueue.run(function(callback) {
766 // TODO(yoshiki): Remove '--file-manager-enable-new-audio-player' flag after
767 // the feature is launched.
768 var newAudioPlayerEnabled = true;
770 var audioPlayerHTML =
771 newAudioPlayerEnabled ? 'audio_player.html' : 'mediaplayer.html';
774 * Audio player window create options.
777 var audioPlayerCreateOptions = Object.freeze({
781 newAudioPlayerEnabled ?
782 (44 + 73) : // 44px: track, 73px: controller
783 (35 + 58), // 35px: track, 58px: controller
784 minWidth: newAudioPlayerEnabled ? 292 : 280,
785 height: newAudioPlayerEnabled ? (44 + 73) : (35 + 58), // collapsed
786 width: newAudioPlayerEnabled ? 292 : 280,
789 audioPlayer = new SingletonAppWindowWrapper(audioPlayerHTML,
790 audioPlayerCreateOptions);
795 * Launches the audio player.
796 * @param {Object} playlist Playlist.
797 * @param {string=} opt_displayedId ProfileID of the desktop where the audio
798 * player should show.
800 function launchAudioPlayer(playlist, opt_displayedId) {
801 audioPlayerInitializationQueue.run(function(callback) {
802 audioPlayer.launch(playlist, false, function(appWindow) {
803 audioPlayer.setIcon(AUDIO_PLAYER_ICON);
804 AppWindowWrapper.focusOnDesktop(audioPlayer.rawAppWindow,
815 Background.prototype.onLaunched_ = function() {
816 if (nextFileManagerWindowID == 0) {
817 // The app just launched. Remove window state records that are not needed
819 chrome.storage.local.get(function(items) {
820 for (var key in items) {
821 if (items.hasOwnProperty(key)) {
822 if (key.match(FILES_ID_PATTERN))
823 chrome.storage.local.remove(key);
828 launchFileManager(null, null, LaunchType.FOCUS_ANY_OR_CREATE);
832 * Restarted the app, restore windows.
835 Background.prototype.onRestarted_ = function() {
836 // Reopen file manager windows.
837 chrome.storage.local.get(function(items) {
838 for (var key in items) {
839 if (items.hasOwnProperty(key)) {
840 var match = key.match(FILES_ID_PATTERN);
842 var id = Number(match[1]);
844 var appState = JSON.parse(items[key]);
845 launchFileManager(appState, id);
847 console.error('Corrupt launch data for ' + id);
854 // Reopen audio player.
855 audioPlayerInitializationQueue.run(function(callback) {
856 audioPlayer.reopen(function() {
857 // If the audioPlayer is reopened, change its window's icon. Otherwise
858 // there is no reopened window so just skip the call of setIcon.
859 if (audioPlayer.rawAppWindow)
860 audioPlayer.setIcon(AUDIO_PLAYER_ICON);
867 * Handles clicks on a custom item on the launcher context menu.
868 * @param {OnClickData} info Event details.
871 Background.prototype.onContextMenuClicked_ = function(info) {
872 if (info.menuItemId == 'new-window') {
873 // Find the focused window (if any) and use it's current url for the
874 // new window. If not found, then launch with the default url.
875 for (var key in background.appWindows) {
877 if (background.appWindows[key].contentWindow.isFocused()) {
879 // Do not clone the selection url, only the current directory.
880 currentDirectoryURL: background.appWindows[key].contentWindow.
881 appState.currentDirectoryURL
883 launchFileManager(appState);
887 // The isFocused method may not be defined during initialization.
888 // Therefore, wrapped with a try-catch block.
892 // Launch with the default URL.
898 * Initializes the context menu. Recreates if already exists.
901 Background.prototype.initContextMenu_ = function() {
903 // According to the spec [1], the callback is optional. But no callback
904 // causes an error for some reason, so we call it with null-callback to
905 // prevent the error. http://crbug.com/353877
906 // - [1] https://developer.chrome.com/extensions/contextMenus#method-remove
907 chrome.contextMenus.remove('new-window', function() {});
909 // There is no way to detect if the context menu is already added, therefore
910 // try to recreate it every time.
912 chrome.contextMenus.create({
914 contexts: ['launcher'],
915 title: str('NEW_WINDOW_BUTTON_LABEL')
920 * Singleton instance of Background.
923 window.background = new Background();