| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 const std::string& proxy_id); | 75 const std::string& proxy_id); |
| 76 | 76 |
| 77 // Called on the CloudPrintProxyBackend core_thread_ to perform | 77 // Called on the CloudPrintProxyBackend core_thread_ to perform |
| 78 // shutdown. | 78 // shutdown. |
| 79 void DoShutdown(); | 79 void DoShutdown(); |
| 80 void DoRegisterSelectedPrinters( | 80 void DoRegisterSelectedPrinters( |
| 81 const printing::PrinterList& printer_list); | 81 const printing::PrinterList& printer_list); |
| 82 | 82 |
| 83 // CloudPrintURLFetcher::Delegate implementation. | 83 // CloudPrintURLFetcher::Delegate implementation. |
| 84 virtual CloudPrintURLFetcher::ResponseAction HandleJSONData( | 84 virtual CloudPrintURLFetcher::ResponseAction HandleJSONData( |
| 85 const URLFetcher* source, | 85 const content::URLFetcher* source, |
| 86 const GURL& url, | 86 const GURL& url, |
| 87 DictionaryValue* json_data, | 87 DictionaryValue* json_data, |
| 88 bool succeeded); | 88 bool succeeded); |
| 89 | 89 |
| 90 virtual void OnRequestAuthError(); | 90 virtual void OnRequestAuthError(); |
| 91 | 91 |
| 92 // cloud_print::PrintServerWatcherDelegate implementation | 92 // cloud_print::PrintServerWatcherDelegate implementation |
| 93 virtual void OnPrinterAdded(); | 93 virtual void OnPrinterAdded(); |
| 94 // PrinterJobHandler::Delegate implementation | 94 // PrinterJobHandler::Delegate implementation |
| 95 virtual void OnPrinterJobHandlerShutdown(PrinterJobHandler* job_handler, | 95 virtual void OnPrinterJobHandlerShutdown(PrinterJobHandler* job_handler, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 111 int expires_in_seconds); | 111 int expires_in_seconds); |
| 112 virtual void OnRefreshTokenResponse(const std::string& access_token, | 112 virtual void OnRefreshTokenResponse(const std::string& access_token, |
| 113 int expires_in_seconds); | 113 int expires_in_seconds); |
| 114 virtual void OnOAuthError(); | 114 virtual void OnOAuthError(); |
| 115 virtual void OnNetworkError(int response_code); | 115 virtual void OnNetworkError(int response_code); |
| 116 | 116 |
| 117 private: | 117 private: |
| 118 // Prototype for a response handler. | 118 // Prototype for a response handler. |
| 119 typedef CloudPrintURLFetcher::ResponseAction | 119 typedef CloudPrintURLFetcher::ResponseAction |
| 120 (CloudPrintProxyBackend::Core::*ResponseHandler)( | 120 (CloudPrintProxyBackend::Core::*ResponseHandler)( |
| 121 const URLFetcher* source, | 121 const content::URLFetcher* source, |
| 122 const GURL& url, | 122 const GURL& url, |
| 123 DictionaryValue* json_data, | 123 DictionaryValue* json_data, |
| 124 bool succeeded); | 124 bool succeeded); |
| 125 // Begin response handlers | 125 // Begin response handlers |
| 126 CloudPrintURLFetcher::ResponseAction HandlePrinterListResponse( | 126 CloudPrintURLFetcher::ResponseAction HandlePrinterListResponse( |
| 127 const URLFetcher* source, | 127 const content::URLFetcher* source, |
| 128 const GURL& url, | 128 const GURL& url, |
| 129 DictionaryValue* json_data, | 129 DictionaryValue* json_data, |
| 130 bool succeeded); | 130 bool succeeded); |
| 131 | 131 |
| 132 CloudPrintURLFetcher::ResponseAction HandleRegisterPrinterResponse( | 132 CloudPrintURLFetcher::ResponseAction HandleRegisterPrinterResponse( |
| 133 const URLFetcher* source, | 133 const content::URLFetcher* source, |
| 134 const GURL& url, | 134 const GURL& url, |
| 135 DictionaryValue* json_data, | 135 DictionaryValue* json_data, |
| 136 bool succeeded); | 136 bool succeeded); |
| 137 | 137 |
| 138 CloudPrintURLFetcher::ResponseAction HandleRegisterFailedStatusResponse( | 138 CloudPrintURLFetcher::ResponseAction HandleRegisterFailedStatusResponse( |
| 139 const URLFetcher* source, | 139 const content::URLFetcher* source, |
| 140 const GURL& url, | 140 const GURL& url, |
| 141 DictionaryValue* json_data, | 141 DictionaryValue* json_data, |
| 142 bool succeeded); | 142 bool succeeded); |
| 143 | 143 |
| 144 CloudPrintURLFetcher::ResponseAction HandlePrintSystemUnavailableResponse( | 144 CloudPrintURLFetcher::ResponseAction HandlePrintSystemUnavailableResponse( |
| 145 const URLFetcher* source, | 145 const content::URLFetcher* source, |
| 146 const GURL& url, | 146 const GURL& url, |
| 147 DictionaryValue* json_data, | 147 DictionaryValue* json_data, |
| 148 bool succeeded); | 148 bool succeeded); |
| 149 | 149 |
| 150 CloudPrintURLFetcher::ResponseAction HandleEnumPrintersFailedResponse( | 150 CloudPrintURLFetcher::ResponseAction HandleEnumPrintersFailedResponse( |
| 151 const URLFetcher* source, | 151 const content::URLFetcher* source, |
| 152 const GURL& url, | 152 const GURL& url, |
| 153 DictionaryValue* json_data, | 153 DictionaryValue* json_data, |
| 154 bool succeeded); | 154 bool succeeded); |
| 155 | 155 |
| 156 CloudPrintURLFetcher::ResponseAction HandleGetAuthCodeResponse( | 156 CloudPrintURLFetcher::ResponseAction HandleGetAuthCodeResponse( |
| 157 const URLFetcher* source, | 157 const content::URLFetcher* source, |
| 158 const GURL& url, | 158 const GURL& url, |
| 159 DictionaryValue* json_data, | 159 DictionaryValue* json_data, |
| 160 bool succeeded); | 160 bool succeeded); |
| 161 | 161 |
| 162 // End response handlers | 162 // End response handlers |
| 163 | 163 |
| 164 // NotifyXXX is how the Core communicates with the frontend across | 164 // NotifyXXX is how the Core communicates with the frontend across |
| 165 // threads. | 165 // threads. |
| 166 void NotifyPrinterListAvailable( | 166 void NotifyPrinterListAvailable( |
| 167 const printing::PrinterList& printer_list); | 167 const printing::PrinterList& printer_list); |
| (...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 CloudPrintTokenStore* CloudPrintProxyBackend::Core::GetTokenStore() { | 759 CloudPrintTokenStore* CloudPrintProxyBackend::Core::GetTokenStore() { |
| 760 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 760 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 761 if (!token_store_.get()) | 761 if (!token_store_.get()) |
| 762 token_store_.reset(new CloudPrintTokenStore); | 762 token_store_.reset(new CloudPrintTokenStore); |
| 763 return token_store_.get(); | 763 return token_store_.get(); |
| 764 } | 764 } |
| 765 | 765 |
| 766 // CloudPrintURLFetcher::Delegate implementation. | 766 // CloudPrintURLFetcher::Delegate implementation. |
| 767 CloudPrintURLFetcher::ResponseAction | 767 CloudPrintURLFetcher::ResponseAction |
| 768 CloudPrintProxyBackend::Core::HandleJSONData( | 768 CloudPrintProxyBackend::Core::HandleJSONData( |
| 769 const URLFetcher* source, | 769 const content::URLFetcher* source, |
| 770 const GURL& url, | 770 const GURL& url, |
| 771 DictionaryValue* json_data, | 771 DictionaryValue* json_data, |
| 772 bool succeeded) { | 772 bool succeeded) { |
| 773 DCHECK(next_response_handler_); | 773 DCHECK(next_response_handler_); |
| 774 return (this->*next_response_handler_)(source, url, json_data, succeeded); | 774 return (this->*next_response_handler_)(source, url, json_data, succeeded); |
| 775 } | 775 } |
| 776 | 776 |
| 777 void CloudPrintProxyBackend::Core::OnRequestAuthError() { | 777 void CloudPrintProxyBackend::Core::OnRequestAuthError() { |
| 778 OnAuthError(); | 778 OnAuthError(); |
| 779 } | 779 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 799 backend_->frontend_->OnAuthenticationFailed(); | 799 backend_->frontend_->OnAuthenticationFailed(); |
| 800 } | 800 } |
| 801 | 801 |
| 802 void CloudPrintProxyBackend::Core::NotifyPrintSystemUnavailable() { | 802 void CloudPrintProxyBackend::Core::NotifyPrintSystemUnavailable() { |
| 803 DCHECK(MessageLoop::current() == backend_->frontend_loop_); | 803 DCHECK(MessageLoop::current() == backend_->frontend_loop_); |
| 804 backend_->frontend_->OnPrintSystemUnavailable(); | 804 backend_->frontend_->OnPrintSystemUnavailable(); |
| 805 } | 805 } |
| 806 | 806 |
| 807 CloudPrintURLFetcher::ResponseAction | 807 CloudPrintURLFetcher::ResponseAction |
| 808 CloudPrintProxyBackend::Core::HandleGetAuthCodeResponse( | 808 CloudPrintProxyBackend::Core::HandleGetAuthCodeResponse( |
| 809 const URLFetcher* source, | 809 const content::URLFetcher* source, |
| 810 const GURL& url, | 810 const GURL& url, |
| 811 DictionaryValue* json_data, | 811 DictionaryValue* json_data, |
| 812 bool succeeded) { | 812 bool succeeded) { |
| 813 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 813 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 814 if (!succeeded) { | 814 if (!succeeded) { |
| 815 OnAuthError(); | 815 OnAuthError(); |
| 816 return CloudPrintURLFetcher::STOP_PROCESSING; | 816 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 817 } | 817 } |
| 818 std::string auth_code; | 818 std::string auth_code; |
| 819 if (!json_data->GetString(kOAuthCodeValue, &auth_code)) { | 819 if (!json_data->GetString(kOAuthCodeValue, &auth_code)) { |
| 820 OnAuthError(); | 820 OnAuthError(); |
| 821 return CloudPrintURLFetcher::STOP_PROCESSING; | 821 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 822 } | 822 } |
| 823 json_data->GetString(kXMPPJidValue, &robot_email_); | 823 json_data->GetString(kXMPPJidValue, &robot_email_); |
| 824 // Now that we have an auth code we need to get the refresh and access tokens. | 824 // Now that we have an auth code we need to get the refresh and access tokens. |
| 825 oauth_client_.reset(new gaia::GaiaOAuthClient( | 825 oauth_client_.reset(new gaia::GaiaOAuthClient( |
| 826 gaia::kGaiaOAuth2Url, | 826 gaia::kGaiaOAuth2Url, |
| 827 g_service_process->GetServiceURLRequestContextGetter())); | 827 g_service_process->GetServiceURLRequestContextGetter())); |
| 828 oauth_client_->GetTokensFromAuthCode(oauth_client_info_, | 828 oauth_client_->GetTokensFromAuthCode(oauth_client_info_, |
| 829 auth_code, | 829 auth_code, |
| 830 kCloudPrintAPIMaxRetryCount, | 830 kCloudPrintAPIMaxRetryCount, |
| 831 this); | 831 this); |
| 832 | 832 |
| 833 return CloudPrintURLFetcher::STOP_PROCESSING; | 833 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 834 } | 834 } |
| 835 | 835 |
| 836 CloudPrintURLFetcher::ResponseAction | 836 CloudPrintURLFetcher::ResponseAction |
| 837 CloudPrintProxyBackend::Core::HandlePrinterListResponse( | 837 CloudPrintProxyBackend::Core::HandlePrinterListResponse( |
| 838 const URLFetcher* source, | 838 const content::URLFetcher* source, |
| 839 const GURL& url, | 839 const GURL& url, |
| 840 DictionaryValue* json_data, | 840 DictionaryValue* json_data, |
| 841 bool succeeded) { | 841 bool succeeded) { |
| 842 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 842 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 843 if (!succeeded) { | 843 if (!succeeded) { |
| 844 NOTREACHED(); | 844 NOTREACHED(); |
| 845 return CloudPrintURLFetcher::RETRY_REQUEST; | 845 return CloudPrintURLFetcher::RETRY_REQUEST; |
| 846 } | 846 } |
| 847 ListValue* printer_list = NULL; | 847 ListValue* printer_list = NULL; |
| 848 // There may be no "printers" value in the JSON | 848 // There may be no "printers" value in the JSON |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 947 request_->StartPostRequest(post_url, | 947 request_->StartPostRequest(post_url, |
| 948 this, | 948 this, |
| 949 kCloudPrintAPIMaxRetryCount, | 949 kCloudPrintAPIMaxRetryCount, |
| 950 mime_type, | 950 mime_type, |
| 951 post_data, | 951 post_data, |
| 952 std::string()); | 952 std::string()); |
| 953 } | 953 } |
| 954 | 954 |
| 955 CloudPrintURLFetcher::ResponseAction | 955 CloudPrintURLFetcher::ResponseAction |
| 956 CloudPrintProxyBackend::Core::HandleRegisterPrinterResponse( | 956 CloudPrintProxyBackend::Core::HandleRegisterPrinterResponse( |
| 957 const URLFetcher* source, | 957 const content::URLFetcher* source, |
| 958 const GURL& url, | 958 const GURL& url, |
| 959 DictionaryValue* json_data, | 959 DictionaryValue* json_data, |
| 960 bool succeeded) { | 960 bool succeeded) { |
| 961 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 961 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 962 if (succeeded) { | 962 if (succeeded) { |
| 963 ListValue* printer_list = NULL; | 963 ListValue* printer_list = NULL; |
| 964 // There should be a "printers" value in the JSON | 964 // There should be a "printers" value in the JSON |
| 965 if (json_data->GetList(kPrinterListValue, &printer_list)) { | 965 if (json_data->GetList(kPrinterListValue, &printer_list)) { |
| 966 DictionaryValue* printer_data = NULL; | 966 DictionaryValue* printer_data = NULL; |
| 967 if (printer_list->GetDictionary(0, &printer_data)) | 967 if (printer_list->GetDictionary(0, &printer_data)) |
| 968 InitJobHandlerForPrinter(printer_data); | 968 InitJobHandlerForPrinter(printer_data); |
| 969 } | 969 } |
| 970 } | 970 } |
| 971 next_upload_index_++; | 971 next_upload_index_++; |
| 972 MessageLoop::current()->PostTask( | 972 MessageLoop::current()->PostTask( |
| 973 FROM_HERE, | 973 FROM_HERE, |
| 974 NewRunnableMethod(this, | 974 NewRunnableMethod(this, |
| 975 &CloudPrintProxyBackend::Core::RegisterNextPrinter)); | 975 &CloudPrintProxyBackend::Core::RegisterNextPrinter)); |
| 976 return CloudPrintURLFetcher::STOP_PROCESSING; | 976 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 977 } | 977 } |
| 978 | 978 |
| 979 CloudPrintURLFetcher::ResponseAction | 979 CloudPrintURLFetcher::ResponseAction |
| 980 CloudPrintProxyBackend::Core::HandleRegisterFailedStatusResponse( | 980 CloudPrintProxyBackend::Core::HandleRegisterFailedStatusResponse( |
| 981 const URLFetcher* source, | 981 const content::URLFetcher* source, |
| 982 const GURL& url, | 982 const GURL& url, |
| 983 DictionaryValue* json_data, | 983 DictionaryValue* json_data, |
| 984 bool succeeded) { | 984 bool succeeded) { |
| 985 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 985 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 986 next_upload_index_++; | 986 next_upload_index_++; |
| 987 MessageLoop::current()->PostTask( | 987 MessageLoop::current()->PostTask( |
| 988 FROM_HERE, | 988 FROM_HERE, |
| 989 NewRunnableMethod(this, | 989 NewRunnableMethod(this, |
| 990 &CloudPrintProxyBackend::Core::RegisterNextPrinter)); | 990 &CloudPrintProxyBackend::Core::RegisterNextPrinter)); |
| 991 return CloudPrintURLFetcher::STOP_PROCESSING; | 991 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 992 } | 992 } |
| 993 | 993 |
| 994 CloudPrintURLFetcher::ResponseAction | 994 CloudPrintURLFetcher::ResponseAction |
| 995 CloudPrintProxyBackend::Core::HandlePrintSystemUnavailableResponse( | 995 CloudPrintProxyBackend::Core::HandlePrintSystemUnavailableResponse( |
| 996 const URLFetcher* source, | 996 const content::URLFetcher* source, |
| 997 const GURL& url, | 997 const GURL& url, |
| 998 DictionaryValue* json_data, | 998 DictionaryValue* json_data, |
| 999 bool succeeded) { | 999 bool succeeded) { |
| 1000 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 1000 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 1001 // Let the frontend know that we do not have a print system. | 1001 // Let the frontend know that we do not have a print system. |
| 1002 backend_->frontend_loop_->PostTask( | 1002 backend_->frontend_loop_->PostTask( |
| 1003 FROM_HERE, | 1003 FROM_HERE, |
| 1004 NewRunnableMethod(this, | 1004 NewRunnableMethod(this, |
| 1005 &Core::NotifyPrintSystemUnavailable)); | 1005 &Core::NotifyPrintSystemUnavailable)); |
| 1006 return CloudPrintURLFetcher::STOP_PROCESSING; | 1006 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 1007 } | 1007 } |
| 1008 | 1008 |
| 1009 CloudPrintURLFetcher::ResponseAction | 1009 CloudPrintURLFetcher::ResponseAction |
| 1010 CloudPrintProxyBackend::Core::HandleEnumPrintersFailedResponse( | 1010 CloudPrintProxyBackend::Core::HandleEnumPrintersFailedResponse( |
| 1011 const URLFetcher* source, | 1011 const content::URLFetcher* source, |
| 1012 const GURL& url, | 1012 const GURL& url, |
| 1013 DictionaryValue* json_data, | 1013 DictionaryValue* json_data, |
| 1014 bool succeeded) { | 1014 bool succeeded) { |
| 1015 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); | 1015 DCHECK(MessageLoop::current() == backend_->core_thread_.message_loop()); |
| 1016 // Now proceed with printer registration. | 1016 // Now proceed with printer registration. |
| 1017 GetRegisteredPrinters(); | 1017 GetRegisteredPrinters(); |
| 1018 return CloudPrintURLFetcher::STOP_PROCESSING; | 1018 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 1019 } | 1019 } |
| 1020 | 1020 |
| 1021 | 1021 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1159 OnAuthError(); | 1159 OnAuthError(); |
| 1160 } | 1160 } |
| 1161 | 1161 |
| 1162 void CloudPrintProxyBackend::Core::OnNetworkError(int response_code) { | 1162 void CloudPrintProxyBackend::Core::OnNetworkError(int response_code) { |
| 1163 // Since we specify inifinite retries on network errors, this should never | 1163 // Since we specify inifinite retries on network errors, this should never |
| 1164 // be called. | 1164 // be called. |
| 1165 NOTREACHED() << | 1165 NOTREACHED() << |
| 1166 "OnNetworkError invoked when not expected, response code is " << | 1166 "OnNetworkError invoked when not expected, response code is " << |
| 1167 response_code; | 1167 response_code; |
| 1168 } | 1168 } |
| OLD | NEW |