Isolate ARC specific logic from Chrome

CQ-DEPEND=CL:*256462
TEST=test_that some tests
BUG=None

Change-Id: I40d16414adecf14e52b088c83a8aac6463843383
Reviewed-on: https://chromium-review.googlesource.com/340437
Commit-Ready: Victor Hsieh <victorhsieh@chromium.org>
Tested-by: Victor Hsieh <victorhsieh@chromium.org>
Reviewed-by: Achuith Bhandarkar <achuith@chromium.org>
diff --git a/client/common_lib/cros/chrome.py b/client/common_lib/cros/chrome.py
index 40040bc..da65814 100644
--- a/client/common_lib/cros/chrome.py
+++ b/client/common_lib/cros/chrome.py
@@ -13,29 +13,38 @@
 Error = exceptions.Error
 
 
-def is_arc_available():
+# Cached result of whether ARC is available on current device.
+_arc_available = None
+
+
+def _is_arc_available():
     """Returns true if ARC is available on current device."""
-    with open('/etc/lsb-release') as f:
-        for line in f:
-            if line.startswith('CHROMEOS_ARC_VERSION='):
-                return True
-    return False
+    global _arc_available
+    if _arc_available is not None:
+        return _arc_available
+
+    def _check_lsb_release():
+        lsb_release = '/etc/lsb-release'
+        if not os.path.exists(lsb_release):
+            return False
+        with open(lsb_release) as f:
+            for line in f:
+                if line.startswith('CHROMEOS_ARC_VERSION='):
+                    return True
+        return False
+
+    _arc_available = _check_lsb_release()
+    return _arc_available
+
+
+if _is_arc_available():
+    from autotest_lib.client.common_lib.cros import arc_util
 
 
 class Chrome(object):
     """Wrapper for creating a telemetry browser instance with extensions."""
 
 
-    # Chrome will start ARC instance and the script will block until ARC's boot
-    # completed event.
-    ARC_MODE_ENABLED = "enabled"
-    # Similar to "enabled", except that it will not block.
-    ARC_MODE_ENABLED_ASYNC = "enabled_async"
-    # Chrome will not start ARC instance.
-    ARC_MODE_DISABLED = "disabled"
-    # All available ARC options.
-    ARC_MODES = [ARC_MODE_ENABLED, ARC_MODE_ENABLED_ASYNC, ARC_MODE_DISABLED]
-
     BROWSER_TYPE_LOGIN = 'system'
     BROWSER_TYPE_GUEST = 'system-guest'
 
@@ -46,7 +55,7 @@
                  disable_gaia_services=True, disable_default_apps = True,
                  auto_login=True, gaia_login=False,
                  username=None, password=None, gaia_id=None,
-                 arc_mode=ARC_MODE_DISABLED):
+                 arc_mode=None):
         """
         Constructor of telemetry wrapper.
 
@@ -73,7 +82,8 @@
         @param username: Log in using this username instead of the default.
         @param password: Log in using this password instead of the default.
         @param gaia_id: Log in using this gaia_id instead of the default.
-        @param arc_mode: How ARC instance should be started.
+        @param arc_mode: How ARC instance should be started.  Default is to not
+                         start.
         """
         self._autotest_ext_path = None
         if autotest_ext:
@@ -82,12 +92,13 @@
             extension_paths.append(self._autotest_ext_path)
 
         finder_options = browser_options.BrowserFinderOptions()
-        if is_arc_available():
-            if arc_mode in [self.ARC_MODE_ENABLED, self.ARC_MODE_ENABLED_ASYNC]:
-                logging.debug('ARC is enabled in mode ' + arc_mode)
-                from autotest_lib.client.common_lib.cros import arc_util
-                extra_browser_args = arc_util.append_extra_args(extra_browser_args)
-                logged_in = True
+        if _is_arc_available() and arc_util.should_start_arc(arc_mode):
+            # TODO(achuith): Fix extra_browser_args, so that appending the
+            # following flags to it is simpler.
+            finder_options.browser_options.AppendExtraBrowserArgs(
+                    arc_util.get_extra_chrome_flags())
+            logged_in = True
+
         self._browser_type = (self.BROWSER_TYPE_LOGIN
                 if logged_in else self.BROWSER_TYPE_GUEST)
         finder_options.browser_type = self.browser_type
@@ -154,9 +165,8 @@
             try:
                 browser_to_create = browser_finder.FindBrowser(finder_options)
                 self._browser = browser_to_create.Create(finder_options)
-                if (is_arc_available() and arc_mode == self.ARC_MODE_ENABLED):
-                    from autotest_lib.client.common_lib.cros import arc_util
-                    arc_util.post_processing_after_browser()
+                if _is_arc_available():
+                    arc_util.post_processing_after_browser(arc_mode)
                 break
             except (exceptions.LoginException) as e:
                 logging.error('Timed out logging in, tries=%d, error=%s',
@@ -284,8 +294,7 @@
     def close(self):
         """Closes the browser."""
         try:
-            if is_arc_available():
-                from autotest_lib.client.common_lib.cros import arc_util
+            if _is_arc_available():
                 arc_util.pre_processing_before_close()
         finally:
             self._browser.Close()