2 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
11 class BrowserProcessBase(object):
13 def __init__(self, handle):
15 print 'PID', self.handle.pid
17 def GetReturnCode(self):
18 return self.handle.returncode
21 return self.handle.poll() is None
23 def Wait(self, wait_steps, sleep_time):
27 # Terminating the handle can raise an exception. There is likely no point
28 # in waiting if the termination didn't succeed.
32 # subprocess.wait() doesn't have a timeout, unfortunately.
33 while self.IsRunning() and i < wait_steps:
34 time.sleep(sleep_time)
39 print 'KILLING the browser'
42 # If it doesn't die, we hang. Oh well.
45 # If it is already dead, then it's ok.
46 # This may happen if the browser dies after the first poll, but
51 class BrowserProcess(BrowserProcessBase):
54 self.handle.terminate()
60 class BrowserProcessPosix(BrowserProcessBase):
61 """ This variant of BrowserProcess uses process groups to manage browser
65 os.killpg(self.handle.pid, signal.SIGTERM)
68 os.killpg(self.handle.pid, signal.SIGKILL)
71 def RunCommandWithSubprocess(cmd, env=None):
72 handle = subprocess.Popen(cmd, env=env)
73 return BrowserProcess(handle)
76 def RunCommandInProcessGroup(cmd, env=None):
79 print 'I\'M THE SESSION LEADER!'
80 handle = subprocess.Popen(cmd, env=env, preexec_fn=SetPGrp)
81 return BrowserProcessPosix(handle)