1 // Copyright (c) 2012 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.
5 #ifndef CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_
6 #define CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "net/url_request/url_fetcher.h"
14 #include "net/url_request/url_fetcher_delegate.h"
19 class DictionaryValue
;
23 class URLRequestContextGetter
;
24 class URLRequestStatus
;
27 namespace cloud_print
{
29 // Factory for creating CloudPrintURLFetchers.
30 class CloudPrintURLFetcher
;
31 class CloudPrintURLFetcherFactory
{
33 virtual CloudPrintURLFetcher
* CreateCloudPrintURLFetcher() = 0;
34 virtual ~CloudPrintURLFetcherFactory();
37 // A wrapper around URLFetcher for CloudPrint. URLFetcher applies retry logic
38 // only on HTTP response codes >= 500. In the cloud print case, we want to
39 // retry on all network errors. In addition, we want to treat non-JSON responses
40 // (for all CloudPrint APIs that expect JSON responses) as errors and they
41 // must also be retried.
42 class CloudPrintURLFetcher
43 : public base::RefCountedThreadSafe
<CloudPrintURLFetcher
>,
44 public net::URLFetcherDelegate
{
56 REQUEST_UPDATE_PRINTER
,
67 // Override this to handle the raw response as it is available. No response
68 // error checking is done before this method is called. If the delegate
69 // returns CONTINUE_PROCESSING, we will then check for network
70 // errors. Most implementations will not override this.
71 virtual ResponseAction
HandleRawResponse(
72 const net::URLFetcher
* source
,
74 const net::URLRequestStatus
& status
,
76 const net::ResponseCookies
& cookies
,
77 const std::string
& data
);
79 // This will be invoked only if HandleRawResponse returns
80 // CONTINUE_PROCESSING AND if there are no network errors and the HTTP
81 // response code is 200. The delegate implementation returns
82 // CONTINUE_PROCESSING if it does not want to handle the raw data itself.
83 // Handling the raw data is needed when the expected response is NOT JSON
84 // (like in the case of a print ticket response or a print job download
86 virtual ResponseAction
HandleRawData(const net::URLFetcher
* source
,
88 const std::string
& data
);
90 // This will be invoked only if HandleRawResponse and HandleRawData return
91 // CONTINUE_PROCESSING AND if the response contains a valid JSON dictionary.
92 // |succeeded| is the value of the "success" field in the response JSON.
93 virtual ResponseAction
HandleJSONData(const net::URLFetcher
* source
,
95 base::DictionaryValue
* json_data
,
98 // Invoked when the retry limit for this request has been reached (if there
99 // was a retry limit - a limit of -1 implies no limit).
100 virtual void OnRequestGiveUp() { }
102 // Invoked when the request returns a 403 error (applicable only when
103 // HandleRawResponse returns CONTINUE_PROCESSING).
104 // Returning RETRY_REQUEST will retry current request. (auth information
105 // may have been updated and new info is available through the
106 // Authenticator interface).
107 // Returning CONTINUE_PROCESSING will treat auth error as a network error.
108 virtual ResponseAction
OnRequestAuthError() = 0;
110 // Authentication information may change between retries.
111 // CloudPrintURLFetcher will request auth info before sending any request.
112 virtual std::string
GetAuthHeader() = 0;
115 virtual ~Delegate() {}
118 static CloudPrintURLFetcher
* Create();
119 static void set_factory(CloudPrintURLFetcherFactory
* factory
);
121 bool IsSameRequest(const net::URLFetcher
* source
);
123 void StartGetRequest(RequestType type
,
127 const std::string
& additional_headers
);
128 void StartPostRequest(RequestType type
,
132 const std::string
& post_data_mime_type
,
133 const std::string
& post_data
,
134 const std::string
& additional_headers
);
136 // net::URLFetcherDelegate implementation.
137 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
140 CloudPrintURLFetcher();
141 friend class base::RefCountedThreadSafe
<CloudPrintURLFetcher
>;
142 ~CloudPrintURLFetcher() override
;
144 // Virtual for testing.
145 virtual net::URLRequestContextGetter
* GetRequestContextGetter();
148 void StartRequestHelper(RequestType type
,
150 net::URLFetcher::RequestType request_type
,
153 const std::string
& post_data_mime_type
,
154 const std::string
& post_data
,
155 const std::string
& additional_headers
);
156 void SetupRequestHeaders();
157 static CloudPrintURLFetcherFactory
* factory();
159 scoped_ptr
<net::URLFetcher
> request_
;
162 std::string additional_headers_
;
163 std::string post_data_mime_type_
;
164 std::string post_data_
;
167 base::Time start_time_
;
170 typedef CloudPrintURLFetcher::Delegate CloudPrintURLFetcherDelegate
;
172 } // namespace cloud_print
174 #endif // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_URL_FETCHER_H_