This source file includes following definitions.
- CreateDistillerURLFetcher
- FetchURL
- CreateURLFetcher
- OnURLFetchComplete
#include "components/dom_distiller/core/distiller_url_fetcher.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_status.h"
#include "url/gurl.h"
using net::URLFetcher;
namespace dom_distiller {
DistillerURLFetcherFactory::DistillerURLFetcherFactory(
net::URLRequestContextGetter* context_getter)
: context_getter_(context_getter) {
}
DistillerURLFetcher*
DistillerURLFetcherFactory::CreateDistillerURLFetcher() const {
return new DistillerURLFetcher(context_getter_);
}
DistillerURLFetcher::DistillerURLFetcher(
net::URLRequestContextGetter* context_getter)
: context_getter_(context_getter) {
}
DistillerURLFetcher::~DistillerURLFetcher() {
}
void DistillerURLFetcher::FetchURL(const std::string& url,
const URLFetcherCallback& callback) {
DCHECK(!url_fetcher_ || !url_fetcher_->GetStatus().is_io_pending());
callback_ = callback;
url_fetcher_.reset(CreateURLFetcher(context_getter_, url));
url_fetcher_->Start();
}
URLFetcher* DistillerURLFetcher::CreateURLFetcher(
net::URLRequestContextGetter* context_getter,
const std::string& url) {
net::URLFetcher* fetcher =
URLFetcher::Create(GURL(url), URLFetcher::GET, this);
fetcher->SetRequestContext(context_getter);
static const int kMaxRetries = 5;
fetcher->SetMaxRetriesOn5xx(kMaxRetries);
return fetcher;
}
void DistillerURLFetcher::OnURLFetchComplete(
const URLFetcher* source) {
std::string response;
if (source && source->GetStatus().is_success() &&
source->GetResponseCode() == net::HTTP_OK) {
source->GetResponseAsString(&response);
}
callback_.Run(response);
}
}