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 PrecacheDelegate
* precache_delegate
);
79 virtual ~PrecacheFetcher();
81 // Starts fetching resources to precache. URLs are fetched sequentially. Can
82 // be called from any thread. Start should only be called once on a
83 // PrecacheFetcher instance.
89 // Fetches the next resource or manifest URL, if any remain. Fetching is done
90 // sequentially and depth-first: all resources are fetched for a manifest
91 // before the next manifest is fetched. This is done to limit the length of
92 // the |resource_urls_to_fetch_| list, reducing the memory usage.
93 void StartNextFetch();
95 // Called when the precache configuration settings have been fetched.
96 // Determines the list of manifest URLs to fetch according to the list of
97 // |starting_hosts_| and information from the precache configuration settings.
98 // If the fetch of the configuration settings fails, then precaching ends.
99 void OnConfigFetchComplete(const net::URLFetcher
& source
);
101 // Called when a precache manifest has been fetched. Builds the list of
102 // resource URLs to fetch according to the URLs in the manifest. If the fetch
103 // of a manifest fails, then it skips to the next manifest.
104 void OnManifestFetchComplete(const net::URLFetcher
& source
);
106 // Called when a resource has been fetched.
107 void OnResourceFetchComplete(const net::URLFetcher
& source
);
109 // The prioritized list of starting hosts that the server will pick resource
110 // URLs to be precached for.
111 const std::vector
<std::string
> starting_hosts_
;
113 // The request context used when fetching URLs.
114 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
116 // Non-owning pointer. Should not be NULL.
117 PrecacheDelegate
* precache_delegate_
;
119 scoped_ptr
<Fetcher
> fetcher_
;
121 std::list
<GURL
> manifest_urls_to_fetch_
;
122 std::list
<GURL
> resource_urls_to_fetch_
;
124 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher
);
127 } // namespace precache
129 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_