Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2012 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. | |
| 5 | |
| 6 """Mock pref pane for testing purpose on Mac.""" | |
| 7 | |
| 8 import Foundation | |
| 9 import os | |
| 10 import signal | |
| 11 import subprocess | |
| 12 import sys | |
| 13 import tempfile | |
| 14 import time | |
| 15 | |
| 16 | |
| 17 class MockPrefPane(object): | |
| 18 """Mock Pref Pane to enable/disable/changepin without system prompt. | |
| 19 | |
| 20 This only applies to Mac. | |
| 21 """ | |
| 22 | |
| 23 def __init__(self): | |
| 24 self._service_name = 'org.chromium.chromoting' | |
| 25 self._real_user_id = os.getuid() | |
| 26 self._config_file = os.path.join(tempfile.gettempdir(), | |
| 27 '%s.json' % self._service_name) | |
| 28 self._tool_script = '/Library/PrivilegedHelperTools/%s.me2me.sh' % \ | |
| 29 self._service_name | |
| 30 | |
| 31 def _GetJobPid(self): | |
| 32 """Gets the org.chromium.chromoting job id.""" | |
| 33 process = subprocess.Popen(['launchctl', 'list'], stdout=subprocess.PIPE) | |
| 34 pid = None | |
| 35 for line in process.stdout: | |
| 36 # Format is: | |
| 37 # 12345 - my.job (if my.job is running, number is job's PID) | |
| 38 # - 0 my.other.job (if my.other.job is not running) | |
| 39 fields = line.strip().split('\t') | |
| 40 if fields[2] == self._service_name and fields[0] != "-": | |
| 41 pid = fields[0] | |
| 42 break | |
| 43 process.wait() | |
| 44 return pid | |
| 45 | |
| 46 def Enable(self): | |
| 47 """Handles what pref pane does for enabling connection.""" | |
| 48 # Elevate privileges, otherwise tool_script executes with EUID != 0. | |
| 49 os.setuid(0) | |
| 50 subprocess.call([self._tool_script, '--enable'], | |
| 51 stdin=open(self._config_file)) | |
| 52 | |
| 53 # Drop privileges, start the launchd job as the logged-in user. | |
| 54 os.setuid(self._real_user_id) | |
| 55 subprocess.call(['launchctl', 'start', self._service_name]) | |
| 56 | |
| 57 # Starting a launchd job is an asynchronous operation that typically takes | |
| 58 # a couple of seconds, so poll until the job has started. | |
| 59 for _ in range(1, 10): | |
| 60 if self._GetJobPid(): | |
| 61 print '*** org.chromium.chromoting is running ***' | |
| 62 break | |
| 63 time.sleep(2) | |
| 64 | |
| 65 def Disable(self): | |
| 66 """Handles what pref pane does for disabling connection.""" | |
| 67 # Elevate privileges, otherwise tool_script executes with EUID != 0. | |
| 68 os.setuid(0) | |
| 69 subprocess.call([self._tool_script, '--disable'], | |
| 70 stdin=open(self._config_file)) | |
| 71 | |
| 72 # Drop privileges, stop the launchd job as the logged-in user. | |
| 73 os.setuid(self._real_user_id) | |
| 74 subprocess.call(['launchctl', 'stop', self._service_name]) | |
| 75 | |
| 76 # Stopping a launchd job is an asynchronous operation that typically takes | |
| 77 # a couple of seconds, so poll until the job has stopped. | |
| 78 for _ in range(1, 10): | |
| 79 if not self._GetJobPid(): | |
| 80 print '*** org.chromium.chromoting is not running ***' | |
| 81 break | |
| 82 time.sleep(2) | |
| 83 | |
| 84 def ChangePin(self): | |
| 85 """Handles what pref pane does for changing pin.""" | |
| 86 # Elevate privileges, otherwise tool_script executes with EUID != 0. | |
| 87 os.setuid(0) | |
| 88 subprocess.call([self._tool_script, '--save-config'], | |
| 89 stdin=open(self._config_file)) | |
| 90 | |
| 91 # Drop privileges and send SIGHUP to org.chromium.chromoting | |
| 92 os.setuid(self._real_user_id) | |
| 93 os.kill(int(self._GetJobPid()), signal.SIGHUP) | |
| 94 | |
| 95 def NotifyWebapp(self): | |
| 96 """Notifies the web app that pref pane operation is done.""" | |
| 97 notif_center = Foundation.NSDistributedNotificationCenter.defaultCenter() | |
| 98 notif_center.postNotificationName_object_userInfo_( | |
| 99 self._service_name + '.update_succeeded', None, None) | |
| 100 | |
| 101 | |
| 102 def Main(): | |
| 103 """Handles the mock pref pane actions.""" | |
|
Nirnimesh
2012/08/14 22:09:30
assert sys.platform == 'darwin'
yihongg
2012/08/15 17:48:38
Done.
| |
| 104 print '*** Started mock pref pane ***' | |
| 105 print '*** EUID=%d, UID=%d ***' % (os.geteuid(), os.getuid()) | |
| 106 | |
| 107 pref_pane = MockPrefPane() | |
| 108 | |
| 109 if sys.argv[1] == 'enable': | |
| 110 pref_pane.Enable() | |
| 111 elif sys.argv[1] == 'disable': | |
| 112 pref_pane.Disable() | |
| 113 elif sys.argv[1] == 'changepin': | |
| 114 pref_pane.ChangePin() | |
| 115 else: | |
| 116 print >>sys.stderr, 'Invalid syntax' | |
| 117 return | |
| 118 | |
| 119 pref_pane.NotifyWebapp() | |
| 120 | |
| 121 | |
| 122 if __name__ == '__main__': | |
| 123 Main() | |
| 124 | |
| OLD | NEW |