| 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/compiler_specific.h" |
| 11 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 12 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "chrome/common/net/gaia/gaia_oauth_client.h" | 15 #include "chrome/common/net/gaia/gaia_oauth_client.h" |
| 15 #include "chrome/common/net/gaia/gaia_urls.h" | 16 #include "chrome/common/net/gaia/gaia_urls.h" |
| 16 #include "chrome/service/cloud_print/cloud_print_auth.h" | 17 #include "chrome/service/cloud_print/cloud_print_auth.h" |
| 17 #include "chrome/service/cloud_print/cloud_print_connector.h" | 18 #include "chrome/service/cloud_print/cloud_print_connector.h" |
| 18 #include "chrome/service/cloud_print/cloud_print_consts.h" | 19 #include "chrome/service/cloud_print/cloud_print_consts.h" |
| 19 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 20 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
| 20 #include "chrome/service/cloud_print/cloud_print_token_store.h" | 21 #include "chrome/service/cloud_print/cloud_print_token_store.h" |
| 21 #include "chrome/service/gaia/service_gaia_authenticator.h" | 22 #include "chrome/service/gaia/service_gaia_authenticator.h" |
| 22 #include "chrome/service/net/service_url_request_context.h" | 23 #include "chrome/service/net/service_url_request_context.h" |
| 23 #include "chrome/service/service_process.h" | 24 #include "chrome/service/service_process.h" |
| 24 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
| 25 #include "grit/generated_resources.h" | 26 #include "grit/generated_resources.h" |
| 26 #include "jingle/notifier/base/notifier_options.h" | 27 #include "jingle/notifier/base/notifier_options.h" |
| 27 #include "jingle/notifier/listener/mediator_thread_impl.h" | 28 #include "jingle/notifier/listener/push_client.h" |
| 28 #include "jingle/notifier/listener/talk_mediator_impl.h" | |
| 29 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| 30 | 30 |
| 31 // The real guts of CloudPrintProxyBackend, to keep the public client API clean. | 31 // The real guts of CloudPrintProxyBackend, to keep the public client API clean. |
| 32 class CloudPrintProxyBackend::Core | 32 class CloudPrintProxyBackend::Core |
| 33 : public base::RefCountedThreadSafe<CloudPrintProxyBackend::Core>, | 33 : public base::RefCountedThreadSafe<CloudPrintProxyBackend::Core>, |
| 34 public CloudPrintAuth::Client, | 34 public CloudPrintAuth::Client, |
| 35 public CloudPrintConnector::Client, | 35 public CloudPrintConnector::Client, |
| 36 public notifier::TalkMediator::Delegate { | 36 public notifier::PushClient::Observer { |
| 37 public: | 37 public: |
| 38 // It is OK for print_server_url to be empty. In this case system should | 38 // It is OK for print_server_url to be empty. In this case system should |
| 39 // use system default (local) print server. | 39 // use system default (local) print server. |
| 40 Core(CloudPrintProxyBackend* backend, | 40 Core(CloudPrintProxyBackend* backend, |
| 41 const std::string& proxy_id, | 41 const std::string& proxy_id, |
| 42 const GURL& cloud_print_server_url, | 42 const GURL& cloud_print_server_url, |
| 43 const DictionaryValue* print_system_settings, | 43 const DictionaryValue* print_system_settings, |
| 44 const gaia::OAuthClientInfo& oauth_client_info, | 44 const gaia::OAuthClientInfo& oauth_client_info, |
| 45 bool enable_job_poll); | 45 bool enable_job_poll); |
| 46 | 46 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 73 void DoShutdown(); | 73 void DoShutdown(); |
| 74 void DoRegisterSelectedPrinters( | 74 void DoRegisterSelectedPrinters( |
| 75 const printing::PrinterList& printer_list); | 75 const printing::PrinterList& printer_list); |
| 76 void DoUnregisterPrinters(); | 76 void DoUnregisterPrinters(); |
| 77 | 77 |
| 78 // CloudPrintAuth::Client implementation. | 78 // CloudPrintAuth::Client implementation. |
| 79 virtual void OnAuthenticationComplete( | 79 virtual void OnAuthenticationComplete( |
| 80 const std::string& access_token, | 80 const std::string& access_token, |
| 81 const std::string& robot_oauth_refresh_token, | 81 const std::string& robot_oauth_refresh_token, |
| 82 const std::string& robot_email, | 82 const std::string& robot_email, |
| 83 const std::string& user_email); | 83 const std::string& user_email) OVERRIDE; |
| 84 virtual void OnInvalidCredentials(); | 84 virtual void OnInvalidCredentials() OVERRIDE; |
| 85 | 85 |
| 86 // CloudPrintConnector::Client implementation. | 86 // CloudPrintConnector::Client implementation. |
| 87 virtual void OnAuthFailed(); | 87 virtual void OnAuthFailed() OVERRIDE; |
| 88 | 88 |
| 89 // notifier::TalkMediator::Delegate implementation. | 89 // notifier::PushClient::Delegate implementation. |
| 90 virtual void OnNotificationStateChange( | 90 virtual void OnNotificationStateChange( |
| 91 bool notifications_enabled); | 91 bool notifications_enabled) OVERRIDE; |
| 92 virtual void OnIncomingNotification( | 92 virtual void OnIncomingNotification( |
| 93 const notifier::Notification& notification); | 93 const notifier::Notification& notification) OVERRIDE; |
| 94 virtual void OnOutgoingNotification(); | |
| 95 | 94 |
| 96 private: | 95 private: |
| 97 friend class base::RefCountedThreadSafe<Core>; | 96 friend class base::RefCountedThreadSafe<Core>; |
| 98 | 97 |
| 99 virtual ~Core() {} | 98 virtual ~Core() {} |
| 100 | 99 |
| 101 void CreateAuthAndConnector(); | 100 void CreateAuthAndConnector(); |
| 102 void DestroyAuthAndConnector(); | 101 void DestroyAuthAndConnector(); |
| 103 | 102 |
| 104 // NotifyXXX is how the Core communicates with the frontend across | 103 // NotifyXXX is how the Core communicates with the frontend across |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 | 135 |
| 137 // Server URL. | 136 // Server URL. |
| 138 GURL cloud_print_server_url_; | 137 GURL cloud_print_server_url_; |
| 139 // Proxy Id. | 138 // Proxy Id. |
| 140 std::string proxy_id_; | 139 std::string proxy_id_; |
| 141 // Print system settings. | 140 // Print system settings. |
| 142 scoped_ptr<DictionaryValue> print_system_settings_; | 141 scoped_ptr<DictionaryValue> print_system_settings_; |
| 143 // OAuth client info. | 142 // OAuth client info. |
| 144 gaia::OAuthClientInfo oauth_client_info_; | 143 gaia::OAuthClientInfo oauth_client_info_; |
| 145 // Notification (xmpp) handler. | 144 // Notification (xmpp) handler. |
| 146 scoped_ptr<notifier::TalkMediator> talk_mediator_; | 145 scoped_ptr<notifier::PushClient> push_client_; |
| 147 // Indicates whether XMPP notifications are currently enabled. | 146 // Indicates whether XMPP notifications are currently enabled. |
| 148 bool notifications_enabled_; | 147 bool notifications_enabled_; |
| 149 // The time when notifications were enabled. Valid only when | 148 // The time when notifications were enabled. Valid only when |
| 150 // notifications_enabled_ is true. | 149 // notifications_enabled_ is true. |
| 151 base::TimeTicks notifications_enabled_since_; | 150 base::TimeTicks notifications_enabled_since_; |
| 152 // Indicates whether a task to poll for jobs has been scheduled. | 151 // Indicates whether a task to poll for jobs has been scheduled. |
| 153 bool job_poll_scheduled_; | 152 bool job_poll_scheduled_; |
| 154 // Indicates whether we should poll for jobs when we lose XMPP connection. | 153 // Indicates whether we should poll for jobs when we lose XMPP connection. |
| 155 bool enable_job_poll_; | 154 bool enable_job_poll_; |
| 156 scoped_ptr<CloudPrintTokenStore> token_store_; | 155 scoped_ptr<CloudPrintTokenStore> token_store_; |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 token_store->SetToken(access_token); | 343 token_store->SetToken(access_token); |
| 345 // Let the frontend know that we have authenticated. | 344 // Let the frontend know that we have authenticated. |
| 346 backend_->frontend_loop_->PostTask( | 345 backend_->frontend_loop_->PostTask( |
| 347 FROM_HERE, | 346 FROM_HERE, |
| 348 base::Bind(&Core::NotifyAuthenticated, this, robot_oauth_refresh_token, | 347 base::Bind(&Core::NotifyAuthenticated, this, robot_oauth_refresh_token, |
| 349 robot_email, user_email)); | 348 robot_email, user_email)); |
| 350 if (first_time) { | 349 if (first_time) { |
| 351 InitNotifications(robot_email, access_token); | 350 InitNotifications(robot_email, access_token); |
| 352 } else { | 351 } else { |
| 353 // If we are refreshing a token, update the XMPP token too. | 352 // If we are refreshing a token, update the XMPP token too. |
| 354 DCHECK(talk_mediator_.get()); | 353 DCHECK(push_client_.get()); |
| 355 talk_mediator_->SetAuthToken(robot_email, | 354 push_client_->UpdateCredentials(robot_email, access_token); |
| 356 access_token, | |
| 357 kSyncGaiaServiceId); | |
| 358 } | 355 } |
| 359 // Start cloud print connector if needed. | 356 // Start cloud print connector if needed. |
| 360 if (!connector_->IsRunning()) { | 357 if (!connector_->IsRunning()) { |
| 361 if (!connector_->Start()) { | 358 if (!connector_->Start()) { |
| 362 // Let the frontend know that we do not have a print system. | 359 // Let the frontend know that we do not have a print system. |
| 363 backend_->frontend_loop_->PostTask( | 360 backend_->frontend_loop_->PostTask( |
| 364 FROM_HERE, base::Bind(&Core::NotifyPrintSystemUnavailable, this)); | 361 FROM_HERE, base::Bind(&Core::NotifyPrintSystemUnavailable, this)); |
| 365 } | 362 } |
| 366 } | 363 } |
| 367 } | 364 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 386 | 383 |
| 387 void CloudPrintProxyBackend::Core::InitNotifications( | 384 void CloudPrintProxyBackend::Core::InitNotifications( |
| 388 const std::string& robot_email, | 385 const std::string& robot_email, |
| 389 const std::string& access_token) { | 386 const std::string& access_token) { |
| 390 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 387 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 391 | 388 |
| 392 notifier::NotifierOptions notifier_options; | 389 notifier::NotifierOptions notifier_options; |
| 393 notifier_options.request_context_getter = | 390 notifier_options.request_context_getter = |
| 394 g_service_process->GetServiceURLRequestContextGetter(); | 391 g_service_process->GetServiceURLRequestContextGetter(); |
| 395 notifier_options.auth_mechanism = "X-OAUTH2"; | 392 notifier_options.auth_mechanism = "X-OAUTH2"; |
| 396 talk_mediator_.reset(new notifier::TalkMediatorImpl( | 393 push_client_.reset(new notifier::PushClient(notifier_options)); |
| 397 new notifier::MediatorThreadImpl(notifier_options), | 394 push_client_->AddObserver(this); |
| 398 notifier_options)); | |
| 399 notifier::Subscription subscription; | 395 notifier::Subscription subscription; |
| 400 subscription.channel = kCloudPrintPushNotificationsSource; | 396 subscription.channel = kCloudPrintPushNotificationsSource; |
| 401 subscription.from = kCloudPrintPushNotificationsSource; | 397 subscription.from = kCloudPrintPushNotificationsSource; |
| 402 talk_mediator_->AddSubscription(subscription); | 398 push_client_->UpdateSubscriptions( |
| 403 talk_mediator_->SetDelegate(this); | 399 notifier::SubscriptionList(1, subscription)); |
| 404 talk_mediator_->SetAuthToken(robot_email, access_token, kSyncGaiaServiceId); | 400 push_client_->UpdateCredentials(robot_email, access_token); |
| 405 talk_mediator_->Login(); | |
| 406 } | 401 } |
| 407 | 402 |
| 408 void CloudPrintProxyBackend::Core::DoShutdown() { | 403 void CloudPrintProxyBackend::Core::DoShutdown() { |
| 409 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 404 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 410 VLOG(1) << "CP_CONNECTOR: Shutdown connector, id: " << proxy_id_; | 405 VLOG(1) << "CP_CONNECTOR: Shutdown connector, id: " << proxy_id_; |
| 411 | 406 |
| 412 if (connector_->IsRunning()) | 407 if (connector_->IsRunning()) |
| 413 connector_->Stop(); | 408 connector_->Stop(); |
| 414 | 409 |
| 415 // Important to delete the TalkMediator on this thread. | 410 // Important to delete the PushClient on this thread. |
| 416 if (talk_mediator_.get()) | 411 if (push_client_.get()) { |
| 417 talk_mediator_->Logout(); | 412 push_client_->RemoveObserver(this); |
| 418 talk_mediator_.reset(); | 413 } |
| 414 push_client_.reset(); |
| 419 notifications_enabled_ = false; | 415 notifications_enabled_ = false; |
| 420 notifications_enabled_since_ = base::TimeTicks(); | 416 notifications_enabled_since_ = base::TimeTicks(); |
| 421 token_store_.reset(); | 417 token_store_.reset(); |
| 422 | 418 |
| 423 DestroyAuthAndConnector(); | 419 DestroyAuthAndConnector(); |
| 424 } | 420 } |
| 425 | 421 |
| 426 void CloudPrintProxyBackend::Core::DoUnregisterPrinters() { | 422 void CloudPrintProxyBackend::Core::DoUnregisterPrinters() { |
| 427 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 423 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 428 | 424 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 529 | 525 |
| 530 | 526 |
| 531 void CloudPrintProxyBackend::Core::OnIncomingNotification( | 527 void CloudPrintProxyBackend::Core::OnIncomingNotification( |
| 532 const notifier::Notification& notification) { | 528 const notifier::Notification& notification) { |
| 533 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 529 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 534 VLOG(1) << "CP_CONNECTOR: Incoming notification."; | 530 VLOG(1) << "CP_CONNECTOR: Incoming notification."; |
| 535 if (0 == base::strcasecmp(kCloudPrintPushNotificationsSource, | 531 if (0 == base::strcasecmp(kCloudPrintPushNotificationsSource, |
| 536 notification.channel.c_str())) | 532 notification.channel.c_str())) |
| 537 HandlePrinterNotification(notification.data); | 533 HandlePrinterNotification(notification.data); |
| 538 } | 534 } |
| 539 | |
| 540 void CloudPrintProxyBackend::Core::OnOutgoingNotification() {} | |
| OLD | NEW |