Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / translate / core / browser / translate_url_fetcher.h
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 namespace translate {
14
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;
21
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.
28   };
29
30   explicit TranslateURLFetcher(int id);
31   ~TranslateURLFetcher() override;
32
33   int max_retry_on_5xx() {
34     return max_retry_on_5xx_;
35   }
36   void set_max_retry_on_5xx(int count) {
37     max_retry_on_5xx_ = count;
38   }
39
40   const std::string& extra_request_header() {
41     return extra_request_header_;
42   }
43   void set_extra_request_header(const std::string& header) {
44     extra_request_header_ = header;
45   }
46
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);
52
53   // Gets internal state.
54   State state() { return state_; }
55
56   // net::URLFetcherDelegate implementation:
57   void OnURLFetchComplete(const net::URLFetcher* source) override;
58
59  private:
60   // URL to send the request.
61   GURL url_;
62
63   // ID which is assigned to the URLFetcher.
64   const int id_;
65
66   // Internal state.
67   enum State state_;
68
69   // URLFetcher instance.
70   scoped_ptr<net::URLFetcher> fetcher_;
71
72   // Callback passed at Request(). It will be invoked when an asynchronous
73   // fetch operation is finished.
74   Callback callback_;
75
76   // Counts how many times did it try to fetch the language list.
77   int retry_count_;
78
79   // Max number how many times to retry on the server error
80   int max_retry_on_5xx_;
81
82   // An extra HTTP request header
83   std::string extra_request_header_;
84
85   DISALLOW_COPY_AND_ASSIGN(TranslateURLFetcher);
86 };
87
88 }  // namespace translate
89
90 #endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_URL_FETCHER_H_