| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/precache/core/precache_fetcher.h" | 5 #include "components/precache/core/precache_fetcher.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 callback_.Run(*source); | 129 callback_.Run(*source); |
| 130 } | 130 } |
| 131 | 131 |
| 132 PrecacheFetcher::PrecacheFetcher( | 132 PrecacheFetcher::PrecacheFetcher( |
| 133 const std::list<GURL>& starting_urls, | 133 const std::list<GURL>& starting_urls, |
| 134 net::URLRequestContextGetter* request_context, | 134 net::URLRequestContextGetter* request_context, |
| 135 PrecacheFetcher::PrecacheDelegate* precache_delegate) | 135 PrecacheFetcher::PrecacheDelegate* precache_delegate) |
| 136 : starting_urls_(starting_urls), | 136 : starting_urls_(starting_urls), |
| 137 request_context_(request_context), | 137 request_context_(request_context), |
| 138 precache_delegate_(precache_delegate) { | 138 precache_delegate_(precache_delegate) { |
| 139 DCHECK(request_context_); // Request context must be non-NULL. | 139 DCHECK(request_context_.get()); // Request context must be non-NULL. |
| 140 DCHECK(precache_delegate_); // Precache delegate must be non-NULL. | 140 DCHECK(precache_delegate_); // Precache delegate must be non-NULL. |
| 141 | 141 |
| 142 DCHECK_NE(GURL(), GetConfigURL()) | 142 DCHECK_NE(GURL(), GetConfigURL()) |
| 143 << "Could not determine the precache config settings URL."; | 143 << "Could not determine the precache config settings URL."; |
| 144 DCHECK_NE(std::string(), GetManifestURLPrefix()) | 144 DCHECK_NE(std::string(), GetManifestURLPrefix()) |
| 145 << "Could not determine the precache manifest URL prefix."; | 145 << "Could not determine the precache manifest URL prefix."; |
| 146 } | 146 } |
| 147 | 147 |
| 148 PrecacheFetcher::~PrecacheFetcher() { | 148 PrecacheFetcher::~PrecacheFetcher() { |
| 149 } | 149 } |
| 150 | 150 |
| 151 void PrecacheFetcher::Start() { | 151 void PrecacheFetcher::Start() { |
| 152 DCHECK(!fetcher_); // Start shouldn't be called repeatedly. | 152 DCHECK(!fetcher_); // Start shouldn't be called repeatedly. |
| 153 | 153 |
| 154 GURL config_url = GetConfigURL(); | 154 GURL config_url = GetConfigURL(); |
| 155 DCHECK(config_url.is_valid()); | 155 DCHECK(config_url.is_valid()); |
| 156 | 156 |
| 157 // Fetch the precache configuration settings from the server. | 157 // Fetch the precache configuration settings from the server. |
| 158 fetcher_.reset(new Fetcher(request_context_, config_url, | 158 fetcher_.reset(new Fetcher(request_context_.get(), |
| 159 config_url, |
| 159 base::Bind(&PrecacheFetcher::OnConfigFetchComplete, | 160 base::Bind(&PrecacheFetcher::OnConfigFetchComplete, |
| 160 base::Unretained(this)))); | 161 base::Unretained(this)))); |
| 161 } | 162 } |
| 162 | 163 |
| 163 void PrecacheFetcher::StartNextFetch() { | 164 void PrecacheFetcher::StartNextFetch() { |
| 164 if (!resource_urls_to_fetch_.empty()) { | 165 if (!resource_urls_to_fetch_.empty()) { |
| 165 // Fetch the next resource URL. | 166 // Fetch the next resource URL. |
| 166 fetcher_.reset( | 167 fetcher_.reset( |
| 167 new Fetcher(request_context_, resource_urls_to_fetch_.front(), | 168 new Fetcher(request_context_.get(), |
| 169 resource_urls_to_fetch_.front(), |
| 168 base::Bind(&PrecacheFetcher::OnResourceFetchComplete, | 170 base::Bind(&PrecacheFetcher::OnResourceFetchComplete, |
| 169 base::Unretained(this)))); | 171 base::Unretained(this)))); |
| 170 | 172 |
| 171 resource_urls_to_fetch_.pop_front(); | 173 resource_urls_to_fetch_.pop_front(); |
| 172 return; | 174 return; |
| 173 } | 175 } |
| 174 | 176 |
| 175 if (!manifest_urls_to_fetch_.empty()) { | 177 if (!manifest_urls_to_fetch_.empty()) { |
| 176 // Fetch the next manifest URL. | 178 // Fetch the next manifest URL. |
| 177 fetcher_.reset( | 179 fetcher_.reset( |
| 178 new Fetcher(request_context_, manifest_urls_to_fetch_.front(), | 180 new Fetcher(request_context_.get(), |
| 181 manifest_urls_to_fetch_.front(), |
| 179 base::Bind(&PrecacheFetcher::OnManifestFetchComplete, | 182 base::Bind(&PrecacheFetcher::OnManifestFetchComplete, |
| 180 base::Unretained(this)))); | 183 base::Unretained(this)))); |
| 181 | 184 |
| 182 manifest_urls_to_fetch_.pop_front(); | 185 manifest_urls_to_fetch_.pop_front(); |
| 183 return; | 186 return; |
| 184 } | 187 } |
| 185 | 188 |
| 186 // There are no more URLs to fetch, so end the precache cycle. | 189 // There are no more URLs to fetch, so end the precache cycle. |
| 187 precache_delegate_->OnDone(); | 190 precache_delegate_->OnDone(); |
| 188 // OnDone may have deleted this PrecacheFetcher, so don't do anything after it | 191 // OnDone may have deleted this PrecacheFetcher, so don't do anything after it |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 StartNextFetch(); | 246 StartNextFetch(); |
| 244 } | 247 } |
| 245 | 248 |
| 246 void PrecacheFetcher::OnResourceFetchComplete(const URLFetcher& source) { | 249 void PrecacheFetcher::OnResourceFetchComplete(const URLFetcher& source) { |
| 247 // The resource has already been put in the cache during the fetch process, so | 250 // The resource has already been put in the cache during the fetch process, so |
| 248 // nothing more needs to be done for the resource. | 251 // nothing more needs to be done for the resource. |
| 249 StartNextFetch(); | 252 StartNextFetch(); |
| 250 } | 253 } |
| 251 | 254 |
| 252 } // namespace precache | 255 } // namespace precache |
| OLD | NEW |