Open SWA version if it fails to uninstall itself on recent CrOS image

There is a known issue that might prevent CCA from unsintalling itself
on a recent Chrome OS image due to the enterprise policy settings.
Therefore, if it fails to uninstall itself and try to load the Chrome
App version, open the SWA version instead.

BUG=b:178837261
TEST=None

Change-Id: I8bb5cbfb8d0bd084759b060090daa12c22bb2fd2
Reviewed-on: https://chromium-review.googlesource.com/c/apps/camera/+/2687299
Tested-by: Wei Lee <wtlee@chromium.org>
Reviewed-by: Shik Chen <shik@chromium.org>
diff --git a/src/js/background.js b/src/js/background.js
index 39f3c5c..586d054 100644
--- a/src/js/background.js
+++ b/src/js/background.js
@@ -104,6 +104,10 @@
 
   const chromeVersion = /Chrome\/([0-9.]+)/.exec(navigator.userAgent)[1];
   if (isNoOlderThan(chromeVersion, '88.0.4324.22')) {
-    chrome.management.uninstallSelf();
+    try {
+      chrome.management.uninstallSelf();
+    } catch (e) {
+      console.error("Failed to uninstall legacy CCA: ", e);
+    }
   }
 });
diff --git a/src/js/main.js b/src/js/main.js
index 020fd32..7fb75fc 100644
--- a/src/js/main.js
+++ b/src/js/main.js
@@ -258,11 +258,50 @@
  * Creates the Camera object and starts screen capturing.
  */
 document.addEventListener('DOMContentLoaded', () => {
-  var appWindow = chrome.app.window.current();
-  if (!cca.App.instance_) {
-    var inner = appWindow.innerBounds;
-    cca.App.instance_ = new cca.App(inner.width / inner.height);
-  }
-  cca.App.instance_.start();
-  appWindow.show();
+  // Check the Chrome version and platform. If it is on recent Chrome OS, it is
+  // unexpected to load the Chrome app version of the camera app. Open SWA
+  // version instead.
+  const loadChromeAppVersion = () => {
+    var appWindow = chrome.app.window.current();
+    if (!cca.App.instance_) {
+      var inner = appWindow.innerBounds;
+      cca.App.instance_ = new cca.App(inner.width / inner.height);
+    }
+    cca.App.instance_.start();
+    appWindow.show();
+  };
+
+  chrome.runtime.getPlatformInfo(function(info) {
+    if (info.os !== 'cros') {
+      loadChromeAppVersion();
+      return;
+    }
+
+    const isNoOlderThan = (srcVer, targetVer) => {
+      const srcVers = srcVer.split('.');
+      const targetVers = targetVer.split('.');
+      if (srcVers.length !== targetVers.length) {
+        // Inavlid input versions.
+        return false;
+      }
+
+      for (let i = 0; i < srcVers.length; i++) {
+        const src = parseInt(srcVers[i]);
+        const target = parseInt(targetVers[i]);
+        if (src !== target) {
+          return src > target;
+        }
+      }
+      return true;
+    }
+
+    const chromeVersion = /Chrome\/([0-9.]+)/.exec(navigator.userAgent)[1];
+    if (isNoOlderThan(chromeVersion, '88.0.4324.22')) {
+      // Load the SWA version of the camera app instead.
+      chrome.browser.openTab({url: 'chrome://camera-app/views/main.html'});
+      window.close();
+    } else {
+      loadChromeAppVersion();
+    }
+  });
 });
diff --git a/src/manifest.json b/src/manifest.json
index 687e933..2024a80 100644
--- a/src/manifest.json
+++ b/src/manifest.json
@@ -2,7 +2,7 @@
   "manifest_version": 2,
   "name": "__MSG_name__",
   "description": "__MSG_description__",
-  "version": "5.3.7",
+  "version": "5.3.8",
   "default_locale": "en",
   "minimum_chrome_version": "60.0.0.0",
   "icons": {
@@ -10,6 +10,7 @@
     "128": "images/camera_app_icons_128.png"
   },
   "permissions": [
+    "browser",
     "videoCapture",
     "audioCapture",
     "storage",