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 #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 |
(...skipping 28 matching lines...) Expand all Loading... |
39 RETRY_REQUEST, | 39 RETRY_REQUEST, |
40 }; | 40 }; |
41 class Delegate { | 41 class Delegate { |
42 public: | 42 public: |
43 virtual ~Delegate() { } | 43 virtual ~Delegate() { } |
44 // Override this to handle the raw response as it is available. No response | 44 // Override this to handle the raw response as it is available. No response |
45 // error checking is done before this method is called. If the delegate | 45 // error checking is done before this method is called. If the delegate |
46 // returns CONTINUE_PROCESSING, we will then check for network | 46 // returns CONTINUE_PROCESSING, we will then check for network |
47 // errors. Most implementations will not override this. | 47 // errors. Most implementations will not override this. |
48 virtual ResponseAction HandleRawResponse( | 48 virtual ResponseAction HandleRawResponse( |
49 const URLFetcher* source, | 49 const content::URLFetcher* source, |
50 const GURL& url, | 50 const GURL& url, |
51 const net::URLRequestStatus& status, | 51 const net::URLRequestStatus& status, |
52 int response_code, | 52 int response_code, |
53 const net::ResponseCookies& cookies, | 53 const net::ResponseCookies& cookies, |
54 const std::string& data) { | 54 const std::string& data) { |
55 return CONTINUE_PROCESSING; | 55 return CONTINUE_PROCESSING; |
56 } | 56 } |
57 // This will be invoked only if HandleRawResponse returns | 57 // This will be invoked only if HandleRawResponse returns |
58 // CONTINUE_PROCESSING AND if there are no network errors and the HTTP | 58 // CONTINUE_PROCESSING AND if there are no network errors and the HTTP |
59 // response code is 200. The delegate implementation returns | 59 // response code is 200. The delegate implementation returns |
60 // CONTINUE_PROCESSING if it does not want to handle the raw data itself. | 60 // CONTINUE_PROCESSING if it does not want to handle the raw data itself. |
61 // Handling the raw data is needed when the expected response is NOT JSON | 61 // Handling the raw data is needed when the expected response is NOT JSON |
62 // (like in the case of a print ticket response or a print job download | 62 // (like in the case of a print ticket response or a print job download |
63 // response). | 63 // response). |
64 virtual ResponseAction HandleRawData(const URLFetcher* source, | 64 virtual ResponseAction HandleRawData(const content::URLFetcher* source, |
65 const GURL& url, | 65 const GURL& url, |
66 const std::string& data) { | 66 const std::string& data) { |
67 return CONTINUE_PROCESSING; | 67 return CONTINUE_PROCESSING; |
68 } | 68 } |
69 // This will be invoked only if HandleRawResponse and HandleRawData return | 69 // This will be invoked only if HandleRawResponse and HandleRawData return |
70 // CONTINUE_PROCESSING AND if the response contains a valid JSON dictionary. | 70 // CONTINUE_PROCESSING AND if the response contains a valid JSON dictionary. |
71 // |succeeded| is the value of the "success" field in the response JSON. | 71 // |succeeded| is the value of the "success" field in the response JSON. |
72 virtual ResponseAction HandleJSONData(const URLFetcher* source, | 72 virtual ResponseAction HandleJSONData(const content::URLFetcher* source, |
73 const GURL& url, | 73 const GURL& url, |
74 base::DictionaryValue* json_data, | 74 base::DictionaryValue* json_data, |
75 bool succeeded) { | 75 bool succeeded) { |
76 return CONTINUE_PROCESSING; | 76 return CONTINUE_PROCESSING; |
77 } | 77 } |
78 // Invoked when the retry limit for this request has been reached (if there | 78 // Invoked when the retry limit for this request has been reached (if there |
79 // was a retry limit - a limit of -1 implies no limit). | 79 // was a retry limit - a limit of -1 implies no limit). |
80 virtual void OnRequestGiveUp() { } | 80 virtual void OnRequestGiveUp() { } |
81 // Invoked when the request returns a 403 error (applicable only when | 81 // Invoked when the request returns a 403 error (applicable only when |
82 // HandleRawResponse returns CONTINUE_PROCESSING). | 82 // HandleRawResponse returns CONTINUE_PROCESSING). |
83 virtual void OnRequestAuthError() = 0; | 83 virtual void OnRequestAuthError() = 0; |
84 }; | 84 }; |
85 CloudPrintURLFetcher(); | 85 CloudPrintURLFetcher(); |
86 | 86 |
87 void StartGetRequest(const GURL& url, | 87 void StartGetRequest(const GURL& url, |
88 Delegate* delegate, | 88 Delegate* delegate, |
89 int max_retries, | 89 int max_retries, |
90 const std::string& additional_headers); | 90 const std::string& additional_headers); |
91 void StartPostRequest(const GURL& url, | 91 void StartPostRequest(const GURL& url, |
92 Delegate* delegate, | 92 Delegate* delegate, |
93 int max_retries, | 93 int max_retries, |
94 const std::string& post_data_mime_type, | 94 const std::string& post_data_mime_type, |
95 const std::string& post_data, | 95 const std::string& post_data, |
96 const std::string& additional_headers); | 96 const std::string& additional_headers); |
97 | 97 |
98 // content::URLFetcherDelegate implementation. | 98 // content::URLFetcherDelegate implementation. |
99 virtual void OnURLFetchComplete(const URLFetcher* source); | 99 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
100 | 100 |
101 protected: | 101 protected: |
102 friend class base::RefCountedThreadSafe<CloudPrintURLFetcher>; | 102 friend class base::RefCountedThreadSafe<CloudPrintURLFetcher>; |
103 virtual ~CloudPrintURLFetcher(); | 103 virtual ~CloudPrintURLFetcher(); |
104 | 104 |
105 // Virtual for testing. | 105 // Virtual for testing. |
106 virtual net::URLRequestContextGetter* GetRequestContextGetter(); | 106 virtual net::URLRequestContextGetter* GetRequestContextGetter(); |
107 | 107 |
108 private: | 108 private: |
109 void StartRequestHelper(const GURL& url, | 109 void StartRequestHelper(const GURL& url, |
110 URLFetcher::RequestType request_type, | 110 URLFetcher::RequestType request_type, |
111 Delegate* delegate, | 111 Delegate* delegate, |
112 int max_retries, | 112 int max_retries, |
113 const std::string& post_data_mime_type, | 113 const std::string& post_data_mime_type, |
114 const std::string& post_data, | 114 const std::string& post_data, |
115 const std::string& additional_headers); | 115 const std::string& additional_headers); |
116 void SetupRequestHeaders(); | 116 void SetupRequestHeaders(); |
117 | 117 |
118 scoped_ptr<URLFetcher> request_; | 118 scoped_ptr<content::URLFetcher> request_; |
119 Delegate* delegate_; | 119 Delegate* delegate_; |
120 int num_retries_; | 120 int num_retries_; |
121 URLFetcher::RequestType request_type_; | 121 URLFetcher::RequestType request_type_; |
122 std::string additional_headers_; | 122 std::string additional_headers_; |
123 std::string post_data_mime_type_; | 123 std::string post_data_mime_type_; |
124 std::string post_data_; | 124 std::string post_data_; |
125 }; | 125 }; |
126 | 126 |
127 typedef CloudPrintURLFetcher::Delegate CloudPrintURLFetcherDelegate; | 127 typedef CloudPrintURLFetcher::Delegate CloudPrintURLFetcherDelegate; |
128 | 128 |
129 #endif // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_ | 129 #endif // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_ |
OLD | NEW |