Skip to content

Commit

Permalink
Added list of forced starting URLs to precache configuration settings.
Browse files Browse the repository at this point in the history
BUG=306185

Review URL: https://codereview.chromium.org/103373006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@241174 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
sclittle@chromium.org committed Dec 17, 2013
1 parent 3c51507 commit 12e164f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
25 changes: 24 additions & 1 deletion components/precache/core/precache_fetcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#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"
Expand Down Expand Up @@ -192,14 +193,36 @@ void PrecacheFetcher::OnConfigFetchComplete(const URLFetcher& source) {
PrecacheConfigurationSettings config;

if (ParseProtoFromFetchResponse(source, &config)) {
// Keep track of starting URLs that manifests are being fetched for, in
// order to remove duplicates. This is a hash set on strings, and not GURLs,
// because there is no hash function defined for GURL.
base::hash_set<std::string> unique_starting_urls;

// Attempt to fetch manifests for starting URLs up to the maximum top sites
// count. If a manifest does not exist for a particular starting URL, then
// the fetch will fail, and that starting URL will be ignored.
int64 rank = 0;
for (std::list<GURL>::const_iterator it = starting_urls_.begin();
it != starting_urls_.end() && rank < config.top_sites_count();
++it, ++rank) {
manifest_urls_to_fetch_.push_back(ConstructManifestURL(*it));
if (unique_starting_urls.find(it->spec()) == unique_starting_urls.end()) {
// Only add a fetch for the manifest URL if this manifest isn't already
// going to be fetched.
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) {
// Convert the string URL into a GURL and take the spec() of it so that
// the URL string gets canonicalized.
GURL url(config.forced_starting_url(i));
if (unique_starting_urls.find(url.spec()) == unique_starting_urls.end()) {
// Only add a fetch for the manifest URL if this manifest isn't already
// going to be fetched.
manifest_urls_to_fetch_.push_back(ConstructManifestURL(url));
unique_starting_urls.insert(url.spec());
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions components/precache/core/precache_fetcher_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ const char kGoodManifestURL[] =
"http://manifest-url-prefix.com/http%253A%252F%252Fgood-manifest.com%252F";
const char kResourceFetchFailureURL[] = "http://resource-fetch-failure.com";
const char kGoodResourceURL[] = "http://good-resource.com";
const char kForcedStartingURLManifestURL[] =
"http://manifest-url-prefix.com/"
"http%253A%252F%252Fforced-starting-url.com%252F";

TEST_F(PrecacheFetcherTest, FullPrecache) {
CommandLine::ForCurrentProcess()->AppendSwitchASCII(
Expand All @@ -114,6 +117,9 @@ TEST_F(PrecacheFetcherTest, FullPrecache) {

PrecacheConfigurationSettings config;
config.set_top_sites_count(3);
config.add_forced_starting_url("http://forced-starting-url.com");
// Duplicate starting URL, the manifest for this should only be fetched once.
config.add_forced_starting_url("http://good-manifest.com");

PrecacheManifest good_manifest;
good_manifest.add_resource()->set_url(kResourceFetchFailureURL);
Expand All @@ -135,6 +141,9 @@ TEST_F(PrecacheFetcherTest, FullPrecache) {
net::URLRequestStatus::FAILED);
factory_.SetFakeResponse(GURL(kGoodResourceURL), "good", net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
factory_.SetFakeResponse(GURL(kForcedStartingURLManifestURL),
PrecacheManifest().SerializeAsString(), net::HTTP_OK,
net::URLRequestStatus::SUCCESS);

PrecacheFetcher precache_fetcher(starting_urls, request_context_.get(),
&precache_delegate_);
Expand All @@ -149,6 +158,7 @@ TEST_F(PrecacheFetcherTest, FullPrecache) {
expected_requested_urls.insert(GURL(kGoodManifestURL));
expected_requested_urls.insert(GURL(kResourceFetchFailureURL));
expected_requested_urls.insert(GURL(kGoodResourceURL));
expected_requested_urls.insert(GURL(kForcedStartingURLManifestURL));

EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls());

Expand Down
5 changes: 5 additions & 0 deletions components/precache/core/proto/precache.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ message PrecacheConfigurationSettings {
// Values that are zero or lower indicate that none of the user's top sites
// will be used for precaching.
optional int64 top_sites_count = 1 [default = 10];

// List of additional starting URLs that resources will be precached for.
// These are URLs that the server predicts that the user will visit, as a
// result of server-side analytics.
repeated string forced_starting_url = 2;
};

0 comments on commit 12e164f

Please sign in to comment.