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"
17 // Retry parameter for fetching.
18 const int kMaxRetry
= 16;
22 TranslateURLFetcher::TranslateURLFetcher(int id
) : id_(id
),
27 TranslateURLFetcher::~TranslateURLFetcher() {
30 bool TranslateURLFetcher::Request(
32 const TranslateURLFetcher::Callback
& callback
) {
33 // This function is not supposed to be called before previous operaion is not
35 if (state_
== REQUESTING
) {
40 if (retry_count_
>= kMaxRetry
)
48 fetcher_
.reset(net::URLFetcher::Create(
53 fetcher_
->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES
|
54 net::LOAD_DO_NOT_SAVE_COOKIES
);
55 fetcher_
->SetRequestContext(
56 TranslateDownloadManager::GetInstance()->request_context());
57 // Set retry parameter for HTTP status code 5xx. This doesn't work against
58 // 106 (net::ERR_INTERNET_DISCONNECTED) and so on.
59 // TranslateLanguageList handles network status, and implements retry.
60 fetcher_
->SetMaxRetriesOn5xx(max_retry_on_5xx_
);
61 if (!extra_request_header_
.empty())
62 fetcher_
->SetExtraRequestHeaders(extra_request_header_
);
69 void TranslateURLFetcher::OnURLFetchComplete(const net::URLFetcher
* source
) {
70 DCHECK(fetcher_
.get() == source
);
73 if (source
->GetStatus().status() == net::URLRequestStatus::SUCCESS
&&
74 source
->GetResponseCode() == net::HTTP_OK
) {
76 source
->GetResponseAsString(&data
);
81 // Transfer URLFetcher's ownership before invoking a callback.
82 scoped_ptr
<const net::URLFetcher
> delete_ptr(fetcher_
.release());
83 callback_
.Run(id_
, state_
== COMPLETED
, data
);
86 } // namespace translate