summaryrefslogtreecommitdiffstats
path: root/remoting/host/setup/daemon_controller_delegate_mac.mm
blob: 4363a405d7cf2219c9663b6de2255f6aec5ab843 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <CoreFoundation/CoreFoundation.h>

#include "remoting/host/setup/daemon_controller_delegate_mac.h"

#include <launch.h>
#include <stdio.h>
#include <sys/types.h>

#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/mac/launchd.h"
#include "base/mac/mac_logging.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_launch_data.h"
#include "base/time/time.h"
#include "base/values.h"
#include "remoting/host/constants_mac.h"
#include "remoting/host/host_config.h"
#include "remoting/host/usage_stats_consent.h"

namespace remoting {

DaemonControllerDelegateMac::DaemonControllerDelegateMac() {
}

DaemonControllerDelegateMac::~DaemonControllerDelegateMac() {
  DeregisterForPreferencePaneNotifications();
}

DaemonController::State DaemonControllerDelegateMac::GetState() {
  pid_t job_pid = base::mac::PIDForJob(kServiceName);
  if (job_pid < 0) {
    return DaemonController::STATE_UNKNOWN;
  } else if (job_pid == 0) {
    // Service is stopped, or a start attempt failed.
    return DaemonController::STATE_STOPPED;
  } else {
    return DaemonController::STATE_STARTED;
  }
}

scoped_ptr<base::DictionaryValue> DaemonControllerDelegateMac::GetConfig() {
  base::FilePath config_path(kHostConfigFilePath);
  scoped_ptr<base::DictionaryValue> host_config(
      HostConfigFromJsonFile(config_path));
  if (!host_config)
    return nullptr;

  scoped_ptr<base::DictionaryValue> config(new base::DictionaryValue);
  std::string value;
  if (host_config->GetString(kHostIdConfigPath, &value))
    config->SetString(kHostIdConfigPath, value);
  if (host_config->GetString(kXmppLoginConfigPath, &value))
    config->SetString(kXmppLoginConfigPath, value);
  return config;
}

void DaemonControllerDelegateMac::SetConfigAndStart(
    scoped_ptr<base::DictionaryValue> config,
    bool consent,
    const DaemonController::CompletionCallback& done) {
  config->SetBoolean(kUsageStatsConsentConfigPath, consent);
  ShowPreferencePane(HostConfigToJson(*config), done);
}

void DaemonControllerDelegateMac::UpdateConfig(
    scoped_ptr<base::DictionaryValue> config,
    const DaemonController::CompletionCallback& done) {
  base::FilePath config_file_path(kHostConfigFilePath);
  scoped_ptr<base::DictionaryValue> host_config(
      HostConfigFromJsonFile(config_file_path));
  if (!host_config) {
    done.Run(DaemonController::RESULT_FAILED);
    return;
  }

  host_config->MergeDictionary(config.get());
  ShowPreferencePane(HostConfigToJson(*host_config), done);
}

void DaemonControllerDelegateMac::Stop(
    const DaemonController::CompletionCallback& done) {
  ShowPreferencePane("", done);
}

DaemonController::UsageStatsConsent
DaemonControllerDelegateMac::GetUsageStatsConsent() {
  DaemonController::UsageStatsConsent consent;
  consent.supported = true;
  consent.allowed = false;
  // set_by_policy is not yet supported.
  consent.set_by_policy = false;

  base::FilePath config_file_path(kHostConfigFilePath);
  scoped_ptr<base::DictionaryValue> host_config(
      HostConfigFromJsonFile(config_file_path));
  if (host_config) {
    host_config->GetBoolean(kUsageStatsConsentConfigPath, &consent.allowed);
  }

  return consent;
}

void DaemonControllerDelegateMac::ShowPreferencePane(
    const std::string& config_data,
    const DaemonController::CompletionCallback& done) {
  if (DoShowPreferencePane(config_data)) {
    RegisterForPreferencePaneNotifications(done);
  } else {
    done.Run(DaemonController::RESULT_FAILED);
  }
}

// CFNotificationCenterAddObserver ties the thread on which distributed
// notifications are received to the one on which it is first called.
// This is safe because HostNPScriptObject::InvokeAsyncResultCallback
// bounces the invocation to the correct thread, so it doesn't matter
// which thread CompletionCallbacks are called on.
void DaemonControllerDelegateMac::RegisterForPreferencePaneNotifications(
    const DaemonController::CompletionCallback& done) {
  // We can only have one callback registered at a time. This is enforced by the
  // UX flow of the web-app.
  DCHECK(current_callback_.is_null());
  current_callback_ = done;

  CFNotificationCenterAddObserver(
      CFNotificationCenterGetDistributedCenter(),
      this,
      &DaemonControllerDelegateMac::PreferencePaneCallback,
      CFSTR(UPDATE_SUCCEEDED_NOTIFICATION_NAME),
      nullptr,
      CFNotificationSuspensionBehaviorDeliverImmediately);
  CFNotificationCenterAddObserver(
      CFNotificationCenterGetDistributedCenter(),
      this,
      &DaemonControllerDelegateMac::PreferencePaneCallback,
      CFSTR(UPDATE_FAILED_NOTIFICATION_NAME),
      nullptr,
      CFNotificationSuspensionBehaviorDeliverImmediately);
}

void DaemonControllerDelegateMac::DeregisterForPreferencePaneNotifications() {
  CFNotificationCenterRemoveObserver(
      CFNotificationCenterGetDistributedCenter(),
      this,
      CFSTR(UPDATE_SUCCEEDED_NOTIFICATION_NAME),
      nullptr);
  CFNotificationCenterRemoveObserver(
      CFNotificationCenterGetDistributedCenter(),
      this,
      CFSTR(UPDATE_FAILED_NOTIFICATION_NAME),
      nullptr);
}

void DaemonControllerDelegateMac::PreferencePaneCallbackDelegate(
    CFStringRef name) {
  DaemonController::AsyncResult result = DaemonController::RESULT_FAILED;
  if (CFStringCompare(name, CFSTR(UPDATE_SUCCEEDED_NOTIFICATION_NAME), 0) ==
          kCFCompareEqualTo) {
    result = DaemonController::RESULT_OK;
  } else if (CFStringCompare(name, CFSTR(UPDATE_FAILED_NOTIFICATION_NAME), 0) ==
          kCFCompareEqualTo) {
    result = DaemonController::RESULT_FAILED;
  } else {
    LOG(WARNING) << "Ignoring unexpected notification: " << name;
    return;
  }

  DeregisterForPreferencePaneNotifications();

  DCHECK(!current_callback_.is_null());
  base::ResetAndReturn(&current_callback_).Run(result);
}

// static
bool DaemonControllerDelegateMac::DoShowPreferencePane(
    const std::string& config_data) {
  if (!config_data.empty()) {
    base::FilePath config_path;
    if (!base::GetTempDir(&config_path)) {
      LOG(ERROR) << "Failed to get filename for saving configuration data.";
      return false;
    }
    config_path = config_path.Append(kHostConfigFileName);

    int written = base::WriteFile(config_path, config_data.data(),
                                       config_data.size());
    if (written != static_cast<int>(config_data.size())) {
      LOG(ERROR) << "Failed to save configuration data to: "
                 << config_path.value();
      return false;
    }
  }

  base::FilePath pane_path;
  // TODO(lambroslambrou): Use NSPreferencePanesDirectory once we start
  // building against SDK 10.6.
  if (!base::mac::GetLocalDirectory(NSLibraryDirectory, &pane_path)) {
    LOG(ERROR) << "Failed to get directory for local preference panes.";
    return false;
  }
  pane_path = pane_path.Append("PreferencePanes").Append(kPrefPaneFileName);

  FSRef pane_path_ref;
  if (!base::mac::FSRefFromPath(pane_path.value(), &pane_path_ref)) {
    LOG(ERROR) << "Failed to create FSRef";
    return false;
  }
  OSStatus status = LSOpenFSRef(&pane_path_ref, nullptr);
  if (status != noErr) {
    OSSTATUS_LOG(ERROR, status) << "LSOpenFSRef failed for path: "
                                << pane_path.value();
    return false;
  }

  CFNotificationCenterRef center =
      CFNotificationCenterGetDistributedCenter();
  base::ScopedCFTypeRef<CFStringRef> service_name(CFStringCreateWithCString(
      kCFAllocatorDefault, remoting::kServiceName, kCFStringEncodingUTF8));
  CFNotificationCenterPostNotification(center, service_name, nullptr, nullptr,
                                       TRUE);
  return true;
}

// static
void DaemonControllerDelegateMac::PreferencePaneCallback(
    CFNotificationCenterRef center,
    void* observer,
    CFStringRef name,
    const void* object,
    CFDictionaryRef user_info) {
  DaemonControllerDelegateMac* self =
      reinterpret_cast<DaemonControllerDelegateMac*>(observer);
  if (!self) {
    LOG(WARNING) << "Ignoring notification with nullptr observer: " << name;
    return;
  }

  self->PreferencePaneCallbackDelegate(name);
}

scoped_refptr<DaemonController> DaemonController::Create() {
  return new DaemonController(
      make_scoped_ptr(new DaemonControllerDelegateMac()));
}

}  // namespace remoting