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 "base/basictypes.h" | |
gavinp
2012/07/14 22:12:30
Whoops. I'll fix this shortly.
| |
11 #include "chrome/browser/prerender/prerender_contents.h" | 12 #include "chrome/browser/prerender/prerender_contents.h" |
13 #include "chrome/browser/prerender/prerender_handle.h" | |
12 #include "chrome/browser/prerender/prerender_manager.h" | 14 #include "chrome/browser/prerender/prerender_manager.h" |
13 #include "chrome/browser/prerender/prerender_manager_factory.h" | 15 #include "chrome/browser/prerender/prerender_manager_factory.h" |
14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
15 #include "content/public/browser/render_view_host.h" | 17 #include "content/public/browser/render_view_host.h" |
16 #include "content/public/browser/session_storage_namespace.h" | 18 #include "content/public/browser/session_storage_namespace.h" |
17 #include "content/public/common/referrer.h" | 19 #include "content/public/common/referrer.h" |
18 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
19 #include "googleurl/src/url_canon.h" | 21 #include "googleurl/src/url_canon.h" |
20 #include "ui/gfx/size.h" | 22 #include "ui/gfx/size.h" |
21 | 23 |
22 using content::RenderViewHost; | 24 using content::RenderViewHost; |
23 using content::SessionStorageNamespace; | 25 using content::SessionStorageNamespace; |
24 | 26 |
25 namespace prerender { | 27 namespace prerender { |
26 | 28 |
27 PrerenderLinkManager::PrerenderLinkManager(PrerenderManager* manager) | 29 PrerenderLinkManager::PrerenderLinkManager(PrerenderManager* manager) |
28 : manager_(manager) { | 30 : manager_(manager) { |
29 } | 31 } |
30 | 32 |
31 PrerenderLinkManager::~PrerenderLinkManager() { | 33 PrerenderLinkManager::~PrerenderLinkManager() { |
34 for (IdPairToPrerenderHandleMap::iterator it = ids_to_handle_map_.begin(); | |
35 it != ids_to_handle_map_.end(); | |
36 ++it) { | |
37 PrerenderHandle* prerender_handle = it->second; | |
38 prerender_handle->OnCancel(); | |
39 delete prerender_handle; | |
40 } | |
32 } | 41 } |
33 | 42 |
34 bool PrerenderLinkManager::OnAddPrerender(int child_id, | 43 bool PrerenderLinkManager::OnAddPrerender(int child_id, |
35 int prerender_id, | 44 int prerender_id, |
36 const GURL& orig_url, | 45 const GURL& orig_url, |
37 const content::Referrer& referrer, | 46 const content::Referrer& referrer, |
38 const gfx::Size& size, | 47 const gfx::Size& size, |
39 int render_view_route_id) { | 48 int render_view_route_id) { |
40 DVLOG(2) << "OnAddPrerender, child_id = " << child_id | 49 DVLOG(2) << "OnAddPrerender, child_id = " << child_id |
41 << ", prerender_id = " << prerender_id | 50 << ", prerender_id = " << prerender_id |
42 << ", url = " << orig_url.spec(); | 51 << ", url = " << orig_url.spec(); |
43 DVLOG(3) << "... referrer url = " << referrer.url.spec() | 52 DVLOG(3) << "... referrer url = " << referrer.url.spec() |
44 << ", size = (" << size.width() << ", " << size.height() << ")" | 53 << ", size = (" << size.width() << ", " << size.height() << ")" |
45 << ", render_view_route_id = " << render_view_route_id; | 54 << ", render_view_route_id = " << render_view_route_id; |
46 | 55 |
56 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); | |
57 DCHECK_EQ(0U, ids_to_handle_map_.count(child_and_prerender_id)); | |
58 | |
47 // TODO(gavinp): Add tests to ensure fragments work, then remove this fragment | 59 // TODO(gavinp): Add tests to ensure fragments work, then remove this fragment |
48 // clearing code. | 60 // clearing code. |
49 url_canon::Replacements<char> replacements; | 61 url_canon::Replacements<char> replacements; |
50 replacements.ClearRef(); | 62 replacements.ClearRef(); |
51 const GURL url = orig_url.ReplaceComponents(replacements); | 63 const GURL url = orig_url.ReplaceComponents(replacements); |
52 | 64 |
53 if (!manager_->AddPrerenderFromLinkRelPrerender( | 65 scoped_ptr<PrerenderHandle> prerender_handle( |
54 child_id, render_view_route_id, url, referrer, size)) { | 66 manager_->AddPrerenderFromLinkRelPrerender( |
55 return false; | 67 child_id, render_view_route_id, url, referrer, size)); |
68 if (prerender_handle.get()) { | |
69 PrerenderHandle* null = NULL; | |
70 std::pair<IdPairToPrerenderHandleMap::iterator, bool> insert_result = | |
71 ids_to_handle_map_.insert(IdPairToPrerenderHandleMap::value_type( | |
72 child_and_prerender_id, null)); | |
gavinp
2012/07/14 21:15:33
The windows try bots were OK with this, but someth
Peter Kasting
2012/07/14 21:53:34
Did you try std::make_pair?
gavinp
2012/07/14 22:12:30
Yes, with that it won't compile in clang or gcc. I
Peter Kasting
2012/07/15 00:50:15
That seems slightly better to me. We have to stat
| |
73 DCHECK(insert_result.second); | |
74 delete insert_result.first->second; | |
75 insert_result.first->second = prerender_handle.release(); | |
76 return true; | |
56 } | 77 } |
57 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); | 78 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 } | 79 } |
62 | 80 |
63 // TODO(gavinp): Once an observer interface is provided down to the WebKit | 81 // 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 | 82 // 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 | 83 // 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 | 84 // the WebKit layer isn't even aware if we didn't add the prerender to the map |
67 // in OnAddPrerender above. | 85 // in OnAddPrerender above. |
68 void PrerenderLinkManager::OnCancelPrerender(int child_id, int prerender_id) { | 86 void PrerenderLinkManager::OnCancelPrerender(int child_id, int prerender_id) { |
69 DVLOG(2) << "OnCancelPrerender, child_id = " << child_id | 87 DVLOG(2) << "OnCancelPrerender, child_id = " << child_id |
70 << ", prerender_id = " << prerender_id; | 88 << ", prerender_id = " << prerender_id; |
71 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); | 89 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); |
72 IdPairToUrlMap::iterator id_url_iter = | 90 IdPairToPrerenderHandleMap::iterator id_to_handle_iter = |
73 ids_to_url_map_.find(child_and_prerender_id); | 91 ids_to_handle_map_.find(child_and_prerender_id); |
74 if (id_url_iter == ids_to_url_map_.end()) { | 92 if (id_to_handle_iter == ids_to_handle_map_.end()) { |
75 DVLOG(5) << "... canceling a prerender that doesn't exist."; | 93 DVLOG(5) << "... canceling a prerender that doesn't exist."; |
76 return; | 94 return; |
77 } | 95 } |
78 const GURL url = id_url_iter->second; | 96 PrerenderHandle* prerender_handle = id_to_handle_iter->second; |
79 ids_to_url_map_.erase(id_url_iter); | 97 prerender_handle->OnCancel(); |
80 manager_->MaybeCancelPrerender(url); | 98 RemovePrerender(id_to_handle_iter); |
81 } | 99 } |
82 | 100 |
83 void PrerenderLinkManager::OnAbandonPrerender(int child_id, int prerender_id) { | 101 void PrerenderLinkManager::OnAbandonPrerender(int child_id, int prerender_id) { |
84 DVLOG(2) << "OnAbandonPrerender, child_id = " << child_id | 102 DVLOG(2) << "OnAbandonPrerender, child_id = " << child_id |
85 << ", prerender_id = " << prerender_id; | 103 << ", prerender_id = " << prerender_id; |
86 // TODO(gavinp,cbentzel): Implement reasonable behaviour for | |
87 // navigation away from launcher. | |
88 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); | 104 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); |
89 ids_to_url_map_.erase(child_and_prerender_id); | 105 IdPairToPrerenderHandleMap::iterator id_to_handle_iter = |
106 ids_to_handle_map_.find(child_and_prerender_id); | |
107 if (id_to_handle_iter == ids_to_handle_map_.end()) | |
108 return; | |
109 PrerenderHandle* prerender_handle = id_to_handle_iter->second; | |
110 prerender_handle->OnNavigateAway(); | |
111 RemovePrerender(id_to_handle_iter); | |
90 } | 112 } |
91 | 113 |
92 void PrerenderLinkManager::OnChannelClosing(int child_id) { | 114 void PrerenderLinkManager::OnChannelClosing(int child_id) { |
93 DVLOG(2) << "OnChannelClosing, child id = " << child_id; | 115 DVLOG(2) << "OnChannelClosing, child id = " << child_id; |
94 const ChildAndPrerenderIdPair child_and_minimum_prerender_id( | 116 const ChildAndPrerenderIdPair child_and_minimum_prerender_id( |
95 child_id, std::numeric_limits<int>::min()); | 117 child_id, std::numeric_limits<int>::min()); |
96 const ChildAndPrerenderIdPair child_and_maximum_prerender_id( | 118 const ChildAndPrerenderIdPair child_and_maximum_prerender_id( |
97 child_id, std::numeric_limits<int>::max()); | 119 child_id, std::numeric_limits<int>::max()); |
98 std::queue<int> prerender_ids_to_abandon; | 120 std::queue<int> prerender_ids_to_abandon; |
99 for (IdPairToUrlMap::iterator | 121 for (IdPairToPrerenderHandleMap::iterator |
100 i = ids_to_url_map_.lower_bound(child_and_minimum_prerender_id), | 122 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); | 123 e = ids_to_handle_map_.upper_bound(child_and_maximum_prerender_id); |
102 i != e; ++i) { | 124 i != e; ++i) { |
103 prerender_ids_to_abandon.push(i->first.second); | 125 prerender_ids_to_abandon.push(i->first.second); |
104 } | 126 } |
105 while (!prerender_ids_to_abandon.empty()) { | 127 while (!prerender_ids_to_abandon.empty()) { |
106 DVLOG(4) << "---> abandon prerender_id = " | 128 DVLOG(4) << "---> abandon prerender_id = " |
107 << prerender_ids_to_abandon.front(); | 129 << prerender_ids_to_abandon.front(); |
108 OnAbandonPrerender(child_id, prerender_ids_to_abandon.front()); | 130 OnAbandonPrerender(child_id, prerender_ids_to_abandon.front()); |
109 prerender_ids_to_abandon.pop(); | 131 prerender_ids_to_abandon.pop(); |
110 } | 132 } |
111 } | 133 } |
112 | 134 |
113 bool PrerenderLinkManager::IsEmpty() const { | 135 bool PrerenderLinkManager::IsEmpty() const { |
114 return ids_to_url_map_.empty(); | 136 return ids_to_handle_map_.empty(); |
137 } | |
138 | |
139 void PrerenderLinkManager::RemovePrerender( | |
140 const IdPairToPrerenderHandleMap::iterator& id_to_handle_iter) { | |
141 PrerenderHandle* prerender_handle = id_to_handle_iter->second; | |
142 delete prerender_handle; | |
143 ids_to_handle_map_.erase(id_to_handle_iter); | |
115 } | 144 } |
116 | 145 |
117 } // namespace prerender | 146 } // namespace prerender |
118 | |
OLD | NEW |