Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / backends / chrome / cros_interface.py
index bb49ced..bad6022 100644 (file)
@@ -15,6 +15,12 @@ import tempfile
 # around pexpect, I suspect, if we wanted it to be faster. But, this was
 # convenient.
 
+# Some developers' workflow includes running the Chrome process from
+# /usr/local/... instead of the default location. We have to check for both
+# paths in order to support this workflow.
+_CHROME_PATHS = ['/opt/google/chrome/chrome ',
+                '/usr/local/opt/google/chrome/chrome ']
+
 def IsRunningOnCrosDevice():
   """Returns True if we're on a ChromeOS device."""
   lsb_release = '/etc/lsb-release'
@@ -287,6 +293,47 @@ class CrOSInterface(object):
     logging.debug("ListProcesses(<predicate>)->[%i processes]" % len(procs))
     return procs
 
+  def _GetSessionManagerPid(self, procs):
+    """Returns the pid of the session_manager process, given the list of
+    processes."""
+    for pid, process, _, _ in procs:
+      if process.startswith('/sbin/session_manager '):
+        return pid
+    return None
+
+  def GetChromeProcess(self):
+    """Locates the the main chrome browser process.
+
+    Chrome on cros is usually in /opt/google/chrome, but could be in
+    /usr/local/ for developer workflows - debug chrome is too large to fit on
+    rootfs.
+
+    Chrome spawns multiple processes for renderers. pids wrap around after they
+    are exhausted so looking for the smallest pid is not always correct. We
+    locate the session_manager's pid, and look for the chrome process that's an
+    immediate child. This is the main browser process.
+    """
+    procs = self.ListProcesses()
+    session_manager_pid = self._GetSessionManagerPid(procs)
+    if not session_manager_pid:
+      return None
+
+    # Find the chrome process that is the child of the session_manager.
+    for pid, process, ppid, _ in procs:
+      if ppid != session_manager_pid:
+        continue
+      for path in _CHROME_PATHS:
+        if process.startswith(path):
+          return {'pid': pid, 'path': path, 'args': process}
+    return None
+
+  def GetChromePid(self):
+    """Returns pid of main chrome browser process."""
+    result = self.GetChromeProcess()
+    if result and 'pid' in result:
+      return result['pid']
+    return None
+
   def RmRF(self, filename):
     logging.debug("rm -rf %s" % filename)
     self.RunCmdOnDevice(['rm', '-rf', filename], quiet=True)
@@ -357,11 +404,11 @@ class CrOSInterface(object):
     return self.RunCmdOnDevice(
         ['cryptohome-path', 'user', "'%s'" % user])[0].strip()
 
-  def IsCryptohomeMounted(self, username):
+  def IsCryptohomeMounted(self, username, is_guest):
     """Returns True iff |user|'s cryptohome is mounted."""
     profile_path = self.CryptohomePath(username)
     mount = self.FilesystemMountedAt(profile_path)
-    mount_prefix = 'guestfs' if username == '$guest' else '/home/.shadow/'
+    mount_prefix = 'guestfs' if is_guest else '/home/.shadow/'
     return mount and mount.startswith(mount_prefix)
 
   def TakeScreenShot(self, screenshot_prefix):
@@ -384,3 +431,15 @@ class CrOSInterface(object):
             screenshot_file])
         return
     logging.warning('screenshot directory full.')
+
+  def RestartUI(self, clear_enterprise_policy):
+    logging.info('(Re)starting the ui (logs the user out)')
+    if clear_enterprise_policy:
+      self.RunCmdOnDevice(['stop', 'ui'])
+      self.RmRF('/var/lib/whitelist/*')
+      self.RmRF('/home/chronos/Local\ State')
+
+    if self.IsServiceRunning('ui'):
+      self.RunCmdOnDevice(['restart', 'ui'])
+    else:
+      self.RunCmdOnDevice(['start', 'ui'])