| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/service/cloud_print/cloud_print_proxy.h" | 5 #include "chrome/service/cloud_print/cloud_print_proxy.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 service_prefs_->GetString(prefs::kCloudPrintRobotRefreshToken, | 94 service_prefs_->GetString(prefs::kCloudPrintRobotRefreshToken, |
| 95 &robot_refresh_token); | 95 &robot_refresh_token); |
| 96 std::string robot_email; | 96 std::string robot_email; |
| 97 service_prefs_->GetString(prefs::kCloudPrintRobotEmail, | 97 service_prefs_->GetString(prefs::kCloudPrintRobotEmail, |
| 98 &robot_email); | 98 &robot_email); |
| 99 service_prefs_->GetString(prefs::kCloudPrintEmail, &user_email_); | 99 service_prefs_->GetString(prefs::kCloudPrintEmail, &user_email_); |
| 100 | 100 |
| 101 // If we have been passed in an LSID, we want to use this to authenticate. | 101 // If we have been passed in an LSID, we want to use this to authenticate. |
| 102 // Else we will try and retrieve the last used auth tokens from prefs. | 102 // Else we will try and retrieve the last used auth tokens from prefs. |
| 103 if (!lsid.empty()) { | 103 if (!lsid.empty()) { |
| 104 backend_->InitializeWithLsid(lsid, | 104 backend_->InitializeWithLsid(lsid, robot_refresh_token, robot_email, |
| 105 proxy_id_, | |
| 106 robot_refresh_token, | |
| 107 robot_email, | |
| 108 user_email_); | 105 user_email_); |
| 109 } else { | 106 } else { |
| 110 // See if we have persisted robot credentials. | 107 // See if we have persisted robot credentials. |
| 111 if (!robot_refresh_token.empty()) { | 108 if (!robot_refresh_token.empty()) { |
| 112 DCHECK(!robot_email.empty()); | 109 DCHECK(!robot_email.empty()); |
| 113 backend_->InitializeWithRobotToken(robot_refresh_token, | 110 backend_->InitializeWithRobotToken(robot_refresh_token, robot_email); |
| 114 robot_email, | |
| 115 proxy_id_); | |
| 116 } else { | 111 } else { |
| 117 // Finally see if we have persisted user credentials (legacy case). | 112 // Finally see if we have persisted user credentials (legacy case). |
| 118 std::string cloud_print_token; | 113 std::string cloud_print_token; |
| 119 service_prefs_->GetString(prefs::kCloudPrintAuthToken, | 114 service_prefs_->GetString(prefs::kCloudPrintAuthToken, |
| 120 &cloud_print_token); | 115 &cloud_print_token); |
| 121 DCHECK(!cloud_print_token.empty()); | 116 DCHECK(!cloud_print_token.empty()); |
| 122 backend_->InitializeWithToken(cloud_print_token, proxy_id_); | 117 backend_->InitializeWithToken(cloud_print_token); |
| 123 } | 118 } |
| 124 } | 119 } |
| 125 if (client_) { | 120 if (client_) { |
| 126 client_->OnCloudPrintProxyEnabled(true); | 121 client_->OnCloudPrintProxyEnabled(true); |
| 127 } | 122 } |
| 128 } | 123 } |
| 129 | 124 |
| 130 void CloudPrintProxy::EnableForUserWithRobot( | 125 void CloudPrintProxy::EnableForUserWithRobot( |
| 131 const std::string& robot_auth_code, | 126 const std::string& robot_auth_code, |
| 132 const std::string& robot_email, | 127 const std::string& robot_email, |
| 133 const std::string& user_email) { | 128 const std::string& user_email) { |
| 134 DCHECK(CalledOnValidThread()); | 129 DCHECK(CalledOnValidThread()); |
| 135 if (!CreateBackend()) | 130 if (!CreateBackend()) |
| 136 return; | 131 return; |
| 137 DCHECK(backend_.get()); | 132 DCHECK(backend_.get()); |
| 138 user_email_ = user_email; | 133 user_email_ = user_email; |
| 139 backend_->InitializeWithRobotAuthCode(robot_auth_code, | 134 backend_->InitializeWithRobotAuthCode(robot_auth_code, robot_email); |
| 140 robot_email, | |
| 141 proxy_id_); | |
| 142 if (client_) { | 135 if (client_) { |
| 143 client_->OnCloudPrintProxyEnabled(true); | 136 client_->OnCloudPrintProxyEnabled(true); |
| 144 } | 137 } |
| 145 } | 138 } |
| 146 | 139 |
| 147 bool CloudPrintProxy::CreateBackend() { | 140 bool CloudPrintProxy::CreateBackend() { |
| 148 DCHECK(CalledOnValidThread()); | 141 DCHECK(CalledOnValidThread()); |
| 149 if (backend_.get()) | 142 if (backend_.get()) |
| 150 return false; | 143 return false; |
| 151 | 144 |
| 152 service_prefs_->GetString(prefs::kCloudPrintProxyId, &proxy_id_); | 145 settings_.InitFrom(service_prefs_); |
| 153 if (proxy_id_.empty()) { | |
| 154 proxy_id_ = cloud_print::PrintSystem::GenerateProxyId(); | |
| 155 service_prefs_->SetString(prefs::kCloudPrintProxyId, proxy_id_); | |
| 156 service_prefs_->WritePrefs(); | |
| 157 } | |
| 158 | 146 |
| 159 // Getting print system specific settings from the preferences. | 147 // TODO(sanjeevr): Allow overriding OAuthClientInfo in prefs. |
| 160 const DictionaryValue* print_system_settings = NULL; | 148 gaia::OAuthClientInfo oauth_client_info; |
| 161 service_prefs_->GetDictionary(prefs::kCloudPrintPrintSystemSettings, | 149 oauth_client_info.client_id = |
| 162 &print_system_settings); | 150 google_apis::GetOAuth2ClientID(google_apis::CLIENT_CLOUD_PRINT); |
| 163 | 151 oauth_client_info.client_secret = |
| 164 // Check if there is an override for the cloud print server URL. | 152 google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_CLOUD_PRINT); |
| 165 std::string cloud_print_server_url_str; | |
| 166 service_prefs_->GetString(prefs::kCloudPrintServiceURL, | |
| 167 &cloud_print_server_url_str); | |
| 168 if (cloud_print_server_url_str.empty()) { | |
| 169 cloud_print_server_url_str = kDefaultCloudPrintServerUrl; | |
| 170 } | |
| 171 | 153 |
| 172 // By default we don't poll for jobs when we lose XMPP connection. But this | 154 // By default we don't poll for jobs when we lose XMPP connection. But this |
| 173 // behavior can be overridden by a preference. | 155 // behavior can be overridden by a preference. |
| 174 bool enable_job_poll = false; | 156 bool enable_job_poll = false; |
| 175 service_prefs_->GetBoolean(prefs::kCloudPrintEnableJobPoll, | 157 service_prefs_->GetBoolean(prefs::kCloudPrintEnableJobPoll, |
| 176 &enable_job_poll); | 158 &enable_job_poll); |
| 177 | 159 |
| 178 // TODO(sanjeevr): Allow overriding OAuthClientInfo in prefs. | 160 backend_.reset(new CloudPrintProxyBackend(this, settings_, oauth_client_info, |
| 179 gaia::OAuthClientInfo oauth_client_info; | |
| 180 oauth_client_info.client_id = | |
| 181 google_apis::GetOAuth2ClientID(google_apis::CLIENT_CLOUD_PRINT); | |
| 182 oauth_client_info.client_secret = | |
| 183 google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_CLOUD_PRINT); | |
| 184 | |
| 185 cloud_print_server_url_ = GURL(cloud_print_server_url_str.c_str()); | |
| 186 DCHECK(cloud_print_server_url_.is_valid()); | |
| 187 backend_.reset(new CloudPrintProxyBackend(this, proxy_id_, | |
| 188 cloud_print_server_url_, | |
| 189 print_system_settings, | |
| 190 oauth_client_info, | |
| 191 enable_job_poll)); | 161 enable_job_poll)); |
| 192 return true; | 162 return true; |
| 193 } | 163 } |
| 194 | 164 |
| 195 void CloudPrintProxy::UnregisterPrintersAndDisableForUser() { | 165 void CloudPrintProxy::UnregisterPrintersAndDisableForUser() { |
| 196 DCHECK(CalledOnValidThread()); | 166 DCHECK(CalledOnValidThread()); |
| 197 if (backend_.get()) { | 167 if (backend_.get()) { |
| 198 // Try getting auth and printers info from the backend. | 168 // Try getting auth and printers info from the backend. |
| 199 // We'll get notified in this case. | 169 // We'll get notified in this case. |
| 200 backend_->UnregisterPrinters(); | 170 backend_->UnregisterPrinters(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 212 client_->OnCloudPrintProxyDisabled(true); | 182 client_->OnCloudPrintProxyDisabled(true); |
| 213 } | 183 } |
| 214 ShutdownBackend(); | 184 ShutdownBackend(); |
| 215 } | 185 } |
| 216 | 186 |
| 217 void CloudPrintProxy::GetProxyInfo(cloud_print::CloudPrintProxyInfo* info) { | 187 void CloudPrintProxy::GetProxyInfo(cloud_print::CloudPrintProxyInfo* info) { |
| 218 info->enabled = enabled_; | 188 info->enabled = enabled_; |
| 219 info->email.clear(); | 189 info->email.clear(); |
| 220 if (enabled_) | 190 if (enabled_) |
| 221 info->email = user_email(); | 191 info->email = user_email(); |
| 222 info->proxy_id = proxy_id_; | 192 info->proxy_id = settings_.proxy_id(); |
| 223 // If the Cloud Print service is not enabled, we may need to read the old | 193 // If the Cloud Print service is not enabled, we may need to read the old |
| 224 // value of proxy_id from prefs. | 194 // value of proxy_id from prefs. |
| 225 if (info->proxy_id.empty()) | 195 if (info->proxy_id.empty()) |
| 226 service_prefs_->GetString(prefs::kCloudPrintProxyId, &info->proxy_id); | 196 service_prefs_->GetString(prefs::kCloudPrintProxyId, &info->proxy_id); |
| 227 } | 197 } |
| 228 | 198 |
| 229 void CloudPrintProxy::CheckCloudPrintProxyPolicy() { | 199 void CloudPrintProxy::CheckCloudPrintProxyPolicy() { |
| 230 g_service_process->io_thread()->message_loop_proxy()->PostTask( | 200 g_service_process->io_thread()->message_loop_proxy()->PostTask( |
| 231 FROM_HERE, base::Bind(&CheckCloudPrintProxyPolicyInBrowser)); | 201 FROM_HERE, base::Bind(&CheckCloudPrintProxyPolicyInBrowser)); |
| 232 } | 202 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 ShutdownBackend(); | 244 ShutdownBackend(); |
| 275 if (client_) { | 245 if (client_) { |
| 276 client_->OnCloudPrintProxyDisabled(false); | 246 client_->OnCloudPrintProxyDisabled(false); |
| 277 } | 247 } |
| 278 } | 248 } |
| 279 | 249 |
| 280 void CloudPrintProxy::OnUnregisterPrinters( | 250 void CloudPrintProxy::OnUnregisterPrinters( |
| 281 const std::string& auth_token, | 251 const std::string& auth_token, |
| 282 const std::list<std::string>& printer_ids) { | 252 const std::list<std::string>& printer_ids) { |
| 283 ShutdownBackend(); | 253 ShutdownBackend(); |
| 284 wipeout_.reset(new CloudPrintWipeout(this, cloud_print_server_url_)); | 254 wipeout_.reset(new CloudPrintWipeout(this, settings_.server_url())); |
| 285 wipeout_->UnregisterPrinters(auth_token, printer_ids); | 255 wipeout_->UnregisterPrinters(auth_token, printer_ids); |
| 286 } | 256 } |
| 287 | 257 |
| 288 void CloudPrintProxy::OnUnregisterPrintersComplete() { | 258 void CloudPrintProxy::OnUnregisterPrintersComplete() { |
| 289 wipeout_.reset(); | 259 wipeout_.reset(); |
| 290 // Finish disabling cloud print for this user. | 260 // Finish disabling cloud print for this user. |
| 291 DisableForUser(); | 261 DisableForUser(); |
| 292 } | 262 } |
| 293 | 263 |
| 294 void CloudPrintProxy::ShutdownBackend() { | 264 void CloudPrintProxy::ShutdownBackend() { |
| 295 DCHECK(CalledOnValidThread()); | 265 DCHECK(CalledOnValidThread()); |
| 296 if (backend_.get()) | 266 if (backend_.get()) |
| 297 backend_->Shutdown(); | 267 backend_->Shutdown(); |
| 298 backend_.reset(); | 268 backend_.reset(); |
| 299 } | 269 } |
| OLD | NEW |