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 "chrome/browser/prerender/prerender_tracker.h" | 5 #include "chrome/browser/prerender/prerender_tracker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/prerender/prerender_contents.h" |
10 #include "chrome/browser/prerender/prerender_manager.h" | 11 #include "chrome/browser/prerender/prerender_manager.h" |
11 #include "chrome/browser/prerender/prerender_pending_swap_throttle.h" | 12 #include "chrome/browser/prerender/prerender_pending_swap_throttle.h" |
12 #include "chrome/browser/prerender/prerender_resource_throttle.h" | 13 #include "chrome/browser/prerender/prerender_resource_throttle.h" |
13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/resource_context.h" | 15 #include "content/public/browser/resource_context.h" |
15 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
16 | 17 |
17 using content::BrowserThread; | 18 using content::BrowserThread; |
18 | 19 |
19 namespace prerender { | 20 namespace prerender { |
20 | 21 |
21 struct RenderViewInfo { | 22 struct RenderViewInfo { |
22 explicit RenderViewInfo(PrerenderManager* prerender_manager) | 23 explicit RenderViewInfo(PrerenderManager* prerender_manager) |
23 : final_status(FINAL_STATUS_MAX), | 24 : final_status(FINAL_STATUS_MAX), |
24 prerender_manager(prerender_manager->AsWeakPtr()) { | 25 prerender_manager(prerender_manager->AsWeakPtr()) { |
25 } | 26 } |
26 ~RenderViewInfo() {} | 27 ~RenderViewInfo() {} |
27 | 28 |
28 FinalStatus final_status; | 29 FinalStatus final_status; |
29 base::WeakPtr<PrerenderManager> prerender_manager; | 30 base::WeakPtr<PrerenderManager> prerender_manager; |
30 }; | 31 }; |
31 | 32 |
32 PrerenderTracker::PrerenderTracker() { | 33 PrerenderTracker::PrerenderTracker() { |
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
34 } | 35 } |
35 | 36 |
36 PrerenderTracker::~PrerenderTracker() { | 37 PrerenderTracker::~PrerenderTracker() { |
37 } | 38 } |
38 | 39 |
39 void PrerenderTracker::OnPrerenderStart( | |
40 PrerenderContents* prerender_contents) { | |
41 DCHECK(CalledOnValidThread()); | |
42 int child_id, route_id; | |
43 bool got_child_id = prerender_contents->GetChildId(&child_id); | |
44 DCHECK(got_child_id); | |
45 bool got_route_id = prerender_contents->GetRouteId(&route_id); | |
46 DCHECK(got_route_id); | |
47 | |
48 ChildRouteIdPair child_route_id_pair(child_id, route_id); | |
49 | |
50 BrowserThread::PostTask( | |
51 BrowserThread::IO, FROM_HERE, | |
52 base::Bind(&PrerenderTracker::AddPrerenderOnIOThread, | |
53 base::Unretained(this), child_route_id_pair)); | |
54 } | |
55 | |
56 void PrerenderTracker::OnPrerenderStop( | |
57 PrerenderContents* prerender_contents) { | |
58 DCHECK(CalledOnValidThread()); | |
59 int child_id, route_id; | |
60 bool got_child_id = prerender_contents->GetChildId(&child_id); | |
61 DCHECK(got_child_id); | |
62 bool got_route_id = prerender_contents->GetRouteId(&route_id); | |
63 DCHECK(got_route_id); | |
64 | |
65 ChildRouteIdPair child_route_id_pair(child_id, route_id); | |
66 | |
67 DCHECK_LT(prerender_contents->final_status(), FINAL_STATUS_MAX); | |
68 BrowserThread::PostTask( | |
69 BrowserThread::IO, FROM_HERE, | |
70 base::Bind(&PrerenderTracker::RemovePrerenderOnIOThread, | |
71 base::Unretained(this), child_route_id_pair, | |
72 prerender_contents->final_status())); | |
73 } | |
74 | |
75 bool PrerenderTracker::IsPrerenderingOnIOThread(int child_id, | |
76 int route_id) const { | |
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
78 | |
79 ChildRouteIdPair child_route_id_pair(child_id, route_id); | |
80 return resource_throttle_io_thread_map_.count(child_route_id_pair) > 0; | |
81 } | |
82 | |
83 bool PrerenderTracker::IsPendingSwapRequestOnIOThread( | 40 bool PrerenderTracker::IsPendingSwapRequestOnIOThread( |
84 int render_process_id, int render_frame_id, const GURL& url) const { | 41 int render_process_id, int render_frame_id, const GURL& url) const { |
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
86 | 43 |
87 ChildRouteIdPair render_frame_route_id_pair( | 44 ChildRouteIdPair render_frame_route_id_pair( |
88 render_process_id, render_frame_id); | 45 render_process_id, render_frame_id); |
89 PendingSwapThrottleMap::const_iterator it = | 46 PendingSwapThrottleMap::const_iterator it = |
90 pending_swap_throttle_map_.find(render_frame_route_id_pair); | 47 pending_swap_throttle_map_.find(render_frame_route_id_pair); |
91 return (it != pending_swap_throttle_map_.end() && it->second.url == url); | 48 return (it != pending_swap_throttle_map_.end() && it->second.url == url); |
92 } | 49 } |
93 | 50 |
94 void PrerenderTracker::AddResourceThrottleOnIOThread( | |
95 int child_id, | |
96 int route_id, | |
97 const base::WeakPtr<PrerenderResourceThrottle>& throttle) { | |
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
99 | |
100 ChildRouteIdPair child_route_id_pair(child_id, route_id); | |
101 ResourceThrottleMap::iterator resource_throttle_map_it = | |
102 resource_throttle_io_thread_map_.find(child_route_id_pair); | |
103 DCHECK(resource_throttle_map_it != resource_throttle_io_thread_map_.end()); | |
104 resource_throttle_map_it->second.push_back(throttle); | |
105 } | |
106 | |
107 void PrerenderTracker::AddPendingSwapThrottleOnIOThread( | 51 void PrerenderTracker::AddPendingSwapThrottleOnIOThread( |
108 int render_process_id, | 52 int render_process_id, |
109 int render_frame_id, | 53 int render_frame_id, |
110 const GURL& url, | 54 const GURL& url, |
111 const base::WeakPtr<PrerenderPendingSwapThrottle>& throttle) { | 55 const base::WeakPtr<PrerenderPendingSwapThrottle>& throttle) { |
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
113 | 57 |
114 ChildRouteIdPair render_frame_route_id_pair( | 58 ChildRouteIdPair render_frame_route_id_pair( |
115 render_process_id, render_frame_id); | 59 render_process_id, render_frame_id); |
116 PendingSwapThrottleMap::iterator it = | 60 PendingSwapThrottleMap::iterator it = |
117 pending_swap_throttle_map_.find(render_frame_route_id_pair); | 61 pending_swap_throttle_map_.find(render_frame_route_id_pair); |
118 DCHECK(it != pending_swap_throttle_map_.end()); | 62 DCHECK(it != pending_swap_throttle_map_.end()); |
119 if (it == pending_swap_throttle_map_.end()) | 63 if (it == pending_swap_throttle_map_.end()) |
120 return; | 64 return; |
121 CHECK(!it->second.throttle); | 65 CHECK(!it->second.throttle); |
122 it->second.throttle = throttle; | 66 it->second.throttle = throttle; |
123 } | 67 } |
124 | 68 |
125 void PrerenderTracker::AddPrerenderOnIOThread( | |
126 const ChildRouteIdPair& child_route_id_pair) { | |
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
128 DCHECK(!IsPrerenderingOnIOThread(child_route_id_pair.first, | |
129 child_route_id_pair.second)); | |
130 | |
131 resource_throttle_io_thread_map_.insert( | |
132 std::make_pair(child_route_id_pair, ResourceThrottleList())); | |
133 } | |
134 | |
135 void PrerenderTracker::RemovePrerenderOnIOThread( | |
136 const ChildRouteIdPair& child_route_id_pair, | |
137 FinalStatus final_status) { | |
138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
139 DCHECK(IsPrerenderingOnIOThread(child_route_id_pair.first, | |
140 child_route_id_pair.second)); | |
141 | |
142 // Cancel or resume all throttled resources. | |
143 ResourceThrottleMap::iterator resource_throttle_map_it = | |
144 resource_throttle_io_thread_map_.find(child_route_id_pair); | |
145 DCHECK(resource_throttle_map_it != resource_throttle_io_thread_map_.end()); | |
146 ResourceThrottleList& throttles = resource_throttle_map_it->second; | |
147 for (size_t i = 0; i < throttles.size(); i++) { | |
148 if (throttles[i]) { | |
149 if (final_status == FINAL_STATUS_USED) { | |
150 throttles[i]->Resume(); | |
151 } else { | |
152 throttles[i]->Cancel(); | |
153 } | |
154 } | |
155 } | |
156 resource_throttle_io_thread_map_.erase(resource_throttle_map_it); | |
157 } | |
158 | |
159 void PrerenderTracker::AddPrerenderPendingSwapOnIOThread( | 69 void PrerenderTracker::AddPrerenderPendingSwapOnIOThread( |
160 const ChildRouteIdPair& render_frame_route_id_pair, | 70 const ChildRouteIdPair& render_frame_route_id_pair, |
161 const GURL& url) { | 71 const GURL& url) { |
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
163 std::pair<PendingSwapThrottleMap::iterator, bool> insert_result = | 73 std::pair<PendingSwapThrottleMap::iterator, bool> insert_result = |
164 pending_swap_throttle_map_.insert(std::make_pair( | 74 pending_swap_throttle_map_.insert(std::make_pair( |
165 render_frame_route_id_pair, PendingSwapThrottleData(url))); | 75 render_frame_route_id_pair, PendingSwapThrottleData(url))); |
166 DCHECK(insert_result.second); | 76 DCHECK(insert_result.second); |
167 } | 77 } |
168 | 78 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 | 119 |
210 PrerenderTracker::PendingSwapThrottleData::~PendingSwapThrottleData() { | 120 PrerenderTracker::PendingSwapThrottleData::~PendingSwapThrottleData() { |
211 } | 121 } |
212 | 122 |
213 // static | 123 // static |
214 PrerenderTracker* PrerenderTracker::GetDefault() { | 124 PrerenderTracker* PrerenderTracker::GetDefault() { |
215 return g_browser_process->prerender_tracker(); | 125 return g_browser_process->prerender_tracker(); |
216 } | 126 } |
217 | 127 |
218 } // namespace prerender | 128 } // namespace prerender |
OLD | NEW |