OLD | NEW |
---|---|
(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/browser/render_view_host.h" | |
16 #include "content/public/browser/session_storage_namespace.h" | |
17 #include "content/public/common/referrer.h" | |
18 #include "googleurl/src/gurl.h" | |
19 #include "googleurl/src/url_canon.h" | |
20 #include "ui/gfx/size.h" | |
21 | |
22 using content::RenderViewHost; | |
23 using content::SessionStorageNamespace; | |
24 | |
25 namespace prerender { | |
26 | |
27 PrerenderLinkManager::PrerenderLinkManager(PrerenderManager* manager) | |
28 : manager_(manager) { | |
29 } | |
30 | |
31 PrerenderLinkManager::~PrerenderLinkManager() { | |
32 } | |
33 | |
34 bool PrerenderLinkManager::OnAddPrerender(int child_id, | |
35 int prerender_id, | |
36 const GURL& orig_url, | |
37 const content::Referrer& referrer, | |
38 const gfx::Size& size, | |
39 int render_view_route_id) { | |
40 DVLOG(2) << "OnAddPrerender, child_id = " << child_id | |
41 << ", prerender_id = " << prerender_id | |
42 << ", url = " << orig_url.spec(); | |
43 DVLOG(3) << "... render_view_route_id = " << render_view_route_id | |
cbentzel
2012/05/04 15:32:25
May as well DVLOG(3) the size.
gavinp
2012/05/04 23:10:51
Done.
| |
44 << ", referrer url = " << referrer.url.spec(); | |
45 // TODO(gavinp): Add tests to ensure fragments work, then remove this fragment | |
46 // clearing code. | |
47 url_canon::Replacements<char> replacements; | |
48 replacements.ClearRef(); | |
49 const GURL url = orig_url.ReplaceComponents(replacements); | |
50 | |
51 if (!manager_->AddPrerenderFromLinkRelPrerender( | |
52 child_id, render_view_route_id, url, referrer, size)) { | |
53 return false; | |
54 } | |
55 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); | |
56 DCHECK_EQ(0U, ids_to_url_map_.count(child_and_prerender_id)); | |
57 ids_to_url_map_.insert(std::make_pair(child_and_prerender_id, url)); | |
58 return true; | |
59 } | |
60 | |
61 // TODO(gavinp): Once an observer interface is provided down to the WebKit | |
62 // layer, we should add DCHECK_NE(0L, ids_to_url_map_.count(...)) to both | |
63 // OnCancelPrerender and OnAbandonPrerender. We can't do this now, since | |
64 // the WebKit layer isn't even aware if we didn't add the prerender to the map | |
65 // in OnAddPrerender above. | |
66 void PrerenderLinkManager::OnCancelPrerender(int child_id, int prerender_id) { | |
67 DVLOG(2) << "OnCancelPrerender, child_id = " << child_id | |
68 << ", prerender_id = " << prerender_id; | |
69 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); | |
70 IdPairToUrlMap::iterator id_url_iter = | |
71 ids_to_url_map_.find(child_and_prerender_id); | |
72 if (id_url_iter == ids_to_url_map_.end()) { | |
73 DVLOG(5) << "... canceling a prerender that doesn't exist."; | |
74 return; | |
75 } | |
76 const GURL url = id_url_iter->second; | |
77 ids_to_url_map_.erase(id_url_iter); | |
78 manager_->MaybeCancelPrerender(url); | |
79 } | |
80 | |
81 void PrerenderLinkManager::OnAbandonPrerender(int child_id, int prerender_id) { | |
82 DVLOG(2) << "OnAbandonPrerender, child_id = " << child_id | |
83 << ", prerender_id = " << prerender_id; | |
84 // TODO(gavinp,cbentzel): Implement reasonable behaviour for | |
85 // navigation away from launcher. | |
86 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); | |
87 ids_to_url_map_.erase(child_and_prerender_id); | |
88 } | |
89 | |
90 void PrerenderLinkManager::OnChannelClosing(int child_id) { | |
91 DVLOG(2) << "OnChannelClosing, child id = " << child_id; | |
92 const ChildAndPrerenderIdPair child_and_minimum_prerender_id( | |
93 child_id, std::numeric_limits<int>::min()); | |
94 const ChildAndPrerenderIdPair child_and_maximum_prerender_id( | |
95 child_id, std::numeric_limits<int>::max()); | |
96 std::queue<int> prerender_ids_to_abandon; | |
97 for (IdPairToUrlMap::iterator | |
98 i = ids_to_url_map_.lower_bound(child_and_minimum_prerender_id), | |
99 e = ids_to_url_map_.upper_bound(child_and_maximum_prerender_id); | |
100 i != e; ++i) { | |
101 prerender_ids_to_abandon.push(i->first.second); | |
102 } | |
103 while (!prerender_ids_to_abandon.empty()) { | |
104 DVLOG(4) << "---> abandon prerender_id = " | |
105 << prerender_ids_to_abandon.front(); | |
106 OnAbandonPrerender(child_id, prerender_ids_to_abandon.front()); | |
107 prerender_ids_to_abandon.pop(); | |
108 } | |
109 } | |
110 | |
111 bool PrerenderLinkManager::IsEmpty() const { | |
112 return ids_to_url_map_.empty(); | |
113 } | |
114 | |
115 } // namespace prerender | |
116 | |
OLD | NEW |