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.
5 """Mock pref pane for testing purpose on Mac."""
16 class MockPrefPane(object):
17 """Mock Pref Pane to enable/disable/changepin without system prompt.
19 This only applies to Mac.
23 self
._service
_name
= 'org.chromium.chromoting'
24 self
._real
_user
_id
= os
.getuid()
25 self
._config
_file
= os
.path
.join(tempfile
.gettempdir(),
26 '%s.json' % self
._service
_name
)
27 self
._tool
_script
= '/Library/PrivilegedHelperTools/%s.me2me.sh' % \
31 """Gets the org.chromium.chromoting job id."""
32 process
= subprocess
.Popen(['launchctl', 'list'], stdout
=subprocess
.PIPE
)
34 for line
in process
.stdout
:
36 # 12345 - my.job (if my.job is running, number is job's PID)
37 # - 0 my.other.job (if my.other.job is not running)
38 fields
= line
.strip().split('\t')
39 if fields
[2] == self
._service
_name
and fields
[0] != "-":
46 """Handles what pref pane does for enabling connection."""
47 # Elevate privileges, otherwise tool_script executes with EUID != 0.
49 subprocess
.call([self
._tool
_script
, '--enable'],
50 stdin
=open(self
._config
_file
))
52 # Drop privileges, start the launchd job as the logged-in user.
53 os
.setuid(self
._real
_user
_id
)
54 subprocess
.call(['launchctl', 'start', self
._service
_name
])
56 # Starting a launchd job is an asynchronous operation that typically takes
57 # a couple of seconds, so poll until the job has started.
58 for _
in range(1, 10):
60 print '*** org.chromium.chromoting is running ***'
65 """Handles what pref pane does for disabling connection."""
66 # Elevate privileges, otherwise tool_script executes with EUID != 0.
68 subprocess
.call([self
._tool
_script
, '--disable'],
69 stdin
=open(self
._config
_file
))
71 # Drop privileges, stop the launchd job as the logged-in user.
72 os
.setuid(self
._real
_user
_id
)
73 subprocess
.call(['launchctl', 'stop', self
._service
_name
])
75 # Stopping a launchd job is an asynchronous operation that typically takes
76 # a couple of seconds, so poll until the job has stopped.
77 for _
in range(1, 10):
78 if not self
._GetJobPid
():
79 print '*** org.chromium.chromoting is not running ***'
84 """Handles what pref pane does for changing pin."""
85 # Elevate privileges, otherwise tool_script executes with EUID != 0.
87 subprocess
.call([self
._tool
_script
, '--save-config'],
88 stdin
=open(self
._config
_file
))
90 # Drop privileges and send SIGHUP to org.chromium.chromoting
91 os
.setuid(self
._real
_user
_id
)
92 os
.kill(int(self
._GetJobPid
()), signal
.SIGHUP
)
94 def NotifyWebapp(self
):
95 """Notifies the web app that pref pane operation is done."""
96 notif_center
= Foundation
.NSDistributedNotificationCenter
.defaultCenter()
97 notif_center
.postNotificationName_object_userInfo_(
98 self
._service
_name
+ '.update_succeeded', None, None)
102 """Handles the mock pref pane actions."""
103 assert sys
.platform
.startswith('darwin')
105 print '*** Started mock pref pane ***'
106 print '*** EUID=%d, UID=%d ***' % (os
.geteuid(), os
.getuid())
108 pref_pane
= MockPrefPane()
110 if sys
.argv
[1] == 'enable':
112 elif sys
.argv
[1] == 'disable':
114 elif sys
.argv
[1] == 'changepin':
115 pref_pane
.ChangePin()
117 print >>sys
.stderr
, 'Invalid syntax'
120 pref_pane
.NotifyWebapp()
123 if __name__
== '__main__':