Chromium Code Reviews| Index: chrome/service/cloud_print/cloud_print_proxy_backend.cc |
| =================================================================== |
| --- chrome/service/cloud_print/cloud_print_proxy_backend.cc (revision 70946) |
| +++ chrome/service/cloud_print/cloud_print_proxy_backend.cc (working copy) |
| @@ -7,6 +7,7 @@ |
| #include <map> |
| #include <vector> |
| +#include "app/l10n_util.h" |
| #include "base/file_util.h" |
| #include "base/md5.h" |
| #include "base/rand_util.h" |
| @@ -22,6 +23,7 @@ |
| #include "chrome/service/gaia/service_gaia_authenticator.h" |
| #include "chrome/service/service_process.h" |
| #include "googleurl/src/gurl.h" |
| +#include "grit/generated_resources.h" |
| #include "jingle/notifier/base/notifier_options.h" |
| #include "jingle/notifier/listener/push_notifications_thread.h" |
| #include "jingle/notifier/listener/talk_mediator_impl.h" |
| @@ -106,6 +108,18 @@ |
| const GURL& url, |
| DictionaryValue* json_data, |
| bool succeeded); |
| + |
| + CloudPrintURLFetcher::ResponseAction HandleRegisterFailedStatusResponse( |
| + const URLFetcher* source, |
| + const GURL& url, |
| + DictionaryValue* json_data, |
| + bool succeeded); |
| + |
| +CloudPrintURLFetcher::ResponseAction HandlePrintSystemUnavailableResponse( |
| + const URLFetcher* source, |
| + const GURL& url, |
| + DictionaryValue* json_data, |
| + bool succeeded); |
| // End response handlers |
| // NotifyXXX is how the Core communicates with the frontend across |
| @@ -117,6 +131,7 @@ |
| const std::string& cloud_print_xmpp_token, |
| const std::string& email); |
| void NotifyAuthenticationFailed(); |
| + void NotifyPrintSystemUnavailable(); |
| // Starts a new printer registration process. |
| void StartRegistration(); |
| @@ -135,6 +150,9 @@ |
| // handler is responsible for checking for pending print jobs for this |
| // printer and print them. |
| void InitJobHandlerForPrinter(DictionaryValue* printer_data); |
| + // Sends a diagnostic message to the cloud print server that the print |
| + // system failed to initialize. |
| + void ReportPrintSystemUnavailable(const std::string& failure_message); |
| // Callback method for GetPrinterCapsAndDefaults. |
| void OnReceivePrinterCaps( |
| @@ -333,34 +351,40 @@ |
| return; // No print system available, fail initalization. |
| } |
| - print_system_->Init(); |
| + cloud_print::PrintSystem::PrintSystemResult result = print_system_->Init(); |
| // TODO(sanjeevr): Validate the tokens. |
| auth_token_ = cloud_print_token; |
| - const notifier::NotifierOptions kNotifierOptions; |
| - const bool kInvalidateXmppAuthToken = false; |
| - const bool kAllowInsecureXmppConnection = false; |
| - talk_mediator_.reset(new notifier::TalkMediatorImpl( |
| - new notifier::PushNotificationsThread(kNotifierOptions, |
| - kCloudPrintPushNotificationsSource), |
| - kInvalidateXmppAuthToken, |
| - kAllowInsecureXmppConnection)); |
| - push_notifications_channel_ = kCloudPrintPushNotificationsSource; |
| - push_notifications_channel_.append("/proxy/"); |
| - push_notifications_channel_.append(proxy_id); |
| - talk_mediator_->AddSubscribedServiceUrl(push_notifications_channel_); |
| - talk_mediator_->SetDelegate(this); |
| - talk_mediator_->SetAuthToken(email, cloud_print_xmpp_token, |
| - kSyncGaiaServiceId); |
| - talk_mediator_->Login(); |
| + if (result.succeeded()) { |
| + const notifier::NotifierOptions kNotifierOptions; |
| + const bool kInvalidateXmppAuthToken = false; |
| + const bool kAllowInsecureXmppConnection = false; |
| + talk_mediator_.reset(new notifier::TalkMediatorImpl( |
| + new notifier::PushNotificationsThread( |
| + kNotifierOptions, |
| + kCloudPrintPushNotificationsSource), |
| + kInvalidateXmppAuthToken, |
| + kAllowInsecureXmppConnection)); |
| + push_notifications_channel_ = kCloudPrintPushNotificationsSource; |
| + push_notifications_channel_.append("/proxy/"); |
| + push_notifications_channel_.append(proxy_id); |
| + talk_mediator_->AddSubscribedServiceUrl(push_notifications_channel_); |
| + talk_mediator_->SetDelegate(this); |
| + talk_mediator_->SetAuthToken(email, cloud_print_xmpp_token, |
| + kSyncGaiaServiceId); |
| + talk_mediator_->Login(); |
| - print_server_watcher_ = print_system_->CreatePrintServerWatcher(); |
| - print_server_watcher_->StartWatching(this); |
| + print_server_watcher_ = print_system_->CreatePrintServerWatcher(); |
| + print_server_watcher_->StartWatching(this); |
| - proxy_id_ = proxy_id; |
| + proxy_id_ = proxy_id; |
| - StartRegistration(); |
| + StartRegistration(); |
| + } else { |
| + // We could not initialize the print system. We need to notify the server. |
| + ReportPrintSystemUnavailable(result.message()); |
| + } |
| } |
| void CloudPrintProxyBackend::Core::StartRegistration() { |
| @@ -455,6 +479,10 @@ |
| const std::string& printer_name, |
| const printing::PrinterCapsAndDefaults& caps_and_defaults) { |
| DCHECK(next_upload_index_ < printer_list_.size()); |
| + std::string mime_boundary; |
| + CloudPrintHelpers::CreateMimeBoundaryForUpload(&mime_boundary); |
| + std::string post_data; |
| + GURL post_url; |
| if (succeeded) { |
| const printing::PrinterBasicInfo& info = |
| printer_list_.at(next_upload_index_); |
| @@ -462,9 +490,6 @@ |
| last_uploaded_printer_name_ = info.printer_name; |
| last_uploaded_printer_info_ = caps_and_defaults; |
| - std::string mime_boundary; |
| - CloudPrintHelpers::CreateMimeBoundaryForUpload(&mime_boundary); |
| - std::string post_data; |
| CloudPrintHelpers::AddMultipartValueForUpload(kProxyIdValue, proxy_id_, |
| mime_boundary, |
| std::string(), &post_data); |
| @@ -498,26 +523,38 @@ |
| kPrinterCapsHashValue, |
| MD5String(last_uploaded_printer_info_.printer_capabilities), |
| mime_boundary, std::string(), &post_data); |
| - // Terminate the request body |
| - post_data.append("--" + mime_boundary + "--\r\n"); |
| - std::string mime_type("multipart/form-data; boundary="); |
| - mime_type += mime_boundary; |
| - GURL register_url = CloudPrintHelpers::GetUrlForPrinterRegistration( |
| + post_url = CloudPrintHelpers::GetUrlForPrinterRegistration( |
| cloud_print_server_url_); |
| next_response_handler_ = |
| &CloudPrintProxyBackend::Core::HandleRegisterPrinterResponse; |
| - request_ = new CloudPrintURLFetcher; |
| - request_->StartPostRequest(register_url, this, auth_token_, |
| - kCloudPrintAPIMaxRetryCount, mime_type, |
| - post_data); |
| } else { |
| LOG(ERROR) << "CP_PROXY: Failed to get printer info for: " << |
| printer_name; |
| - next_upload_index_++; |
| - MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(this, |
| - &CloudPrintProxyBackend::Core::RegisterNextPrinter)); |
| + // This printer failed to register, notify the server of this failure. |
| + post_url = CloudPrintHelpers::GetUrlForUserMessage( |
| + cloud_print_server_url_, |
| + kGetPrinterCapsFailedMessageId); |
| + string16 printer_name_utf16 = UTF8ToUTF16(printer_name); |
| + std::string status_message = l10n_util::GetStringFUTF8( |
| + IDS_CLOUD_PRINT_REGISTER_PRINTER_FAILED, |
| + printer_name_utf16); |
| + CloudPrintHelpers::AddMultipartValueForUpload(kMessageTextValue, |
| + status_message, |
| + mime_boundary, |
| + std::string(), |
| + &post_data); |
| + next_response_handler_ = |
| + &CloudPrintProxyBackend::Core::HandleRegisterFailedStatusResponse; |
| } |
| + // Terminate the request body |
| + post_data.append("--" + mime_boundary + "--\r\n"); |
| + std::string mime_type("multipart/form-data; boundary="); |
| + mime_type += mime_boundary; |
| + request_ = new CloudPrintURLFetcher; |
| + request_->StartPostRequest(post_url, this, auth_token_, |
| + kCloudPrintAPIMaxRetryCount, mime_type, |
| + post_data); |
| } |
| void CloudPrintProxyBackend::Core::HandlePrinterNotification( |
| @@ -595,6 +632,11 @@ |
| backend_->frontend_->OnAuthenticationFailed(); |
| } |
| +void CloudPrintProxyBackend::Core::NotifyPrintSystemUnavailable() { |
| + DCHECK(MessageLoop::current() == backend_->frontend_loop_); |
| + backend_->frontend_->OnPrintSystemUnavailable(); |
| +} |
| + |
| CloudPrintURLFetcher::ResponseAction |
| CloudPrintProxyBackend::Core::HandlePrinterListResponse( |
| const URLFetcher* source, |
| @@ -684,6 +726,32 @@ |
| } |
| } |
| +void CloudPrintProxyBackend::Core::ReportPrintSystemUnavailable( |
| + const std::string& failure_message) { |
| + DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + std::string mime_boundary; |
| + CloudPrintHelpers::CreateMimeBoundaryForUpload(&mime_boundary); |
| + GURL post_url = CloudPrintHelpers::GetUrlForUserMessage( |
| + cloud_print_server_url_, |
| + kPrintSystemFailedMessageId); |
| + std::string post_data; |
| + CloudPrintHelpers::AddMultipartValueForUpload(kMessageTextValue, |
| + failure_message, |
| + mime_boundary, |
| + std::string(), |
| + &post_data); |
| + next_response_handler_ = |
| + &CloudPrintProxyBackend::Core::HandlePrintSystemUnavailableResponse; |
| +// Terminate the request body |
|
Scott Byer
2011/01/20 19:09:03
nit: indentation
|
| + post_data.append("--" + mime_boundary + "--\r\n"); |
| + std::string mime_type("multipart/form-data; boundary="); |
| + mime_type += mime_boundary; |
| + request_ = new CloudPrintURLFetcher; |
| + request_->StartPostRequest(post_url, this, auth_token_, |
| + kCloudPrintAPIMaxRetryCount, mime_type, |
| + post_data); |
| +} |
| + |
| CloudPrintURLFetcher::ResponseAction |
| CloudPrintProxyBackend::Core::HandleRegisterPrinterResponse( |
| const URLFetcher* source, |
| @@ -710,6 +778,34 @@ |
| return CloudPrintURLFetcher::STOP_PROCESSING; |
| } |
| +CloudPrintURLFetcher::ResponseAction |
| +CloudPrintProxyBackend::Core::HandleRegisterFailedStatusResponse( |
| + const URLFetcher* source, |
| + const GURL& url, |
| + DictionaryValue* json_data, |
| + bool succeeded) { |
| + DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + next_upload_index_++; |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + NewRunnableMethod(this, |
| + &CloudPrintProxyBackend::Core::RegisterNextPrinter)); |
| + return CloudPrintURLFetcher::STOP_PROCESSING; |
| +} |
| + |
| +CloudPrintURLFetcher::ResponseAction |
| +CloudPrintProxyBackend::Core::HandlePrintSystemUnavailableResponse( |
| + const URLFetcher* source, |
| + const GURL& url, |
| + DictionaryValue* json_data, |
| + bool succeeded) { |
| + DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| + // Let the frontend know that we do not have a print system. |
| + backend_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, |
|
Scott Byer
2011/01/20 19:09:03
nit: indentation clearer in HandleRegisterFailedSt
|
| + &Core::NotifyPrintSystemUnavailable)); |
| + return CloudPrintURLFetcher::STOP_PROCESSING; |
| +} |
| + |
| bool CloudPrintProxyBackend::Core::RemovePrinterFromList( |
| const std::string& printer_name) { |
| DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |