Rename GetIconID to GetIconId
[chromium-blink-merge.git] / components / translate / core / browser / translate_url_fetcher.cc
blob6a5951723094a0415c0056c3074d2a22ee37ffbd
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.
5 #include "components/translate/core/browser/translate_url_fetcher.h"
7 #include "components/translate/core/browser/translate_download_manager.h"
8 #include "net/base/load_flags.h"
9 #include "net/http/http_status_code.h"
10 #include "net/url_request/url_fetcher.h"
11 #include "net/url_request/url_request_status.h"
13 namespace translate {
15 namespace {
17 // Retry parameter for fetching.
18 const int kMaxRetry = 16;
20 } // namespace
22 TranslateURLFetcher::TranslateURLFetcher(int id) : id_(id),
23 state_(IDLE),
24 retry_count_(0) {
27 TranslateURLFetcher::~TranslateURLFetcher() {
30 bool TranslateURLFetcher::Request(
31 const GURL& url,
32 const TranslateURLFetcher::Callback& callback) {
33 // This function is not supposed to be called before previous operaion is not
34 // finished.
35 if (state_ == REQUESTING) {
36 NOTREACHED();
37 return false;
40 if (retry_count_ >= kMaxRetry)
41 return false;
42 retry_count_++;
44 state_ = REQUESTING;
45 url_ = url;
46 callback_ = callback;
48 fetcher_ = net::URLFetcher::Create(id_, url_, net::URLFetcher::GET, this);
49 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
50 net::LOAD_DO_NOT_SAVE_COOKIES);
51 fetcher_->SetRequestContext(
52 TranslateDownloadManager::GetInstance()->request_context());
53 // Set retry parameter for HTTP status code 5xx. This doesn't work against
54 // 106 (net::ERR_INTERNET_DISCONNECTED) and so on.
55 // TranslateLanguageList handles network status, and implements retry.
56 fetcher_->SetMaxRetriesOn5xx(max_retry_on_5xx_);
57 if (!extra_request_header_.empty())
58 fetcher_->SetExtraRequestHeaders(extra_request_header_);
60 fetcher_->Start();
62 return true;
65 void TranslateURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
66 DCHECK(fetcher_.get() == source);
68 std::string data;
69 if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS &&
70 source->GetResponseCode() == net::HTTP_OK) {
71 state_ = COMPLETED;
72 source->GetResponseAsString(&data);
73 } else {
74 state_ = FAILED;
77 // Transfer URLFetcher's ownership before invoking a callback.
78 scoped_ptr<const net::URLFetcher> delete_ptr(fetcher_.release());
79 callback_.Run(id_, state_ == COMPLETED, data);
82 } // namespace translate