Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Mock pref pane for testing purpose.""" | |
| 6 | |
| 7 import Foundation | |
| 8 import os | |
| 9 import signal | |
| 10 import subprocess | |
| 11 import sys | |
| 12 import tempfile | |
| 13 import time | |
| 14 | |
| 15 | |
| 16 _SERVICE_NAME = "org.chromium.chromoting" | |
|
Nirnimesh
2012/08/06 19:13:53
use ' instead of " for consistency here and everyw
yihongg
2012/08/08 01:04:48
Done.
| |
| 17 | |
| 18 | |
| 19 def _GetJobPid(): | |
| 20 """Gets the org.chromium.chromoting job id.""" | |
| 21 process = subprocess.Popen(["launchctl", "list"], stdout=subprocess.PIPE) | |
| 22 pid = None | |
| 23 for line in process.stdout: | |
| 24 # Format is: | |
| 25 # 12345 - my.job (if my.job is running, number is job's PID) | |
| 26 # - 0 my.other.job (if my.other.job is not running) | |
| 27 fields = line.strip().split('\t') | |
| 28 if fields[2] == _SERVICE_NAME and fields[0] != "-": | |
| 29 pid = fields[0] | |
| 30 break | |
| 31 process.wait() | |
| 32 return pid | |
| 33 | |
| 34 | |
| 35 def main(): | |
| 36 """Handles the mock pref pane actions.""" | |
| 37 print "*** Started mock pref pane ***" | |
| 38 print "*** EUID=%d, UID=%d ***" % (os.geteuid(), os.getuid()) | |
| 39 | |
| 40 real_user_id = os.getuid() | |
| 41 config_file = os.path.join(tempfile.gettempdir(), "%s.json" % _SERVICE_NAME) | |
| 42 tool_script = "/Library/PrivilegedHelperTools/%s.me2me.sh" % _SERVICE_NAME | |
|
Nirnimesh
2012/08/06 19:13:53
This is for mac only. what about other platforms?
yihongg
2012/08/08 01:04:48
This is mac only.
| |
| 43 | |
| 44 if sys.argv[1] == 'enable': | |
| 45 # Elevate privileges, otherwise tool_script executes with EUID != 0. | |
| 46 os.setuid(0) | |
| 47 subprocess.call([tool_script, "--enable"], stdin=open(config_file)) | |
| 48 | |
| 49 # Drop privileges, start the launchd job as the logged-in user. | |
| 50 os.setuid(real_user_id) | |
| 51 subprocess.call(["launchctl", "start", _SERVICE_NAME]) | |
| 52 | |
| 53 # Starting a launchd job is an asynchronous operation that typically takes | |
| 54 # a couple of seconds, so poll until the job has started. | |
| 55 for i in range(1, 10): | |
| 56 if _GetJobPid(): | |
| 57 print "*** org.chromium.chromoting is running ***" | |
| 58 break | |
| 59 time.sleep(2) | |
| 60 elif sys.argv[1] == 'disable': | |
| 61 # Elevate privileges, otherwise tool_script executes with EUID != 0. | |
| 62 os.setuid(0) | |
| 63 subprocess.call([tool_script, "--disable"], stdin=open(config_file)) | |
| 64 | |
| 65 # Drop privileges, stop the launchd job as the logged-in user. | |
| 66 os.setuid(real_user_id) | |
| 67 subprocess.call(["launchctl", "stop", _SERVICE_NAME]) | |
| 68 | |
| 69 # Stopping a launchd job is an asynchronous operation that typically takes | |
| 70 # a couple of seconds, so poll until the job has stopped. | |
| 71 for i in range(1, 10): | |
|
Nirnimesh
2012/08/06 19:13:53
i -> _
since i is unused
yihongg
2012/08/08 01:04:48
Done.
| |
| 72 if not _GetJobPid(): | |
| 73 print "*** org.chromium.chromoting is not running ***" | |
| 74 break | |
| 75 time.sleep(2) | |
|
Nirnimesh
2012/08/06 19:13:53
Sleeping is not allowed, since it makes tests flak
yihongg
2012/08/08 01:04:48
Agree sleep will cause flaky test in general. For
| |
| 76 elif sys.argv[1] == 'changepin': | |
| 77 # Elevate privileges, otherwise tool_script executes with EUID != 0. | |
| 78 os.setuid(0) | |
| 79 subprocess.call([tool_script, "--save-config"], stdin=open(config_file)) | |
| 80 | |
| 81 # Drop privileges and send SIGHUP to org.chromium.chromoting | |
| 82 os.setuid(real_user_id) | |
| 83 os.kill(int(_GetJobPid()), signal.SIGHUP) | |
| 84 else: | |
| 85 print >> sys.stderr, ( | |
| 86 'Invalid syntax') | |
| 87 return 1 | |
| 88 | |
| 89 notif_center = Foundation.NSDistributedNotificationCenter.defaultCenter() | |
| 90 notif_center.postNotificationName_object_userInfo_( | |
| 91 _SERVICE_NAME + ".update_succeeded", None, None) | |
| 92 | |
| 93 time.sleep(10) | |
|
Nirnimesh
2012/08/06 19:13:53
what is this sleep for?
yihongg
2012/08/08 01:04:48
This is put here to wait until webapp gets notific
| |
| 94 | |
| 95 return 0 | |
|
Nirnimesh
2012/08/06 19:13:53
remove
yihongg
2012/08/08 01:04:48
Done.
| |
| 96 | |
| 97 if __name__ == '__main__': | |
| 98 sys.exit(main()) | |
| 99 | |
| OLD | NEW |