Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: chrome/browser/prerender/prerender_link_manager.cc

Issue 10198040: New link rel=prerender api, using WebKit::WebPrerenderingPlatform (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remediate to dominich + jam reviews Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/prerender/prerender_link_manager.h"
6
7 #include <limits>
8 #include <queue>
9 #include <utility>
10
11 #include "chrome/browser/prerender/prerender_contents.h"
12 #include "chrome/browser/prerender/prerender_manager.h"
13 #include "chrome/browser/prerender/prerender_manager_factory.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "content/public/common/referrer.h"
16 #include "googleurl/src/gurl.h"
17 #include "googleurl/src/url_canon.h"
18 #include "ui/gfx/size.h"
19
20 namespace {
21
22 template<typename MapForwards, typename MapBackwards>
23 bool MapsAreInverseOfEachOther(const MapForwards& map_forwards,
24 const MapBackwards& map_backwards) {
25 if (map_forwards.size() != map_backwards.size())
26 return false;
27 for (typename MapForwards::const_iterator i = map_forwards.begin();
28 i != map_forwards.end(); ++i) {
29 // Best to use std::count here both times, as
30 // AssociativeContainer::count returns size_t, vs. std::count
31 // returning iterator_traits<InputIterator>::difference_type
32 typedef typename std::iterator_traits<
33 typename MapForwards::iterator>::difference_type diff_type;
34 diff_type forwards_count =
35 std::count(map_forwards.begin(), map_forwards.end(),
36 typename MapForwards::value_type(i->first, i->second));
37 diff_type backwards_count =
38 std::count(map_backwards.begin(), map_backwards.end(),
39 typename MapBackwards::value_type(i->second, i->first));
40 if (forwards_count != backwards_count)
41 return false;
42 }
43 return true;
44 }
45
46 } // end namespace
47
48 namespace prerender {
49
50 PrerenderLinkManager::PrerenderLinkManager(PrerenderManager* manager)
51 : manager_(manager) {
52 }
53
54 PrerenderLinkManager::~PrerenderLinkManager() {
55 }
56
57 bool PrerenderLinkManager::OnAddPrerender(int child_id,
58 int prerender_id,
59 const GURL& orig_url,
60 const content::Referrer& referrer,
61 const gfx::Size& ALLOW_UNUSED size,
62 int render_view_route_id) {
63 DVLOG(2) << "OnAddPrerender, child_id = " << child_id
64 << ", prerender_id = " << prerender_id
65 << ", url = " << orig_url.spec();
66 DVLOG(3) << "... render_view_route_id = " << render_view_route_id
67 << ", referrer url = " << referrer.url.spec();
68 DCHECK(MapsAreInverseOfEachOther(ids_to_url_map_, urls_to_id_map_));
69 // TODO(gavinp): Add tests to insure fragments work, then remove this fragment
70 // clearing code.
71 url_canon::Replacements<char> replacements;
72 replacements.ClearRef();
73 const GURL url = orig_url.ReplaceComponents(replacements);
74
75 // TODO(gavinp,dominich): After the Prerender API can send events back to
76 // their client links, revisit having both maps and all the trouble that
77 // causes.
78 if (!manager_->AddPrerenderFromLinkRelPrerender(
79 child_id, render_view_route_id, url, referrer))
80 return false;
81 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id);
82 DCHECK(ids_to_url_map_.find(child_and_prerender_id) == ids_to_url_map_.end());
83 ids_to_url_map_.insert(std::make_pair(child_and_prerender_id, url));
84 urls_to_id_map_.insert(std::make_pair(url, child_and_prerender_id));
85 DCHECK(MapsAreInverseOfEachOther(ids_to_url_map_, urls_to_id_map_));
86 return true;
87 }
88
89 void PrerenderLinkManager::OnCancelPrerender(int prerender_id,
90 int child_id) {
91 DVLOG(2) << "OnCancelPrerender, child_id = " << child_id
92 << ", prerender_id = " << prerender_id;
93 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id);
94 IdPairToUrlMap::iterator id_url_iter =
95 ids_to_url_map_.find(child_and_prerender_id);
96 if (id_url_iter == ids_to_url_map_.end()) {
97 DVLOG(5) << "... canceling a prerender that doesn't exist.";
98 return;
99 }
100 const GURL url = id_url_iter->second;
101 RemovePrerender(id_url_iter);
102
103 if (urls_to_id_map_.find(url) != urls_to_id_map_.end())
104 return;
105
106 // TODO(gavinp): Track down the correct prerender and stop it, rather than
107 // this nuclear option, which assumes that only one prerender at a time
108 // runs.
109 if (PrerenderContents* contents = manager_->GetEntry(url))
110 contents->Destroy(FINAL_STATUS_CANCELLED);
111 }
112
113 void PrerenderLinkManager::OnAbandonPrerender(int prerender_id, int child_id) {
114 DVLOG(2) << "OnAbandonPrerender, child_id = " << child_id
115 << ", prerender_id = " << prerender_id;
116 // TODO(gavinp,cbentzel): Implement reasonable behaviour for
117 // navigation away from launcher.
118 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id);
119 IdPairToUrlMap::iterator id_url_iter =
120 ids_to_url_map_.find(child_and_prerender_id);
121 if (id_url_iter == ids_to_url_map_.end()) {
122 // FIXME(gavinp): Currently, canceled prerenders also get abandoned later.
123 // When the WebKit fix to stop this lands, add a NOTREACHED() here.
124 return;
125 }
126 RemovePrerender(id_url_iter);
127 }
128
129 void PrerenderLinkManager::OnChannelClosing(int child_id) {
130 DVLOG(2) << "OnChannelClosing, child id = " << child_id;
131 const ChildAndPrerenderIdPair child_and_minimum_prerender_id(
132 child_id, std::numeric_limits<int>::min());
133 const ChildAndPrerenderIdPair child_and_maximum_prerender_id(
134 child_id, std::numeric_limits<int>::max());
135 std::queue<int> prerender_ids_to_abandon;
136 for (IdPairToUrlMap::iterator
137 i = ids_to_url_map_.lower_bound(child_and_minimum_prerender_id),
138 e = ids_to_url_map_.upper_bound(child_and_maximum_prerender_id);
139 i != e; ++i) {
140 prerender_ids_to_abandon.push(i->first.second);
141 }
142 while (!prerender_ids_to_abandon.empty()) {
143 DVLOG(4) << "---> abandon prerender_id = "
144 << prerender_ids_to_abandon.front();
145 OnAbandonPrerender(prerender_ids_to_abandon.front(), child_id);
146 prerender_ids_to_abandon.pop();
147 }
148 }
149
150 void PrerenderLinkManager::RemovePrerender(
151 const IdPairToUrlMap::iterator& id_url_iter) {
152 DCHECK(MapsAreInverseOfEachOther(ids_to_url_map_, urls_to_id_map_));
153 const GURL url = id_url_iter->second;
154 const ChildAndPrerenderIdPair child_and_prerender_id = id_url_iter->first;
155 ids_to_url_map_.erase(id_url_iter);
156
157 for (UrlToIdPairMap::iterator i = urls_to_id_map_.lower_bound(url),
158 e = urls_to_id_map_.upper_bound(url);
159 i != e; ++i) {
160 if (i->second == child_and_prerender_id) {
161 urls_to_id_map_.erase(i);
162 DCHECK(MapsAreInverseOfEachOther(ids_to_url_map_, urls_to_id_map_));
163 return;
164 }
165 }
166 NOTREACHED();
167 }
168
169 bool PrerenderLinkManager::IsEmpty() const {
170 return ids_to_url_map_.empty();
171 }
172
173 } // namespace prerender
174
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698