1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PRELOADING_PRERENDER_PRERENDER_MANAGER_H_
#define CHROME_BROWSER_PRELOADING_PRERENDER_PRERENDER_MANAGER_H_
#include <string>
#include "components/omnibox/browser/autocomplete_match.h"
#include "content/public/browser/preloading.h"
#include "content/public/browser/prerender_handle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "url/gurl.h"
namespace content {
class NavigationHandle;
}
namespace internal {
extern const char kHistogramPrerenderPredictionStatusDefaultSearchEngine[];
extern const char kHistogramPrerenderPredictionStatusDirectUrlInput[];
} // namespace internal
// If you change this, please follow the process in
// go/preloading-dashboard-updates to update the mapping reflected in dashboard,
// or if you are not a Googler, please file an FYI bug on https://crbug.new with
// component Internals>Preload.
//
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
//
// LINT.IfChange
enum class PrerenderPredictionStatus {
// The prerender was not started at all for this omnibox interaction.
kNotStarted = 0,
// The prerender was cancelled since the prediction is updated.
kCancelled = 1,
// The prerender was unused.
kUnused = 2,
// The predicted URL was used.
kHitFinished = 3,
kMaxValue = kHitFinished,
};
// LINT.ThenChange()
// Manages running prerenders in the //chrome.
// Chrome manages running prerenders separately, as it prioritizes the latest
// prerender requests, while the //content prioritizes the earliest requests.
class PrerenderManager : public content::WebContentsObserver,
public content::WebContentsUserData<PrerenderManager> {
public:
PrerenderManager(const PrerenderManager&) = delete;
PrerenderManager& operator=(const PrerenderManager&) = delete;
~PrerenderManager() override;
// content::WebContentsObserver
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;
// Calling this method will lead to the cancellation of the previous prerender
// if the given `canonical_search_url` differs from the ongoing one's.
void StartPrerenderSearchResult(
const GURL& canonical_search_url,
const GURL& prerendering_url,
base::WeakPtr<content::PreloadingAttempt> attempt);
// Cancels the prerender that is prerendering the given
// `canonical_search_url`.
// TODO(crbug.com/40214220): Use the creator's address to identify the
// owner that can cancels the corresponding prerendering?
void StopPrerenderSearchResult(const GURL& canonical_search_url);
// The entry of bookmark prerender.
// Calling this method will return WeakPtr of the started prerender, and lead
// to the cancellation of the previous prerender if the given url is different
// from the on-going one. If the url given is already on-going, this function
// will return the weak pointer to the on-going prerender handle.
base::WeakPtr<content::PrerenderHandle> StartPrerenderBookmark(
const GURL& prerendering_url);
void StopPrerenderBookmark(
base::WeakPtr<content::PrerenderHandle> prerender_handle);
// The entry of new tab page prerender.
// Calling this method will return WeakPtr of the started prerender, and lead
// to the cancellation of the previous prerender if the given url is different
// from the on-going one. If the url given is already on-going, this function
// will return the weak pointer to the on-going prerender handle.
base::WeakPtr<content::PrerenderHandle> StartPrerenderNewTabPage(
const GURL& prerendering_url,
content::PreloadingPredictor predictor);
void StopPrerenderNewTabPage(
base::WeakPtr<content::PrerenderHandle> prerender_handle);
// The entry of direct url input prerender.
// Calling this method will return WeakPtr of the started prerender, and lead
// to the cancellation of the previous prerender if the given url is different
// from the on-going one. If the url given is already on-going, this function
// will return the weak pointer to the on-going prerender handle.
// PreloadingAttempt represents the attempt corresponding to this prerender to
// log the necessary metrics.
base::WeakPtr<content::PrerenderHandle> StartPrerenderDirectUrlInput(
const GURL& prerendering_url,
content::PreloadingAttempt& preloading_attempt);
// Returns true if the current tab prerendered a search result for omnibox
// inputs.
bool HasSearchResultPagePrerendered() const;
base::WeakPtr<PrerenderManager> GetWeakPtr();
// Returns the prerendered search terms if search_prerender_task_ exists.
// Returns empty string otherwise.
const GURL GetPrerenderCanonicalSearchURLForTesting() const;
private:
class SearchPrerenderTask;
explicit PrerenderManager(content::WebContents* web_contents);
friend class content::WebContentsUserData<PrerenderManager>;
void ResetPrerenderHandlesOnPrimaryPageChanged(
content::NavigationHandle* navigation_handle);
// Maybe cancel the ongoing search prerender to restart a new one if this
// finds the callers' intentions changed. The number of concurrence search
// prerender is limited to 1, so it is needed to cancel the old one in order
// to start a new one. Returns true if this finds the caller wants to
// prerender another search result. Here `attempt` represents the
// PreloadingAttempt corresponding to this prerender attempt to log metrics.
bool ResetSearchPrerenderTaskIfNecessary(
const GURL& canonical_search_url,
base::WeakPtr<content::PreloadingAttempt> attempt);
// Stores the prerender which serves for search results. It is responsible for
// tracking a started search prerender, and informing `SearchPrefetchService`
// of the prerender status.
std::unique_ptr<SearchPrerenderTask> search_prerender_task_;
std::unique_ptr<content::PrerenderHandle> bookmark_prerender_handle_;
std::unique_ptr<content::PrerenderHandle> new_tab_page_prerender_handle_;
std::unique_ptr<content::PrerenderHandle> direct_url_input_prerender_handle_;
base::WeakPtrFactory<PrerenderManager> weak_factory_{this};
WEB_CONTENTS_USER_DATA_KEY_DECL();
};
#endif // CHROME_BROWSER_PRELOADING_PRERENDER_PRERENDER_MANAGER_H_
|