| 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/memory/ptr_util.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 this)); | 45 this)); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void ResourcePrefetcherManager::ShutdownOnIOThread() { | 48 void ResourcePrefetcherManager::ShutdownOnIOThread() { |
| 49 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 49 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 50 prefetcher_map_.clear(); | 50 prefetcher_map_.clear(); |
| 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, | |
| 56 const std::vector<GURL>& urls) { | 55 const std::vector<GURL>& urls) { |
| 57 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 56 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 58 | 57 |
| 59 // Don't add a duplicate prefetch for the same host or URL. | 58 // Don't add a duplicate prefetch for the same host. |
| 60 std::string key = key_type == PREFETCH_KEY_TYPE_HOST ? | 59 const GURL& main_frame_url = navigation_id.main_frame_url; |
| 61 navigation_id.main_frame_url.host() : navigation_id.main_frame_url.spec(); | 60 std::string key = main_frame_url.host(); |
| 62 if (base::ContainsKey(prefetcher_map_, key)) | 61 if (base::ContainsKey(prefetcher_map_, key)) |
| 63 return; | 62 return; |
| 64 | 63 |
| 65 ResourcePrefetcher* prefetcher = | 64 auto prefetcher = |
| 66 new ResourcePrefetcher(this, config_, navigation_id, key_type, urls); | 65 base::MakeUnique<ResourcePrefetcher>(this, config_, main_frame_url, urls); |
| 67 prefetcher_map_[key] = base::WrapUnique(prefetcher); | |
| 68 prefetcher->Start(); | 66 prefetcher->Start(); |
| 67 prefetcher_map_[key] = std::move(prefetcher); |
| 69 } | 68 } |
| 70 | 69 |
| 71 void ResourcePrefetcherManager::MaybeRemovePrefetch( | 70 void ResourcePrefetcherManager::MaybeRemovePrefetch( |
| 72 const NavigationID& navigation_id) { | 71 const NavigationID& navigation_id) { |
| 73 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 72 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 74 | 73 |
| 75 // Look for a URL based prefetch first. | 74 const GURL& main_frame_url = navigation_id.main_frame_url; |
| 76 auto it = prefetcher_map_.find(navigation_id.main_frame_url.spec()); | 75 std::string key = main_frame_url.host(); |
| 76 |
| 77 auto it = prefetcher_map_.find(key); |
| 77 if (it != prefetcher_map_.end() && | 78 if (it != prefetcher_map_.end() && |
| 78 it->second->navigation_id() == navigation_id) { | 79 it->second->main_frame_url() == navigation_id.main_frame_url) { |
| 79 it->second->Stop(); | 80 it->second->Stop(); |
| 80 return; | 81 return; |
| 81 } | 82 } |
| 82 | |
| 83 // No URL based prefetching, look for host based. | |
| 84 it = prefetcher_map_.find(navigation_id.main_frame_url.host()); | |
| 85 if (it != prefetcher_map_.end() && | |
| 86 it->second->navigation_id() == navigation_id) { | |
| 87 it->second->Stop(); | |
| 88 } | |
| 89 } | 83 } |
| 90 | 84 |
| 91 void ResourcePrefetcherManager::ResourcePrefetcherFinished( | 85 void ResourcePrefetcherManager::ResourcePrefetcherFinished( |
| 92 ResourcePrefetcher* resource_prefetcher) { | 86 ResourcePrefetcher* resource_prefetcher) { |
| 93 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 87 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 94 | 88 |
| 95 const GURL& main_frame_url = | 89 const std::string key = resource_prefetcher->main_frame_url().host(); |
| 96 resource_prefetcher->navigation_id().main_frame_url; | |
| 97 const std::string key = | |
| 98 resource_prefetcher->key_type() == PREFETCH_KEY_TYPE_HOST ? | |
| 99 main_frame_url.host() : main_frame_url.spec(); | |
| 100 auto it = prefetcher_map_.find(key); | 90 auto it = prefetcher_map_.find(key); |
| 101 DCHECK(it != prefetcher_map_.end()); | 91 DCHECK(it != prefetcher_map_.end()); |
| 102 prefetcher_map_.erase(it); | 92 prefetcher_map_.erase(it); |
| 103 } | 93 } |
| 104 | 94 |
| 105 net::URLRequestContext* ResourcePrefetcherManager::GetURLRequestContext() { | 95 net::URLRequestContext* ResourcePrefetcherManager::GetURLRequestContext() { |
| 106 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 96 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 107 | 97 |
| 108 return context_getter_->GetURLRequestContext(); | 98 return context_getter_->GetURLRequestContext(); |
| 109 } | 99 } |
| 110 | 100 |
| 111 } // namespace predictors | 101 } // namespace predictors |
| OLD | NEW |