OLD | NEW |
(Empty) | |
| 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_RANKER_TRANSLATE_URL_FETCHER_H_ |
| 6 #define COMPONENTS_RANKER_TRANSLATE_URL_FETCHER_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/callback.h" |
| 11 #include "base/macros.h" |
| 12 #include "net/url_request/url_fetcher_delegate.h" |
| 13 #include "url/gurl.h" |
| 14 |
| 15 namespace ranker { |
| 16 |
| 17 // Downloads Ranker models. |
| 18 class RankerURLFetcher : public net::URLFetcherDelegate { |
| 19 public: |
| 20 // Callback type for Request(). |
| 21 typedef base::Callback<void(bool, const std::string&)> Callback; |
| 22 |
| 23 // Represents internal state if the fetch is completed successfully. |
| 24 enum State { |
| 25 IDLE, // No fetch request was issued. |
| 26 REQUESTING, // A fetch request was issued, but not finished yet. |
| 27 COMPLETED, // The last fetch request was finished successfully. |
| 28 FAILED, // The last fetch request was finished with a failure. |
| 29 }; |
| 30 |
| 31 explicit RankerURLFetcher(); |
| 32 ~RankerURLFetcher() override; |
| 33 |
| 34 int max_retry_on_5xx() { return max_retry_on_5xx_; } |
| 35 void set_max_retry_on_5xx(int count) { max_retry_on_5xx_ = count; } |
| 36 |
| 37 // Requests to |url|. |callback| will be invoked when the function returns |
| 38 // true, and the request is finished asynchronously. |
| 39 // Returns false if the previous request is not finished, or the request |
| 40 // is omitted due to retry limitation. |
| 41 bool Request(const GURL& url, const Callback& callback); |
| 42 |
| 43 // Gets internal state. |
| 44 State state() { return state_; } |
| 45 |
| 46 // net::URLFetcherDelegate implementation: |
| 47 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 48 |
| 49 private: |
| 50 // URL to send the request. |
| 51 GURL url_; |
| 52 |
| 53 // Internal state. |
| 54 enum State state_; |
| 55 |
| 56 // URLFetcher instance. |
| 57 std::unique_ptr<net::URLFetcher> fetcher_; |
| 58 |
| 59 // Callback passed at Request(). It will be invoked when an asynchronous |
| 60 // fetch operation is finished. |
| 61 Callback callback_; |
| 62 |
| 63 // Counts how many times did it try to fetch the language list. |
| 64 int retry_count_; |
| 65 |
| 66 // Max number how many times to retry on the server error |
| 67 int max_retry_on_5xx_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(RankerURLFetcher); |
| 70 }; |
| 71 |
| 72 } // namespace ranker |
| 73 |
| 74 #endif // COMPONENTS_RANKER_TRANSLATE_URL_FETCHER_H_ |
OLD | NEW |