OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/service_process_util_posix.h" | 5 #include "chrome/common/service_process_util_posix.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 #include <launch.h> | 8 #include <launch.h> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/file_util.h" |
12 #include "base/mac/foundation_util.h" | 13 #include "base/mac/foundation_util.h" |
13 #include "base/mac/mac_util.h" | 14 #include "base/mac/mac_util.h" |
14 #include "base/mac/scoped_nsautorelease_pool.h" | 15 #include "base/mac/scoped_nsautorelease_pool.h" |
15 #include "base/path_service.h" | 16 #include "base/path_service.h" |
16 #include "base/scoped_nsobject.h" | 17 #include "base/scoped_nsobject.h" |
17 #include "base/string_util.h" | 18 #include "base/string_util.h" |
18 #include "base/sys_string_conversions.h" | 19 #include "base/sys_string_conversions.h" |
19 #include "base/threading/thread_restrictions.h" | 20 #include "base/threading/thread_restrictions.h" |
20 #include "base/version.h" | 21 #include "base/version.h" |
21 #include "chrome/common/child_process_host.h" | 22 #include "chrome/common/child_process_host.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 error:&err]) { | 75 error:&err]) { |
75 LOG(ERROR) << "GetUserAgentPath: " << err; | 76 LOG(ERROR) << "GetUserAgentPath: " << err; |
76 } | 77 } |
77 | 78 |
78 NSString* plist_file_path = | 79 NSString* plist_file_path = |
79 [launch_agents_path | 80 [launch_agents_path |
80 stringByAppendingPathComponent:GetServiceProcessLaunchDFileName()]; | 81 stringByAppendingPathComponent:GetServiceProcessLaunchDFileName()]; |
81 return [NSURL fileURLWithPath:plist_file_path isDirectory:NO]; | 82 return [NSURL fileURLWithPath:plist_file_path isDirectory:NO]; |
82 } | 83 } |
83 | 84 |
84 } | 85 class ExecPathComponentWatcherDelegate |
| 86 : public base::FilePathComponentWatcher::Delegate { |
| 87 public: |
| 88 ExecPathComponentWatcherDelegate(Task* shutdown_task) |
| 89 : shutdown_task_(shutdown_task) { } |
| 90 virtual ~ExecPathComponentWatcherDelegate(); |
| 91 virtual void OnFilePathComponentsChanged(const FilePath& old_path, |
| 92 const FilePath& new_path) OVERRIDE; |
| 93 |
| 94 private: |
| 95 Task* shutdown_task_; |
| 96 }; |
| 97 |
| 98 } // namespace |
85 | 99 |
86 // Gets the name of the service process IPC channel. | 100 // Gets the name of the service process IPC channel. |
87 IPC::ChannelHandle GetServiceProcessChannel() { | 101 IPC::ChannelHandle GetServiceProcessChannel() { |
88 std::string socket_path; | 102 std::string socket_path; |
89 scoped_nsobject<NSDictionary> dictionary( | 103 scoped_nsobject<NSDictionary> dictionary( |
90 base::mac::CFToNSCast(GTMCopyLaunchdExports())); | 104 base::mac::CFToNSCast(GTMCopyLaunchdExports())); |
91 NSString *ns_socket_path = | 105 NSString *ns_socket_path = |
92 [dictionary objectForKey:GetServiceProcessLaunchDSocketEnvVar()]; | 106 [dictionary objectForKey:GetServiceProcessLaunchDSocketEnvVar()]; |
93 if (ns_socket_path) { | 107 if (ns_socket_path) { |
94 socket_path = base::SysNSStringToUTF8(ns_socket_path); | 108 socket_path = base::SysNSStringToUTF8(ns_socket_path); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 if (!InitializeState()) { | 173 if (!InitializeState()) { |
160 return false; | 174 return false; |
161 } | 175 } |
162 CFErrorRef err = NULL; | 176 CFErrorRef err = NULL; |
163 state_->launchd_conf_.reset(GTMSMJobCheckIn(&err)); | 177 state_->launchd_conf_.reset(GTMSMJobCheckIn(&err)); |
164 if (!state_->launchd_conf_.get()) { | 178 if (!state_->launchd_conf_.get()) { |
165 LOG(ERROR) << "InitializePlatformState: " << err; | 179 LOG(ERROR) << "InitializePlatformState: " << err; |
166 CFRelease(err); | 180 CFRelease(err); |
167 return false; | 181 return false; |
168 } | 182 } |
| 183 |
| 184 |
| 185 return true; |
| 186 } |
| 187 |
| 188 bool ServiceProcessState::SignalReadyPlatformSpecific(Task* shutdown_task) { |
| 189 NSDictionary *ns_launchd_conf = base::mac::CFToNSCast(state_->launchd_conf_); |
| 190 NSString *exe_path = [ns_launchd_conf objectForKey:@ LAUNCH_JOBKEY_PROGRAM]; |
| 191 if (!exe_path) { |
| 192 return false; |
| 193 } |
| 194 |
| 195 FilePath executable_path = FilePath([exe_path fileSystemRepresentation]); |
| 196 if (!state_->executable_watcher_.Watch( |
| 197 executable_path, new ExecPathComponentWatcherDelegate(shutdown_task))) { |
| 198 return false; |
| 199 } |
169 return true; | 200 return true; |
170 } | 201 } |
171 | 202 |
172 IPC::ChannelHandle ServiceProcessState::GetServiceProcessChannel() { | 203 IPC::ChannelHandle ServiceProcessState::GetServiceProcessChannel() { |
173 CHECK(state_); | 204 CHECK(state_); |
174 NSDictionary *ns_launchd_conf = base::mac::CFToNSCast(state_->launchd_conf_); | 205 NSDictionary *ns_launchd_conf = base::mac::CFToNSCast(state_->launchd_conf_); |
175 NSDictionary* socket_dict = | 206 NSDictionary* socket_dict = |
176 [ns_launchd_conf objectForKey:@ LAUNCH_JOBKEY_SOCKETS]; | 207 [ns_launchd_conf objectForKey:@ LAUNCH_JOBKEY_SOCKETS]; |
177 NSArray* sockets = | 208 NSArray* sockets = |
178 [socket_dict objectForKey:GetServiceProcessLaunchDSocketKey()]; | 209 [socket_dict objectForKey:GetServiceProcessLaunchDSocketKey()]; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 | 326 |
296 base::mac::ScopedNSAutoreleasePool pool; | 327 base::mac::ScopedNSAutoreleasePool pool; |
297 NSURL* plist_url = GetUserAgentPath(); | 328 NSURL* plist_url = GetUserAgentPath(); |
298 SInt32 error = 0; | 329 SInt32 error = 0; |
299 if (!CFURLDestroyResource(reinterpret_cast<CFURLRef>(plist_url), &error)) { | 330 if (!CFURLDestroyResource(reinterpret_cast<CFURLRef>(plist_url), &error)) { |
300 LOG(ERROR) << "RemoveFromAutoRun: " << error; | 331 LOG(ERROR) << "RemoveFromAutoRun: " << error; |
301 return false; | 332 return false; |
302 } | 333 } |
303 return true; | 334 return true; |
304 } | 335 } |
| 336 |
| 337 ExecPathComponentWatcherDelegate::~ExecPathComponentWatcherDelegate() { } |
| 338 void ExecPathComponentWatcherDelegate::OnFilePathComponentsChanged( |
| 339 const FilePath& old_path, const FilePath& new_path) { |
| 340 bool needs_shutdown = false; |
| 341 bool needs_plist_rewrite = false; |
| 342 bool needs_plist_deletion = false; |
| 343 |
| 344 if (access(new_path.value().c_str(), W_OK) != 0) { |
| 345 // Can we execute it? |
| 346 needs_shutdown = true; |
| 347 needs_plist_deletion = true; |
| 348 } else if (file_util::IsInTrash(new_path)) { |
| 349 // Is it in the trash? |
| 350 needs_shutdown = true; |
| 351 needs_plist_deletion = true; |
| 352 } else if (!file_util::AreReferringToSameObject(old_path, new_path)) { |
| 353 // Did it get moved? |
| 354 needs_plist_rewrite = true; |
| 355 } |
| 356 |
| 357 if (needs_shutdown) { |
| 358 MessageLoop::current()->PostTask(FROM_HERE, shutdown_task_); |
| 359 } |
| 360 } |
| 361 |
OLD | NEW |