This source file includes following definitions.
- GetConfigURL
- GetManifestURLPrefix
- ConstructManifestURL
- ParseProtoFromFetchResponse
- Fetcher
- OnURLFetchComplete
- precache_delegate_
- Start
- StartNextFetch
- OnConfigFetchComplete
- OnManifestFetchComplete
- OnResourceFetchComplete
#include "components/precache/core/precache_fetcher.h"
#include <string>
#include "base/bind.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/containers/hash_tables.h"
#include "components/precache/core/precache_switches.h"
#include "components/precache/core/proto/precache.pb.h"
#include "net/base/escape.h"
#include "net/base/load_flags.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"
using net::URLFetcher;
namespace precache {
namespace {
GURL GetConfigURL() {
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kPrecacheConfigSettingsURL)) {
return GURL(
command_line.GetSwitchValueASCII(switches::kPrecacheConfigSettingsURL));
}
#if defined(PRECACHE_CONFIG_SETTINGS_URL)
return GURL(PRECACHE_CONFIG_SETTINGS_URL);
#else
return GURL();
#endif
}
std::string GetManifestURLPrefix() {
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kPrecacheManifestURLPrefix)) {
return command_line.GetSwitchValueASCII(
switches::kPrecacheManifestURLPrefix);
}
#if defined(PRECACHE_MANIFEST_URL_PREFIX)
return PRECACHE_MANIFEST_URL_PREFIX;
#else
return std::string();
#endif
}
GURL ConstructManifestURL(const GURL& starting_url) {
return GURL(
GetManifestURLPrefix() +
net::EscapeQueryParamValue(
net::EscapeQueryParamValue(starting_url.spec(), false), false));
}
bool ParseProtoFromFetchResponse(const URLFetcher& source,
::google::protobuf::MessageLite* message) {
std::string response_string;
if (!source.GetStatus().is_success()) {
DLOG(WARNING) << "Fetch failed: " << source.GetOriginalURL().spec();
return false;
}
if (!source.GetResponseAsString(&response_string)) {
DLOG(WARNING) << "No response string present: "
<< source.GetOriginalURL().spec();
return false;
}
if (!message->ParseFromString(response_string)) {
DLOG(WARNING) << "Unable to parse proto served from "
<< source.GetOriginalURL().spec();
return false;
}
return true;
}
}
class PrecacheFetcher::Fetcher : public net::URLFetcherDelegate {
public:
Fetcher(net::URLRequestContextGetter* request_context, const GURL& url,
const base::Callback<void(const URLFetcher&)>& callback);
virtual ~Fetcher() {}
virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE;
private:
const base::Callback<void(const URLFetcher&)> callback_;
scoped_ptr<URLFetcher> url_fetcher_;
DISALLOW_COPY_AND_ASSIGN(Fetcher);
};
PrecacheFetcher::Fetcher::Fetcher(
net::URLRequestContextGetter* request_context, const GURL& url,
const base::Callback<void(const URLFetcher&)>& callback)
: callback_(callback) {
url_fetcher_.reset(URLFetcher::Create(url, URLFetcher::GET, this));
url_fetcher_->SetRequestContext(request_context);
url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_PROMPT_FOR_LOGIN);
url_fetcher_->Start();
}
void PrecacheFetcher::Fetcher::OnURLFetchComplete(const URLFetcher* source) {
callback_.Run(*source);
}
PrecacheFetcher::PrecacheFetcher(
const std::list<GURL>& starting_urls,
net::URLRequestContextGetter* request_context,
PrecacheFetcher::PrecacheDelegate* precache_delegate)
: starting_urls_(starting_urls),
request_context_(request_context),
precache_delegate_(precache_delegate) {
DCHECK(request_context_);
DCHECK(precache_delegate_);
DCHECK_NE(GURL(), GetConfigURL())
<< "Could not determine the precache config settings URL.";
DCHECK_NE(std::string(), GetManifestURLPrefix())
<< "Could not determine the precache manifest URL prefix.";
}
PrecacheFetcher::~PrecacheFetcher() {
}
void PrecacheFetcher::Start() {
DCHECK(!fetcher_);
GURL config_url = GetConfigURL();
DCHECK(config_url.is_valid());
fetcher_.reset(new Fetcher(request_context_, config_url,
base::Bind(&PrecacheFetcher::OnConfigFetchComplete,
base::Unretained(this))));
}
void PrecacheFetcher::StartNextFetch() {
if (!resource_urls_to_fetch_.empty()) {
fetcher_.reset(
new Fetcher(request_context_, resource_urls_to_fetch_.front(),
base::Bind(&PrecacheFetcher::OnResourceFetchComplete,
base::Unretained(this))));
resource_urls_to_fetch_.pop_front();
return;
}
if (!manifest_urls_to_fetch_.empty()) {
fetcher_.reset(
new Fetcher(request_context_, manifest_urls_to_fetch_.front(),
base::Bind(&PrecacheFetcher::OnManifestFetchComplete,
base::Unretained(this))));
manifest_urls_to_fetch_.pop_front();
return;
}
precache_delegate_->OnDone();
}
void PrecacheFetcher::OnConfigFetchComplete(const URLFetcher& source) {
PrecacheConfigurationSettings config;
if (ParseProtoFromFetchResponse(source, &config)) {
base::hash_set<std::string> unique_starting_urls;
int64 rank = 0;
for (std::list<GURL>::const_iterator it = starting_urls_.begin();
it != starting_urls_.end() && rank < config.top_sites_count();
++it, ++rank) {
if (unique_starting_urls.find(it->spec()) == unique_starting_urls.end()) {
manifest_urls_to_fetch_.push_back(ConstructManifestURL(*it));
unique_starting_urls.insert(it->spec());
}
}
for (int i = 0; i < config.forced_starting_url_size(); ++i) {
GURL url(config.forced_starting_url(i));
if (unique_starting_urls.find(url.spec()) == unique_starting_urls.end()) {
manifest_urls_to_fetch_.push_back(ConstructManifestURL(url));
unique_starting_urls.insert(url.spec());
}
}
}
StartNextFetch();
}
void PrecacheFetcher::OnManifestFetchComplete(const URLFetcher& source) {
PrecacheManifest manifest;
if (ParseProtoFromFetchResponse(source, &manifest)) {
for (int i = 0; i < manifest.resource_size(); ++i) {
if (manifest.resource(i).has_url()) {
resource_urls_to_fetch_.push_back(GURL(manifest.resource(i).url()));
}
}
}
StartNextFetch();
}
void PrecacheFetcher::OnResourceFetchComplete(const URLFetcher& source) {
StartNextFetch();
}
}