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