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_link_manager.h" | 5 #include "chrome/browser/prerender/prerender_link_manager.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <queue> | 8 #include <queue> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "chrome/browser/prerender/prerender_contents.h" | 11 #include "chrome/browser/prerender/prerender_contents.h" |
12 #include "chrome/browser/prerender/prerender_handle.h" | |
dominich
2012/06/28 00:34:32
unnecessary include - included in header.
| |
12 #include "chrome/browser/prerender/prerender_manager.h" | 13 #include "chrome/browser/prerender/prerender_manager.h" |
13 #include "chrome/browser/prerender/prerender_manager_factory.h" | 14 #include "chrome/browser/prerender/prerender_manager_factory.h" |
14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
15 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
16 #include "content/public/browser/session_storage_namespace.h" | 17 #include "content/public/browser/session_storage_namespace.h" |
17 #include "content/public/common/referrer.h" | 18 #include "content/public/common/referrer.h" |
18 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
19 #include "googleurl/src/url_canon.h" | 20 #include "googleurl/src/url_canon.h" |
20 #include "ui/gfx/size.h" | 21 #include "ui/gfx/size.h" |
21 | 22 |
(...skipping 21 matching lines...) Expand all Loading... | |
43 DVLOG(3) << "... referrer url = " << referrer.url.spec() | 44 DVLOG(3) << "... referrer url = " << referrer.url.spec() |
44 << ", size = (" << size.width() << ", " << size.height() << ")" | 45 << ", size = (" << size.width() << ", " << size.height() << ")" |
45 << ", render_view_route_id = " << render_view_route_id; | 46 << ", render_view_route_id = " << render_view_route_id; |
46 | 47 |
47 // TODO(gavinp): Add tests to ensure fragments work, then remove this fragment | 48 // TODO(gavinp): Add tests to ensure fragments work, then remove this fragment |
48 // clearing code. | 49 // clearing code. |
49 url_canon::Replacements<char> replacements; | 50 url_canon::Replacements<char> replacements; |
50 replacements.ClearRef(); | 51 replacements.ClearRef(); |
51 const GURL url = orig_url.ReplaceComponents(replacements); | 52 const GURL url = orig_url.ReplaceComponents(replacements); |
52 | 53 |
53 if (!manager_->AddPrerenderFromLinkRelPrerender( | 54 if (PrerenderHandle prerender_handle = |
54 child_id, render_view_route_id, url, referrer, size)) { | 55 manager_->AddPrerenderFromLinkRelPrerender( |
55 return false; | 56 child_id, render_view_route_id, url, referrer, size)) { |
57 ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); | |
58 DCHECK_EQ(0U, ids_to_handle_map_.count(child_and_prerender_id)); | |
dominich
2012/06/28 00:34:32
in the case of multiple prerenders, this could hap
mmenke
2012/06/28 16:01:28
Could happen in the case of supporting one prerend
| |
59 ids_to_handle_map_.insert( | |
60 std::make_pair(child_and_prerender_id, prerender_handle)); | |
61 return true; | |
56 } | 62 } |
57 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); | 63 return false; |
58 DCHECK_EQ(0U, ids_to_url_map_.count(child_and_prerender_id)); | |
59 ids_to_url_map_.insert(std::make_pair(child_and_prerender_id, url)); | |
60 return true; | |
61 } | 64 } |
62 | 65 |
63 // TODO(gavinp): Once an observer interface is provided down to the WebKit | 66 // TODO(gavinp): Once an observer interface is provided down to the WebKit |
64 // layer, we should add DCHECK_NE(0L, ids_to_url_map_.count(...)) to both | 67 // layer, we should add DCHECK_NE(0L, ids_to_url_map_.count(...)) to both |
65 // OnCancelPrerender and OnAbandonPrerender. We can't do this now, since | 68 // OnCancelPrerender and OnAbandonPrerender. We can't do this now, since |
66 // the WebKit layer isn't even aware if we didn't add the prerender to the map | 69 // the WebKit layer isn't even aware if we didn't add the prerender to the map |
67 // in OnAddPrerender above. | 70 // in OnAddPrerender above. |
68 void PrerenderLinkManager::OnCancelPrerender(int child_id, int prerender_id) { | 71 void PrerenderLinkManager::OnCancelPrerender(int child_id, int prerender_id) { |
69 DVLOG(2) << "OnCancelPrerender, child_id = " << child_id | 72 DVLOG(2) << "OnCancelPrerender, child_id = " << child_id |
70 << ", prerender_id = " << prerender_id; | 73 << ", prerender_id = " << prerender_id; |
71 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); | 74 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); |
72 IdPairToUrlMap::iterator id_url_iter = | 75 IdPairToPrerenderHandleMap::iterator id_handle_iter = |
73 ids_to_url_map_.find(child_and_prerender_id); | 76 ids_to_handle_map_.find(child_and_prerender_id); |
74 if (id_url_iter == ids_to_url_map_.end()) { | 77 if (id_handle_iter == ids_to_handle_map_.end()) { |
75 DVLOG(5) << "... canceling a prerender that doesn't exist."; | 78 DVLOG(5) << "... canceling a prerender that doesn't exist."; |
76 return; | 79 return; |
77 } | 80 } |
78 const GURL url = id_url_iter->second; | 81 if (PrerenderHandle prerender_handle = id_handle_iter->second) |
79 ids_to_url_map_.erase(id_url_iter); | 82 prerender_handle->Release(); |
80 manager_->MaybeCancelPrerender(url); | 83 else |
84 DVLOG(5) << "... and the handle was no good."; | |
dominich
2012/06/28 00:34:32
does this ever happen? maybe DCHECK instead.
| |
85 | |
86 ids_to_handle_map_.erase(id_handle_iter); | |
81 } | 87 } |
82 | 88 |
83 void PrerenderLinkManager::OnAbandonPrerender(int child_id, int prerender_id) { | 89 void PrerenderLinkManager::OnAbandonPrerender(int child_id, int prerender_id) { |
84 DVLOG(2) << "OnAbandonPrerender, child_id = " << child_id | 90 DVLOG(2) << "OnAbandonPrerender, child_id = " << child_id |
85 << ", prerender_id = " << prerender_id; | 91 << ", prerender_id = " << prerender_id; |
86 // TODO(gavinp,cbentzel): Implement reasonable behaviour for | 92 // TODO(gavinp,cbentzel): Implement reasonable behaviour for |
87 // navigation away from launcher. | 93 // navigation away from launcher. |
88 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); | 94 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); |
89 ids_to_url_map_.erase(child_and_prerender_id); | 95 ids_to_handle_map_.erase(child_and_prerender_id); |
90 } | 96 } |
91 | 97 |
92 void PrerenderLinkManager::OnChannelClosing(int child_id) { | 98 void PrerenderLinkManager::OnChannelClosing(int child_id) { |
93 DVLOG(2) << "OnChannelClosing, child id = " << child_id; | 99 DVLOG(2) << "OnChannelClosing, child id = " << child_id; |
94 const ChildAndPrerenderIdPair child_and_minimum_prerender_id( | 100 const ChildAndPrerenderIdPair child_and_minimum_prerender_id( |
95 child_id, std::numeric_limits<int>::min()); | 101 child_id, std::numeric_limits<int>::min()); |
96 const ChildAndPrerenderIdPair child_and_maximum_prerender_id( | 102 const ChildAndPrerenderIdPair child_and_maximum_prerender_id( |
97 child_id, std::numeric_limits<int>::max()); | 103 child_id, std::numeric_limits<int>::max()); |
98 std::queue<int> prerender_ids_to_abandon; | 104 std::queue<int> prerender_ids_to_abandon; |
99 for (IdPairToUrlMap::iterator | 105 for (IdPairToPrerenderHandleMap::iterator |
100 i = ids_to_url_map_.lower_bound(child_and_minimum_prerender_id), | 106 i = ids_to_handle_map_.lower_bound(child_and_minimum_prerender_id), |
101 e = ids_to_url_map_.upper_bound(child_and_maximum_prerender_id); | 107 e = ids_to_handle_map_.upper_bound(child_and_maximum_prerender_id); |
102 i != e; ++i) { | 108 i != e; ++i) { |
103 prerender_ids_to_abandon.push(i->first.second); | 109 prerender_ids_to_abandon.push(i->first.second); |
104 } | 110 } |
105 while (!prerender_ids_to_abandon.empty()) { | 111 while (!prerender_ids_to_abandon.empty()) { |
106 DVLOG(4) << "---> abandon prerender_id = " | 112 DVLOG(4) << "---> abandon prerender_id = " |
107 << prerender_ids_to_abandon.front(); | 113 << prerender_ids_to_abandon.front(); |
108 OnAbandonPrerender(child_id, prerender_ids_to_abandon.front()); | 114 OnAbandonPrerender(child_id, prerender_ids_to_abandon.front()); |
109 prerender_ids_to_abandon.pop(); | 115 prerender_ids_to_abandon.pop(); |
110 } | 116 } |
111 } | 117 } |
112 | 118 |
113 bool PrerenderLinkManager::IsEmpty() const { | 119 bool PrerenderLinkManager::IsEmpty() const { |
114 return ids_to_url_map_.empty(); | 120 return ids_to_handle_map_.empty(); |
115 } | 121 } |
116 | 122 |
117 } // namespace prerender | 123 } // namespace prerender |
118 | 124 |
OLD | NEW |