| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/md5.h" | 8 #include "base/md5.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/string_split.h" | 10 #include "base/string_split.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/common/net/http_return.h" | 14 #include "chrome/common/net/http_return.h" |
| 15 #include "chrome/service/cloud_print/cloud_print_consts.h" | 15 #include "chrome/service/cloud_print/cloud_print_consts.h" |
| 16 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 16 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
| 17 #include "chrome/service/cloud_print/printer_job_handler.h" | 17 #include "chrome/service/cloud_print/printer_job_handler.h" |
| 18 #include "chrome/service/gaia/service_gaia_authenticator.h" | 18 #include "chrome/service/gaia/service_gaia_authenticator.h" |
| 19 #include "chrome/service/service_process.h" | 19 #include "chrome/service/service_process.h" |
| 20 #include "jingle/notifier/base/notifier_options.h" | 20 #include "jingle/notifier/base/notifier_options.h" |
| 21 #include "jingle/notifier/listener/mediator_thread_impl.h" | 21 #include "jingle/notifier/listener/push_notifications_thread.h" |
| 22 #include "jingle/notifier/listener/talk_mediator_impl.h" | 22 #include "jingle/notifier/listener/talk_mediator_impl.h" |
| 23 | 23 |
| 24 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" |
| 25 #include "net/url_request/url_request_status.h" | 25 #include "net/url_request/url_request_status.h" |
| 26 | 26 |
| 27 // The real guts of SyncBackendHost, to keep the public client API clean. | 27 // The real guts of CloudPrintProxyBackend, to keep the public client API clean. |
| 28 class CloudPrintProxyBackend::Core | 28 class CloudPrintProxyBackend::Core |
| 29 : public base::RefCountedThreadSafe<CloudPrintProxyBackend::Core>, | 29 : public base::RefCountedThreadSafe<CloudPrintProxyBackend::Core>, |
| 30 public URLFetcherDelegate, | 30 public URLFetcherDelegate, |
| 31 public cloud_print::PrintServerWatcherDelegate, | 31 public cloud_print::PrintServerWatcherDelegate, |
| 32 public PrinterJobHandlerDelegate, | 32 public PrinterJobHandlerDelegate, |
| 33 public notifier::TalkMediator::Delegate { | 33 public notifier::TalkMediator::Delegate { |
| 34 public: | 34 public: |
| 35 // It is OK for print_server_url to be empty. In this case system should | 35 // It is OK for print_server_url to be empty. In this case system should |
| 36 // use system default (local) print server. | 36 // use system default (local) print server. |
| 37 explicit Core(CloudPrintProxyBackend* backend, | 37 explicit Core(CloudPrintProxyBackend* backend, |
| 38 const GURL& cloud_print_server_url, | 38 const GURL& cloud_print_server_url, |
| 39 const DictionaryValue* print_system_settings); | 39 const DictionaryValue* print_system_settings); |
| 40 | 40 |
| 41 // Note: | 41 // Note: |
| 42 // | 42 // |
| 43 // The Do* methods are the various entry points from CloudPrintProxyBackend | 43 // The Do* methods are the various entry points from CloudPrintProxyBackend |
| 44 // It calls us on a dedicated thread to actually perform synchronous | 44 // It calls us on a dedicated thread to actually perform synchronous |
| 45 // (and potentially blocking) syncapi operations. | 45 // (and potentially blocking) operations. |
| 46 // | 46 // |
| 47 // Called on the CloudPrintProxyBackend core_thread_ to perform | 47 // Called on the CloudPrintProxyBackend core_thread_ to perform |
| 48 // initialization. When we are passed in an LSID we authenticate using that | 48 // initialization. When we are passed in an LSID we authenticate using that |
| 49 // and retrieve new auth tokens. | 49 // and retrieve new auth tokens. |
| 50 void DoInitializeWithLsid(const std::string& lsid, | 50 void DoInitializeWithLsid(const std::string& lsid, |
| 51 const std::string& proxy_id); | 51 const std::string& proxy_id); |
| 52 void DoInitializeWithToken(const std::string cloud_print_token, | 52 void DoInitializeWithToken(const std::string cloud_print_token, |
| 53 const std::string cloud_print_xmpp_token, | 53 const std::string cloud_print_xmpp_token, |
| 54 const std::string email, | 54 const std::string email, |
| 55 const std::string& proxy_id); | 55 const std::string& proxy_id); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 ResponseHandler next_response_handler_; | 172 ResponseHandler next_response_handler_; |
| 173 scoped_refptr<cloud_print::PrintSystem::PrintServerWatcher> | 173 scoped_refptr<cloud_print::PrintSystem::PrintServerWatcher> |
| 174 print_server_watcher_; | 174 print_server_watcher_; |
| 175 bool new_printers_available_; | 175 bool new_printers_available_; |
| 176 // Notification (xmpp) handler. | 176 // Notification (xmpp) handler. |
| 177 scoped_ptr<notifier::TalkMediator> talk_mediator_; | 177 scoped_ptr<notifier::TalkMediator> talk_mediator_; |
| 178 // Indicates whether XMPP notifications are currently enabled. | 178 // Indicates whether XMPP notifications are currently enabled. |
| 179 bool notifications_enabled_; | 179 bool notifications_enabled_; |
| 180 // Indicates whether a task to poll for jobs has been scheduled. | 180 // Indicates whether a task to poll for jobs has been scheduled. |
| 181 bool job_poll_scheduled_; | 181 bool job_poll_scheduled_; |
| 182 // The channel we are interested in receiving push notifications for. |
| 183 // This is "cloudprint.google.com/proxy/<proxy_id>" |
| 184 std::string push_notifications_channel_; |
| 182 | 185 |
| 183 DISALLOW_COPY_AND_ASSIGN(Core); | 186 DISALLOW_COPY_AND_ASSIGN(Core); |
| 184 }; | 187 }; |
| 185 | 188 |
| 186 CloudPrintProxyBackend::CloudPrintProxyBackend( | 189 CloudPrintProxyBackend::CloudPrintProxyBackend( |
| 187 CloudPrintProxyFrontend* frontend, | 190 CloudPrintProxyFrontend* frontend, |
| 188 const GURL& cloud_print_server_url, | 191 const GURL& cloud_print_server_url, |
| 189 const DictionaryValue* print_system_settings) | 192 const DictionaryValue* print_system_settings) |
| 190 : core_thread_("Chrome_CloudPrintProxyCoreThread"), | 193 : core_thread_("Chrome_CloudPrintProxyCoreThread"), |
| 191 frontend_loop_(MessageLoop::current()), | 194 frontend_loop_(MessageLoop::current()), |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 NOTREACHED(); | 315 NOTREACHED(); |
| 313 return; // No print system available, fail initalization. | 316 return; // No print system available, fail initalization. |
| 314 } | 317 } |
| 315 | 318 |
| 316 // TODO(sanjeevr): Validate the tokens. | 319 // TODO(sanjeevr): Validate the tokens. |
| 317 auth_token_ = cloud_print_token; | 320 auth_token_ = cloud_print_token; |
| 318 | 321 |
| 319 const notifier::NotifierOptions kNotifierOptions; | 322 const notifier::NotifierOptions kNotifierOptions; |
| 320 const bool kInvalidateXmppAuthToken = false; | 323 const bool kInvalidateXmppAuthToken = false; |
| 321 talk_mediator_.reset(new notifier::TalkMediatorImpl( | 324 talk_mediator_.reset(new notifier::TalkMediatorImpl( |
| 322 new notifier::MediatorThreadImpl(kNotifierOptions), | 325 new notifier::PushNotificationsThread(kNotifierOptions, |
| 326 kCloudPrintPushNotificationsSource), |
| 323 kInvalidateXmppAuthToken)); | 327 kInvalidateXmppAuthToken)); |
| 324 talk_mediator_->AddSubscribedServiceUrl(kCloudPrintTalkServiceUrl); | 328 push_notifications_channel_ = kCloudPrintPushNotificationsSource; |
| 329 push_notifications_channel_.append("/proxy/"); |
| 330 push_notifications_channel_.append(proxy_id); |
| 331 talk_mediator_->AddSubscribedServiceUrl(push_notifications_channel_); |
| 325 talk_mediator_->SetDelegate(this); | 332 talk_mediator_->SetDelegate(this); |
| 326 talk_mediator_->SetAuthToken(email, cloud_print_xmpp_token, | 333 talk_mediator_->SetAuthToken(email, cloud_print_xmpp_token, |
| 327 kSyncGaiaServiceId); | 334 kSyncGaiaServiceId); |
| 328 talk_mediator_->Login(); | 335 talk_mediator_->Login(); |
| 329 | 336 |
| 330 print_server_watcher_ = print_system_->CreatePrintServerWatcher(); | 337 print_server_watcher_ = print_system_->CreatePrintServerWatcher(); |
| 331 print_server_watcher_->StartWatching(this); | 338 print_server_watcher_->StartWatching(this); |
| 332 | 339 |
| 333 proxy_id_ = proxy_id; | 340 proxy_id_ = proxy_id; |
| 334 StartRegistration(); | 341 StartRegistration(); |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 } else if (!previously_enabled) { | 727 } else if (!previously_enabled) { |
| 721 PollForJobs(); | 728 PollForJobs(); |
| 722 } | 729 } |
| 723 } | 730 } |
| 724 | 731 |
| 725 | 732 |
| 726 void CloudPrintProxyBackend::Core::OnIncomingNotification( | 733 void CloudPrintProxyBackend::Core::OnIncomingNotification( |
| 727 const IncomingNotificationData& notification_data) { | 734 const IncomingNotificationData& notification_data) { |
| 728 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 735 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 729 LOG(INFO) << "CP_PROXY: Incoming notification."; | 736 LOG(INFO) << "CP_PROXY: Incoming notification."; |
| 730 if (0 == base::strcasecmp(kCloudPrintTalkServiceUrl, | 737 if (0 == base::strcasecmp(push_notifications_channel_.c_str(), |
| 731 notification_data.service_url.c_str())) { | 738 notification_data.service_url.c_str())) { |
| 732 HandlePrinterNotification(notification_data.service_specific_data); | 739 HandlePrinterNotification(notification_data.service_specific_data); |
| 733 } | 740 } |
| 734 } | 741 } |
| 735 | 742 |
| 736 void CloudPrintProxyBackend::Core::OnOutgoingNotification() {} | 743 void CloudPrintProxyBackend::Core::OnOutgoingNotification() {} |
| 737 | 744 |
| 738 // cloud_print::PrinterChangeNotifier::Delegate implementation | 745 // cloud_print::PrinterChangeNotifier::Delegate implementation |
| 739 void CloudPrintProxyBackend::Core::OnPrinterAdded() { | 746 void CloudPrintProxyBackend::Core::OnPrinterAdded() { |
| 740 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 747 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 741 if (request_.get()) { | 748 if (request_.get()) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 753 job_handler_map_.erase(printer_id); | 760 job_handler_map_.erase(printer_id); |
| 754 } | 761 } |
| 755 | 762 |
| 756 void CloudPrintProxyBackend::Core::OnAuthError() { | 763 void CloudPrintProxyBackend::Core::OnAuthError() { |
| 757 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 764 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 758 LOG(INFO) << "CP_PROXY: Auth Error"; | 765 LOG(INFO) << "CP_PROXY: Auth Error"; |
| 759 backend_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, | 766 backend_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, |
| 760 &Core::NotifyAuthenticationFailed)); | 767 &Core::NotifyAuthenticationFailed)); |
| 761 } | 768 } |
| 762 | 769 |
| OLD | NEW |