Commit b225b380 authored by Luciano Pacheco's avatar Luciano Pacheco Committed by Commit Bot

Files app: Refactor launcher.launchFileManager() to avoid repeated regex

The code was performing a regex in all open windows several times,
refactor to perform only once.

Bug: 1067478
Change-Id: I31d481dfb153f0f47f65bac081f1b4d73dccc8ad
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2148793
Commit-Queue: Luciano Pacheco <lucmult@chromium.org>
Reviewed-by: default avatarAlex Danilo <adanilo@chromium.org>
Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#759549}
parent 0c010e46
......@@ -89,30 +89,36 @@ launcher.launchFileManager = async (opt_appState, opt_id, opt_type) => {
await launcher.initializationPromise_;
const filesWindows =
Object.entries(window.appWindows).filter(([key, appWindow]) => {
return key.match(FILES_ID_PATTERN);
});
// Check if there is already a window with the same URL. If so, then
// reuse it instead of opening a new one.
if (opt_appState &&
(type == LaunchType.FOCUS_SAME_OR_CREATE ||
type == LaunchType.FOCUS_ANY_OR_CREATE)) {
for (const key in window.appWindows) {
if (!key.match(FILES_ID_PATTERN)) {
continue;
}
const contentWindow = window.appWindows[key].contentWindow;
for (const [key, appWindow] of filesWindows) {
const contentWindow = appWindow.contentWindow;
if (!contentWindow.appState) {
continue;
}
// Different current directories.
if (opt_appState.currentDirectoryURL !==
contentWindow.appState.currentDirectoryURL) {
continue;
}
// Selection URL specified, and it is different.
if (opt_appState.selectionURL &&
opt_appState.selectionURL !== contentWindow.appState.selectionURL) {
continue;
}
window.appWindows[key].focus();
// Found compatible window.
appWindow.focus();
return Promise.resolve(key);
}
}
......@@ -120,46 +126,35 @@ launcher.launchFileManager = async (opt_appState, opt_id, opt_type) => {
// Focus any window if none is focused. Try restored first.
if (type == LaunchType.FOCUS_ANY_OR_CREATE) {
// If there is already a focused window, then finish.
for (const key in window.appWindows) {
if (!key.match(FILES_ID_PATTERN)) {
continue;
}
for (const [key, appWindow] of filesWindows) {
// The isFocused() method should always be available, but in case
// the Files app's failed on some error, wrap it with try catch.
try {
if (window.appWindows[key].contentWindow.isFocused()) {
if (appWindow.contentWindow.isFocused()) {
return Promise.resolve(key);
}
} catch (e) {
console.error(e.message);
console.error(e);
}
}
// Try to focus the first non-minimized window.
for (const key in window.appWindows) {
if (!key.match(FILES_ID_PATTERN)) {
continue;
}
if (!window.appWindows[key].isMinimized()) {
window.appWindows[key].focus();
// Try to focus the first non-minimized window.
for (const [key, appWindow] of filesWindows) {
if (!appWindow.isMinimized()) {
appWindow.focus();
return Promise.resolve(key);
}
}
// Restore and focus any window.
for (const key in window.appWindows) {
if (!key.match(FILES_ID_PATTERN)) {
continue;
}
window.appWindows[key].focus();
// Restore and focus any window.
for (const [key, appWindow] of filesWindows) {
appWindow.focus();
return Promise.resolve(key);
}
}
// Create a new instance in case of ALWAYS_CREATE type, or as a fallback
// for other types.
const id = opt_id || nextFileManagerWindowID;
nextFileManagerWindowID = Math.max(nextFileManagerWindowID, id + 1);
const appId = FILES_ID_PREFIX + id;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment