| 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..657bc13df5a92b1b58adc69106ec29d2ada0cbeb
|
| --- /dev/null
|
| +++ b/chrome/browser/prerender/prerender_link_manager.cc
|
| @@ -0,0 +1,174 @@
|
| +// 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();
|
| + i != map_forwards.end(); ++i) {
|
| + // 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)
|
| + return false;
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +} // end namespace
|
| +
|
| +namespace prerender {
|
| +
|
| +PrerenderLinkManager::PrerenderLinkManager(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
|
| + // their client links, revisit having both maps and all the trouble that
|
| + // causes.
|
| + if (!manager_->AddPrerenderFromLinkRelPrerender(
|
| + child_id, render_view_route_id, url, referrer))
|
| + return false;
|
| + 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));
|
| + urls_to_id_map_.insert(std::make_pair(url, child_and_prerender_id));
|
| + DCHECK(MapsAreInverseOfEachOther(ids_to_url_map_, urls_to_id_map_));
|
| + return true;
|
| +}
|
| +
|
| +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()) {
|
| + DVLOG(5) << "... 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);
|
| + 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
|
| +
|
|
|