| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_backend.h" | 5 #include "chrome/service/cloud_print/cloud_print_proxy_backend.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" |
| 10 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 11 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "chrome/common/net/gaia/gaia_oauth_client.h" | 14 #include "chrome/common/net/gaia/gaia_oauth_client.h" |
| 14 #include "chrome/common/net/gaia/gaia_urls.h" | 15 #include "chrome/common/net/gaia/gaia_urls.h" |
| 15 #include "chrome/service/cloud_print/cloud_print_auth.h" | 16 #include "chrome/service/cloud_print/cloud_print_auth.h" |
| 16 #include "chrome/service/cloud_print/cloud_print_connector.h" | 17 #include "chrome/service/cloud_print/cloud_print_connector.h" |
| 17 #include "chrome/service/cloud_print/cloud_print_consts.h" | 18 #include "chrome/service/cloud_print/cloud_print_consts.h" |
| 18 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 19 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
| 19 #include "chrome/service/cloud_print/cloud_print_token_store.h" | 20 #include "chrome/service/cloud_print/cloud_print_token_store.h" |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 } | 168 } |
| 168 | 169 |
| 169 bool CloudPrintProxyBackend::InitializeWithLsid( | 170 bool CloudPrintProxyBackend::InitializeWithLsid( |
| 170 const std::string& lsid, | 171 const std::string& lsid, |
| 171 const std::string& proxy_id, | 172 const std::string& proxy_id, |
| 172 const std::string& last_robot_refresh_token, | 173 const std::string& last_robot_refresh_token, |
| 173 const std::string& last_robot_email, | 174 const std::string& last_robot_email, |
| 174 const std::string& last_user_email) { | 175 const std::string& last_user_email) { |
| 175 if (!core_thread_.Start()) | 176 if (!core_thread_.Start()) |
| 176 return false; | 177 return false; |
| 177 core_thread_.message_loop()->PostTask(FROM_HERE, | 178 core_thread_.message_loop()->PostTask( |
| 178 NewRunnableMethod( | 179 FROM_HERE, |
| 179 core_.get(), | 180 base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithLsid, |
| 180 &CloudPrintProxyBackend::Core::DoInitializeWithLsid, | 181 core_.get(), lsid, proxy_id, last_robot_refresh_token, |
| 181 lsid, | 182 last_robot_email, last_user_email)); |
| 182 proxy_id, | |
| 183 last_robot_refresh_token, | |
| 184 last_robot_email, | |
| 185 last_user_email)); | |
| 186 return true; | 183 return true; |
| 187 } | 184 } |
| 188 | 185 |
| 189 bool CloudPrintProxyBackend::InitializeWithToken( | 186 bool CloudPrintProxyBackend::InitializeWithToken( |
| 190 const std::string& cloud_print_token, | 187 const std::string& cloud_print_token, |
| 191 const std::string& proxy_id) { | 188 const std::string& proxy_id) { |
| 192 if (!core_thread_.Start()) | 189 if (!core_thread_.Start()) |
| 193 return false; | 190 return false; |
| 194 core_thread_.message_loop()->PostTask(FROM_HERE, | 191 core_thread_.message_loop()->PostTask( |
| 195 NewRunnableMethod( | 192 FROM_HERE, |
| 196 core_.get(), &CloudPrintProxyBackend::Core::DoInitializeWithToken, | 193 base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithToken, |
| 197 cloud_print_token, proxy_id)); | 194 core_.get(), cloud_print_token, proxy_id)); |
| 198 return true; | 195 return true; |
| 199 } | 196 } |
| 200 | 197 |
| 201 bool CloudPrintProxyBackend::InitializeWithRobotToken( | 198 bool CloudPrintProxyBackend::InitializeWithRobotToken( |
| 202 const std::string& robot_oauth_refresh_token, | 199 const std::string& robot_oauth_refresh_token, |
| 203 const std::string& robot_email, | 200 const std::string& robot_email, |
| 204 const std::string& proxy_id) { | 201 const std::string& proxy_id) { |
| 205 if (!core_thread_.Start()) | 202 if (!core_thread_.Start()) |
| 206 return false; | 203 return false; |
| 207 core_thread_.message_loop()->PostTask(FROM_HERE, | 204 core_thread_.message_loop()->PostTask( |
| 208 NewRunnableMethod( | 205 FROM_HERE, |
| 209 core_.get(), | 206 base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithRobotToken, |
| 210 &CloudPrintProxyBackend::Core::DoInitializeWithRobotToken, | 207 core_.get(), robot_oauth_refresh_token, robot_email, |
| 211 robot_oauth_refresh_token, | 208 proxy_id)); |
| 212 robot_email, | |
| 213 proxy_id)); | |
| 214 return true; | 209 return true; |
| 215 } | 210 } |
| 216 | 211 |
| 217 bool CloudPrintProxyBackend::InitializeWithRobotAuthCode( | 212 bool CloudPrintProxyBackend::InitializeWithRobotAuthCode( |
| 218 const std::string& robot_oauth_auth_code, | 213 const std::string& robot_oauth_auth_code, |
| 219 const std::string& robot_email, | 214 const std::string& robot_email, |
| 220 const std::string& proxy_id) { | 215 const std::string& proxy_id) { |
| 221 if (!core_thread_.Start()) | 216 if (!core_thread_.Start()) |
| 222 return false; | 217 return false; |
| 223 core_thread_.message_loop()->PostTask(FROM_HERE, | 218 core_thread_.message_loop()->PostTask( |
| 224 NewRunnableMethod( | 219 FROM_HERE, |
| 225 core_.get(), | 220 base::Bind(&CloudPrintProxyBackend::Core::DoInitializeWithRobotAuthCode, |
| 226 &CloudPrintProxyBackend::Core::DoInitializeWithRobotAuthCode, | 221 core_.get(), robot_oauth_auth_code, robot_email, proxy_id)); |
| 227 robot_oauth_auth_code, | |
| 228 robot_email, | |
| 229 proxy_id)); | |
| 230 return true; | 222 return true; |
| 231 } | 223 } |
| 232 | 224 |
| 233 void CloudPrintProxyBackend::Shutdown() { | 225 void CloudPrintProxyBackend::Shutdown() { |
| 234 core_thread_.message_loop()->PostTask(FROM_HERE, | 226 core_thread_.message_loop()->PostTask( |
| 235 NewRunnableMethod(core_.get(), | 227 FROM_HERE, |
| 236 &CloudPrintProxyBackend::Core::DoShutdown)); | 228 base::Bind(&CloudPrintProxyBackend::Core::DoShutdown, core_.get())); |
| 237 core_thread_.Stop(); | 229 core_thread_.Stop(); |
| 238 core_ = NULL; // Releases reference to core_. | 230 core_ = NULL; // Releases reference to core_. |
| 239 } | 231 } |
| 240 | 232 |
| 241 CloudPrintProxyBackend::Core::Core( | 233 CloudPrintProxyBackend::Core::Core( |
| 242 CloudPrintProxyBackend* backend, | 234 CloudPrintProxyBackend* backend, |
| 243 const std::string& proxy_id, | 235 const std::string& proxy_id, |
| 244 const GURL& cloud_print_server_url, | 236 const GURL& cloud_print_server_url, |
| 245 const DictionaryValue* print_system_settings, | 237 const DictionaryValue* print_system_settings, |
| 246 const gaia::OAuthClientInfo& oauth_client_info, | 238 const gaia::OAuthClientInfo& oauth_client_info, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 void CloudPrintProxyBackend::Core::OnAuthenticationComplete( | 293 void CloudPrintProxyBackend::Core::OnAuthenticationComplete( |
| 302 const std::string& access_token, | 294 const std::string& access_token, |
| 303 const std::string& robot_oauth_refresh_token, | 295 const std::string& robot_oauth_refresh_token, |
| 304 const std::string& robot_email, | 296 const std::string& robot_email, |
| 305 const std::string& user_email) { | 297 const std::string& user_email) { |
| 306 CloudPrintTokenStore* token_store = GetTokenStore(); | 298 CloudPrintTokenStore* token_store = GetTokenStore(); |
| 307 bool first_time = token_store->token().empty(); | 299 bool first_time = token_store->token().empty(); |
| 308 token_store->SetToken(access_token); | 300 token_store->SetToken(access_token); |
| 309 // Let the frontend know that we have authenticated. | 301 // Let the frontend know that we have authenticated. |
| 310 backend_->frontend_loop_->PostTask( | 302 backend_->frontend_loop_->PostTask( |
| 311 FROM_HERE, NewRunnableMethod(this, | 303 FROM_HERE, |
| 312 &Core::NotifyAuthenticated, | 304 base::Bind(&Core::NotifyAuthenticated, this, robot_oauth_refresh_token, |
| 313 robot_oauth_refresh_token, | 305 robot_email, user_email)); |
| 314 robot_email, | |
| 315 user_email)); | |
| 316 if (first_time) { | 306 if (first_time) { |
| 317 InitNotifications(robot_email, access_token); | 307 InitNotifications(robot_email, access_token); |
| 318 } else { | 308 } else { |
| 319 // If we are refreshing a token, update the XMPP token too. | 309 // If we are refreshing a token, update the XMPP token too. |
| 320 DCHECK(talk_mediator_.get()); | 310 DCHECK(talk_mediator_.get()); |
| 321 talk_mediator_->SetAuthToken(robot_email, | 311 talk_mediator_->SetAuthToken(robot_email, |
| 322 access_token, | 312 access_token, |
| 323 kSyncGaiaServiceId); | 313 kSyncGaiaServiceId); |
| 324 } | 314 } |
| 325 // Start cloud print connector if needed. | 315 // Start cloud print connector if needed. |
| 326 if (!connector_->IsRunning()) { | 316 if (!connector_->IsRunning()) { |
| 327 if (!connector_->Start()) { | 317 if (!connector_->Start()) { |
| 328 // Let the frontend know that we do not have a print system. | 318 // Let the frontend know that we do not have a print system. |
| 329 backend_->frontend_loop_->PostTask( | 319 backend_->frontend_loop_->PostTask( |
| 330 FROM_HERE, | 320 FROM_HERE, base::Bind(&Core::NotifyPrintSystemUnavailable, this)); |
| 331 NewRunnableMethod(this, | |
| 332 &Core::NotifyPrintSystemUnavailable)); | |
| 333 } | 321 } |
| 334 } | 322 } |
| 335 } | 323 } |
| 336 | 324 |
| 337 void CloudPrintProxyBackend::Core::OnInvalidCredentials() { | 325 void CloudPrintProxyBackend::Core::OnInvalidCredentials() { |
| 338 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 326 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 339 VLOG(1) << "CP_CONNECTOR: Auth Error"; | 327 VLOG(1) << "CP_CONNECTOR: Auth Error"; |
| 340 backend_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, | 328 backend_->frontend_loop_->PostTask( |
| 341 &Core::NotifyAuthenticationFailed)); | 329 FROM_HERE, base::Bind(&Core::NotifyAuthenticationFailed, this)); |
| 342 } | 330 } |
| 343 | 331 |
| 344 void CloudPrintProxyBackend::Core::OnAuthFailed() { | 332 void CloudPrintProxyBackend::Core::OnAuthFailed() { |
| 345 VLOG(1) << "CP_CONNECTOR: Authentication failed in connector."; | 333 VLOG(1) << "CP_CONNECTOR: Authentication failed in connector."; |
| 346 // Let's stop connecter and refresh token. We'll restart connecter once | 334 // Let's stop connecter and refresh token. We'll restart connecter once |
| 347 // new token available. | 335 // new token available. |
| 348 if (connector_->IsRunning()) | 336 if (connector_->IsRunning()) |
| 349 connector_->Stop(); | 337 connector_->Stop(); |
| 350 | 338 |
| 351 // Refresh Auth token. | 339 // Refresh Auth token. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 if (!notifications_enabled_ && enable_job_poll_) | 396 if (!notifications_enabled_ && enable_job_poll_) |
| 409 ScheduleJobPoll(); | 397 ScheduleJobPoll(); |
| 410 } | 398 } |
| 411 | 399 |
| 412 void CloudPrintProxyBackend::Core::ScheduleJobPoll() { | 400 void CloudPrintProxyBackend::Core::ScheduleJobPoll() { |
| 413 if (!job_poll_scheduled_) { | 401 if (!job_poll_scheduled_) { |
| 414 int interval_in_seconds = base::RandInt(kMinJobPollIntervalSecs, | 402 int interval_in_seconds = base::RandInt(kMinJobPollIntervalSecs, |
| 415 kMaxJobPollIntervalSecs); | 403 kMaxJobPollIntervalSecs); |
| 416 MessageLoop::current()->PostDelayedTask( | 404 MessageLoop::current()->PostDelayedTask( |
| 417 FROM_HERE, | 405 FROM_HERE, |
| 418 NewRunnableMethod(this, &CloudPrintProxyBackend::Core::PollForJobs), | 406 base::Bind(&CloudPrintProxyBackend::Core::PollForJobs, this), |
| 419 interval_in_seconds * 1000); | 407 interval_in_seconds * 1000); |
| 420 job_poll_scheduled_ = true; | 408 job_poll_scheduled_ = true; |
| 421 } | 409 } |
| 422 } | 410 } |
| 423 | 411 |
| 424 CloudPrintTokenStore* CloudPrintProxyBackend::Core::GetTokenStore() { | 412 CloudPrintTokenStore* CloudPrintProxyBackend::Core::GetTokenStore() { |
| 425 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 413 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 426 if (!token_store_.get()) | 414 if (!token_store_.get()) |
| 427 token_store_.reset(new CloudPrintTokenStore); | 415 token_store_.reset(new CloudPrintTokenStore); |
| 428 return token_store_.get(); | 416 return token_store_.get(); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 void CloudPrintProxyBackend::Core::OnIncomingNotification( | 464 void CloudPrintProxyBackend::Core::OnIncomingNotification( |
| 477 const notifier::Notification& notification) { | 465 const notifier::Notification& notification) { |
| 478 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 466 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 479 VLOG(1) << "CP_CONNECTOR: Incoming notification."; | 467 VLOG(1) << "CP_CONNECTOR: Incoming notification."; |
| 480 if (0 == base::strcasecmp(kCloudPrintPushNotificationsSource, | 468 if (0 == base::strcasecmp(kCloudPrintPushNotificationsSource, |
| 481 notification.channel.c_str())) | 469 notification.channel.c_str())) |
| 482 HandlePrinterNotification(notification.data); | 470 HandlePrinterNotification(notification.data); |
| 483 } | 471 } |
| 484 | 472 |
| 485 void CloudPrintProxyBackend::Core::OnOutgoingNotification() {} | 473 void CloudPrintProxyBackend::Core::OnOutgoingNotification() {} |
| OLD | NEW |