summaryrefslogtreecommitdiffstats
path: root/chrome/service/cloud_print/cloud_print_proxy.cc
blob: a65da813798a22d865423a59e6b2be928515dd21 (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (c) 2012 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 "chrome/service/cloud_print/cloud_print_proxy.h"

#include "base/bind.h"
#include "base/command_line.h"
#include "base/path_service.h"
#include "base/process_util.h"
#include "base/values.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/cloud_print/cloud_print_constants.h"
#include "chrome/common/cloud_print/cloud_print_proxy_info.h"
#include "chrome/common/pref_names.h"
#include "chrome/service/cloud_print/print_system.h"
#include "chrome/service/service_process.h"
#include "chrome/service/service_process_prefs.h"
#include "google_apis/gaia/gaia_oauth_client.h"
#include "google_apis/google_api_keys.h"
#include "googleurl/src/gurl.h"

namespace {

void LaunchBrowserProcessWithSwitch(const std::string& switch_string) {
  DCHECK(g_service_process->io_thread()->message_loop_proxy()->
      BelongsToCurrentThread());
  base::FilePath exe_path;
  PathService::Get(base::FILE_EXE, &exe_path);
  if (exe_path.empty()) {
    NOTREACHED() << "Unable to get browser process binary name.";
  }
  CommandLine cmd_line(exe_path);

  const CommandLine& process_command_line = *CommandLine::ForCurrentProcess();
  base::FilePath user_data_dir =
      process_command_line.GetSwitchValuePath(switches::kUserDataDir);
  if (!user_data_dir.empty())
    cmd_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir);
  cmd_line.AppendSwitch(switch_string);

#if defined(OS_POSIX) && !defined(OS_MACOSX)
  base::ProcessHandle pid = 0;
  base::LaunchProcess(cmd_line, base::LaunchOptions(), &pid);
  base::EnsureProcessGetsReaped(pid);
#else
  base::LaunchOptions launch_options;
#if defined(OS_WIN)
  launch_options.force_breakaway_from_job_ = true;
#endif  // OS_WIN
  base::LaunchProcess(cmd_line, launch_options, NULL);
#endif
}

void CheckCloudPrintProxyPolicyInBrowser() {
  LaunchBrowserProcessWithSwitch(switches::kCheckCloudPrintConnectorPolicy);
}

}  // namespace

namespace cloud_print {

CloudPrintProxy::CloudPrintProxy()
    : service_prefs_(NULL),
      client_(NULL),
      enabled_(false) {
}

CloudPrintProxy::~CloudPrintProxy() {
  DCHECK(CalledOnValidThread());
  ShutdownBackend();
}

void CloudPrintProxy::Initialize(ServiceProcessPrefs* service_prefs,
                                 Client* client) {
  DCHECK(CalledOnValidThread());
  service_prefs_ = service_prefs;
  client_ = client;
}

void CloudPrintProxy::EnableForUser(const std::string& lsid) {
  DCHECK(CalledOnValidThread());
  if (!CreateBackend())
    return;
  DCHECK(backend_.get());
  // Read persisted robot credentials because we may decide to reuse it if the
  // passed in LSID belongs the same user.
  std::string robot_refresh_token =
      service_prefs_->GetString(prefs::kCloudPrintRobotRefreshToken, "");
  std::string robot_email =
      service_prefs_->GetString(prefs::kCloudPrintRobotEmail, "");
  user_email_ = service_prefs_->GetString(prefs::kCloudPrintEmail, user_email_);

  // If we have been passed in an LSID, we want to use this to authenticate.
  // Else we will try and retrieve the last used auth tokens from prefs.
  if (!lsid.empty()) {
    backend_->InitializeWithLsid(lsid, robot_refresh_token, robot_email,
                                 user_email_);
  } else {
    // See if we have persisted robot credentials.
    if (!robot_refresh_token.empty()) {
      DCHECK(!robot_email.empty());
      backend_->InitializeWithRobotToken(robot_refresh_token, robot_email);
    } else {
      // Finally see if we have persisted user credentials (legacy case).
      std::string cloud_print_token =
          service_prefs_->GetString(prefs::kCloudPrintAuthToken, "");
      DCHECK(!cloud_print_token.empty());
      backend_->InitializeWithToken(cloud_print_token);
    }
  }
  if (client_) {
    client_->OnCloudPrintProxyEnabled(true);
  }
}

void CloudPrintProxy::EnableForUserWithRobot(
    const std::string& robot_auth_code,
    const std::string& robot_email,
    const std::string& user_email,
    bool connect_new_printers,
    const std::vector<std::string>& printer_blacklist) {
  DCHECK(CalledOnValidThread());

  ShutdownBackend();
  std::string proxy_id(
      service_prefs_->GetString(prefs::kCloudPrintProxyId, ""));
  service_prefs_->RemovePref(prefs::kCloudPrintRoot);
  if (!proxy_id.empty()) {
    // Keep only proxy id;
    service_prefs_->SetString(prefs::kCloudPrintProxyId, proxy_id);
  }
  service_prefs_->SetBoolean(prefs::kCloudPrintConnectNewPrinters,
                             connect_new_printers);
  if (!printer_blacklist.empty()) {
    scoped_ptr<base::ListValue> printers(new base::ListValue());
    printers->AppendStrings(printer_blacklist);
    service_prefs_->SetValue(prefs::kCloudPrintPrinterBlacklist,
                             printers.release());
  }
  service_prefs_->WritePrefs();

  if (!CreateBackend())
    return;
  DCHECK(backend_.get());
  user_email_ = user_email;
  backend_->InitializeWithRobotAuthCode(robot_auth_code, robot_email);
  if (client_) {
    client_->OnCloudPrintProxyEnabled(true);
  }
}

bool CloudPrintProxy::CreateBackend() {
  DCHECK(CalledOnValidThread());
  if (backend_.get())
    return false;

  settings_.InitFrom(service_prefs_);

  // By default we don't poll for jobs when we lose XMPP connection. But this
  // behavior can be overridden by a preference.
  bool enable_job_poll =
    service_prefs_->GetBoolean(prefs::kCloudPrintEnableJobPoll, false);

  gaia::OAuthClientInfo oauth_client_info;
  oauth_client_info.client_id =
    google_apis::GetOAuth2ClientID(google_apis::CLIENT_CLOUD_PRINT);
  oauth_client_info.client_secret =
    google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_CLOUD_PRINT);
  oauth_client_info.redirect_uri = "oob";
  backend_.reset(new CloudPrintProxyBackend(this, settings_, oauth_client_info,
                                            enable_job_poll));
  return true;
}

void CloudPrintProxy::UnregisterPrintersAndDisableForUser() {
  DCHECK(CalledOnValidThread());
  if (backend_.get()) {
    // Try getting auth and printers info from the backend.
    // We'll get notified in this case.
    backend_->UnregisterPrinters();
  } else {
    // If no backend avaialble, disable connector immidiately.
    DisableForUser();
  }
}

void CloudPrintProxy::DisableForUser() {
  DCHECK(CalledOnValidThread());
  user_email_.clear();
  enabled_ = false;
  if (client_) {
    client_->OnCloudPrintProxyDisabled(true);
  }
  ShutdownBackend();
}

void CloudPrintProxy::GetProxyInfo(CloudPrintProxyInfo* info) {
  info->enabled = enabled_;
  info->email.clear();
  if (enabled_)
    info->email = user_email();
  info->proxy_id = settings_.proxy_id();
  // If the Cloud Print service is not enabled, we may need to read the old
  // value of proxy_id from prefs.
  if (info->proxy_id.empty())
    info->proxy_id = service_prefs_->GetString(prefs::kCloudPrintProxyId, "");
}

void CloudPrintProxy::CheckCloudPrintProxyPolicy() {
  g_service_process->io_thread()->message_loop_proxy()->PostTask(
      FROM_HERE, base::Bind(&CheckCloudPrintProxyPolicyInBrowser));
}

void CloudPrintProxy::OnAuthenticated(
    const std::string& robot_oauth_refresh_token,
    const std::string& robot_email,
    const std::string& user_email) {
  DCHECK(CalledOnValidThread());
  service_prefs_->SetString(prefs::kCloudPrintRobotRefreshToken,
                            robot_oauth_refresh_token);
  service_prefs_->SetString(prefs::kCloudPrintRobotEmail,
                            robot_email);
  // If authenticating from a robot, the user email will be empty.
  if (!user_email.empty()) {
    user_email_ = user_email;
  }
  service_prefs_->SetString(prefs::kCloudPrintEmail, user_email_);
  enabled_ = true;
  DCHECK(!user_email_.empty());
  service_prefs_->WritePrefs();
  // When this switch used we don't want connector continue running, we just
  // need authentication.
  if (CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kCloudPrintSetupProxy)) {
    ShutdownBackend();
    if (client_) {
      client_->OnCloudPrintProxyDisabled(false);
    }
  }
}

void CloudPrintProxy::OnAuthenticationFailed() {
  DCHECK(CalledOnValidThread());
  // Don't disable permanently. Could be just connection issue.
  ShutdownBackend();
  if (client_) {
    client_->OnCloudPrintProxyDisabled(false);
  }
}

void CloudPrintProxy::OnPrintSystemUnavailable() {
  // If the print system is unavailable, we want to shutdown the proxy and
  // disable it non-persistently.
  ShutdownBackend();
  if (client_) {
    client_->OnCloudPrintProxyDisabled(false);
  }
}

void CloudPrintProxy::OnUnregisterPrinters(
    const std::string& auth_token,
    const std::list<std::string>& printer_ids) {
  ShutdownBackend();
  wipeout_.reset(new CloudPrintWipeout(this, settings_.server_url()));
  wipeout_->UnregisterPrinters(auth_token, printer_ids);
}

void CloudPrintProxy::OnUnregisterPrintersComplete() {
  wipeout_.reset();
  // Finish disabling cloud print for this user.
  DisableForUser();
}

void CloudPrintProxy::ShutdownBackend() {
  DCHECK(CalledOnValidThread());
  if (backend_.get())
    backend_->Shutdown();
  backend_.reset();
}

}  // namespace cloud_print