OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/translate/translate_url_fetcher.h" | 5 #include "chrome/browser/translate/translate_url_fetcher.h" |
6 | 6 |
7 #include "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
8 #include "net/base/load_flags.h" | 8 #include "net/base/load_flags.h" |
9 #include "net/http/http_status_code.h" | 9 #include "net/http/http_status_code.h" |
10 #include "net/url_request/url_fetcher.h" | 10 #include "net/url_request/url_fetcher.h" |
11 #include "net/url_request/url_request_status.h" | 11 #include "net/url_request/url_request_status.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Retry parameter for fetching. | 15 // Retry parameter for fetching. |
16 const int kMaxRetry = 5; | 16 const int kMaxRetryOn5xx = 5; |
| 17 const int kMaxRetry = 16; |
17 | 18 |
18 } // namespace | 19 } // namespace |
19 | 20 |
20 TranslateURLFetcher::TranslateURLFetcher(int id) | 21 TranslateURLFetcher::TranslateURLFetcher(int id) |
21 : id_(id), | 22 : id_(id), |
22 state_(IDLE) { | 23 state_(IDLE), |
| 24 retry_count_(0) { |
23 } | 25 } |
24 | 26 |
25 TranslateURLFetcher::~TranslateURLFetcher() { | 27 TranslateURLFetcher::~TranslateURLFetcher() { |
26 } | 28 } |
27 | 29 |
28 bool TranslateURLFetcher::Request( | 30 bool TranslateURLFetcher::Request( |
29 const GURL& url, | 31 const GURL& url, |
30 const TranslateURLFetcher::Callback& callback) { | 32 const TranslateURLFetcher::Callback& callback) { |
31 // This function is not supporsed to be called before previous operaion is not | 33 // This function is not supposed to be called before previous operaion is not |
32 // finished. | 34 // finished. |
33 if (state_ == REQUESTING) { | 35 if (state_ == REQUESTING) { |
34 NOTREACHED(); | 36 NOTREACHED(); |
35 return false; | 37 return false; |
36 } | 38 } |
37 | 39 |
| 40 if (retry_count_ >= kMaxRetry) |
| 41 return false; |
| 42 retry_count_++; |
| 43 |
38 state_ = REQUESTING; | 44 state_ = REQUESTING; |
39 url_ = url; | 45 url_ = url; |
40 callback_ = callback; | 46 callback_ = callback; |
41 | 47 |
42 fetcher_.reset(net::URLFetcher::Create( | 48 fetcher_.reset(net::URLFetcher::Create( |
43 id_, | 49 id_, |
44 url_, | 50 url_, |
45 net::URLFetcher::GET, | 51 net::URLFetcher::GET, |
46 this)); | 52 this)); |
47 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 53 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
48 net::LOAD_DO_NOT_SAVE_COOKIES); | 54 net::LOAD_DO_NOT_SAVE_COOKIES); |
49 fetcher_->SetRequestContext(g_browser_process->system_request_context()); | 55 fetcher_->SetRequestContext(g_browser_process->system_request_context()); |
50 fetcher_->SetMaxRetriesOn5xx(kMaxRetry); | 56 // Set retry parameter for HTTP status code 5xx. This doesn't work against |
| 57 // 106 (net::ERR_INTERNET_DISCONNECTED) and so on. |
| 58 // TranslateLanguageList handles network status, and implements retry. |
| 59 fetcher_->SetMaxRetriesOn5xx(kMaxRetryOn5xx); |
51 fetcher_->Start(); | 60 fetcher_->Start(); |
52 | 61 |
53 return true; | 62 return true; |
54 } | 63 } |
55 | 64 |
56 void TranslateURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 65 void TranslateURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
57 DCHECK(fetcher_.get() == source); | 66 DCHECK(fetcher_.get() == source); |
58 | 67 |
59 std::string data; | 68 std::string data; |
60 if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS && | 69 if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS && |
61 source->GetResponseCode() == net::HTTP_OK) { | 70 source->GetResponseCode() == net::HTTP_OK) { |
62 state_ = COMPLETED; | 71 state_ = COMPLETED; |
63 source->GetResponseAsString(&data); | 72 source->GetResponseAsString(&data); |
64 } else { | 73 } else { |
65 state_ = FAILED; | 74 state_ = FAILED; |
66 } | 75 } |
67 | 76 |
| 77 // Transfer URLFetcher's ownership before invoking a callback. |
68 scoped_ptr<const net::URLFetcher> delete_ptr(fetcher_.release()); | 78 scoped_ptr<const net::URLFetcher> delete_ptr(fetcher_.release()); |
69 callback_.Run(id_, state_ == COMPLETED, data); | 79 callback_.Run(id_, state_ == COMPLETED, data); |
70 } | 80 } |
OLD | NEW |