OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_ |
| 6 #define CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "chrome/common/net/url_fetcher.h" |
| 12 |
| 13 class DictionaryValue; |
| 14 class GURL; |
| 15 class URLFetcherProtectEntry; |
| 16 class URLRequestStatus; |
| 17 |
| 18 // A wrapper around URLFetcher for CloudPrint. URLFetcher applies retry logic |
| 19 // only on HTTP response codes >= 500. In the cloud print case, we want to |
| 20 // retry on all network errors. In addition, we want to treat non-JSON responses |
| 21 // (for all CloudPrint APIs that expect JSON responses) as errors and they |
| 22 // must also be retried. Also URLFetcher uses the host name of the URL as the |
| 23 // key for applying the retry policy. In our case, we want to apply one global |
| 24 // policy for many requests (not necessarily scoped by hostname). |
| 25 class CloudPrintURLFetcher |
| 26 : public base::RefCountedThreadSafe<CloudPrintURLFetcher>, |
| 27 public URLFetcher::Delegate { |
| 28 public: |
| 29 enum ResponseAction { |
| 30 CONTINUE_PROCESSING, |
| 31 STOP_PROCESSING, |
| 32 RETRY_REQUEST, |
| 33 }; |
| 34 class Delegate { |
| 35 public: |
| 36 virtual ~Delegate() { } |
| 37 // Override this to handle the raw response as it is available. No response |
| 38 // error checking is done before this method is called. If the delegate |
| 39 // returns CONTINUE_PROCESSING, we will then check for network |
| 40 // errors. Most implementations will not override this. |
| 41 virtual ResponseAction HandleRawResponse(const URLFetcher* source, |
| 42 const GURL& url, |
| 43 const URLRequestStatus& status, |
| 44 int response_code, |
| 45 const ResponseCookies& cookies, |
| 46 const std::string& data) { |
| 47 return CONTINUE_PROCESSING; |
| 48 } |
| 49 // This will be invoked only if HandleRawResponse returns |
| 50 // CONTINUE_PROCESSING AND if there are no network errors and the HTTP |
| 51 // response code is 200. The delegate implementation returns |
| 52 // CONTINUE_PROCESSING if it does not want to handle the raw data itself. |
| 53 // Handling the raw data is needed when the expected response is NOT JSON |
| 54 // (like in the case of a print ticket response or a print job download |
| 55 // response). |
| 56 virtual ResponseAction HandleRawData(const URLFetcher* source, |
| 57 const GURL& url, |
| 58 const std::string& data) { |
| 59 return CONTINUE_PROCESSING; |
| 60 } |
| 61 // This will be invoked only if HandleRawResponse and HandleRawData return |
| 62 // CONTINUE_PROCESSING AND if the response contains a valid JSON dictionary. |
| 63 // |succeeded| is the value of the "success" field in the response JSON. |
| 64 virtual ResponseAction HandleJSONData(const URLFetcher* source, |
| 65 const GURL& url, |
| 66 DictionaryValue* json_data, |
| 67 bool succeeded) { |
| 68 return CONTINUE_PROCESSING; |
| 69 } |
| 70 // Invoked when the retry limit for this request has been reached (if there |
| 71 // was a retry limit - a limit of -1 implies no limit). |
| 72 virtual void OnRequestGiveUp() { } |
| 73 // Invoked when the request returns a 403 error (applicable only when |
| 74 // HandleRawResponse returns CONTINUE_PROCESSING) |
| 75 virtual void OnRequestAuthError() = 0; |
| 76 }; |
| 77 CloudPrintURLFetcher(); |
| 78 |
| 79 void StartGetRequest(const GURL& url, |
| 80 Delegate* delegate, |
| 81 const std::string& auth_token, |
| 82 const std::string& retry_policy); |
| 83 void StartPostRequest(const GURL& url, |
| 84 Delegate* delegate, |
| 85 const std::string& auth_token, |
| 86 const std::string& retry_policy, |
| 87 const std::string& post_data_mime_type, |
| 88 const std::string& post_data); |
| 89 |
| 90 // URLFetcher::Delegate implementation. |
| 91 virtual void OnURLFetchComplete(const URLFetcher* source, const GURL& url, |
| 92 const URLRequestStatus& status, |
| 93 int response_code, |
| 94 const ResponseCookies& cookies, |
| 95 const std::string& data); |
| 96 protected: |
| 97 // Virtual for testing. |
| 98 virtual URLRequestContextGetter* GetRequestContextGetter(); |
| 99 |
| 100 private: |
| 101 void StartRequestHelper(const GURL& url, |
| 102 URLFetcher::RequestType request_type, |
| 103 Delegate* delegate, |
| 104 const std::string& auth_token, |
| 105 const std::string& retry_policy, |
| 106 const std::string& post_data_mime_type, |
| 107 const std::string& post_data); |
| 108 void StartRequestNow(); |
| 109 |
| 110 scoped_ptr<URLFetcher> request_; |
| 111 Delegate* delegate_; |
| 112 URLFetcherProtectEntry* protect_entry_; |
| 113 int num_retries_; |
| 114 }; |
| 115 |
| 116 typedef CloudPrintURLFetcher::Delegate CloudPrintURLFetcherDelegate; |
| 117 |
| 118 #endif // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_ |
OLD | NEW |