| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/appcache/appcache_update_job.h" | 5 #include "content/browser/appcache/appcache_update_job.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "content/browser/appcache/appcache_group.h" | 15 #include "content/browser/appcache/appcache_group.h" |
| 16 #include "content/browser/appcache/appcache_histograms.h" | 16 #include "content/browser/appcache/appcache_histograms.h" |
| 17 #include "content/browser/appcache/appcache_update_request_base.h" | 17 #include "content/browser/appcache/appcache_update_request_base.h" |
| 18 #include "content/browser/appcache/appcache_update_url_fetcher.h" | 18 #include "content/browser/appcache/appcache_update_url_fetcher.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
| 21 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
| 22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 23 #include "net/base/request_priority.h" | 23 #include "net/base/request_priority.h" |
| 24 #include "url/origin.h" | 24 #include "url/origin.h" |
| 25 | 25 |
| 26 namespace content { | 26 namespace content { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 const int kBufferSize = 32768; | 30 const int kAppCacheFetchBufferSize = 32768; |
| 31 const size_t kMaxConcurrentUrlFetches = 2; | 31 const size_t kMaxConcurrentUrlFetches = 2; |
| 32 | 32 |
| 33 std::string FormatUrlErrorMessage( | 33 std::string FormatUrlErrorMessage( |
| 34 const char* format, const GURL& url, | 34 const char* format, const GURL& url, |
| 35 AppCacheUpdateJob::ResultType error, | 35 AppCacheUpdateJob::ResultType error, |
| 36 int response_code) { | 36 int response_code) { |
| 37 // Show the net response code if we have one. | 37 // Show the net response code if we have one. |
| 38 int code = response_code; | 38 int code = response_code; |
| 39 if (error != AppCacheUpdateJob::SERVER_ERROR) | 39 if (error != AppCacheUpdateJob::SERVER_ERROR) |
| 40 code = static_cast<int>(error); | 40 code = static_cast<int>(error); |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 | 306 |
| 307 DeleteSoon(); // To unwind the stack prior to deletion. | 307 DeleteSoon(); // To unwind the stack prior to deletion. |
| 308 } | 308 } |
| 309 | 309 |
| 310 void AppCacheUpdateJob::FetchManifest(bool is_first_fetch) { | 310 void AppCacheUpdateJob::FetchManifest(bool is_first_fetch) { |
| 311 DCHECK(!manifest_fetcher_); | 311 DCHECK(!manifest_fetcher_); |
| 312 manifest_fetcher_ = | 312 manifest_fetcher_ = |
| 313 new URLFetcher(manifest_url_, | 313 new URLFetcher(manifest_url_, |
| 314 is_first_fetch ? URLFetcher::MANIFEST_FETCH | 314 is_first_fetch ? URLFetcher::MANIFEST_FETCH |
| 315 : URLFetcher::MANIFEST_REFETCH, | 315 : URLFetcher::MANIFEST_REFETCH, |
| 316 this, kBufferSize); | 316 this, kAppCacheFetchBufferSize); |
| 317 | 317 |
| 318 if (is_first_fetch) { | 318 if (is_first_fetch) { |
| 319 // Maybe load the cached headers to make a condiditional request. | 319 // Maybe load the cached headers to make a condiditional request. |
| 320 AppCacheEntry* entry = (update_type_ == UPGRADE_ATTEMPT) ? | 320 AppCacheEntry* entry = (update_type_ == UPGRADE_ATTEMPT) ? |
| 321 group_->newest_complete_cache()->GetEntry(manifest_url_) : NULL; | 321 group_->newest_complete_cache()->GetEntry(manifest_url_) : NULL; |
| 322 if (entry && !doing_full_update_check_) { | 322 if (entry && !doing_full_update_check_) { |
| 323 // Asynchronously load response info for manifest from newest cache. | 323 // Asynchronously load response info for manifest from newest cache. |
| 324 storage_->LoadResponseInfo(manifest_url_, entry->response_id(), this); | 324 storage_->LoadResponseInfo(manifest_url_, entry->response_id(), this); |
| 325 return; | 325 return; |
| 326 } | 326 } |
| (...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 907 AppCacheHistograms::AddMissingManifestEntrySample(); | 907 AppCacheHistograms::AddMissingManifestEntrySample(); |
| 908 service->DeleteAppCacheGroup(manifest_url_, net::CompletionCallback()); | 908 service->DeleteAppCacheGroup(manifest_url_, net::CompletionCallback()); |
| 909 } | 909 } |
| 910 return; | 910 return; |
| 911 } | 911 } |
| 912 | 912 |
| 913 // Load manifest data from storage to compare against fetched manifest. | 913 // Load manifest data from storage to compare against fetched manifest. |
| 914 manifest_response_reader_.reset( | 914 manifest_response_reader_.reset( |
| 915 storage_->CreateResponseReader(manifest_url_, | 915 storage_->CreateResponseReader(manifest_url_, |
| 916 entry->response_id())); | 916 entry->response_id())); |
| 917 read_manifest_buffer_ = new net::IOBuffer(kBufferSize); | 917 read_manifest_buffer_ = new net::IOBuffer(kAppCacheFetchBufferSize); |
| 918 manifest_response_reader_->ReadData( | 918 manifest_response_reader_->ReadData( |
| 919 read_manifest_buffer_.get(), | 919 read_manifest_buffer_.get(), |
| 920 kBufferSize, | 920 kAppCacheFetchBufferSize, |
| 921 base::Bind(&AppCacheUpdateJob::OnManifestDataReadComplete, | 921 base::Bind(&AppCacheUpdateJob::OnManifestDataReadComplete, |
| 922 base::Unretained(this))); // async read | 922 base::Unretained(this))); // async read |
| 923 } | 923 } |
| 924 | 924 |
| 925 void AppCacheUpdateJob::OnManifestDataReadComplete(int result) { | 925 void AppCacheUpdateJob::OnManifestDataReadComplete(int result) { |
| 926 if (result > 0) { | 926 if (result > 0) { |
| 927 loaded_manifest_data_.append(read_manifest_buffer_->data(), result); | 927 loaded_manifest_data_.append(read_manifest_buffer_->data(), result); |
| 928 manifest_response_reader_->ReadData( | 928 manifest_response_reader_->ReadData( |
| 929 read_manifest_buffer_.get(), | 929 read_manifest_buffer_.get(), |
| 930 kBufferSize, | 930 kAppCacheFetchBufferSize, |
| 931 base::Bind(&AppCacheUpdateJob::OnManifestDataReadComplete, | 931 base::Bind(&AppCacheUpdateJob::OnManifestDataReadComplete, |
| 932 base::Unretained(this))); // read more | 932 base::Unretained(this))); // read more |
| 933 } else { | 933 } else { |
| 934 read_manifest_buffer_ = NULL; | 934 read_manifest_buffer_ = NULL; |
| 935 manifest_response_reader_.reset(); | 935 manifest_response_reader_.reset(); |
| 936 ContinueHandleManifestFetchCompleted( | 936 ContinueHandleManifestFetchCompleted( |
| 937 result < 0 || manifest_data_ != loaded_manifest_data_); | 937 result < 0 || manifest_data_ != loaded_manifest_data_); |
| 938 } | 938 } |
| 939 } | 939 } |
| 940 | 940 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1003 NotifyAllProgress(url_to_fetch.url); | 1003 NotifyAllProgress(url_to_fetch.url); |
| 1004 ++url_fetches_completed_; | 1004 ++url_fetches_completed_; |
| 1005 } else if (AlreadyFetchedEntry(url_to_fetch.url, entry.types())) { | 1005 } else if (AlreadyFetchedEntry(url_to_fetch.url, entry.types())) { |
| 1006 NotifyAllProgress(url_to_fetch.url); | 1006 NotifyAllProgress(url_to_fetch.url); |
| 1007 ++url_fetches_completed_; // saved a URL request | 1007 ++url_fetches_completed_; // saved a URL request |
| 1008 } else if (!url_to_fetch.storage_checked && | 1008 } else if (!url_to_fetch.storage_checked && |
| 1009 MaybeLoadFromNewestCache(url_to_fetch.url, entry)) { | 1009 MaybeLoadFromNewestCache(url_to_fetch.url, entry)) { |
| 1010 // Continues asynchronously after data is loaded from newest cache. | 1010 // Continues asynchronously after data is loaded from newest cache. |
| 1011 } else { | 1011 } else { |
| 1012 URLFetcher* fetcher = new URLFetcher( | 1012 URLFetcher* fetcher = new URLFetcher( |
| 1013 url_to_fetch.url, URLFetcher::URL_FETCH, this, kBufferSize); | 1013 url_to_fetch.url, URLFetcher::URL_FETCH, this, kAppCacheFetchBufferSiz
e); |
| 1014 if (url_to_fetch.existing_response_info.get() && | 1014 if (url_to_fetch.existing_response_info.get() && |
| 1015 group_->newest_complete_cache()) { | 1015 group_->newest_complete_cache()) { |
| 1016 AppCacheEntry* existing_entry = | 1016 AppCacheEntry* existing_entry = |
| 1017 group_->newest_complete_cache()->GetEntry(url_to_fetch.url); | 1017 group_->newest_complete_cache()->GetEntry(url_to_fetch.url); |
| 1018 DCHECK(existing_entry); | 1018 DCHECK(existing_entry); |
| 1019 DCHECK(existing_entry->response_id() == | 1019 DCHECK(existing_entry->response_id() == |
| 1020 url_to_fetch.existing_response_info->response_id()); | 1020 url_to_fetch.existing_response_info->response_id()); |
| 1021 fetcher->set_existing_response_headers( | 1021 fetcher->set_existing_response_headers( |
| 1022 url_to_fetch.existing_response_info->http_response_info()->headers | 1022 url_to_fetch.existing_response_info->http_response_info()->headers |
| 1023 .get()); | 1023 .get()); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1126 PendingMasters::iterator found = pending_master_entries_.find(url); | 1126 PendingMasters::iterator found = pending_master_entries_.find(url); |
| 1127 DCHECK(found != pending_master_entries_.end()); | 1127 DCHECK(found != pending_master_entries_.end()); |
| 1128 PendingHosts& hosts = found->second; | 1128 PendingHosts& hosts = found->second; |
| 1129 for (PendingHosts::iterator host_it = hosts.begin(); | 1129 for (PendingHosts::iterator host_it = hosts.begin(); |
| 1130 host_it != hosts.end(); ++host_it) { | 1130 host_it != hosts.end(); ++host_it) { |
| 1131 (*host_it)->AssociateCompleteCache(cache); | 1131 (*host_it)->AssociateCompleteCache(cache); |
| 1132 } | 1132 } |
| 1133 } | 1133 } |
| 1134 } else { | 1134 } else { |
| 1135 URLFetcher* fetcher = new URLFetcher(url, URLFetcher::MASTER_ENTRY_FETCH, | 1135 URLFetcher* fetcher = new URLFetcher(url, URLFetcher::MASTER_ENTRY_FETCH, |
| 1136 this, kBufferSize); | 1136 this, kAppCacheFetchBufferSize); |
| 1137 fetcher->Start(); | 1137 fetcher->Start(); |
| 1138 master_entry_fetches_.insert(PendingUrlFetches::value_type(url, fetcher)); | 1138 master_entry_fetches_.insert(PendingUrlFetches::value_type(url, fetcher)); |
| 1139 } | 1139 } |
| 1140 | 1140 |
| 1141 master_entries_to_fetch_.erase(master_entries_to_fetch_.begin()); | 1141 master_entries_to_fetch_.erase(master_entries_to_fetch_.begin()); |
| 1142 } | 1142 } |
| 1143 } | 1143 } |
| 1144 | 1144 |
| 1145 void AppCacheUpdateJob::CancelAllMasterEntryFetches( | 1145 void AppCacheUpdateJob::CancelAllMasterEntryFetches( |
| 1146 const AppCacheErrorDetails& error_details) { | 1146 const AppCacheErrorDetails& error_details) { |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1463 // on this object after we've posted a task to delete ourselves. | 1463 // on this object after we've posted a task to delete ourselves. |
| 1464 if (group_) { | 1464 if (group_) { |
| 1465 group_->SetUpdateAppCacheStatus(AppCacheGroup::IDLE); | 1465 group_->SetUpdateAppCacheStatus(AppCacheGroup::IDLE); |
| 1466 group_ = NULL; | 1466 group_ = NULL; |
| 1467 } | 1467 } |
| 1468 | 1468 |
| 1469 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | 1469 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
| 1470 } | 1470 } |
| 1471 | 1471 |
| 1472 } // namespace content | 1472 } // namespace content |
| OLD | NEW |