| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <CoreFoundation/CoreFoundation.h> | 5 #include <CoreFoundation/CoreFoundation.h> |
| 6 | 6 |
| 7 #include "remoting/host/setup/daemon_controller_delegate_mac.h" | 7 #include "remoting/host/setup/daemon_controller_delegate_mac.h" |
| 8 | 8 |
| 9 #include <launch.h> | 9 #include <launch.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| 11 #include <sys/types.h> | 11 #include <sys/types.h> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/callback_helpers.h" | 15 #include "base/callback_helpers.h" |
| 16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 18 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/mac/foundation_util.h" | 20 #include "base/mac/foundation_util.h" |
| 21 #include "base/mac/launchd.h" | 21 #include "base/mac/launchd.h" |
| 22 #include "base/mac/mac_logging.h" | 22 #include "base/mac/mac_logging.h" |
| 23 #include "base/mac/mac_util.h" | 23 #include "base/mac/mac_util.h" |
| 24 #include "base/mac/scoped_launch_data.h" | 24 #include "base/mac/scoped_launch_data.h" |
| 25 #include "base/time/time.h" | 25 #include "base/time/time.h" |
| 26 #include "base/tracked_objects.h" |
| 26 #include "base/values.h" | 27 #include "base/values.h" |
| 27 #include "remoting/host/constants_mac.h" | 28 #include "remoting/host/constants_mac.h" |
| 28 #include "remoting/host/host_config.h" | 29 #include "remoting/host/host_config.h" |
| 29 #include "remoting/host/usage_stats_consent.h" | 30 #include "remoting/host/usage_stats_consent.h" |
| 30 | 31 |
| 31 namespace remoting { | 32 namespace remoting { |
| 32 | 33 |
| 33 DaemonControllerDelegateMac::DaemonControllerDelegateMac() { | 34 DaemonControllerDelegateMac::DaemonControllerDelegateMac() { |
| 34 } | 35 } |
| 35 | 36 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 61 if (host_config->GetString(kHostIdConfigPath, &value)) | 62 if (host_config->GetString(kHostIdConfigPath, &value)) |
| 62 config->SetString(kHostIdConfigPath, value); | 63 config->SetString(kHostIdConfigPath, value); |
| 63 if (host_config->GetString(kXmppLoginConfigPath, &value)) | 64 if (host_config->GetString(kXmppLoginConfigPath, &value)) |
| 64 config->SetString(kXmppLoginConfigPath, value); | 65 config->SetString(kXmppLoginConfigPath, value); |
| 65 return config.Pass(); | 66 return config.Pass(); |
| 66 } | 67 } |
| 67 | 68 |
| 68 void DaemonControllerDelegateMac::SetConfigAndStart( | 69 void DaemonControllerDelegateMac::SetConfigAndStart( |
| 69 scoped_ptr<base::DictionaryValue> config, | 70 scoped_ptr<base::DictionaryValue> config, |
| 70 bool consent, | 71 bool consent, |
| 71 const DaemonController::CompletionCallback& done) { | 72 const base::Closure& on_done, |
| 73 const DaemonController::ErrorCallback& on_error) { |
| 72 config->SetBoolean(kUsageStatsConsentConfigPath, consent); | 74 config->SetBoolean(kUsageStatsConsentConfigPath, consent); |
| 73 ShowPreferencePane(HostConfigToJson(*config), done); | 75 ShowPreferencePane(HostConfigToJson(*config), on_done, on_error); |
| 74 } | 76 } |
| 75 | 77 |
| 76 void DaemonControllerDelegateMac::UpdateConfig( | 78 void DaemonControllerDelegateMac::UpdateConfig( |
| 77 scoped_ptr<base::DictionaryValue> config, | 79 scoped_ptr<base::DictionaryValue> config, |
| 78 const DaemonController::CompletionCallback& done) { | 80 const base::Closure& on_done, |
| 81 const DaemonController::ErrorCallback& on_error) { |
| 79 base::FilePath config_file_path(kHostConfigFilePath); | 82 base::FilePath config_file_path(kHostConfigFilePath); |
| 80 scoped_ptr<base::DictionaryValue> host_config( | 83 scoped_ptr<base::DictionaryValue> host_config( |
| 81 HostConfigFromJsonFile(config_file_path)); | 84 HostConfigFromJsonFile(config_file_path)); |
| 82 if (!host_config) { | 85 if (!host_config) { |
| 83 done.Run(DaemonController::RESULT_FAILED); | 86 std::ostringstream error_message; |
| 87 error_message << "Reading config from " << config_file_path.value() |
| 88 << " failed"; |
| 89 LOG(ERROR) << error_message; |
| 90 on_error.Run(error_message.str(), FROM_HERE); |
| 84 return; | 91 return; |
| 85 } | 92 } |
| 86 | 93 |
| 87 host_config->MergeDictionary(config.get()); | 94 host_config->MergeDictionary(config.get()); |
| 88 ShowPreferencePane(HostConfigToJson(*host_config), done); | 95 ShowPreferencePane(HostConfigToJson(*host_config), on_done, on_error); |
| 89 } | 96 } |
| 90 | 97 |
| 91 void DaemonControllerDelegateMac::Stop( | 98 void DaemonControllerDelegateMac::Stop( |
| 92 const DaemonController::CompletionCallback& done) { | 99 const base::Closure& on_done, |
| 93 ShowPreferencePane("", done); | 100 const DaemonController::ErrorCallback& on_error) { |
| 101 ShowPreferencePane("", on_done, on_error); |
| 94 } | 102 } |
| 95 | 103 |
| 96 DaemonController::UsageStatsConsent | 104 DaemonController::UsageStatsConsent |
| 97 DaemonControllerDelegateMac::GetUsageStatsConsent() { | 105 DaemonControllerDelegateMac::GetUsageStatsConsent() { |
| 98 DaemonController::UsageStatsConsent consent; | 106 DaemonController::UsageStatsConsent consent; |
| 99 consent.supported = true; | 107 consent.supported = true; |
| 100 consent.allowed = false; | 108 consent.allowed = false; |
| 101 // set_by_policy is not yet supported. | 109 // set_by_policy is not yet supported. |
| 102 consent.set_by_policy = false; | 110 consent.set_by_policy = false; |
| 103 | 111 |
| 104 base::FilePath config_file_path(kHostConfigFilePath); | 112 base::FilePath config_file_path(kHostConfigFilePath); |
| 105 scoped_ptr<base::DictionaryValue> host_config( | 113 scoped_ptr<base::DictionaryValue> host_config( |
| 106 HostConfigFromJsonFile(config_file_path)); | 114 HostConfigFromJsonFile(config_file_path)); |
| 107 if (host_config) { | 115 if (host_config) { |
| 108 host_config->GetBoolean(kUsageStatsConsentConfigPath, &consent.allowed); | 116 host_config->GetBoolean(kUsageStatsConsentConfigPath, &consent.allowed); |
| 109 } | 117 } |
| 110 | 118 |
| 111 return consent; | 119 return consent; |
| 112 } | 120 } |
| 113 | 121 |
| 114 void DaemonControllerDelegateMac::ShowPreferencePane( | 122 void DaemonControllerDelegateMac::ShowPreferencePane( |
| 115 const std::string& config_data, | 123 const std::string& config_data, |
| 116 const DaemonController::CompletionCallback& done) { | 124 const base::Closure& on_done, |
| 117 if (DoShowPreferencePane(config_data)) { | 125 const DaemonController::ErrorCallback& on_error) { |
| 118 RegisterForPreferencePaneNotifications(done); | 126 if (DoShowPreferencePane(config_data, on_error)) { |
| 119 } else { | 127 RegisterForPreferencePaneNotifications(on_done, on_error); |
| 120 done.Run(DaemonController::RESULT_FAILED); | |
| 121 } | 128 } |
| 122 } | 129 } |
| 123 | 130 |
| 124 // CFNotificationCenterAddObserver ties the thread on which distributed | 131 // CFNotificationCenterAddObserver ties the thread on which distributed |
| 125 // notifications are received to the one on which it is first called. | 132 // notifications are received to the one on which it is first called. |
| 126 // This is safe because HostNPScriptObject::InvokeAsyncResultCallback | 133 // This is safe because HostNPScriptObject::InvokeAsyncResultCallback |
| 127 // bounces the invocation to the correct thread, so it doesn't matter | 134 // bounces the invocation to the correct thread, so it doesn't matter |
| 128 // which thread CompletionCallbacks are called on. | 135 // which thread CompletionCallbacks are called on. |
| 129 void DaemonControllerDelegateMac::RegisterForPreferencePaneNotifications( | 136 void DaemonControllerDelegateMac::RegisterForPreferencePaneNotifications( |
| 130 const DaemonController::CompletionCallback& done) { | 137 const base::Closure& on_done, |
| 138 const DaemonController::ErrorCallback& on_error) { |
| 131 // We can only have one callback registered at a time. This is enforced by the | 139 // We can only have one callback registered at a time. This is enforced by the |
| 132 // UX flow of the web-app. | 140 // UX flow of the web-app. |
| 133 DCHECK(current_callback_.is_null()); | 141 DCHECK(on_done_.is_null()); |
| 134 current_callback_ = done; | 142 DCHECK(on_error_.is_null()); |
| 143 on_done_ = on_done; |
| 144 on_error_ = on_error; |
| 135 | 145 |
| 136 CFNotificationCenterAddObserver( | 146 CFNotificationCenterAddObserver( |
| 137 CFNotificationCenterGetDistributedCenter(), | 147 CFNotificationCenterGetDistributedCenter(), |
| 138 this, | 148 this, |
| 139 &DaemonControllerDelegateMac::PreferencePaneCallback, | 149 &DaemonControllerDelegateMac::PreferencePaneCallback, |
| 140 CFSTR(UPDATE_SUCCEEDED_NOTIFICATION_NAME), | 150 CFSTR(UPDATE_SUCCEEDED_NOTIFICATION_NAME), |
| 141 nullptr, | 151 nullptr, |
| 142 CFNotificationSuspensionBehaviorDeliverImmediately); | 152 CFNotificationSuspensionBehaviorDeliverImmediately); |
| 143 CFNotificationCenterAddObserver( | 153 CFNotificationCenterAddObserver( |
| 144 CFNotificationCenterGetDistributedCenter(), | 154 CFNotificationCenterGetDistributedCenter(), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 157 nullptr); | 167 nullptr); |
| 158 CFNotificationCenterRemoveObserver( | 168 CFNotificationCenterRemoveObserver( |
| 159 CFNotificationCenterGetDistributedCenter(), | 169 CFNotificationCenterGetDistributedCenter(), |
| 160 this, | 170 this, |
| 161 CFSTR(UPDATE_FAILED_NOTIFICATION_NAME), | 171 CFSTR(UPDATE_FAILED_NOTIFICATION_NAME), |
| 162 nullptr); | 172 nullptr); |
| 163 } | 173 } |
| 164 | 174 |
| 165 void DaemonControllerDelegateMac::PreferencePaneCallbackDelegate( | 175 void DaemonControllerDelegateMac::PreferencePaneCallbackDelegate( |
| 166 CFStringRef name) { | 176 CFStringRef name) { |
| 167 DaemonController::AsyncResult result = DaemonController::RESULT_FAILED; | 177 bool success = |
| 168 if (CFStringCompare(name, CFSTR(UPDATE_SUCCEEDED_NOTIFICATION_NAME), 0) == | 178 CFStringCompare(name, CFSTR(UPDATE_SUCCEEDED_NOTIFICATION_NAME), 0) == |
| 169 kCFCompareEqualTo) { | 179 kCFCompareEqualTo; |
| 170 result = DaemonController::RESULT_OK; | 180 bool failure = |
| 171 } else if (CFStringCompare(name, CFSTR(UPDATE_FAILED_NOTIFICATION_NAME), 0) == | 181 CFStringCompare(name, CFSTR(UPDATE_FAILED_NOTIFICATION_NAME), 0) == |
| 172 kCFCompareEqualTo) { | 182 kCFCompareEqualTo; |
| 173 result = DaemonController::RESULT_FAILED; | 183 if (success) { |
| 184 on_error_.Reset(); |
| 185 base::ResetAndReturn(&on_done_).Run(); |
| 186 } else if (failure) { |
| 187 on_done_.Reset(); |
| 188 std::string error_message = |
| 189 "Received failure notification from Preference Pane"; |
| 190 LOG(ERROR) << error_message; |
| 191 base::ResetAndReturn(&on_error_).Run(error_message, FROM_HERE); |
| 174 } else { | 192 } else { |
| 175 LOG(WARNING) << "Ignoring unexpected notification: " << name; | 193 LOG(WARNING) << "Ignoring unexpected notification: " << name; |
| 176 return; | 194 return; |
| 177 } | 195 } |
| 178 | 196 |
| 179 DeregisterForPreferencePaneNotifications(); | 197 DeregisterForPreferencePaneNotifications(); |
| 180 | |
| 181 DCHECK(!current_callback_.is_null()); | |
| 182 base::ResetAndReturn(¤t_callback_).Run(result); | |
| 183 } | 198 } |
| 184 | 199 |
| 185 // static | 200 // static |
| 186 bool DaemonControllerDelegateMac::DoShowPreferencePane( | 201 bool DaemonControllerDelegateMac::DoShowPreferencePane( |
| 187 const std::string& config_data) { | 202 const std::string& config_data, |
| 203 const DaemonController::ErrorCallback& on_error) { |
| 188 if (!config_data.empty()) { | 204 if (!config_data.empty()) { |
| 189 base::FilePath config_path; | 205 base::FilePath config_path; |
| 190 if (!base::GetTempDir(&config_path)) { | 206 if (!base::GetTempDir(&config_path)) { |
| 191 LOG(ERROR) << "Failed to get filename for saving configuration data."; | 207 std::string error_message = |
| 208 "Failed to get filename for saving configuration data."; |
| 209 LOG(ERROR) << error_message; |
| 210 on_error.Run(error_message, FROM_HERE); |
| 192 return false; | 211 return false; |
| 193 } | 212 } |
| 213 |
| 194 config_path = config_path.Append(kHostConfigFileName); | 214 config_path = config_path.Append(kHostConfigFileName); |
| 195 | |
| 196 int written = base::WriteFile(config_path, config_data.data(), | 215 int written = base::WriteFile(config_path, config_data.data(), |
| 197 config_data.size()); | 216 config_data.size()); |
| 198 if (written != static_cast<int>(config_data.size())) { | 217 if (written != static_cast<int>(config_data.size())) { |
| 199 LOG(ERROR) << "Failed to save configuration data to: " | 218 std::ostringstream error_message; |
| 200 << config_path.value(); | 219 error_message << "Failed to save configuration data to: " |
| 220 << config_path.value(); |
| 221 LOG(ERROR) << error_message; |
| 222 on_error.Run(error_message.str(), FROM_HERE); |
| 201 return false; | 223 return false; |
| 202 } | 224 } |
| 203 } | 225 } |
| 204 | 226 |
| 205 base::FilePath pane_path; | 227 base::FilePath pane_path; |
| 206 // TODO(lambroslambrou): Use NSPreferencePanesDirectory once we start | 228 // TODO(lambroslambrou): Use NSPreferencePanesDirectory once we start |
| 207 // building against SDK 10.6. | 229 // building against SDK 10.6. |
| 208 if (!base::mac::GetLocalDirectory(NSLibraryDirectory, &pane_path)) { | 230 if (!base::mac::GetLocalDirectory(NSLibraryDirectory, &pane_path)) { |
| 209 LOG(ERROR) << "Failed to get directory for local preference panes."; | 231 std::string error_message = |
| 210 return false; | 232 "Failed to get directory for local preference panes."; |
| 211 } | 233 LOG(ERROR) << error_message; |
| 212 pane_path = pane_path.Append("PreferencePanes").Append(kPrefPaneFileName); | 234 on_error.Run(error_message, FROM_HERE); |
| 213 | |
| 214 FSRef pane_path_ref; | |
| 215 if (!base::mac::FSRefFromPath(pane_path.value(), &pane_path_ref)) { | |
| 216 LOG(ERROR) << "Failed to create FSRef"; | |
| 217 return false; | |
| 218 } | |
| 219 OSStatus status = LSOpenFSRef(&pane_path_ref, nullptr); | |
| 220 if (status != noErr) { | |
| 221 OSSTATUS_LOG(ERROR, status) << "LSOpenFSRef failed for path: " | |
| 222 << pane_path.value(); | |
| 223 return false; | 235 return false; |
| 224 } | 236 } |
| 225 | 237 |
| 238 pane_path = pane_path.Append("PreferencePanes").Append(kPrefPaneFileName); |
| 239 FSRef pane_path_ref; |
| 240 if (!base::mac::FSRefFromPath(pane_path.value(), &pane_path_ref)) { |
| 241 std::string error_message = "Failed to create FSRef"; |
| 242 LOG(ERROR) << error_message; |
| 243 on_error.Run(error_message, FROM_HERE); |
| 244 return false; |
| 245 } |
| 246 |
| 247 OSStatus status = LSOpenFSRef(&pane_path_ref, nullptr); |
| 248 if (status != noErr) { |
| 249 std::ostringstream error_message; |
| 250 error_message << "LSOpenFSRef failed for path: " |
| 251 << pane_path.value() << ": " |
| 252 << GetMacOSStatusErrorString(status); |
| 253 LOG(ERROR) << error_message; |
| 254 on_error.Run(error_message.str(), FROM_HERE); |
| 255 return false; |
| 256 } |
| 257 |
| 226 CFNotificationCenterRef center = | 258 CFNotificationCenterRef center = |
| 227 CFNotificationCenterGetDistributedCenter(); | 259 CFNotificationCenterGetDistributedCenter(); |
| 228 base::ScopedCFTypeRef<CFStringRef> service_name(CFStringCreateWithCString( | 260 base::ScopedCFTypeRef<CFStringRef> service_name(CFStringCreateWithCString( |
| 229 kCFAllocatorDefault, remoting::kServiceName, kCFStringEncodingUTF8)); | 261 kCFAllocatorDefault, remoting::kServiceName, kCFStringEncodingUTF8)); |
| 230 CFNotificationCenterPostNotification(center, service_name, nullptr, nullptr, | 262 CFNotificationCenterPostNotification(center, service_name, nullptr, nullptr, |
| 231 TRUE); | 263 TRUE); |
| 232 return true; | 264 return true; |
| 233 } | 265 } |
| 234 | 266 |
| 235 // static | 267 // static |
| (...skipping 13 matching lines...) Expand all Loading... |
| 249 self->PreferencePaneCallbackDelegate(name); | 281 self->PreferencePaneCallbackDelegate(name); |
| 250 } | 282 } |
| 251 | 283 |
| 252 scoped_refptr<DaemonController> DaemonController::Create() { | 284 scoped_refptr<DaemonController> DaemonController::Create() { |
| 253 scoped_ptr<DaemonController::Delegate> delegate( | 285 scoped_ptr<DaemonController::Delegate> delegate( |
| 254 new DaemonControllerDelegateMac()); | 286 new DaemonControllerDelegateMac()); |
| 255 return new DaemonController(delegate.Pass()); | 287 return new DaemonController(delegate.Pass()); |
| 256 } | 288 } |
| 257 | 289 |
| 258 } // namespace remoting | 290 } // namespace remoting |
| OLD | NEW |