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 std::unique_ptr<ResourcePrefetcher::RequestVector> requests) { | 56 std::unique_ptr<ResourcePrefetcher::RequestVector> requests) { |
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 = new ResourcePrefetcher( |
66 this, config_, navigation_id, key_type, std::move(requests)); | 66 this, config_, navigation_id, key_type, std::move(requests)); |
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) { |
(...skipping 13 matching lines...) Expand all Loading... |
101 this, | 100 this, |
102 resource_prefetcher->navigation_id(), | 101 resource_prefetcher->navigation_id(), |
103 resource_prefetcher->key_type(), | 102 resource_prefetcher->key_type(), |
104 base::Passed(&scoped_requests))); | 103 base::Passed(&scoped_requests))); |
105 | 104 |
106 const GURL& main_frame_url = | 105 const GURL& main_frame_url = |
107 resource_prefetcher->navigation_id().main_frame_url; | 106 resource_prefetcher->navigation_id().main_frame_url; |
108 const std::string key = | 107 const std::string key = |
109 resource_prefetcher->key_type() == PREFETCH_KEY_TYPE_HOST ? | 108 resource_prefetcher->key_type() == PREFETCH_KEY_TYPE_HOST ? |
110 main_frame_url.host() : main_frame_url.spec(); | 109 main_frame_url.host() : main_frame_url.spec(); |
111 PrefetcherMap::iterator it = prefetcher_map_.find(key); | 110 auto it = prefetcher_map_.find(key); |
112 DCHECK(it != prefetcher_map_.end()); | 111 DCHECK(it != prefetcher_map_.end()); |
113 delete it->second; | |
114 prefetcher_map_.erase(it); | 112 prefetcher_map_.erase(it); |
115 } | 113 } |
116 | 114 |
117 void ResourcePrefetcherManager::ResourcePrefetcherFinishedOnUI( | 115 void ResourcePrefetcherManager::ResourcePrefetcherFinishedOnUI( |
118 const NavigationID& navigation_id, | 116 const NavigationID& navigation_id, |
119 PrefetchKeyType key_type, | 117 PrefetchKeyType key_type, |
120 std::unique_ptr<ResourcePrefetcher::RequestVector> requests) { | 118 std::unique_ptr<ResourcePrefetcher::RequestVector> requests) { |
121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 119 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
122 | 120 |
123 // |predictor_| may have been set to NULL if the predictor is shutting down. | 121 // |predictor_| may have been set to NULL if the predictor is shutting down. |
124 if (predictor_) { | 122 if (predictor_) { |
125 predictor_->FinishedPrefetchForNavigation(navigation_id, | 123 predictor_->FinishedPrefetchForNavigation(navigation_id, |
126 key_type, | 124 key_type, |
127 requests.release()); | 125 requests.release()); |
128 } | 126 } |
129 } | 127 } |
130 | 128 |
131 net::URLRequestContext* ResourcePrefetcherManager::GetURLRequestContext() { | 129 net::URLRequestContext* ResourcePrefetcherManager::GetURLRequestContext() { |
132 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 130 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
133 | 131 |
134 return context_getter_->GetURLRequestContext(); | 132 return context_getter_->GetURLRequestContext(); |
135 } | 133 } |
136 | 134 |
137 } // namespace predictors | 135 } // namespace predictors |
OLD | NEW |