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

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

Powered by Google App Engine
This is Rietveld 408576698