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. 4 5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_URL_FETCHER_H_ 6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_URL_FETCHER_H_ 7 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" 12 13 class TranslateURLFetcher : public net::URLFetcherDelegate { 14 public: 15 // Callback type for Request(). 16 typedef base::Callback<void(int, bool, const std::string&)> Callback; 17 18 // Represents internal state if the fetch is completed successfully. 19 enum State { 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. 24 }; 25 26 explicit TranslateURLFetcher(int id); 27 virtual ~TranslateURLFetcher(); 28 29 int max_retry_on_5xx() { 30 return max_retry_on_5xx_; 31 } 32 void set_max_retry_on_5xx(int count) { 33 max_retry_on_5xx_ = count; 34 } 35 36 const std::string& extra_request_header() { 37 return extra_request_header_; 38 } 39 void set_extra_request_header(const std::string& header) { 40 extra_request_header_ = header; 41 } 42 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); 48 49 // Gets internal state. 50 State state() { return state_; } 51 52 // net::URLFetcherDelegate implementation: 53 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 54 55 private: 56 // URL to send the request. 57 GURL url_; 58 59 // ID which is assigned to the URLFetcher. 60 const int id_; 61 62 // Internal state. 63 enum State state_; 64 65 // URLFetcher instance. 66 scoped_ptr<net::URLFetcher> fetcher_; 67 68 // Callback passed at Request(). It will be invoked when an asynchronous 69 // fetch operation is finished. 70 Callback callback_; 71 72 // Counts how many times did it try to fetch the language list. 73 int retry_count_; 74 75 // Max number how many times to retry on the server error 76 int max_retry_on_5xx_; 77 78 // An extra HTTP request header 79 std::string extra_request_header_; 80 81 DISALLOW_COPY_AND_ASSIGN(TranslateURLFetcher); 82 }; 83 84 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_URL_FETCHER_H_ 85