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

Unified 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: fix base Created 8 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/prerender/prerender_link_manager.cc
diff --git a/chrome/browser/prerender/prerender_link_manager.cc b/chrome/browser/prerender/prerender_link_manager.cc
new file mode 100644
index 0000000000000000000000000000000000000000..eaf1e3aa5fc1724516b09a73349a50e11856181b
--- /dev/null
+++ b/chrome/browser/prerender/prerender_link_manager.cc
@@ -0,0 +1,176 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/prerender/prerender_link_manager.h"
+
+#include <limits>
+#include <queue>
+#include <utility>
+
+#include "chrome/browser/prerender/prerender_contents.h"
+#include "chrome/browser/prerender/prerender_manager.h"
+#include "chrome/browser/prerender/prerender_manager_factory.h"
+#include "chrome/browser/profiles/profile.h"
+#include "content/public/common/referrer.h"
+#include "googleurl/src/gurl.h"
+#include "googleurl/src/url_canon.h"
+#include "ui/gfx/size.h"
+
+namespace {
+
+template<typename MapForwards, typename MapBackwards>
+bool MapsAreInverseOfEachOther(const MapForwards& map_forwards,
+ const MapBackwards& map_backwards) {
+ if (map_forwards.size() != map_backwards.size())
+ return false;
+ for (typename MapForwards::const_iterator i = map_forwards.begin(),
+ e = map_forwards.end();
+ i != e; ++i) {
dominich 2012/04/26 22:50:58 why do you need to declare e in the first part? ie
gavinp 2012/04/27 20:31:32 Pure superstition. The standard guarantees that t
+ // Best to use std::count here both times, as
+ // AssociativeContainer::count returns size_t, vs. std::count
+ // returning iterator_traits<InputIterator>::difference_type
+ typedef typename std::iterator_traits<
+ typename MapForwards::iterator>::difference_type diff_type;
+ diff_type forwards_count =
+ std::count(map_forwards.begin(), map_forwards.end(),
+ typename MapForwards::value_type(i->first, i->second));
+ diff_type backwards_count =
+ std::count(map_backwards.begin(), map_backwards.end(),
+ typename MapBackwards::value_type(i->second, i->first));
+ if (forwards_count != backwards_count) {
dominich 2012/04/26 22:50:58 nit: {} not necessary here
gavinp 2012/04/27 20:31:32 Done.
+ return false;
+ }
+ }
+ return true;
+}
+
+}
+
+namespace prerender {
+
+PrerenderLinkManager::PrerenderLinkManager(
dominich 2012/04/26 22:50:58 nit: unnecessary to wrap this line
gavinp 2012/04/27 20:31:32 Done.
+ PrerenderManager* manager)
+ : manager_(manager) {
+}
+
+PrerenderLinkManager::~PrerenderLinkManager() {
+}
+
+bool PrerenderLinkManager::OnAddPrerender(int child_id,
+ int prerender_id,
+ const GURL& orig_url,
+ const content::Referrer& referrer,
+ const gfx::Size& ALLOW_UNUSED size,
+ int render_view_route_id) {
+ DVLOG(2) << "OnAddPrerender, child_id = " << child_id
+ << ", prerender_id = " << prerender_id
+ << ", url = " << orig_url.spec();
+ DVLOG(3) << "... render_view_route_id = " << render_view_route_id
+ << ", referrer url = " << referrer.url.spec();
+ DCHECK(MapsAreInverseOfEachOther(ids_to_url_map_, urls_to_id_map_));
+ // TODO(gavinp): Add tests to insure fragments work, then remove this fragment
+ // clearing code.
+ url_canon::Replacements<char> replacements;
+ replacements.ClearRef();
+ const GURL url = orig_url.ReplaceComponents(replacements);
+
+ // TODO(gavinp,dominich): After the Prerender API can send events back to
dominich 2012/04/26 22:50:58 From this code, I don't see why the urls_to_id_map
gavinp 2012/04/27 20:31:32 Two links prerender the same page from the same or
dominich 2012/04/27 21:52:01 So they'd have the same child_id but different pre
gavinp 2012/04/27 23:31:45 Yes. However, notice that OnCancelPrerender has d
+ // their client links, revisit having both maps and all the trouble that
+ // causes.
+ const bool did_successfully_add = manager_->AddPrerenderFromLinkRelPrerender(
+ child_id, render_view_route_id, url, referrer);
+ const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id);
+ DCHECK(ids_to_url_map_.find(child_and_prerender_id) == ids_to_url_map_.end());
+ ids_to_url_map_.insert(std::make_pair(child_and_prerender_id, url));
dominich 2012/04/26 22:50:58 Why insert these if |did_successfully_add| is fals
gavinp 2012/04/27 20:31:32 Done. What I liked about the original approach wa
+ urls_to_id_map_.insert(std::make_pair(url, child_and_prerender_id));
+ DCHECK(MapsAreInverseOfEachOther(ids_to_url_map_, urls_to_id_map_));
+ return did_successfully_add;
+}
+
+void PrerenderLinkManager::OnCancelPrerender(int prerender_id,
+ int child_id) {
+ DVLOG(2) << "OnCancelPrerender, child_id = " << child_id
+ << ", prerender_id = " << prerender_id;
+ const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id);
+ IdPairToUrlMap::iterator id_url_iter =
+ ids_to_url_map_.find(child_and_prerender_id);
+ if (id_url_iter == ids_to_url_map_.end()) {
+ NOTREACHED() << "Canceling a prerender that doesn't exist.";
+ return;
+ }
+ const GURL url = id_url_iter->second;
+ RemovePrerender(id_url_iter);
+
+ if (urls_to_id_map_.find(url) != urls_to_id_map_.end())
+ return;
+
+ // TODO(gavinp): Track down the correct prerender and stop it, rather than
+ // this nuclear option, which assumes that only one prerender at a time
+ // runs.
+ if (PrerenderContents* contents = manager_->GetEntry(url))
+ contents->Destroy(FINAL_STATUS_CANCELLED);
+}
+
+void PrerenderLinkManager::OnAbandonPrerender(int prerender_id, int child_id) {
+ DVLOG(2) << "OnAbandonPrerender, child_id = " << child_id
+ << ", prerender_id = " << prerender_id;
+ // TODO(gavinp,cbentzel): Implement reasonable behaviour for
+ // navigation away from launcher.
+ const ChildAndPrerenderIdPair child_and_prerender_id(child_id, prerender_id);
+ IdPairToUrlMap::iterator id_url_iter =
+ ids_to_url_map_.find(child_and_prerender_id);
+ if (id_url_iter == ids_to_url_map_.end()) {
+ // FIXME(gavinp): Currently, canceled prerenders also get abandoned later.
+ // When the WebKit fix to stop this lands, add a NOTREACHED() here.
+ return;
+ }
+ RemovePrerender(id_url_iter);
+}
+
+void PrerenderLinkManager::OnChannelClosing(int child_id) {
+ DVLOG(2) << "OnChannelClosing, child id = " << child_id;
+ const ChildAndPrerenderIdPair child_and_minimum_prerender_id(
+ child_id, std::numeric_limits<int>::min());
+ const ChildAndPrerenderIdPair child_and_maximum_prerender_id(
+ child_id, std::numeric_limits<int>::max());
+ std::queue<int> prerender_ids_to_abandon;
+ for (IdPairToUrlMap::iterator
+ i = ids_to_url_map_.lower_bound(child_and_minimum_prerender_id),
+ e = ids_to_url_map_.upper_bound(child_and_maximum_prerender_id);
+ i != e; ++i) {
+ prerender_ids_to_abandon.push(i->first.second);
+ }
+ while (!prerender_ids_to_abandon.empty()) {
+ DVLOG(4) << "---> abandon prerender_id = "
+ << prerender_ids_to_abandon.front();
+ OnAbandonPrerender(prerender_ids_to_abandon.front(), child_id);
+ prerender_ids_to_abandon.pop();
+ }
+}
+
+void PrerenderLinkManager::RemovePrerender(
+ const IdPairToUrlMap::iterator& id_url_iter) {
+ DCHECK(MapsAreInverseOfEachOther(ids_to_url_map_, urls_to_id_map_));
+ const GURL url = id_url_iter->second;
+ const ChildAndPrerenderIdPair child_and_prerender_id = id_url_iter->first;
+ ids_to_url_map_.erase(id_url_iter);
+
+ for (UrlToIdPairMap::iterator i = urls_to_id_map_.lower_bound(url),
+ e = urls_to_id_map_.upper_bound(url);
gavinp 2012/04/27 20:31:32 I'm leaving this one here, multimap::upper_bound()
+ i != e; ++i) {
+ if (i->second == child_and_prerender_id) {
+ urls_to_id_map_.erase(i);
+ DCHECK(MapsAreInverseOfEachOther(ids_to_url_map_, urls_to_id_map_));
+ return;
+ }
+ }
+ NOTREACHED();
+}
+
+bool PrerenderLinkManager::IsEmpty() const {
+ return ids_to_url_map_.empty();
+}
+
+} // namespace prerender
+

Powered by Google App Engine
This is Rietveld 408576698