1 // Copyright 2013 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 #ifndef COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_
6 #define COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
19 class URLRequestContextGetter
;
24 // Public interface to code that fetches resources that the user is likely to
25 // want to fetch in the future, putting them in the network stack disk cache.
26 // Precaching is intended to be done when Chrome is not actively in use, likely
27 // hours ahead of the time when the resources are actually needed.
29 // This class takes as input a prioritized list of URL domains that the user
30 // commonly visits, referred to as starting hosts. This class interacts with a
31 // server, sending it the list of starting hosts sequentially. For each starting
32 // host, the server returns a manifest of resource URLs that are good candidates
33 // for precaching. Every resource returned is fetched, and responses are cached
34 // as they are received. Destroying the PrecacheFetcher while it is precaching
35 // will cancel any fetch in progress and cancel precaching.
37 // The URLs of the server-side component must be specified in order for the
38 // PrecacheFetcher to work. This includes the URL that the precache
39 // configuration settings are fetched from and the prefix of URLs where precache
40 // manifests are fetched from. These can be set by using command line switches
41 // or by providing default values.
43 // Sample interaction:
45 // class MyPrecacheFetcherDelegate : public PrecacheFetcher::PrecacheDelegate {
47 // void PrecacheResourcesForTopURLs(
48 // net::URLRequestContextGetter* request_context,
49 // const std::list<GURL>& top_urls) {
50 // fetcher_.reset(new PrecacheFetcher(request_context, top_urls, this));
54 // virtual void OnDone() {
55 // // Do something when precaching is done.
59 // scoped_ptr<PrecacheFetcher> fetcher_;
61 class PrecacheFetcher
{
63 class PrecacheDelegate
{
65 // Called when the fetching of resources has finished, whether the resources
66 // were fetched or not. If the PrecacheFetcher is destroyed before OnDone is
67 // called, then precaching will be canceled and OnDone will not be called.
68 virtual void OnDone() = 0;
71 // Constructs a new PrecacheFetcher. The |starting_hosts| parameter is a
72 // prioritized list of hosts that the user commonly visits. These hosts are
73 // used by a server side component to construct a list of resource URLs that
74 // the user is likely to fetch.
75 PrecacheFetcher(const std::vector
<std::string
>& starting_hosts
,
76 net::URLRequestContextGetter
* request_context
,
77 const std::string
& manifest_url_prefix
,
78 PrecacheDelegate
* precache_delegate
);
80 virtual ~PrecacheFetcher();
82 // Starts fetching resources to precache. URLs are fetched sequentially. Can
83 // be called from any thread. Start should only be called once on a
84 // PrecacheFetcher instance.
90 // Fetches the next resource or manifest URL, if any remain. Fetching is done
91 // sequentially and depth-first: all resources are fetched for a manifest
92 // before the next manifest is fetched. This is done to limit the length of
93 // the |resource_urls_to_fetch_| list, reducing the memory usage.
94 void StartNextFetch();
96 // Called when the precache configuration settings have been fetched.
97 // Determines the list of manifest URLs to fetch according to the list of
98 // |starting_hosts_| and information from the precache configuration settings.
99 // If the fetch of the configuration settings fails, then precaching ends.
100 void OnConfigFetchComplete(const net::URLFetcher
& source
);
102 // Called when a precache manifest has been fetched. Builds the list of
103 // resource URLs to fetch according to the URLs in the manifest. If the fetch
104 // of a manifest fails, then it skips to the next manifest.
105 void OnManifestFetchComplete(const net::URLFetcher
& source
);
107 // Called when a resource has been fetched.
108 void OnResourceFetchComplete(const net::URLFetcher
& source
);
110 // The prioritized list of starting hosts that the server will pick resource
111 // URLs to be precached for.
112 const std::vector
<std::string
> starting_hosts_
;
114 // The request context used when fetching URLs.
115 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
117 // The custom URL prefix to use when fetching manifests. If not provided, the
118 // default flag-specified prefix will be used.
119 const std::string manifest_url_prefix_
;
121 // Non-owning pointer. Should not be NULL.
122 PrecacheDelegate
* precache_delegate_
;
124 // Tally of the total number of bytes received from URL fetches, including
125 // config, manifests, and resources.
126 int total_response_bytes_
;
128 scoped_ptr
<Fetcher
> fetcher_
;
130 int num_manifest_urls_to_fetch_
;
131 std::list
<GURL
> manifest_urls_to_fetch_
;
132 std::list
<GURL
> resource_urls_to_fetch_
;
134 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher
);
137 } // namespace precache
139 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_