| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/predictors/resource_prefetcher_manager.h" | 5 #include "chrome/browser/predictors/resource_prefetcher_manager.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 void ResourcePrefetcherManager::ShutdownOnIOThread() { | 47 void ResourcePrefetcherManager::ShutdownOnIOThread() { |
| 48 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 48 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 49 base::STLDeleteContainerPairSecondPointers(prefetcher_map_.begin(), | 49 base::STLDeleteContainerPairSecondPointers(prefetcher_map_.begin(), |
| 50 prefetcher_map_.end()); | 50 prefetcher_map_.end()); |
| 51 } | 51 } |
| 52 | 52 |
| 53 void ResourcePrefetcherManager::MaybeAddPrefetch( | 53 void ResourcePrefetcherManager::MaybeAddPrefetch( |
| 54 const NavigationID& navigation_id, | 54 const NavigationID& navigation_id, |
| 55 PrefetchKeyType key_type, | 55 PrefetchKeyType key_type, |
| 56 std::unique_ptr<ResourcePrefetcher::RequestVector> requests) { | 56 const std::vector<GURL>& urls) { |
| 57 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 57 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 58 | 58 |
| 59 // Don't add a duplicate prefetch for the same host or URL. | 59 // Don't add a duplicate prefetch for the same host or URL. |
| 60 std::string key = key_type == PREFETCH_KEY_TYPE_HOST ? | 60 std::string key = key_type == PREFETCH_KEY_TYPE_HOST ? |
| 61 navigation_id.main_frame_url.host() : navigation_id.main_frame_url.spec(); | 61 navigation_id.main_frame_url.host() : navigation_id.main_frame_url.spec(); |
| 62 if (base::ContainsKey(prefetcher_map_, key)) | 62 if (base::ContainsKey(prefetcher_map_, key)) |
| 63 return; | 63 return; |
| 64 | 64 |
| 65 ResourcePrefetcher* prefetcher = new ResourcePrefetcher( | 65 ResourcePrefetcher* prefetcher = |
| 66 this, config_, navigation_id, key_type, std::move(requests)); | 66 new ResourcePrefetcher(this, config_, navigation_id, key_type, urls); |
| 67 prefetcher_map_.insert(std::make_pair(key, prefetcher)); | 67 prefetcher_map_.insert(std::make_pair(key, prefetcher)); |
| 68 prefetcher->Start(); | 68 prefetcher->Start(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void ResourcePrefetcherManager::MaybeRemovePrefetch( | 71 void ResourcePrefetcherManager::MaybeRemovePrefetch( |
| 72 const NavigationID& navigation_id) { | 72 const NavigationID& navigation_id) { |
| 73 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 73 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 74 | 74 |
| 75 // Look for a URL based prefetch first. | 75 // Look for a URL based prefetch first. |
| 76 PrefetcherMap::iterator it = prefetcher_map_.find( | 76 PrefetcherMap::iterator it = prefetcher_map_.find( |
| 77 navigation_id.main_frame_url.spec()); | 77 navigation_id.main_frame_url.spec()); |
| 78 if (it != prefetcher_map_.end() && | 78 if (it != prefetcher_map_.end() && |
| 79 it->second->navigation_id() == navigation_id) { | 79 it->second->navigation_id() == navigation_id) { |
| 80 it->second->Stop(); | 80 it->second->Stop(); |
| 81 return; | 81 return; |
| 82 } | 82 } |
| 83 | 83 |
| 84 // No URL based prefetching, look for host based. | 84 // No URL based prefetching, look for host based. |
| 85 it = prefetcher_map_.find(navigation_id.main_frame_url.host()); | 85 it = prefetcher_map_.find(navigation_id.main_frame_url.host()); |
| 86 if (it != prefetcher_map_.end() && | 86 if (it != prefetcher_map_.end() && |
| 87 it->second->navigation_id() == navigation_id) { | 87 it->second->navigation_id() == navigation_id) { |
| 88 it->second->Stop(); | 88 it->second->Stop(); |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 | 91 |
| 92 void ResourcePrefetcherManager::ResourcePrefetcherFinished( | 92 void ResourcePrefetcherManager::ResourcePrefetcherFinished( |
| 93 ResourcePrefetcher* resource_prefetcher, | 93 ResourcePrefetcher* resource_prefetcher) { |
| 94 ResourcePrefetcher::RequestVector* requests) { | |
| 95 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 94 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 96 | 95 |
| 97 // |predictor_| can only be accessed from the UI thread. | |
| 98 std::unique_ptr<ResourcePrefetcher::RequestVector> scoped_requests(requests); | |
| 99 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 100 base::Bind(&ResourcePrefetcherManager::ResourcePrefetcherFinishedOnUI, | |
| 101 this, | |
| 102 resource_prefetcher->navigation_id(), | |
| 103 resource_prefetcher->key_type(), | |
| 104 base::Passed(&scoped_requests))); | |
| 105 | |
| 106 const GURL& main_frame_url = | 96 const GURL& main_frame_url = |
| 107 resource_prefetcher->navigation_id().main_frame_url; | 97 resource_prefetcher->navigation_id().main_frame_url; |
| 108 const std::string key = | 98 const std::string key = |
| 109 resource_prefetcher->key_type() == PREFETCH_KEY_TYPE_HOST ? | 99 resource_prefetcher->key_type() == PREFETCH_KEY_TYPE_HOST ? |
| 110 main_frame_url.host() : main_frame_url.spec(); | 100 main_frame_url.host() : main_frame_url.spec(); |
| 111 PrefetcherMap::iterator it = prefetcher_map_.find(key); | 101 PrefetcherMap::iterator it = prefetcher_map_.find(key); |
| 112 DCHECK(it != prefetcher_map_.end()); | 102 DCHECK(it != prefetcher_map_.end()); |
| 113 delete it->second; | 103 delete it->second; |
| 114 prefetcher_map_.erase(it); | 104 prefetcher_map_.erase(it); |
| 115 } | 105 } |
| 116 | 106 |
| 117 void ResourcePrefetcherManager::ResourcePrefetcherFinishedOnUI( | |
| 118 const NavigationID& navigation_id, | |
| 119 PrefetchKeyType key_type, | |
| 120 std::unique_ptr<ResourcePrefetcher::RequestVector> requests) { | |
| 121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 122 | |
| 123 // |predictor_| may have been set to NULL if the predictor is shutting down. | |
| 124 if (predictor_) { | |
| 125 predictor_->FinishedPrefetchForNavigation(navigation_id, | |
| 126 key_type, | |
| 127 requests.release()); | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 net::URLRequestContext* ResourcePrefetcherManager::GetURLRequestContext() { | 107 net::URLRequestContext* ResourcePrefetcherManager::GetURLRequestContext() { |
| 132 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 108 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 133 | 109 |
| 134 return context_getter_->GetURLRequestContext(); | 110 return context_getter_->GetURLRequestContext(); |
| 135 } | 111 } |
| 136 | 112 |
| 137 } // namespace predictors | 113 } // namespace predictors |
| OLD | NEW |