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_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/bind.h" |
| 11 #include "base/command_line.h" |
11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
12 #include "base/file_util.h" | 13 #include "base/file_util.h" |
13 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/service/cloud_print/cloud_print_auth.h" | 17 #include "chrome/service/cloud_print/cloud_print_auth.h" |
16 #include "chrome/service/cloud_print/cloud_print_connector.h" | 18 #include "chrome/service/cloud_print/cloud_print_connector.h" |
17 #include "chrome/service/cloud_print/cloud_print_consts.h" | 19 #include "chrome/service/cloud_print/cloud_print_consts.h" |
18 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 20 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
19 #include "chrome/service/cloud_print/cloud_print_token_store.h" | 21 #include "chrome/service/cloud_print/cloud_print_token_store.h" |
20 #include "chrome/service/cloud_print/connector_settings.h" | 22 #include "chrome/service/cloud_print/connector_settings.h" |
21 #include "chrome/service/gaia/service_gaia_authenticator.h" | 23 #include "chrome/service/gaia/service_gaia_authenticator.h" |
22 #include "chrome/service/net/service_url_request_context.h" | 24 #include "chrome/service/net/service_url_request_context.h" |
23 #include "chrome/service/service_process.h" | 25 #include "chrome/service/service_process.h" |
24 #include "google_apis/gaia/gaia_oauth_client.h" | 26 #include "google_apis/gaia/gaia_oauth_client.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 114 |
113 // Init XMPP channel | 115 // Init XMPP channel |
114 void InitNotifications(const std::string& robot_email, | 116 void InitNotifications(const std::string& robot_email, |
115 const std::string& access_token); | 117 const std::string& access_token); |
116 | 118 |
117 void HandlePrinterNotification(const std::string& printer_id); | 119 void HandlePrinterNotification(const std::string& printer_id); |
118 void PollForJobs(); | 120 void PollForJobs(); |
119 // Schedules a task to poll for jobs. Does nothing if a task is already | 121 // Schedules a task to poll for jobs. Does nothing if a task is already |
120 // scheduled. | 122 // scheduled. |
121 void ScheduleJobPoll(); | 123 void ScheduleJobPoll(); |
| 124 void PingXmppServer(); |
| 125 void ScheduleXmppPing(); |
| 126 void CheckXmppPingStatus(); |
| 127 |
122 CloudPrintTokenStore* GetTokenStore(); | 128 CloudPrintTokenStore* GetTokenStore(); |
123 | 129 |
124 // Our parent CloudPrintProxyBackend | 130 // Our parent CloudPrintProxyBackend |
125 CloudPrintProxyBackend* backend_; | 131 CloudPrintProxyBackend* backend_; |
126 | 132 |
127 // Cloud Print authenticator. | 133 // Cloud Print authenticator. |
128 scoped_refptr<CloudPrintAuth> auth_; | 134 scoped_refptr<CloudPrintAuth> auth_; |
129 | 135 |
130 // Cloud Print connector. | 136 // Cloud Print connector. |
131 scoped_refptr<CloudPrintConnector> connector_; | 137 scoped_refptr<CloudPrintConnector> connector_; |
132 | 138 |
133 // OAuth client info. | 139 // OAuth client info. |
134 gaia::OAuthClientInfo oauth_client_info_; | 140 gaia::OAuthClientInfo oauth_client_info_; |
135 // Notification (xmpp) handler. | 141 // Notification (xmpp) handler. |
136 scoped_ptr<notifier::PushClient> push_client_; | 142 scoped_ptr<notifier::PushClient> push_client_; |
137 // Indicates whether XMPP notifications are currently enabled. | 143 // Indicates whether XMPP notifications are currently enabled. |
138 bool notifications_enabled_; | 144 bool notifications_enabled_; |
139 // The time when notifications were enabled. Valid only when | 145 // The time when notifications were enabled. Valid only when |
140 // notifications_enabled_ is true. | 146 // notifications_enabled_ is true. |
141 base::TimeTicks notifications_enabled_since_; | 147 base::TimeTicks notifications_enabled_since_; |
142 // Indicates whether a task to poll for jobs has been scheduled. | 148 // Indicates whether a task to poll for jobs has been scheduled. |
143 bool job_poll_scheduled_; | 149 bool job_poll_scheduled_; |
144 // Indicates whether we should poll for jobs when we lose XMPP connection. | 150 // Indicates whether we should poll for jobs when we lose XMPP connection. |
145 bool enable_job_poll_; | 151 bool enable_job_poll_; |
| 152 // Indicates whether a task to ping xmpp server has been scheduled. |
| 153 bool xmpp_ping_scheduled_; |
| 154 // Number of XMPP pings pending reply from the server. |
| 155 int pending_xmpp_pings_; |
146 // Connector settings. | 156 // Connector settings. |
147 ConnectorSettings settings_; | 157 ConnectorSettings settings_; |
| 158 std::string robot_email_; |
148 scoped_ptr<CloudPrintTokenStore> token_store_; | 159 scoped_ptr<CloudPrintTokenStore> token_store_; |
149 | 160 |
150 DISALLOW_COPY_AND_ASSIGN(Core); | 161 DISALLOW_COPY_AND_ASSIGN(Core); |
151 }; | 162 }; |
152 | 163 |
153 CloudPrintProxyBackend::CloudPrintProxyBackend( | 164 CloudPrintProxyBackend::CloudPrintProxyBackend( |
154 CloudPrintProxyFrontend* frontend, | 165 CloudPrintProxyFrontend* frontend, |
155 const ConnectorSettings& settings, | 166 const ConnectorSettings& settings, |
156 const gaia::OAuthClientInfo& oauth_client_info, | 167 const gaia::OAuthClientInfo& oauth_client_info, |
157 bool enable_job_poll) | 168 bool enable_job_poll) |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 | 244 |
234 CloudPrintProxyBackend::Core::Core( | 245 CloudPrintProxyBackend::Core::Core( |
235 CloudPrintProxyBackend* backend, | 246 CloudPrintProxyBackend* backend, |
236 const ConnectorSettings& settings, | 247 const ConnectorSettings& settings, |
237 const gaia::OAuthClientInfo& oauth_client_info, | 248 const gaia::OAuthClientInfo& oauth_client_info, |
238 bool enable_job_poll) | 249 bool enable_job_poll) |
239 : backend_(backend), | 250 : backend_(backend), |
240 oauth_client_info_(oauth_client_info), | 251 oauth_client_info_(oauth_client_info), |
241 notifications_enabled_(false), | 252 notifications_enabled_(false), |
242 job_poll_scheduled_(false), | 253 job_poll_scheduled_(false), |
243 enable_job_poll_(enable_job_poll) { | 254 enable_job_poll_(enable_job_poll), |
| 255 xmpp_ping_scheduled_(false), |
| 256 pending_xmpp_pings_(kDefaultXmppPingTimeoutSecs) { |
244 settings_.CopyFrom(settings); | 257 settings_.CopyFrom(settings); |
245 } | 258 } |
246 | 259 |
247 void CloudPrintProxyBackend::Core::CreateAuthAndConnector() { | 260 void CloudPrintProxyBackend::Core::CreateAuthAndConnector() { |
248 if (!auth_.get()) { | 261 if (!auth_.get()) { |
249 auth_ = new CloudPrintAuth(this, settings_.server_url(), oauth_client_info_, | 262 auth_ = new CloudPrintAuth(this, settings_.server_url(), oauth_client_info_, |
250 settings_.proxy_id()); | 263 settings_.proxy_id()); |
251 } | 264 } |
252 | 265 |
253 if (!connector_.get()) { | 266 if (!connector_.get()) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 } | 311 } |
299 | 312 |
300 void CloudPrintProxyBackend::Core::OnAuthenticationComplete( | 313 void CloudPrintProxyBackend::Core::OnAuthenticationComplete( |
301 const std::string& access_token, | 314 const std::string& access_token, |
302 const std::string& robot_oauth_refresh_token, | 315 const std::string& robot_oauth_refresh_token, |
303 const std::string& robot_email, | 316 const std::string& robot_email, |
304 const std::string& user_email) { | 317 const std::string& user_email) { |
305 CloudPrintTokenStore* token_store = GetTokenStore(); | 318 CloudPrintTokenStore* token_store = GetTokenStore(); |
306 bool first_time = token_store->token().empty(); | 319 bool first_time = token_store->token().empty(); |
307 token_store->SetToken(access_token); | 320 token_store->SetToken(access_token); |
| 321 robot_email_ = robot_email; |
308 // Let the frontend know that we have authenticated. | 322 // Let the frontend know that we have authenticated. |
309 backend_->frontend_loop_->PostTask( | 323 backend_->frontend_loop_->PostTask( |
310 FROM_HERE, | 324 FROM_HERE, |
311 base::Bind(&Core::NotifyAuthenticated, this, robot_oauth_refresh_token, | 325 base::Bind(&Core::NotifyAuthenticated, this, robot_oauth_refresh_token, |
312 robot_email, user_email)); | 326 robot_email, user_email)); |
313 if (first_time) { | 327 if (first_time) { |
314 InitNotifications(robot_email, access_token); | 328 InitNotifications(robot_email, access_token); |
315 } else { | 329 } else { |
316 // If we are refreshing a token, update the XMPP token too. | 330 // If we are refreshing a token, update the XMPP token too. |
317 DCHECK(push_client_.get()); | 331 DCHECK(push_client_.get()); |
(...skipping 25 matching lines...) Expand all Loading... |
343 | 357 |
344 // Refresh Auth token. | 358 // Refresh Auth token. |
345 auth_->RefreshAccessToken(); | 359 auth_->RefreshAccessToken(); |
346 } | 360 } |
347 | 361 |
348 void CloudPrintProxyBackend::Core::InitNotifications( | 362 void CloudPrintProxyBackend::Core::InitNotifications( |
349 const std::string& robot_email, | 363 const std::string& robot_email, |
350 const std::string& access_token) { | 364 const std::string& access_token) { |
351 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 365 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
352 | 366 |
| 367 pending_xmpp_pings_ = 0; |
353 notifier::NotifierOptions notifier_options; | 368 notifier::NotifierOptions notifier_options; |
354 notifier_options.request_context_getter = | 369 notifier_options.request_context_getter = |
355 g_service_process->GetServiceURLRequestContextGetter(); | 370 g_service_process->GetServiceURLRequestContextGetter(); |
356 notifier_options.auth_mechanism = "X-OAUTH2"; | 371 notifier_options.auth_mechanism = "X-OAUTH2"; |
357 notifier_options.try_ssltcp_first = true; | 372 notifier_options.try_ssltcp_first = true; |
358 push_client_ = notifier::PushClient::CreateDefault(notifier_options); | 373 push_client_ = notifier::PushClient::CreateDefault(notifier_options); |
359 push_client_->AddObserver(this); | 374 push_client_->AddObserver(this); |
360 notifier::Subscription subscription; | 375 notifier::Subscription subscription; |
361 subscription.channel = kCloudPrintPushNotificationsSource; | 376 subscription.channel = kCloudPrintPushNotificationsSource; |
362 subscription.from = kCloudPrintPushNotificationsSource; | 377 subscription.from = kCloudPrintPushNotificationsSource; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 base::TimeDelta interval = base::TimeDelta::FromSeconds( | 438 base::TimeDelta interval = base::TimeDelta::FromSeconds( |
424 base::RandInt(kMinJobPollIntervalSecs, kMaxJobPollIntervalSecs)); | 439 base::RandInt(kMinJobPollIntervalSecs, kMaxJobPollIntervalSecs)); |
425 MessageLoop::current()->PostDelayedTask( | 440 MessageLoop::current()->PostDelayedTask( |
426 FROM_HERE, | 441 FROM_HERE, |
427 base::Bind(&CloudPrintProxyBackend::Core::PollForJobs, this), | 442 base::Bind(&CloudPrintProxyBackend::Core::PollForJobs, this), |
428 interval); | 443 interval); |
429 job_poll_scheduled_ = true; | 444 job_poll_scheduled_ = true; |
430 } | 445 } |
431 } | 446 } |
432 | 447 |
| 448 void CloudPrintProxyBackend::Core::PingXmppServer() { |
| 449 xmpp_ping_scheduled_ = false; |
| 450 |
| 451 if (!push_client_.get()) |
| 452 return; |
| 453 |
| 454 notifier::Notification notification; |
| 455 notification.ping = true; |
| 456 push_client_->SendNotification(notification); |
| 457 |
| 458 pending_xmpp_pings_++; |
| 459 if (pending_xmpp_pings_ >= kMaxFailedXmppPings) { |
| 460 // Check ping status is we a close to the limit. |
| 461 MessageLoop::current()->PostDelayedTask( |
| 462 FROM_HERE, |
| 463 base::Bind(&CloudPrintProxyBackend::Core::CheckXmppPingStatus, this), |
| 464 base::TimeDelta::FromSeconds(kXmppPingCheckIntervalSecs)); |
| 465 } |
| 466 |
| 467 // Schedule next ping if needed. |
| 468 if (notifications_enabled_) |
| 469 ScheduleXmppPing(); |
| 470 } |
| 471 |
| 472 void CloudPrintProxyBackend::Core::ScheduleXmppPing() { |
| 473 if (!settings_.xmpp_ping_enabled()) |
| 474 return; |
| 475 |
| 476 if (!xmpp_ping_scheduled_) { |
| 477 base::TimeDelta interval = base::TimeDelta::FromSeconds( |
| 478 base::RandInt(settings_.xmpp_ping_timeout_sec() * 0.9, |
| 479 settings_.xmpp_ping_timeout_sec() * 1.1)); |
| 480 MessageLoop::current()->PostDelayedTask( |
| 481 FROM_HERE, |
| 482 base::Bind(&CloudPrintProxyBackend::Core::PingXmppServer, this), |
| 483 interval); |
| 484 xmpp_ping_scheduled_ = true; |
| 485 } |
| 486 } |
| 487 |
| 488 void CloudPrintProxyBackend::Core::CheckXmppPingStatus() { |
| 489 if (pending_xmpp_pings_ >= kMaxFailedXmppPings) { |
| 490 // Reconnect to XMPP. |
| 491 pending_xmpp_pings_ = 0; |
| 492 push_client_.reset(); |
| 493 InitNotifications(robot_email_, GetTokenStore()->token()); |
| 494 } |
| 495 } |
| 496 |
433 CloudPrintTokenStore* CloudPrintProxyBackend::Core::GetTokenStore() { | 497 CloudPrintTokenStore* CloudPrintProxyBackend::Core::GetTokenStore() { |
434 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 498 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
435 if (!token_store_.get()) | 499 if (!token_store_.get()) |
436 token_store_.reset(new CloudPrintTokenStore); | 500 token_store_.reset(new CloudPrintTokenStore); |
437 return token_store_.get(); | 501 return token_store_.get(); |
438 } | 502 } |
439 | 503 |
440 void CloudPrintProxyBackend::Core::NotifyAuthenticated( | 504 void CloudPrintProxyBackend::Core::NotifyAuthenticated( |
441 const std::string& robot_oauth_refresh_token, | 505 const std::string& robot_oauth_refresh_token, |
442 const std::string& robot_email, | 506 const std::string& robot_email, |
(...skipping 26 matching lines...) Expand all Loading... |
469 notifications_enabled_ = true; | 533 notifications_enabled_ = true; |
470 notifications_enabled_since_ = base::TimeTicks::Now(); | 534 notifications_enabled_since_ = base::TimeTicks::Now(); |
471 VLOG(1) << "Notifications for connector " << settings_.proxy_id() | 535 VLOG(1) << "Notifications for connector " << settings_.proxy_id() |
472 << " were enabled at " | 536 << " were enabled at " |
473 << notifications_enabled_since_.ToInternalValue(); | 537 << notifications_enabled_since_.ToInternalValue(); |
474 // Notifications just got re-enabled. In this case we want to schedule | 538 // Notifications just got re-enabled. In this case we want to schedule |
475 // a poll once for jobs we might have missed when we were dark. | 539 // a poll once for jobs we might have missed when we were dark. |
476 // Note that ScheduleJobPoll will not schedule again if a job poll task is | 540 // Note that ScheduleJobPoll will not schedule again if a job poll task is |
477 // already scheduled. | 541 // already scheduled. |
478 ScheduleJobPoll(); | 542 ScheduleJobPoll(); |
| 543 |
| 544 // Schedule periodic ping for XMPP notification channel. |
| 545 ScheduleXmppPing(); |
479 } | 546 } |
480 | 547 |
481 void CloudPrintProxyBackend::Core::OnNotificationsDisabled( | 548 void CloudPrintProxyBackend::Core::OnNotificationsDisabled( |
482 notifier::NotificationsDisabledReason reason) { | 549 notifier::NotificationsDisabledReason reason) { |
483 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 550 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
484 notifications_enabled_ = false; | 551 notifications_enabled_ = false; |
485 LOG(ERROR) << "Notifications for connector " << settings_.proxy_id() | 552 LOG(ERROR) << "Notifications for connector " << settings_.proxy_id() |
486 << " disabled."; | 553 << " disabled."; |
487 notifications_enabled_since_ = base::TimeTicks(); | 554 notifications_enabled_since_ = base::TimeTicks(); |
488 // We just lost notifications. This this case we want to schedule a | 555 // We just lost notifications. This this case we want to schedule a |
489 // job poll if enable_job_poll_ is true. | 556 // job poll if enable_job_poll_ is true. |
490 if (enable_job_poll_) | 557 if (enable_job_poll_) |
491 ScheduleJobPoll(); | 558 ScheduleJobPoll(); |
492 } | 559 } |
493 | 560 |
494 | 561 |
495 void CloudPrintProxyBackend::Core::OnIncomingNotification( | 562 void CloudPrintProxyBackend::Core::OnIncomingNotification( |
496 const notifier::Notification& notification) { | 563 const notifier::Notification& notification) { |
| 564 // Since we got some notification from the server (message or ping), |
| 565 // reset pending ping counter to 0. |
| 566 pending_xmpp_pings_ = 0; |
| 567 |
| 568 if (notification.ping) { |
| 569 VLOG(1) << "CP_CONNECTOR: Ping notification received."; |
| 570 return; |
| 571 } |
| 572 |
497 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 573 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
498 VLOG(1) << "CP_CONNECTOR: Incoming notification."; | 574 VLOG(1) << "CP_CONNECTOR: Incoming notification."; |
499 if (0 == base::strcasecmp(kCloudPrintPushNotificationsSource, | 575 if (0 == base::strcasecmp(kCloudPrintPushNotificationsSource, |
500 notification.channel.c_str())) | 576 notification.channel.c_str())) |
501 HandlePrinterNotification(notification.data); | 577 HandlePrinterNotification(notification.data); |
502 } | 578 } |
503 | 579 |
OLD | NEW |