Home | History | Annotate | Download | only in browser
      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 #include "components/translate/core/browser/translate_url_fetcher.h"
      6 
      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"
     12 
     13 namespace {
     14 
     15 // Retry parameter for fetching.
     16 const int kMaxRetry = 16;
     17 
     18 }  // namespace
     19 
     20 TranslateURLFetcher::TranslateURLFetcher(int id) : id_(id),
     21                                                    state_(IDLE),
     22                                                    retry_count_(0) {
     23 }
     24 
     25 TranslateURLFetcher::~TranslateURLFetcher() {
     26 }
     27 
     28 bool TranslateURLFetcher::Request(
     29     const GURL& url,
     30     const TranslateURLFetcher::Callback& callback) {
     31   // This function is not supposed to be called before previous operaion is not
     32   // finished.
     33   if (state_ == REQUESTING) {
     34     NOTREACHED();
     35     return false;
     36   }
     37 
     38   if (retry_count_ >= kMaxRetry)
     39     return false;
     40   retry_count_++;
     41 
     42   state_ = REQUESTING;
     43   url_ = url;
     44   callback_ = callback;
     45 
     46   fetcher_.reset(net::URLFetcher::Create(
     47       id_,
     48       url_,
     49       net::URLFetcher::GET,
     50       this));
     51   fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
     52                          net::LOAD_DO_NOT_SAVE_COOKIES);
     53   fetcher_->SetRequestContext(
     54       TranslateDownloadManager::GetInstance()->request_context());
     55   // Set retry parameter for HTTP status code 5xx. This doesn't work against
     56   // 106 (net::ERR_INTERNET_DISCONNECTED) and so on.
     57   // TranslateLanguageList handles network status, and implements retry.
     58   fetcher_->SetMaxRetriesOn5xx(max_retry_on_5xx_);
     59   if (!extra_request_header_.empty())
     60     fetcher_->SetExtraRequestHeaders(extra_request_header_);
     61 
     62   fetcher_->Start();
     63 
     64   return true;
     65 }
     66 
     67 void TranslateURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
     68   DCHECK(fetcher_.get() == source);
     69 
     70   std::string data;
     71   if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS &&
     72       source->GetResponseCode() == net::HTTP_OK) {
     73     state_ = COMPLETED;
     74     source->GetResponseAsString(&data);
     75   } else {
     76     state_ = FAILED;
     77   }
     78 
     79   // Transfer URLFetcher's ownership before invoking a callback.
     80   scoped_ptr<const net::URLFetcher> delete_ptr(fetcher_.release());
     81   callback_.Run(id_, state_ == COMPLETED, data);
     82 }
     83