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

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

Issue 10553029: Handle interface to prerenders. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remediation to reviews, and unit test improvements. Created 8 years, 5 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
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"
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
22 using content::RenderViewHost; 23 using content::RenderViewHost;
23 using content::SessionStorageNamespace; 24 using content::SessionStorageNamespace;
24 25
25 namespace prerender { 26 namespace prerender {
26 27
27 PrerenderLinkManager::PrerenderLinkManager(PrerenderManager* manager) 28 PrerenderLinkManager::PrerenderLinkManager(PrerenderManager* manager)
28 : manager_(manager) { 29 : manager_(manager) {
29 } 30 }
30 31
31 PrerenderLinkManager::~PrerenderLinkManager() { 32 PrerenderLinkManager::~PrerenderLinkManager() {
33 for (IdPairToPrerenderHandleMap::iterator it = ids_to_handle_map_.begin();
34 it != ids_to_handle_map_.end();
35 ++it) {
36 PrerenderHandle* prerender_handle = it->second;
37 prerender_handle->OnNavigateAway();
dominich 2012/07/02 20:43:03 Why OnNavigateAway? If you delete (as you do below
gavinp 2012/07/03 16:41:02 I removed the delete.
dominich 2012/07/03 17:08:39 But that's the right thing to do :) We're not navi
gavinp 2012/07/03 18:45:40 Done.
38 delete prerender_handle;
dominich 2012/07/02 20:43:03 So the caller of AddPrerender* owns the returned h
gavinp 2012/07/03 16:41:02 Yes.
39 }
32 } 40 }
33 41
34 bool PrerenderLinkManager::OnAddPrerender(int child_id, 42 bool PrerenderLinkManager::OnAddPrerender(int child_id,
35 int prerender_id, 43 int prerender_id,
36 const GURL& orig_url, 44 const GURL& orig_url,
37 const content::Referrer& referrer, 45 const content::Referrer& referrer,
38 const gfx::Size& size, 46 const gfx::Size& size,
39 int render_view_route_id) { 47 int render_view_route_id) {
40 DVLOG(2) << "OnAddPrerender, child_id = " << child_id 48 DVLOG(2) << "OnAddPrerender, child_id = " << child_id
41 << ", prerender_id = " << prerender_id 49 << ", prerender_id = " << prerender_id
42 << ", url = " << orig_url.spec(); 50 << ", url = " << orig_url.spec();
43 DVLOG(3) << "... referrer url = " << referrer.url.spec() 51 DVLOG(3) << "... referrer url = " << referrer.url.spec()
44 << ", size = (" << size.width() << ", " << size.height() << ")" 52 << ", size = (" << size.width() << ", " << size.height() << ")"
45 << ", render_view_route_id = " << render_view_route_id; 53 << ", render_view_route_id = " << render_view_route_id;
46 54
47 // TODO(gavinp): Add tests to ensure fragments work, then remove this fragment 55 // TODO(gavinp): Add tests to ensure fragments work, then remove this fragment
48 // clearing code. 56 // clearing code.
49 url_canon::Replacements<char> replacements; 57 url_canon::Replacements<char> replacements;
50 replacements.ClearRef(); 58 replacements.ClearRef();
51 const GURL url = orig_url.ReplaceComponents(replacements); 59 const GURL url = orig_url.ReplaceComponents(replacements);
52 60
53 if (!manager_->AddPrerenderFromLinkRelPrerender( 61 if (PrerenderHandle* prerender_handle =
54 child_id, render_view_route_id, url, referrer, size)) { 62 manager_->AddPrerenderFromLinkRelPrerender(
55 return false; 63 child_id, render_view_route_id, url, referrer, size)) {
64 ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id);
65 DCHECK_EQ(0U, ids_to_handle_map_.count(child_and_prerender_id));
66 ids_to_handle_map_.insert(
67 std::make_pair(child_and_prerender_id, prerender_handle));
68 return true;
56 } 69 }
57 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); 70 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 } 71 }
62 72
63 // TODO(gavinp): Once an observer interface is provided down to the WebKit 73 // 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 74 // 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 75 // 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 76 // the WebKit layer isn't even aware if we didn't add the prerender to the map
67 // in OnAddPrerender above. 77 // in OnAddPrerender above.
68 void PrerenderLinkManager::OnCancelPrerender(int child_id, int prerender_id) { 78 void PrerenderLinkManager::OnCancelPrerender(int child_id, int prerender_id) {
69 DVLOG(2) << "OnCancelPrerender, child_id = " << child_id 79 DVLOG(2) << "OnCancelPrerender, child_id = " << child_id
70 << ", prerender_id = " << prerender_id; 80 << ", prerender_id = " << prerender_id;
71 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id); 81 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id);
72 IdPairToUrlMap::iterator id_url_iter = 82 IdPairToPrerenderHandleMap::iterator id_handle_iter =
73 ids_to_url_map_.find(child_and_prerender_id); 83 ids_to_handle_map_.find(child_and_prerender_id);
74 if (id_url_iter == ids_to_url_map_.end()) { 84 if (id_handle_iter == ids_to_handle_map_.end()) {
75 DVLOG(5) << "... canceling a prerender that doesn't exist."; 85 DVLOG(5) << "... canceling a prerender that doesn't exist.";
76 return; 86 return;
77 } 87 }
78 const GURL url = id_url_iter->second; 88 PrerenderHandle* prerender_handle = id_handle_iter->second;
79 ids_to_url_map_.erase(id_url_iter); 89 DCHECK(prerender_handle);
80 manager_->MaybeCancelPrerender(url); 90 delete prerender_handle;
91 ids_to_handle_map_.erase(id_handle_iter);
81 } 92 }
82 93
83 void PrerenderLinkManager::OnAbandonPrerender(int child_id, int prerender_id) { 94 void PrerenderLinkManager::OnAbandonPrerender(int child_id, int prerender_id) {
84 DVLOG(2) << "OnAbandonPrerender, child_id = " << child_id 95 DVLOG(2) << "OnAbandonPrerender, child_id = " << child_id
85 << ", prerender_id = " << prerender_id; 96 << ", 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); 97 const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id);
89 ids_to_url_map_.erase(child_and_prerender_id); 98 IdPairToPrerenderHandleMap::iterator id_handle_iter =
99 ids_to_handle_map_.find(child_and_prerender_id);
100 if (id_handle_iter == ids_to_handle_map_.end())
101 return;
102 PrerenderHandle* prerender_handle = id_handle_iter->second;
103 prerender_handle->OnNavigateAway();
dominich 2012/07/02 20:43:03 This will call both OnNavigateAway and OnCancel. I
gavinp 2012/07/03 16:41:02 Both OnNavigateAway and OnCancel invalidate the ha
dominich 2012/07/03 17:08:39 I'm tempted to suggest that OnCancel should delete
gavinp 2012/07/03 18:45:40 I was tempted to write it that way too, but came t
104 delete prerender_handle;
105 ids_to_handle_map_.erase(id_handle_iter);
90 } 106 }
91 107
92 void PrerenderLinkManager::OnChannelClosing(int child_id) { 108 void PrerenderLinkManager::OnChannelClosing(int child_id) {
93 DVLOG(2) << "OnChannelClosing, child id = " << child_id; 109 DVLOG(2) << "OnChannelClosing, child id = " << child_id;
94 const ChildAndPrerenderIdPair child_and_minimum_prerender_id( 110 const ChildAndPrerenderIdPair child_and_minimum_prerender_id(
95 child_id, std::numeric_limits<int>::min()); 111 child_id, std::numeric_limits<int>::min());
96 const ChildAndPrerenderIdPair child_and_maximum_prerender_id( 112 const ChildAndPrerenderIdPair child_and_maximum_prerender_id(
97 child_id, std::numeric_limits<int>::max()); 113 child_id, std::numeric_limits<int>::max());
98 std::queue<int> prerender_ids_to_abandon; 114 std::queue<int> prerender_ids_to_abandon;
99 for (IdPairToUrlMap::iterator 115 for (IdPairToPrerenderHandleMap::iterator
100 i = ids_to_url_map_.lower_bound(child_and_minimum_prerender_id), 116 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); 117 e = ids_to_handle_map_.upper_bound(child_and_maximum_prerender_id);
102 i != e; ++i) { 118 i != e; ++i) {
103 prerender_ids_to_abandon.push(i->first.second); 119 prerender_ids_to_abandon.push(i->first.second);
104 } 120 }
105 while (!prerender_ids_to_abandon.empty()) { 121 while (!prerender_ids_to_abandon.empty()) {
106 DVLOG(4) << "---> abandon prerender_id = " 122 DVLOG(4) << "---> abandon prerender_id = "
107 << prerender_ids_to_abandon.front(); 123 << prerender_ids_to_abandon.front();
108 OnAbandonPrerender(child_id, prerender_ids_to_abandon.front()); 124 OnAbandonPrerender(child_id, prerender_ids_to_abandon.front());
109 prerender_ids_to_abandon.pop(); 125 prerender_ids_to_abandon.pop();
110 } 126 }
111 } 127 }
112 128
113 bool PrerenderLinkManager::IsEmpty() const { 129 bool PrerenderLinkManager::IsEmpty() const {
114 return ids_to_url_map_.empty(); 130 return ids_to_handle_map_.empty();
115 } 131 }
116 132
117 } // namespace prerender 133 } // namespace prerender
118 134
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698