| 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/memory/ptr_util.h" |
| 10 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 11 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 12 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 12 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 13 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
| 14 #include "net/url_request/url_request_context_getter.h" | 15 #include "net/url_request/url_request_context_getter.h" |
| 15 | 16 |
| 16 using content::BrowserThread; | 17 using content::BrowserThread; |
| 17 | 18 |
| 18 namespace predictors { | 19 namespace predictors { |
| 19 | 20 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 39 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 40 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 40 | 41 |
| 41 predictor_ = NULL; | 42 predictor_ = NULL; |
| 42 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 43 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 43 base::Bind(&ResourcePrefetcherManager::ShutdownOnIOThread, | 44 base::Bind(&ResourcePrefetcherManager::ShutdownOnIOThread, |
| 44 this)); | 45 this)); |
| 45 } | 46 } |
| 46 | 47 |
| 47 void ResourcePrefetcherManager::ShutdownOnIOThread() { | 48 void ResourcePrefetcherManager::ShutdownOnIOThread() { |
| 48 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 49 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 49 base::STLDeleteContainerPairSecondPointers(prefetcher_map_.begin(), | 50 prefetcher_map_.clear(); |
| 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 const std::vector<GURL>& urls) { | 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 = | 65 ResourcePrefetcher* prefetcher = |
| 66 new ResourcePrefetcher(this, config_, navigation_id, key_type, urls); | 66 new ResourcePrefetcher(this, config_, navigation_id, key_type, urls); |
| 67 prefetcher_map_.insert(std::make_pair(key, prefetcher)); | 67 prefetcher_map_[key] = base::WrapUnique(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 auto it = prefetcher_map_.find(navigation_id.main_frame_url.spec()); |
| 77 navigation_id.main_frame_url.spec()); | |
| 78 if (it != prefetcher_map_.end() && | 77 if (it != prefetcher_map_.end() && |
| 79 it->second->navigation_id() == navigation_id) { | 78 it->second->navigation_id() == navigation_id) { |
| 80 it->second->Stop(); | 79 it->second->Stop(); |
| 81 return; | 80 return; |
| 82 } | 81 } |
| 83 | 82 |
| 84 // No URL based prefetching, look for host based. | 83 // No URL based prefetching, look for host based. |
| 85 it = prefetcher_map_.find(navigation_id.main_frame_url.host()); | 84 it = prefetcher_map_.find(navigation_id.main_frame_url.host()); |
| 86 if (it != prefetcher_map_.end() && | 85 if (it != prefetcher_map_.end() && |
| 87 it->second->navigation_id() == navigation_id) { | 86 it->second->navigation_id() == navigation_id) { |
| 88 it->second->Stop(); | 87 it->second->Stop(); |
| 89 } | 88 } |
| 90 } | 89 } |
| 91 | 90 |
| 92 void ResourcePrefetcherManager::ResourcePrefetcherFinished( | 91 void ResourcePrefetcherManager::ResourcePrefetcherFinished( |
| 93 ResourcePrefetcher* resource_prefetcher) { | 92 ResourcePrefetcher* resource_prefetcher) { |
| 94 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 93 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 95 | 94 |
| 96 const GURL& main_frame_url = | 95 const GURL& main_frame_url = |
| 97 resource_prefetcher->navigation_id().main_frame_url; | 96 resource_prefetcher->navigation_id().main_frame_url; |
| 98 const std::string key = | 97 const std::string key = |
| 99 resource_prefetcher->key_type() == PREFETCH_KEY_TYPE_HOST ? | 98 resource_prefetcher->key_type() == PREFETCH_KEY_TYPE_HOST ? |
| 100 main_frame_url.host() : main_frame_url.spec(); | 99 main_frame_url.host() : main_frame_url.spec(); |
| 101 PrefetcherMap::iterator it = prefetcher_map_.find(key); | 100 auto it = prefetcher_map_.find(key); |
| 102 DCHECK(it != prefetcher_map_.end()); | 101 DCHECK(it != prefetcher_map_.end()); |
| 103 delete it->second; | |
| 104 prefetcher_map_.erase(it); | 102 prefetcher_map_.erase(it); |
| 105 } | 103 } |
| 106 | 104 |
| 107 net::URLRequestContext* ResourcePrefetcherManager::GetURLRequestContext() { | 105 net::URLRequestContext* ResourcePrefetcherManager::GetURLRequestContext() { |
| 108 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 106 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 109 | 107 |
| 110 return context_getter_->GetURLRequestContext(); | 108 return context_getter_->GetURLRequestContext(); |
| 111 } | 109 } |
| 112 | 110 |
| 113 } // namespace predictors | 111 } // namespace predictors |
| OLD | NEW |