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. 4 5 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_URL_FETCHER_H_ 6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_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 // Downloads raw Translate data such as the Translate script and the language 14 // list. 15 class TranslateURLFetcher : public net::URLFetcherDelegate { 16 public: 17 // Callback type for Request(). 18 typedef base::Callback<void(int, bool, const std::string&)> Callback; 19 20 // Represents internal state if the fetch is completed successfully. 21 enum State { 22 IDLE, // No fetch request was issued. 23 REQUESTING, // A fetch request was issued, but not finished yet. 24 COMPLETED, // The last fetch request was finished successfully. 25 FAILED, // The last fetch request was finished with a failure. 26 }; 27 28 explicit TranslateURLFetcher(int id); 29 virtual ~TranslateURLFetcher(); 30 31 int max_retry_on_5xx() { 32 return max_retry_on_5xx_; 33 } 34 void set_max_retry_on_5xx(int count) { 35 max_retry_on_5xx_ = count; 36 } 37 38 const std::string& extra_request_header() { 39 return extra_request_header_; 40 } 41 void set_extra_request_header(const std::string& header) { 42 extra_request_header_ = header; 43 } 44 45 // Requests to |url|. |callback| will be invoked when the function returns 46 // true, and the request is finished asynchronously. 47 // Returns false if the previous request is not finished, or the request 48 // is omitted due to retry limitation. 49 bool Request(const GURL& url, const Callback& callback); 50 51 // Gets internal state. 52 State state() { return state_; } 53 54 // net::URLFetcherDelegate implementation: 55 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 56 57 private: 58 // URL to send the request. 59 GURL url_; 60 61 // ID which is assigned to the URLFetcher. 62 const int id_; 63 64 // Internal state. 65 enum State state_; 66 67 // URLFetcher instance. 68 scoped_ptr<net::URLFetcher> fetcher_; 69 70 // Callback passed at Request(). It will be invoked when an asynchronous 71 // fetch operation is finished. 72 Callback callback_; 73 74 // Counts how many times did it try to fetch the language list. 75 int retry_count_; 76 77 // Max number how many times to retry on the server error 78 int max_retry_on_5xx_; 79 80 // An extra HTTP request header 81 std::string extra_request_header_; 82 83 DISALLOW_COPY_AND_ASSIGN(TranslateURLFetcher); 84 }; 85 86 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_URL_FETCHER_H_ 87