ProjectingObserverChromeos: Drop DBusThreadManager dependency for better testing.
[chromium-blink-merge.git] / components / translate / core / browser / translate_url_fetcher.h
blob1f76bc84dfdd442ae475a6d4aacfaeda4baee655
1 // Copyright 2014 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 COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_URL_FETCHER_H_
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_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"
11 #include "url/gurl.h"
13 namespace translate {
15 // Downloads raw Translate data such as the Translate script and the language
16 // list.
17 class TranslateURLFetcher : public net::URLFetcherDelegate {
18 public:
19 // Callback type for Request().
20 typedef base::Callback<void(int, bool, const std::string&)> Callback;
22 // Represents internal state if the fetch is completed successfully.
23 enum State {
24 IDLE, // No fetch request was issued.
25 REQUESTING, // A fetch request was issued, but not finished yet.
26 COMPLETED, // The last fetch request was finished successfully.
27 FAILED, // The last fetch request was finished with a failure.
30 explicit TranslateURLFetcher(int id);
31 virtual ~TranslateURLFetcher();
33 int max_retry_on_5xx() {
34 return max_retry_on_5xx_;
36 void set_max_retry_on_5xx(int count) {
37 max_retry_on_5xx_ = count;
40 const std::string& extra_request_header() {
41 return extra_request_header_;
43 void set_extra_request_header(const std::string& header) {
44 extra_request_header_ = header;
47 // Requests to |url|. |callback| will be invoked when the function returns
48 // true, and the request is finished asynchronously.
49 // Returns false if the previous request is not finished, or the request
50 // is omitted due to retry limitation.
51 bool Request(const GURL& url, const Callback& callback);
53 // Gets internal state.
54 State state() { return state_; }
56 // net::URLFetcherDelegate implementation:
57 virtual void OnURLFetchComplete(const net::URLFetcher* source) override;
59 private:
60 // URL to send the request.
61 GURL url_;
63 // ID which is assigned to the URLFetcher.
64 const int id_;
66 // Internal state.
67 enum State state_;
69 // URLFetcher instance.
70 scoped_ptr<net::URLFetcher> fetcher_;
72 // Callback passed at Request(). It will be invoked when an asynchronous
73 // fetch operation is finished.
74 Callback callback_;
76 // Counts how many times did it try to fetch the language list.
77 int retry_count_;
79 // Max number how many times to retry on the server error
80 int max_retry_on_5xx_;
82 // An extra HTTP request header
83 std::string extra_request_header_;
85 DISALLOW_COPY_AND_ASSIGN(TranslateURLFetcher);
88 } // namespace translate
90 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_URL_FETCHER_H_