Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: chrome/service/cloud_print/cloud_print_proxy_backend.cc

Issue 11232048: Adding XMPP ping functionality to CLoudPrint. XMPP ping and timeout is controlled thorugh Service S… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 we should ping XMPP connection.
153 bool xmpp_ping_enabled_;
154 // Indicates timeout between XMPP pings. ) if XMPP pings are disabled.
Vitaly Buka (NO REVIEWS) 2012/10/22 20:52:53 ")"?
gene 2012/10/22 21:44:12 Done.
155 int xmpp_ping_timeout_sec_;
156 // Indicates whether a task to ping xmpp server has been scheduled.
157 bool xmpp_ping_scheduled_;
Vitaly Buka (NO REVIEWS) 2012/10/22 20:52:53 you should use xmpp_ping_timeout_sec_ and xmpp_pin
gene 2012/10/22 21:44:12 unfortunately not :(. current run-time settings ma
158 // Number of XMPP pings pending reply from the server.
159 int pending_xmpp_pings_;
146 // Connector settings. 160 // Connector settings.
147 ConnectorSettings settings_; 161 ConnectorSettings settings_;
162 std::string robot_email_;
Vitaly Buka (NO REVIEWS) 2012/10/22 20:52:53 Not sure, but maybe better solution if you remove
gene 2012/10/22 21:44:12 not sure what you mean here, lets discuss offline
148 scoped_ptr<CloudPrintTokenStore> token_store_; 163 scoped_ptr<CloudPrintTokenStore> token_store_;
149 164
150 DISALLOW_COPY_AND_ASSIGN(Core); 165 DISALLOW_COPY_AND_ASSIGN(Core);
151 }; 166 };
152 167
153 CloudPrintProxyBackend::CloudPrintProxyBackend( 168 CloudPrintProxyBackend::CloudPrintProxyBackend(
154 CloudPrintProxyFrontend* frontend, 169 CloudPrintProxyFrontend* frontend,
155 const ConnectorSettings& settings, 170 const ConnectorSettings& settings,
156 const gaia::OAuthClientInfo& oauth_client_info, 171 const gaia::OAuthClientInfo& oauth_client_info,
157 bool enable_job_poll) 172 bool enable_job_poll)
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 248
234 CloudPrintProxyBackend::Core::Core( 249 CloudPrintProxyBackend::Core::Core(
235 CloudPrintProxyBackend* backend, 250 CloudPrintProxyBackend* backend,
236 const ConnectorSettings& settings, 251 const ConnectorSettings& settings,
237 const gaia::OAuthClientInfo& oauth_client_info, 252 const gaia::OAuthClientInfo& oauth_client_info,
238 bool enable_job_poll) 253 bool enable_job_poll)
239 : backend_(backend), 254 : backend_(backend),
240 oauth_client_info_(oauth_client_info), 255 oauth_client_info_(oauth_client_info),
241 notifications_enabled_(false), 256 notifications_enabled_(false),
242 job_poll_scheduled_(false), 257 job_poll_scheduled_(false),
243 enable_job_poll_(enable_job_poll) { 258 enable_job_poll_(enable_job_poll),
259 xmpp_ping_timeout_sec_(0),
260 xmpp_ping_scheduled_(false),
261 xmpp_ping_enabled_(false),
262 pending_xmpp_pings_(kDefaultXmppPingTimeoutSecs) {
244 settings_.CopyFrom(settings); 263 settings_.CopyFrom(settings);
264 xmpp_ping_enabled_ = settings_.xmpp_ping_enabled();
265 xmpp_ping_timeout_sec_ = settings_.xmpp_ping_timeout_sec();
266 if (xmpp_ping_timeout_sec_ < kMinimumXmppPingTimeoutSecs) {
267 VLOG(1) << "CP_CONNECTOR: XMPP ping timeout is less then minimal value";
Vitaly Buka (NO REVIEWS) 2012/10/22 20:52:53 LOG(WARNING) ?
Vitaly Buka (NO REVIEWS) 2012/10/22 20:52:53 ConnectorSettings::InitFrom is best place to valid
gene 2012/10/22 21:44:12 Done.
268 xmpp_ping_timeout_sec_ = kMinimumXmppPingTimeoutSecs;
269 }
245 } 270 }
246 271
247 void CloudPrintProxyBackend::Core::CreateAuthAndConnector() { 272 void CloudPrintProxyBackend::Core::CreateAuthAndConnector() {
248 if (!auth_.get()) { 273 if (!auth_.get()) {
249 auth_ = new CloudPrintAuth(this, settings_.server_url(), oauth_client_info_, 274 auth_ = new CloudPrintAuth(this, settings_.server_url(), oauth_client_info_,
250 settings_.proxy_id()); 275 settings_.proxy_id());
251 } 276 }
252 277
253 if (!connector_.get()) { 278 if (!connector_.get()) {
254 connector_ = new CloudPrintConnector(this, settings_); 279 connector_ = new CloudPrintConnector(this, settings_);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 } 323 }
299 324
300 void CloudPrintProxyBackend::Core::OnAuthenticationComplete( 325 void CloudPrintProxyBackend::Core::OnAuthenticationComplete(
301 const std::string& access_token, 326 const std::string& access_token,
302 const std::string& robot_oauth_refresh_token, 327 const std::string& robot_oauth_refresh_token,
303 const std::string& robot_email, 328 const std::string& robot_email,
304 const std::string& user_email) { 329 const std::string& user_email) {
305 CloudPrintTokenStore* token_store = GetTokenStore(); 330 CloudPrintTokenStore* token_store = GetTokenStore();
306 bool first_time = token_store->token().empty(); 331 bool first_time = token_store->token().empty();
307 token_store->SetToken(access_token); 332 token_store->SetToken(access_token);
333 robot_email_ = robot_email;
308 // Let the frontend know that we have authenticated. 334 // Let the frontend know that we have authenticated.
309 backend_->frontend_loop_->PostTask( 335 backend_->frontend_loop_->PostTask(
310 FROM_HERE, 336 FROM_HERE,
311 base::Bind(&Core::NotifyAuthenticated, this, robot_oauth_refresh_token, 337 base::Bind(&Core::NotifyAuthenticated, this, robot_oauth_refresh_token,
312 robot_email, user_email)); 338 robot_email, user_email));
313 if (first_time) { 339 if (first_time) {
314 InitNotifications(robot_email, access_token); 340 InitNotifications(robot_email, access_token);
315 } else { 341 } else {
316 // If we are refreshing a token, update the XMPP token too. 342 // If we are refreshing a token, update the XMPP token too.
317 DCHECK(push_client_.get()); 343 DCHECK(push_client_.get());
(...skipping 25 matching lines...) Expand all
343 369
344 // Refresh Auth token. 370 // Refresh Auth token.
345 auth_->RefreshAccessToken(); 371 auth_->RefreshAccessToken();
346 } 372 }
347 373
348 void CloudPrintProxyBackend::Core::InitNotifications( 374 void CloudPrintProxyBackend::Core::InitNotifications(
349 const std::string& robot_email, 375 const std::string& robot_email,
350 const std::string& access_token) { 376 const std::string& access_token) {
351 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); 377 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop());
352 378
379 pending_xmpp_pings_ = 0;
353 notifier::NotifierOptions notifier_options; 380 notifier::NotifierOptions notifier_options;
354 notifier_options.request_context_getter = 381 notifier_options.request_context_getter =
355 g_service_process->GetServiceURLRequestContextGetter(); 382 g_service_process->GetServiceURLRequestContextGetter();
356 notifier_options.auth_mechanism = "X-OAUTH2"; 383 notifier_options.auth_mechanism = "X-OAUTH2";
357 notifier_options.try_ssltcp_first = true; 384 notifier_options.try_ssltcp_first = true;
358 push_client_ = notifier::PushClient::CreateDefault(notifier_options); 385 push_client_ = notifier::PushClient::CreateDefault(notifier_options);
359 push_client_->AddObserver(this); 386 push_client_->AddObserver(this);
360 notifier::Subscription subscription; 387 notifier::Subscription subscription;
361 subscription.channel = kCloudPrintPushNotificationsSource; 388 subscription.channel = kCloudPrintPushNotificationsSource;
362 subscription.from = kCloudPrintPushNotificationsSource; 389 subscription.from = kCloudPrintPushNotificationsSource;
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 base::TimeDelta interval = base::TimeDelta::FromSeconds( 450 base::TimeDelta interval = base::TimeDelta::FromSeconds(
424 base::RandInt(kMinJobPollIntervalSecs, kMaxJobPollIntervalSecs)); 451 base::RandInt(kMinJobPollIntervalSecs, kMaxJobPollIntervalSecs));
425 MessageLoop::current()->PostDelayedTask( 452 MessageLoop::current()->PostDelayedTask(
426 FROM_HERE, 453 FROM_HERE,
427 base::Bind(&CloudPrintProxyBackend::Core::PollForJobs, this), 454 base::Bind(&CloudPrintProxyBackend::Core::PollForJobs, this),
428 interval); 455 interval);
429 job_poll_scheduled_ = true; 456 job_poll_scheduled_ = true;
430 } 457 }
431 } 458 }
432 459
460 void CloudPrintProxyBackend::Core::PingXmppServer() {
461 xmpp_ping_scheduled_ = false;
462
463 if (!push_client_.get())
464 return;
465
466 notifier::Notification notification;
467 notification.ping = true;
468 push_client_->SendNotification(notification);
469
470 pending_xmpp_pings_++;
471 if (pending_xmpp_pings_ >= kMaxFailedXmppPings) {
472 // Check ping status is we a close to the limit.
473 MessageLoop::current()->PostDelayedTask(
474 FROM_HERE,
475 base::Bind(&CloudPrintProxyBackend::Core::CheckXmppPingStatus, this),
476 base::TimeDelta::FromSeconds(kXmppPingCheckIntervalSecs));
477 }
478
479 // Schedule next ping if needed.
480 if (notifications_enabled_)
481 ScheduleXmppPing();
482 }
483
484 void CloudPrintProxyBackend::Core::ScheduleXmppPing() {
485 if (!xmpp_ping_enabled_)
486 return;
487
488 if (!xmpp_ping_scheduled_) {
489 base::TimeDelta interval = base::TimeDelta::FromSeconds(
490 base::RandInt(xmpp_ping_timeout_sec_ * 0.9,
491 xmpp_ping_timeout_sec_ * 1.1));
492 MessageLoop::current()->PostDelayedTask(
493 FROM_HERE,
494 base::Bind(&CloudPrintProxyBackend::Core::PingXmppServer, this),
495 interval);
496 xmpp_ping_scheduled_ = true;
497 }
498 }
499
500 void CloudPrintProxyBackend::Core::CheckXmppPingStatus() {
501 if (pending_xmpp_pings_ >= kMaxFailedXmppPings) {
502 // Reconnect to XMPP.
503 pending_xmpp_pings_ = 0;
504 push_client_.reset();
505 InitNotifications(robot_email_, GetTokenStore()->token());
506 }
507 }
508
433 CloudPrintTokenStore* CloudPrintProxyBackend::Core::GetTokenStore() { 509 CloudPrintTokenStore* CloudPrintProxyBackend::Core::GetTokenStore() {
434 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); 510 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop());
435 if (!token_store_.get()) 511 if (!token_store_.get())
436 token_store_.reset(new CloudPrintTokenStore); 512 token_store_.reset(new CloudPrintTokenStore);
437 return token_store_.get(); 513 return token_store_.get();
438 } 514 }
439 515
440 void CloudPrintProxyBackend::Core::NotifyAuthenticated( 516 void CloudPrintProxyBackend::Core::NotifyAuthenticated(
441 const std::string& robot_oauth_refresh_token, 517 const std::string& robot_oauth_refresh_token,
442 const std::string& robot_email, 518 const std::string& robot_email,
(...skipping 26 matching lines...) Expand all
469 notifications_enabled_ = true; 545 notifications_enabled_ = true;
470 notifications_enabled_since_ = base::TimeTicks::Now(); 546 notifications_enabled_since_ = base::TimeTicks::Now();
471 VLOG(1) << "Notifications for connector " << settings_.proxy_id() 547 VLOG(1) << "Notifications for connector " << settings_.proxy_id()
472 << " were enabled at " 548 << " were enabled at "
473 << notifications_enabled_since_.ToInternalValue(); 549 << notifications_enabled_since_.ToInternalValue();
474 // Notifications just got re-enabled. In this case we want to schedule 550 // 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. 551 // 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 552 // Note that ScheduleJobPoll will not schedule again if a job poll task is
477 // already scheduled. 553 // already scheduled.
478 ScheduleJobPoll(); 554 ScheduleJobPoll();
555
556 // Schedule periodic ping for XMPP notification channel.
557 ScheduleXmppPing();
479 } 558 }
480 559
481 void CloudPrintProxyBackend::Core::OnNotificationsDisabled( 560 void CloudPrintProxyBackend::Core::OnNotificationsDisabled(
482 notifier::NotificationsDisabledReason reason) { 561 notifier::NotificationsDisabledReason reason) {
483 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); 562 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop());
484 notifications_enabled_ = false; 563 notifications_enabled_ = false;
485 LOG(ERROR) << "Notifications for connector " << settings_.proxy_id() 564 LOG(ERROR) << "Notifications for connector " << settings_.proxy_id()
486 << " disabled."; 565 << " disabled.";
487 notifications_enabled_since_ = base::TimeTicks(); 566 notifications_enabled_since_ = base::TimeTicks();
488 // We just lost notifications. This this case we want to schedule a 567 // We just lost notifications. This this case we want to schedule a
489 // job poll if enable_job_poll_ is true. 568 // job poll if enable_job_poll_ is true.
490 if (enable_job_poll_) 569 if (enable_job_poll_)
491 ScheduleJobPoll(); 570 ScheduleJobPoll();
492 } 571 }
493 572
494 573
495 void CloudPrintProxyBackend::Core::OnIncomingNotification( 574 void CloudPrintProxyBackend::Core::OnIncomingNotification(
496 const notifier::Notification& notification) { 575 const notifier::Notification& notification) {
576 // Since we got some notification from the server (message or ping),
577 // reset pending ping counter to 0.
578 pending_xmpp_pings_ = 0;
579
580 if (notification.ping) {
581 VLOG(1) << "CP_CONNECTOR: Ping notification received.";
582 return;
583 }
584
497 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); 585 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop());
498 VLOG(1) << "CP_CONNECTOR: Incoming notification."; 586 VLOG(1) << "CP_CONNECTOR: Incoming notification.";
499 if (0 == base::strcasecmp(kCloudPrintPushNotificationsSource, 587 if (0 == base::strcasecmp(kCloudPrintPushNotificationsSource,
500 notification.channel.c_str())) 588 notification.channel.c_str()))
501 HandlePrinterNotification(notification.data); 589 HandlePrinterNotification(notification.data);
502 } 590 }
503 591
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698