| 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 #ifndef CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_ | 5 #ifndef CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_ |
| 6 #define CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_ | 6 #define CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
| 12 #include "chrome/common/net/url_fetcher.h" | 12 #include "chrome/common/net/url_fetcher.h" |
| 13 | 13 |
| 14 class DictionaryValue; | 14 class DictionaryValue; |
| 15 class GURL; | 15 class GURL; |
| 16 class URLFetcherProtectEntry; | |
| 17 class URLRequestStatus; | 16 class URLRequestStatus; |
| 18 | 17 |
| 19 // A wrapper around URLFetcher for CloudPrint. URLFetcher applies retry logic | 18 // A wrapper around URLFetcher for CloudPrint. URLFetcher applies retry logic |
| 20 // only on HTTP response codes >= 500. In the cloud print case, we want to | 19 // only on HTTP response codes >= 500. In the cloud print case, we want to |
| 21 // retry on all network errors. In addition, we want to treat non-JSON responses | 20 // retry on all network errors. In addition, we want to treat non-JSON responses |
| 22 // (for all CloudPrint APIs that expect JSON responses) as errors and they | 21 // (for all CloudPrint APIs that expect JSON responses) as errors and they |
| 23 // must also be retried. Also URLFetcher uses the host name of the URL as the | 22 // must also be retried. |
| 24 // key for applying the retry policy. In our case, we want to apply one global | |
| 25 // policy for many requests (not necessarily scoped by hostname). | |
| 26 class CloudPrintURLFetcher | 23 class CloudPrintURLFetcher |
| 27 : public base::RefCountedThreadSafe<CloudPrintURLFetcher>, | 24 : public base::RefCountedThreadSafe<CloudPrintURLFetcher>, |
| 28 public URLFetcher::Delegate { | 25 public URLFetcher::Delegate { |
| 29 public: | 26 public: |
| 30 enum ResponseAction { | 27 enum ResponseAction { |
| 31 CONTINUE_PROCESSING, | 28 CONTINUE_PROCESSING, |
| 32 STOP_PROCESSING, | 29 STOP_PROCESSING, |
| 33 RETRY_REQUEST, | 30 RETRY_REQUEST, |
| 34 }; | 31 }; |
| 35 class Delegate { | 32 class Delegate { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 virtual void OnRequestGiveUp() { } | 70 virtual void OnRequestGiveUp() { } |
| 74 // Invoked when the request returns a 403 error (applicable only when | 71 // Invoked when the request returns a 403 error (applicable only when |
| 75 // HandleRawResponse returns CONTINUE_PROCESSING) | 72 // HandleRawResponse returns CONTINUE_PROCESSING) |
| 76 virtual void OnRequestAuthError() = 0; | 73 virtual void OnRequestAuthError() = 0; |
| 77 }; | 74 }; |
| 78 CloudPrintURLFetcher(); | 75 CloudPrintURLFetcher(); |
| 79 | 76 |
| 80 void StartGetRequest(const GURL& url, | 77 void StartGetRequest(const GURL& url, |
| 81 Delegate* delegate, | 78 Delegate* delegate, |
| 82 const std::string& auth_token, | 79 const std::string& auth_token, |
| 83 const std::string& retry_policy); | 80 int max_retries); |
| 84 void StartPostRequest(const GURL& url, | 81 void StartPostRequest(const GURL& url, |
| 85 Delegate* delegate, | 82 Delegate* delegate, |
| 86 const std::string& auth_token, | 83 const std::string& auth_token, |
| 87 const std::string& retry_policy, | 84 int max_retries, |
| 88 const std::string& post_data_mime_type, | 85 const std::string& post_data_mime_type, |
| 89 const std::string& post_data); | 86 const std::string& post_data); |
| 90 | 87 |
| 91 // URLFetcher::Delegate implementation. | 88 // URLFetcher::Delegate implementation. |
| 92 virtual void OnURLFetchComplete(const URLFetcher* source, const GURL& url, | 89 virtual void OnURLFetchComplete(const URLFetcher* source, const GURL& url, |
| 93 const URLRequestStatus& status, | 90 const URLRequestStatus& status, |
| 94 int response_code, | 91 int response_code, |
| 95 const ResponseCookies& cookies, | 92 const ResponseCookies& cookies, |
| 96 const std::string& data); | 93 const std::string& data); |
| 97 protected: | 94 protected: |
| 98 // Virtual for testing. | 95 // Virtual for testing. |
| 99 virtual URLRequestContextGetter* GetRequestContextGetter(); | 96 virtual URLRequestContextGetter* GetRequestContextGetter(); |
| 100 | 97 |
| 101 private: | 98 private: |
| 102 void StartRequestHelper(const GURL& url, | 99 void StartRequestHelper(const GURL& url, |
| 103 URLFetcher::RequestType request_type, | 100 URLFetcher::RequestType request_type, |
| 104 Delegate* delegate, | 101 Delegate* delegate, |
| 105 const std::string& auth_token, | 102 const std::string& auth_token, |
| 106 const std::string& retry_policy, | 103 int max_retries, |
| 107 const std::string& post_data_mime_type, | 104 const std::string& post_data_mime_type, |
| 108 const std::string& post_data); | 105 const std::string& post_data); |
| 109 void StartRequestNow(); | |
| 110 | 106 |
| 111 scoped_ptr<URLFetcher> request_; | 107 scoped_ptr<URLFetcher> request_; |
| 112 Delegate* delegate_; | 108 Delegate* delegate_; |
| 113 URLFetcherProtectEntry* protect_entry_; | |
| 114 int num_retries_; | 109 int num_retries_; |
| 115 }; | 110 }; |
| 116 | 111 |
| 117 typedef CloudPrintURLFetcher::Delegate CloudPrintURLFetcherDelegate; | 112 typedef CloudPrintURLFetcher::Delegate CloudPrintURLFetcherDelegate; |
| 118 | 113 |
| 119 #endif // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_ | 114 #endif // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_ |
| OLD | NEW |