1 // Copyright 2013 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_BROWSER_TRANSLATE_TRANSLATE_URL_FETCHER_H_
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_URL_FETCHER_H_
8 #include "base/callback.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "net/url_request/url_fetcher_delegate.h"
13 class TranslateURLFetcher : public net::URLFetcherDelegate {
15 // Callback type for Request().
16 typedef base::Callback<void(int, bool, const std::string&)> Callback;
18 // Represents internal state if the fetch is completed successfully.
20 IDLE, // No fetch request was issued.
21 REQUESTING, // A fetch request was issued, but not finished yet.
22 COMPLETED, // The last fetch request was finished successfully.
23 FAILED, // The last fetch request was finished with a failure.
26 explicit TranslateURLFetcher(int id);
27 virtual ~TranslateURLFetcher();
29 int max_retry_on_5xx() {
30 return max_retry_on_5xx_;
32 void set_max_retry_on_5xx(int count) {
33 max_retry_on_5xx_ = count;
36 const std::string& extra_request_header() {
37 return extra_request_header_;
39 void set_extra_request_header(const std::string& header) {
40 extra_request_header_ = header;
43 // Requests to |url|. |callback| will be invoked when the function returns
44 // true, and the request is finished asynchronously.
45 // Returns false if the previous request is not finished, or the request
46 // is omitted due to retry limitation.
47 bool Request(const GURL& url, const Callback& callback);
49 // Gets internal state.
50 State state() { return state_; }
52 // net::URLFetcherDelegate implementation:
53 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
56 // URL to send the request.
59 // ID which is assigned to the URLFetcher.
65 // URLFetcher instance.
66 scoped_ptr<net::URLFetcher> fetcher_;
68 // Callback passed at Request(). It will be invoked when an asynchronous
69 // fetch operation is finished.
72 // Counts how many times did it try to fetch the language list.
75 // Max number how many times to retry on the server error
76 int max_retry_on_5xx_;
78 // An extra HTTP request header
79 std::string extra_request_header_;
81 DISALLOW_COPY_AND_ASSIGN(TranslateURLFetcher);
84 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_URL_FETCHER_H_