[go: nahoru, domu]

Reland "[Files SWA]: Chrome App API cleanup"

This is a reland of 0a1158be66a6730d0d8a3d7c9295783cd39fc7e2

I do not believe this CL was the reason for failing tests. As one
can see on https://ci.chromium.org/ui/p/chromium/builders/ci/Linux%20ChromiumOS%20MSan%20Tests/24411/overview
the same tests failed, even though at the revisions these tests failed
(6d6e70194b5ad31b93d372981e6fe79d45ddd944) this CL was already
reverted. Notice lack of url_constants.js in
https://chromium.googlesource.com/chromium/src/+/6d6e70194b5ad31b93d372981e6fe79d45ddd944/ui/file_manager/file_manager/common/js/

Original change's description:
> [Files SWA]: Chrome App API cleanup
>
> Removing dependencies on chrome.runtime.getManifest and
> chrome.runtime.getURL APIs. Since those require URLs based
> on the Files App ID (host), adds url_constants.js.
> The new module centralizes building File Manager URLs for
> files.
>
> Bug: b/194251406, b/194251405
> Change-Id: Ifae98ead43db329c45afac054df9f72709d58cdf
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3056256
> Reviewed-by: Luciano Pacheco <lucmult@chromium.org>
> Commit-Queue: Bo Majewski <majewski@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#906435}

Bug: b/194251406, b/194251405
Change-Id: I9115cf449bd759e0442fcb8da8f460eb5f5732b7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3060136
Commit-Queue: Luciano Pacheco <lucmult@chromium.org>
Reviewed-by: Luciano Pacheco <lucmult@chromium.org>
Cr-Commit-Position: refs/heads/master@{#906902}
diff --git a/ui/file_manager/BUILD.gn b/ui/file_manager/BUILD.gn
index 59ec273..296ac0d 100644
--- a/ui/file_manager/BUILD.gn
+++ b/ui/file_manager/BUILD.gn
@@ -147,6 +147,7 @@
     "file_manager/common/js/test_importer_common.js",
     "file_manager/common/js/trash.js",
     "file_manager/common/js/unittest_util.js",
+    "file_manager/common/js/url_constants.js",
     "file_manager/common/js/util.js",
     "file_manager/common/js/volume_manager_types.js",
     "file_manager/common/js/xfm.js",
diff --git a/ui/file_manager/file_manager/BUILD.gn b/ui/file_manager/file_manager/BUILD.gn
index 5b0ea272..720fba0 100644
--- a/ui/file_manager/file_manager/BUILD.gn
+++ b/ui/file_manager/file_manager/BUILD.gn
@@ -39,6 +39,7 @@
     "common/js/storage_adapter.js",
     "common/js/test_error_reporting.js",
     "common/js/volume_manager_types.js",
+    "common/js/url_constants.js",
     "common/js/xfm.js",
     "foreground/js/metadata/image_orientation.js",
     "externs/background/background_base.js",
diff --git a/ui/file_manager/file_manager/background/js/BUILD.gn b/ui/file_manager/file_manager/background/js/BUILD.gn
index 26a4183..f0cfe46 100644
--- a/ui/file_manager/file_manager/background/js/BUILD.gn
+++ b/ui/file_manager/file_manager/background/js/BUILD.gn
@@ -149,6 +149,7 @@
     ":volume_manager_factory",
     "//ui/file_manager/file_manager/common/js:importer_common",
     "//ui/file_manager/file_manager/common/js:metrics",
+    "//ui/file_manager/file_manager/common/js:url_constants",
     "//ui/file_manager/file_manager/common/js:util",
     "//ui/file_manager/file_manager/common/js:volume_manager_types",
     "//ui/file_manager/file_manager/externs:volume_info",
@@ -234,6 +235,7 @@
     "//ui/file_manager/file_manager/common/js:importer_common",
     "//ui/file_manager/file_manager/common/js:metrics",
     "//ui/file_manager/file_manager/common/js:progress_center_common",
+    "//ui/file_manager/file_manager/common/js:url_constants",
     "//ui/file_manager/file_manager/common/js:util",
     "//ui/file_manager/file_manager/externs:volume_info",
     "//ui/file_manager/file_manager/externs:volume_manager",
@@ -276,6 +278,7 @@
     ":launcher",
     "//ui/file_manager/file_manager/common/js:async_util",
     "//ui/file_manager/file_manager/common/js:progress_center_common",
+    "//ui/file_manager/file_manager/common/js:url_constants",
     "//ui/file_manager/file_manager/common/js:util",
     "//ui/file_manager/file_manager/externs:drive_dialog_controller",
     "//ui/file_manager/file_manager/externs/background:drive_sync_handler",
@@ -291,6 +294,7 @@
     "//chrome/test/data/webui:chai_assert",
     "//ui/file_manager/file_manager/common/js:mock_chrome",
     "//ui/file_manager/file_manager/common/js:progress_center_common",
+    "//ui/file_manager/file_manager/common/js:url_constants",
     "//ui/webui/resources/js:load_time_data.m",
   ]
 }
@@ -578,6 +582,7 @@
   deps = [
     "//ui/file_manager/file_manager/common/js:async_util",
     "//ui/file_manager/file_manager/common/js:progress_center_common",
+    "//ui/file_manager/file_manager/common/js:url_constants",
     "//ui/file_manager/file_manager/common/js:util",
     "//ui/file_manager/file_manager/externs:progress_center_panel",
     "//ui/file_manager/file_manager/externs/background:progress_center",
diff --git a/ui/file_manager/file_manager/background/js/device_handler.js b/ui/file_manager/file_manager/background/js/device_handler.js
index 3cc0b56..1a3686926 100644
--- a/ui/file_manager/file_manager/background/js/device_handler.js
+++ b/ui/file_manager/file_manager/background/js/device_handler.js
@@ -8,6 +8,7 @@
 import {importer} from '../../common/js/importer_common.js';
 import {metrics} from '../../common/js/metrics.js';
 import {ProgressCenterItem, ProgressItemState, ProgressItemType} from '../../common/js/progress_center_common.js';
+import {getFilesAppIconURL} from '../../common/js/url_constants.js';
 import {str, strf, util} from '../../common/js/util.js';
 import {xfm} from '../../common/js/xfm.js';
 import {ProgressCenter} from '../../externs/background/progress_center.js';
@@ -642,7 +643,7 @@
           type: 'basic',
           title: str(this.title),
           message: message || (str(this.message) + additionalMessage),
-          iconUrl: chrome.runtime.getURL('/common/images/icon96.png'),
+          iconUrl: getFilesAppIconURL().toString(),
           buttons: buttons,
           isClickable: this.isClickable
         },
diff --git a/ui/file_manager/file_manager/background/js/drive_sync_handler.js b/ui/file_manager/file_manager/background/js/drive_sync_handler.js
index 88981e1..df8beb33 100644
--- a/ui/file_manager/file_manager/background/js/drive_sync_handler.js
+++ b/ui/file_manager/file_manager/background/js/drive_sync_handler.js
@@ -6,6 +6,7 @@
 
 import {AsyncUtil} from '../../common/js/async_util.js';
 import {ProgressCenterItem, ProgressItemState, ProgressItemType} from '../../common/js/progress_center_common.js';
+import {getFilesAppIconURL, toFilesAppURL} from '../../common/js/url_constants.js';
 import {str, strf} from '../../common/js/util.js';
 import {xfm} from '../../common/js/xfm.js';
 import {DriveSyncHandler} from '../../externs/background/drive_sync_handler.js';
@@ -209,9 +210,9 @@
     xfm.notifications.create(
         DriveSyncHandlerImpl.DISABLED_MOBILE_SYNC_NOTIFICATION_ID_, {
           type: 'basic',
-          title: chrome.runtime.getManifest().name,
+          title: str('FILEMANAGER_APP_NAME'),
           message: str('DISABLED_MOBILE_SYNC_NOTIFICATION_MESSAGE'),
-          iconUrl: chrome.runtime.getURL('/common/images/icon96.png'),
+          iconUrl: getFilesAppIconURL().toString(),
           buttons:
               [{title: str('DISABLED_MOBILE_SYNC_NOTIFICATION_ENABLE_BUTTON')}]
         },
@@ -320,11 +321,7 @@
   isProcessableEvent(event) {
     const fileUrl = event.fileUrl;
     if (fileUrl) {
-      const match = 'filesystem:' +
-          (window.isSWA ?
-               'chrome://file-manager' :
-               'chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj');
-      return fileUrl.startsWith(match);
+      return fileUrl.startsWith(`filesystem:${toFilesAppURL()}`);
     }
     return true;
   }
@@ -434,9 +431,9 @@
       xfm.notifications.create(
           DriveSyncHandlerImpl.ENABLE_DOCS_OFFLINE_NOTIFICATION_ID_, {
             type: 'basic',
-            title: chrome.runtime.getManifest().name,
+            title: str('FILEMANAGER_APP_NAME'),
             message: str('OFFLINE_ENABLE_MESSAGE'),
-            iconUrl: chrome.runtime.getURL('/common/images/icon96.png'),
+            iconUrl: getFilesAppIconURL().toString(),
             buttons: [
               {title: str('OFFLINE_ENABLE_REJECT')},
               {title: str('OFFLINE_ENABLE_ACCEPT')},
diff --git a/ui/file_manager/file_manager/background/js/drive_sync_handler_unittest.m.js b/ui/file_manager/file_manager/background/js/drive_sync_handler_unittest.m.js
index 1a6245f..a1badca 100644
--- a/ui/file_manager/file_manager/background/js/drive_sync_handler_unittest.m.js
+++ b/ui/file_manager/file_manager/background/js/drive_sync_handler_unittest.m.js
@@ -7,6 +7,7 @@
 
 import {installMockChrome} from '../../common/js/mock_chrome.js';
 import {ProgressItemState} from '../../common/js/progress_center_common.js';
+import {toFilesAppURL} from '../../common/js/url_constants.js';
 
 import {DriveSyncHandlerImpl} from './drive_sync_handler.js';
 import {MockProgressCenter} from './mock_progress_center.js';
@@ -26,8 +27,7 @@
  * @return {string} Valid file URL
  */
 function asFileURL(name) {
-  return 'filesystem:chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/' +
-      `external/${name}`;
+  return 'filesystem:' + toFilesAppURL(`external/${name}`).toString();
 }
 
 /**
diff --git a/ui/file_manager/file_manager/background/js/progress_center.js b/ui/file_manager/file_manager/background/js/progress_center.js
index 9665bb0..f818446 100644
--- a/ui/file_manager/file_manager/background/js/progress_center.js
+++ b/ui/file_manager/file_manager/background/js/progress_center.js
@@ -4,6 +4,7 @@
 
 import {AsyncUtil} from '../../common/js/async_util.js';
 import {ProgressCenterItem, ProgressItemState} from '../../common/js/progress_center_common.js';
+import {getFilesAppIconURL} from '../../common/js/url_constants.js';
 import {str} from '../../common/js/util.js';
 import {xfm} from '../../common/js/xfm.js';
 import {ProgressCenter} from '../../externs/background/progress_center.js';
@@ -287,8 +288,8 @@
     // Create/update the notification with the item.
     this.queue_.run(proceed => {
       const params = {
-        title: chrome.runtime.getManifest().name,
-        iconUrl: chrome.runtime.getURL('/common/images/icon96.png'),
+        title: str('FILEMANAGER_APP_NAME'),
+        iconUrl: getFilesAppIconURL().toString(),
         type: item.state === ProgressItemState.PROGRESSING ? 'progress' :
                                                              'basic',
         message: item.message,
diff --git a/ui/file_manager/file_manager/common/js/BUILD.gn b/ui/file_manager/file_manager/common/js/BUILD.gn
index d7ffc71b..d62f121 100644
--- a/ui/file_manager/file_manager/common/js/BUILD.gn
+++ b/ui/file_manager/file_manager/common/js/BUILD.gn
@@ -40,6 +40,7 @@
     ":progress_center_common",
     ":storage_adapter",
     ":trash",
+    ":url_constants",
     ":util",
     ":volume_manager_types",
     ":xfm",
@@ -65,6 +66,7 @@
     ":progress_center_common",
     ":storage_adapter",
     ":trash",
+    ":url_constants",
     ":util",
     ":volume_manager_types",
     ":xfm",
@@ -276,6 +278,9 @@
   deps = [ "//chrome/test/data/webui:chai_assert" ]
 }
 
+js_library("url_constants") {
+}
+
 js_library("util") {
   deps = [
     ":files_app_entry_types",
diff --git a/ui/file_manager/file_manager/common/js/url_constants.js b/ui/file_manager/file_manager/common/js/url_constants.js
new file mode 100644
index 0000000..d05f361
--- /dev/null
+++ b/ui/file_manager/file_manager/common/js/url_constants.js
@@ -0,0 +1,47 @@
+// Copyright 2021 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+ * @const {string}
+ */
+export const LEGACY_FILES_EXTENSION_ID = 'hhaomjibdihmijegdhdafkllkbggdgoj';
+
+/**
+ * @const {string}
+ */
+export const SWA_FILES_APP_HOST = 'file-manager';
+
+/**
+ * The URL of the legacy version of File Manger.
+ * @const {!URL}
+ */
+export const LEGACY_FILES_APP_URL =
+    new URL(`chrome-extension://${LEGACY_FILES_EXTENSION_ID}`);
+
+/**
+ * The URL of the System Web App version of File Manger.
+ * @const {!URL}
+ */
+export const SWA_FILES_APP_URL = new URL(`chrome://${SWA_FILES_APP_HOST}`);
+
+/**
+ * The path to the File Manager icon.
+ * @const {string}
+ */
+export const FILES_APP_ICON_PATH = 'common/images/icon96.png';
+
+/**
+ * @param {string=} path relative to the Files app root.
+ * @return {!URL} The absolute URL for a path within the Files app.
+ */
+export function toFilesAppURL(path = '') {
+  return new URL(path, window.isSWA ? SWA_FILES_APP_URL : LEGACY_FILES_APP_URL);
+}
+
+/**
+ * @return {!URL} The URL of the file that holds Files App icon.
+ */
+export function getFilesAppIconURL() {
+  return toFilesAppURL(FILES_APP_ICON_PATH);
+}
diff --git a/ui/file_manager/file_manager/foreground/js/BUILD.gn b/ui/file_manager/file_manager/foreground/js/BUILD.gn
index 4105cb88..8bd898a9 100644
--- a/ui/file_manager/file_manager/foreground/js/BUILD.gn
+++ b/ui/file_manager/file_manager/foreground/js/BUILD.gn
@@ -667,6 +667,7 @@
     "//ui/file_manager/file_manager/common/js:file_type",
     "//ui/file_manager/file_manager/common/js:metrics",
     "//ui/file_manager/file_manager/common/js:progress_center_common",
+    "//ui/file_manager/file_manager/common/js:url_constants",
     "//ui/file_manager/file_manager/common/js:util",
     "//ui/file_manager/file_manager/common/js:volume_manager_types",
     "//ui/file_manager/file_manager/externs:volume_info",
diff --git a/ui/file_manager/file_manager/foreground/js/constants.js b/ui/file_manager/file_manager/foreground/js/constants.js
index f755213..11d1956 100644
--- a/ui/file_manager/file_manager/foreground/js/constants.js
+++ b/ui/file_manager/file_manager/foreground/js/constants.js
@@ -109,6 +109,3 @@
  * @const {string}
  */
 constants.CROSTINI_CONNECT_ERR = 'CrostiniConnectErr';
-
-/** @const {string} */
-constants.FILES_APP_EXTENSION_ID = 'hhaomjibdihmijegdhdafkllkbggdgoj';
diff --git a/ui/file_manager/file_manager/foreground/js/file_tasks.js b/ui/file_manager/file_manager/foreground/js/file_tasks.js
index f62344d..8231f6d 100644
--- a/ui/file_manager/file_manager/foreground/js/file_tasks.js
+++ b/ui/file_manager/file_manager/foreground/js/file_tasks.js
@@ -11,6 +11,7 @@
 import {FileType} from '../../common/js/file_type.js';
 import {metrics} from '../../common/js/metrics.js';
 import {ProgressCenterItem, ProgressItemState, ProgressItemType} from '../../common/js/progress_center_common.js';
+import {LEGACY_FILES_EXTENSION_ID} from '../../common/js/url_constants.js';
 import {str, strf, util} from '../../common/js/util.js';
 import {VolumeManagerCommon} from '../../common/js/volume_manager_types.js';
 import {Crostini} from '../../externs/background/crostini.js';
@@ -328,7 +329,7 @@
    */
   static isInternalTask_(descriptor) {
     const {appId, taskType, actionId} = descriptor;
-    if (appId !== constants.FILES_APP_EXTENSION_ID || taskType !== 'app') {
+    if (appId !== LEGACY_FILES_EXTENSION_ID || taskType !== 'app') {
       return false;
     }
     switch (actionId) {
@@ -374,7 +375,7 @@
    */
   static annotateTasks_(tasks, entries) {
     const result = [];
-    const id = constants.FILES_APP_EXTENSION_ID;
+    const id = LEGACY_FILES_EXTENSION_ID;
     for (const task of tasks) {
       const {appId, taskType, actionId} = task.descriptor;
 
@@ -646,7 +647,7 @@
 
     this.checkAvailability_(() => {
       const descriptor = {
-        appId: constants.FILES_APP_EXTENSION_ID,
+        appId: LEGACY_FILES_EXTENSION_ID,
         taskType: 'file',
         actionId: 'view-in-browser'
       };
@@ -1317,7 +1318,7 @@
  * @const {!chrome.fileManagerPrivate.FileTaskDescriptor}
  */
 FileTasks.INSTALL_LINUX_PACKAGE_TASK_DESCRIPTOR = {
-  appId: constants.FILES_APP_EXTENSION_ID,
+  appId: LEGACY_FILES_EXTENSION_ID,
   taskType: 'app',
   actionId: 'install-linux-package'
 };
@@ -1327,7 +1328,7 @@
  * @const {!chrome.fileManagerPrivate.FileTaskDescriptor}
  */
 FileTasks.FILES_OPEN_ZIP_TASK_DESCRIPTOR = {
-  appId: constants.FILES_APP_EXTENSION_ID,
+  appId: LEGACY_FILES_EXTENSION_ID,
   taskType: 'app',
   actionId: 'open-zip'
 };
diff --git a/ui/file_manager/file_manager/foreground/js/file_tasks_unittest.m.js b/ui/file_manager/file_manager/foreground/js/file_tasks_unittest.m.js
index 92c5c90..0895631a 100644
--- a/ui/file_manager/file_manager/foreground/js/file_tasks_unittest.m.js
+++ b/ui/file_manager/file_manager/foreground/js/file_tasks_unittest.m.js
@@ -11,6 +11,7 @@
 import {MockFileEntry, MockFileSystem} from '../../common/js/mock_entry.js';
 import {ProgressItemState} from '../../common/js/progress_center_common.js';
 import {reportPromise} from '../../common/js/test_error_reporting.js';
+import {LEGACY_FILES_EXTENSION_ID} from '../../common/js/url_constants.js';
 import {util} from '../../common/js/util.js';
 import {VolumeManagerCommon} from '../../common/js/volume_manager_types.js';
 import {ProgressCenter} from '../../externs/background/progress_center.js';
@@ -593,7 +594,7 @@
   };
   const fileTask = {
     descriptor: {
-      appId: constants.FILES_APP_EXTENSION_ID,
+      appId: LEGACY_FILES_EXTENSION_ID,
       taskType: 'app',
       actionId: 'install-linux-package'
     },
@@ -650,7 +651,7 @@
             [
               {
                 descriptor: {
-                  appId: constants.FILES_APP_EXTENSION_ID,
+                  appId: LEGACY_FILES_EXTENSION_ID,
                   taskType: 'app',
                   actionId: 'import-crostini-image'
                 },
diff --git a/ui/file_manager/file_manager/manifest.json b/ui/file_manager/file_manager/manifest.json
index 7510528..29e53b2b 100644
--- a/ui/file_manager/file_manager/manifest.json
+++ b/ui/file_manager/file_manager/manifest.json
@@ -169,6 +169,7 @@
     "common/js/storage_adapter.js",
     "common/js/test_error_reporting.js",
     "common/js/trash.js",
+    "common/js/url_constants.js",
     "common/js/util.js",
     "common/js/volume_manager_types.js",
     "common/js/xfm.js",