Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / prerender / prerender_manager.h
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 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_
7
8 #include <list>
9 #include <map>
10 #include <string>
11 #include <utility>
12 #include <vector>
13
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/threading/non_thread_safe.h"
19 #include "base/time/time.h"
20 #include "base/timer/timer.h"
21 #include "chrome/browser/history/history_service.h"
22 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
23 #include "chrome/browser/predictors/logged_in_predictor_table.h"
24 #include "chrome/browser/prerender/prerender_config.h"
25 #include "chrome/browser/prerender/prerender_contents.h"
26 #include "chrome/browser/prerender/prerender_events.h"
27 #include "chrome/browser/prerender/prerender_final_status.h"
28 #include "chrome/browser/prerender/prerender_histograms.h"
29 #include "chrome/browser/prerender/prerender_origin.h"
30 #include "chrome/browser/prerender/prerender_tracker.h"
31 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
32 #include "content/public/browser/notification_observer.h"
33 #include "content/public/browser/notification_registrar.h"
34 #include "content/public/browser/session_storage_namespace.h"
35 #include "content/public/browser/web_contents_observer.h"
36 #include "net/cookies/canonical_cookie.h"
37 #include "net/cookies/cookie_monster.h"
38 #include "url/gurl.h"
39
40 class Profile;
41 class InstantSearchPrerendererTest;
42 struct ChromeCookieDetails;
43
44 namespace base {
45 class DictionaryValue;
46 }
47
48 namespace chrome {
49 struct NavigateParams;
50 }
51
52 namespace content {
53 class WebContents;
54 }
55
56 namespace gfx {
57 class Size;
58 }
59
60 namespace net {
61 class URLRequestContextGetter;
62 }
63
64 namespace prerender {
65
66 class PrerenderCondition;
67 class PrerenderHandle;
68 class PrerenderHistory;
69 class PrerenderLocalPredictor;
70
71 // PrerenderManager is responsible for initiating and keeping prerendered
72 // views of web pages. All methods must be called on the UI thread unless
73 // indicated otherwise.
74 class PrerenderManager : public base::SupportsWeakPtr<PrerenderManager>,
75                          public base::NonThreadSafe,
76                          public content::NotificationObserver,
77                          public BrowserContextKeyedService,
78                          public MediaCaptureDevicesDispatcher::Observer {
79  public:
80   // NOTE: New values need to be appended, since they are used in histograms.
81   enum PrerenderManagerMode {
82     PRERENDER_MODE_DISABLED = 0,
83     PRERENDER_MODE_ENABLED = 1,
84     PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP = 2,
85     PRERENDER_MODE_EXPERIMENT_PRERENDER_GROUP = 3,
86     // Obsolete: PRERENDER_MODE_EXPERIMENT_5MIN_TTL_GROUP = 4,
87     PRERENDER_MODE_EXPERIMENT_NO_USE_GROUP = 5,
88     PRERENDER_MODE_EXPERIMENT_MULTI_PRERENDER_GROUP = 6,
89     PRERENDER_MODE_EXPERIMENT_15MIN_TTL_GROUP = 7,
90     PRERENDER_MODE_MAX
91   };
92
93   // One or more of these flags must be passed to ClearData() to specify just
94   // what data to clear.  See function declaration for more information.
95   enum ClearFlags {
96     CLEAR_PRERENDER_CONTENTS = 0x1 << 0,
97     CLEAR_PRERENDER_HISTORY = 0x1 << 1,
98     CLEAR_MAX = 0x1 << 2
99   };
100
101   typedef predictors::LoggedInPredictorTable::LoggedInStateMap LoggedInStateMap;
102
103   // ID indicating that no experiment is active.
104   static const uint8 kNoExperiment = 0;
105
106   // Owned by a Profile object for the lifetime of the profile.
107   PrerenderManager(Profile* profile, PrerenderTracker* prerender_tracker);
108
109   virtual ~PrerenderManager();
110
111   // From BrowserContextKeyedService:
112   virtual void Shutdown() OVERRIDE;
113
114   // Entry points for adding prerenders.
115
116   // Adds a prerender for |url| if valid. |process_id| and |route_id| identify
117   // the RenderView that the prerender request came from. If |size| is empty, a
118   // default from the PrerenderConfig is used. Returns a caller-owned
119   // PrerenderHandle* if the URL was added, NULL if it was not. If the launching
120   // RenderView is itself prerendering, the prerender is added as a pending
121   // prerender.
122   PrerenderHandle* AddPrerenderFromLinkRelPrerender(
123       int process_id,
124       int route_id,
125       const GURL& url,
126       uint32 rel_types,
127       const content::Referrer& referrer,
128       const gfx::Size& size);
129
130   // Adds a prerender for |url| if valid. As the prerender request is coming
131   // from a source without a RenderViewHost (i.e., the omnibox) we don't have a
132   // child or route id, or a referrer. This method uses sensible values for
133   // those. The |session_storage_namespace| matches the namespace of the active
134   // tab at the time the prerender is generated from the omnibox. Returns a
135   // caller-owned PrerenderHandle*, or NULL.
136   PrerenderHandle* AddPrerenderFromOmnibox(
137       const GURL& url,
138       content::SessionStorageNamespace* session_storage_namespace,
139       const gfx::Size& size);
140
141   PrerenderHandle* AddPrerenderFromLocalPredictor(
142       const GURL& url,
143       content::SessionStorageNamespace* session_storage_namespace,
144       const gfx::Size& size);
145
146   PrerenderHandle* AddPrerenderFromExternalRequest(
147       const GURL& url,
148       const content::Referrer& referrer,
149       content::SessionStorageNamespace* session_storage_namespace,
150       const gfx::Size& size);
151
152   // Adds a prerender for Instant Search |url| if valid. The
153   // |session_storage_namespace| matches the namespace of the active tab at the
154   // time the prerender is generated. Returns a caller-owned PrerenderHandle* or
155   // NULL.
156   PrerenderHandle* AddPrerenderForInstant(
157       const GURL& url,
158       content::SessionStorageNamespace* session_storage_namespace,
159       const gfx::Size& size);
160
161   // Cancels all active prerenders.
162   void CancelAllPrerenders();
163
164   // If |url| matches a valid prerendered page and |params| are compatible, try
165   // to swap it and merge browsing histories. Returns |true| and updates
166   // |params->target_contents| if a prerendered page is swapped in, |false|
167   // otherwise.
168   bool MaybeUsePrerenderedPage(const GURL& url,
169                                chrome::NavigateParams* params);
170
171   // Moves a PrerenderContents to the pending delete list from the list of
172   // active prerenders when prerendering should be cancelled.
173   virtual void MoveEntryToPendingDelete(PrerenderContents* entry,
174                                         FinalStatus final_status);
175
176   // Records the page load time for a prerender that wasn't swapped in.
177   void RecordPageLoadTimeNotSwappedIn(Origin origin,
178                                       base::TimeDelta page_load_time,
179                                       const GURL& url);
180
181   // Records the perceived page load time for a page - effectively the time from
182   // when the user navigates to a page to when it finishes loading. The actual
183   // load may have started prior to navigation due to prerender hints.
184   // This must be called on the UI thread.
185   // |fraction_plt_elapsed_at_swap_in| must either be in [0.0, 1.0], or a value
186   // outside that range indicating that it doesn't apply.
187   void RecordPerceivedPageLoadTime(
188       Origin origin,
189       NavigationType navigation_type,
190       base::TimeDelta perceived_page_load_time,
191       double fraction_plt_elapsed_at_swap_in,
192       const GURL& url);
193
194   // Set whether prerendering is currently enabled for this manager.
195   // Must be called on the UI thread.
196   // If |enabled| is false, existing prerendered pages will still persist until
197   // they time out, but new ones will not be generated.
198   void set_enabled(bool enabled);
199
200   static PrerenderManagerMode GetMode();
201   static void SetMode(PrerenderManagerMode mode);
202   static const char* GetModeString();
203   static bool IsPrerenderingPossible();
204   static bool ActuallyPrerendering();
205   static bool IsControlGroup(uint8 experiment_id);
206   static bool IsNoUseGroup();
207
208   // Query the list of current prerender pages to see if the given web contents
209   // is prerendering a page. The optional parameter |origin| is an output
210   // parameter which, if a prerender is found, is set to the Origin of the
211   // prerender |web_contents|.
212   bool IsWebContentsPrerendering(const content::WebContents* web_contents,
213                                  Origin* origin) const;
214
215   // Whether the PrerenderManager has an active prerender with the given url and
216   // SessionStorageNamespace associated with the given WebContens.
217   bool HasPrerenderedUrl(GURL url, content::WebContents* web_contents) const;
218
219   // Returns the PrerenderContents object for the given web_contents, otherwise
220   // returns NULL. Note that the PrerenderContents may have been Destroy()ed,
221   // but not yet deleted.
222   PrerenderContents* GetPrerenderContents(
223       const content::WebContents* web_contents) const;
224
225   // Returns the PrerenderContents object for a given child_id, route_id pair,
226   // otherwise returns NULL. Note that the PrerenderContents may have been
227   // Destroy()ed, but not yet deleted.
228   virtual PrerenderContents* GetPrerenderContentsForRoute(
229       int child_id, int route_id) const;
230
231   // Returns a list of all WebContents being prerendered.
232   const std::vector<content::WebContents*> GetAllPrerenderingContents() const;
233
234   // Checks whether |url| has been recently navigated to.
235   bool HasRecentlyBeenNavigatedTo(Origin origin, const GURL& url);
236
237   // Returns true iff the method given is valid for prerendering.
238   static bool IsValidHttpMethod(const std::string& method);
239
240   // Returns true iff the scheme of the URL given is valid for prerendering.
241   static bool DoesURLHaveValidScheme(const GURL& url);
242
243   // Returns true iff the scheme of the subresource URL given is valid for
244   // prerendering.
245   static bool DoesSubresourceURLHaveValidScheme(const GURL& url);
246
247   // Returns a Value object containing the active pages being prerendered, and
248   // a history of pages which were prerendered. The caller is responsible for
249   // deleting the return value.
250   base::DictionaryValue* GetAsValue() const;
251
252   // Clears the data indicated by which bits of clear_flags are set.
253   //
254   // If the CLEAR_PRERENDER_CONTENTS bit is set, all active prerenders are
255   // cancelled and then deleted, and any WebContents queued for destruction are
256   // destroyed as well.
257   //
258   // If the CLEAR_PRERENDER_HISTORY bit is set, the prerender history is
259   // cleared, including any entries newly created by destroying them in
260   // response to the CLEAR_PRERENDER_CONTENTS flag.
261   //
262   // Intended to be used when clearing the cache or history.
263   void ClearData(int clear_flags);
264
265   // Record a final status of a prerendered page in a histogram.
266   // This variation allows specifying whether prerendering had been started
267   // (necessary to flag MatchComplete dummies).
268   void RecordFinalStatusWithMatchCompleteStatus(
269       Origin origin,
270       uint8 experiment_id,
271       PrerenderContents::MatchCompleteStatus mc_status,
272       FinalStatus final_status) const;
273
274   // Record a cookie status histogram (see prerender_histograms.h).
275   void RecordCookieStatus(Origin origin,
276                           uint8 experiment_id,
277                           int cookie_status) const;
278
279   // content::NotificationObserver
280   virtual void Observe(int type,
281                        const content::NotificationSource& source,
282                        const content::NotificationDetails& details) OVERRIDE;
283
284   // MediaCaptureDevicesDispatcher::Observer
285   virtual void OnCreatingAudioStream(int render_process_id,
286                                      int render_frame_id) OVERRIDE;
287
288   const Config& config() const { return config_; }
289   Config& mutable_config() { return config_; }
290
291   PrerenderTracker* prerender_tracker() { return prerender_tracker_; }
292
293   // Adds a condition. This is owned by the PrerenderManager.
294   void AddCondition(const PrerenderCondition* condition);
295
296   // Records that some visible tab navigated (or was redirected) to the
297   // provided URL.
298   void RecordNavigation(const GURL& url);
299
300   // Updates the LoggedInPredictor state to reflect that a login has likely
301   // on the URL provided.
302   void RecordLikelyLoginOnURL(const GURL& url);
303
304   // Checks if the LoggedInPredictor shows that the user is likely logged on
305   // to the site for the URL provided.
306   void CheckIfLikelyLoggedInOnURL(const GURL& url,
307                                   bool* lookup_result,
308                                   bool* database_was_present,
309                                   const base::Closure& result_cb);
310
311   void OnHistoryServiceDidQueryURL(Origin origin,
312                                    uint8 experiment_id,
313                                    CancelableRequestProvider::Handle handle,
314                                    bool success,
315                                    const history::URLRow* url_row,
316                                    history::VisitVector* visits);
317
318   Profile* profile() const { return profile_; }
319
320   // Classes which will be tested in prerender unit browser tests should use
321   // these methods to get times for comparison, so that the test framework can
322   // mock advancing/retarding time.
323   virtual base::Time GetCurrentTime() const;
324   virtual base::TimeTicks GetCurrentTimeTicks() const;
325
326   scoped_refptr<predictors::LoggedInPredictorTable>
327   logged_in_predictor_table() {
328     return logged_in_predictor_table_;
329   }
330
331   PrerenderLocalPredictor* local_predictor() {
332     return local_predictor_.get();
333   }
334
335   // Notification that a cookie event happened on a render frame. Will record a
336   // cookie event for a given render frame, if it is being prerendered.
337   // If cookies were sent, all cookies must be supplied in |cookie_list|.
338   static void RecordCookieEvent(int process_id,
339                                 int frame_id,
340                                 const GURL& url,
341                                 const GURL& frame_url,
342                                 PrerenderContents::CookieEvent event,
343                                 const net::CookieList* cookie_list);
344
345   // Arranges for all session storage merges to hang indefinitely. This is used
346   // to reliably test various swap abort cases.
347   static void HangSessionStorageMergesForTesting();
348
349   // Notification that a prerender has completed and its bytes should be
350   // recorded.
351   void RecordNetworkBytes(bool used, int64 prerender_bytes);
352
353   // Add to the running tally of bytes transferred over the network for this
354   // profile if prerendering is currently enabled.
355   void AddProfileNetworkBytesIfEnabled(int64 bytes);
356
357  protected:
358   class PendingSwap;
359   class PrerenderData : public base::SupportsWeakPtr<PrerenderData> {
360    public:
361     struct OrderByExpiryTime;
362
363     PrerenderData(PrerenderManager* manager,
364                   PrerenderContents* contents,
365                   base::TimeTicks expiry_time);
366
367     ~PrerenderData();
368
369     // Turn this PrerenderData into a Match Complete replacement for itself,
370     // placing the current prerender contents into |to_delete_prerenders_|.
371     void MakeIntoMatchCompleteReplacement();
372
373     // A new PrerenderHandle has been created for this PrerenderData.
374     void OnHandleCreated(PrerenderHandle* prerender_handle);
375
376     // The launcher associated with a handle is navigating away from the context
377     // that launched this prerender. If the prerender is active, it may stay
378     // alive briefly though, in case we we going through a redirect chain that
379     // will eventually land at it.
380     void OnHandleNavigatedAway(PrerenderHandle* prerender_handle);
381
382     // The launcher associated with a handle has taken explicit action to cancel
383     // this prerender. We may well destroy the prerender in this case if no
384     // other handles continue to track it.
385     void OnHandleCanceled(PrerenderHandle* prerender_handle);
386
387     PrerenderContents* contents() { return contents_.get(); }
388
389     PrerenderContents* ReleaseContents();
390
391     int handle_count() const { return handle_count_; }
392
393     base::TimeTicks expiry_time() const { return expiry_time_; }
394     void set_expiry_time(base::TimeTicks expiry_time) {
395       expiry_time_ = expiry_time;
396     }
397
398     void ClearPendingSwap();
399
400     PendingSwap* pending_swap() { return pending_swap_.get(); }
401     void set_pending_swap(PendingSwap* pending_swap) {
402       pending_swap_.reset(pending_swap);
403     }
404
405    private:
406     PrerenderManager* manager_;
407     scoped_ptr<PrerenderContents> contents_;
408
409     // The number of distinct PrerenderHandles created for |this|, including
410     // ones that have called PrerenderData::OnHandleNavigatedAway(), but not
411     // counting the ones that have called PrerenderData::OnHandleCanceled(). For
412     // pending prerenders, this will always be 1, since the PrerenderManager
413     // only merges handles of running prerenders.
414     int handle_count_;
415
416     // After this time, this prerender is no longer fresh, and should be
417     // removed.
418     base::TimeTicks expiry_time_;
419
420     // If a session storage namespace merge is in progress for this object,
421     // we need to keep track of various state associated with it.
422     scoped_ptr<PendingSwap> pending_swap_;
423
424     DISALLOW_COPY_AND_ASSIGN(PrerenderData);
425   };
426
427   // When a swap can't happen immediately, due to a sesison storage namespace
428   // merge, there will be a pending swap object while the merge is in
429   // progress. It retains all the data needed to do the merge, maintains
430   // throttles for the navigation in the target WebContents that needs to be
431   // delayed, and handles all conditions which would cancel a pending swap.
432   class PendingSwap : public content::WebContentsObserver {
433    public:
434     PendingSwap(PrerenderManager* manager,
435                 content::WebContents* target_contents,
436                 PrerenderData* prerender_data,
437                 const GURL& url,
438                 bool should_replace_current_entry);
439     virtual ~PendingSwap();
440
441     content::WebContents* target_contents() const;
442     void set_swap_successful(bool swap_successful) {
443       swap_successful_ = swap_successful;
444     }
445
446     void BeginSwap();
447
448     // content::WebContentsObserver implementation.
449     virtual void AboutToNavigateRenderView(
450         content::RenderViewHost* render_view_host) OVERRIDE;
451     virtual void ProvisionalChangeToMainFrameUrl(
452         const GURL& url,
453         content::RenderFrameHost* render_frame_host) OVERRIDE;
454     virtual void DidCommitProvisionalLoadForFrame(
455         int64 frame_id,
456         const base::string16& frame_unique_name,
457         bool is_main_frame,
458         const GURL& validated_url,
459         content::PageTransition transition_type,
460         content::RenderViewHost* render_view_host) OVERRIDE;
461     virtual void DidFailProvisionalLoad(
462         int64 frame_id,
463         const base::string16& frame_unique_name,
464         bool is_main_frame,
465         const GURL& validated_url,
466         int error_code,
467         const base::string16& error_description,
468         content::RenderViewHost* render_view_host) OVERRIDE;
469     virtual void WebContentsDestroyed(content::WebContents* web_contents)
470         OVERRIDE;
471
472    private:
473     void RecordEvent(PrerenderEvent event) const;
474
475     void OnMergeCompleted(content::SessionStorageNamespace::MergeResult result);
476     void OnMergeTimeout();
477
478     // Prerender parameters.
479     PrerenderManager* manager_;
480     PrerenderData* prerender_data_;
481     GURL url_;
482     bool should_replace_current_entry_;
483
484     base::TimeTicks start_time_;
485     PrerenderTracker::ChildRouteIdPair target_route_id_;
486     bool seen_target_route_id_;
487     base::OneShotTimer<PendingSwap> merge_timeout_;
488     bool swap_successful_;
489
490     base::WeakPtrFactory<PendingSwap> weak_factory_;
491   };
492
493   void SetPrerenderContentsFactory(
494       PrerenderContents::Factory* prerender_contents_factory);
495
496   // Called by a PrerenderData to signal that the launcher has navigated away
497   // from the context that launched the prerender. A user may have clicked
498   // a link in a page containing a <link rel=prerender> element, or the user
499   // might have committed an omnibox navigation. This is used to possibly
500   // shorten the TTL of the prerendered page.
501   void SourceNavigatedAway(PrerenderData* prerender_data);
502
503  private:
504   friend class ::InstantSearchPrerendererTest;
505   friend class PrerenderBrowserTest;
506   friend class PrerenderContents;
507   friend class PrerenderHandle;
508   friend class UnitTestPrerenderManager;
509
510   class OnCloseWebContentsDeleter;
511   struct NavigationRecord;
512
513   // Time interval before a new prerender is allowed.
514   static const int kMinTimeBetweenPrerendersMs = 500;
515
516   // Time window for which we record old navigations, in milliseconds.
517   static const int kNavigationRecordWindowMs = 5000;
518
519   void OnCancelPrerenderHandle(PrerenderData* prerender_data);
520
521   // Adds a prerender for |url| from |referrer| initiated from the process
522   // |child_id|. The |origin| specifies how the prerender was added. If |size|
523   // is empty, then PrerenderContents::StartPrerendering will instead use a
524   // default from PrerenderConfig. Returns a PrerenderHandle*, owned by the
525   // caller, or NULL.
526   PrerenderHandle* AddPrerender(
527       Origin origin,
528       int child_id,
529       const GURL& url,
530       const content::Referrer& referrer,
531       const gfx::Size& size,
532       content::SessionStorageNamespace* session_storage_namespace);
533
534   void StartSchedulingPeriodicCleanups();
535   void StopSchedulingPeriodicCleanups();
536
537   void EvictOldestPrerendersIfNecessary();
538
539   // Deletes stale and cancelled prerendered PrerenderContents, as well as
540   // WebContents that have been replaced by prerendered WebContents.
541   // Also identifies and kills PrerenderContents that use too much
542   // resources.
543   void PeriodicCleanup();
544
545   // Posts a task to call PeriodicCleanup.  Results in quicker destruction of
546   // objects.  If |this| is deleted before the task is run, the task will
547   // automatically be cancelled.
548   void PostCleanupTask();
549
550   base::TimeTicks GetExpiryTimeForNewPrerender(Origin origin) const;
551   base::TimeTicks GetExpiryTimeForNavigatedAwayPrerender() const;
552
553   void DeleteOldEntries();
554   virtual PrerenderContents* CreatePrerenderContents(
555       const GURL& url,
556       const content::Referrer& referrer,
557       Origin origin,
558       uint8 experiment_id);
559
560   // Insures the |active_prerenders_| are sorted by increasing expiry time. Call
561   // after every mutation of active_prerenders_ that can possibly make it
562   // unsorted (e.g. an insert, or changing an expiry time).
563   void SortActivePrerenders();
564
565   // Finds the active PrerenderData object for a running prerender matching
566   // |url| and |session_storage_namespace|.
567   PrerenderData* FindPrerenderData(
568       const GURL& url,
569       const content::SessionStorageNamespace* session_storage_namespace);
570
571   // Finds the active PrerenderData object currently in a PendingSwap for
572   // |target_contents|. Otherwise, returns NULL.
573   PrerenderData* FindPrerenderDataForTargetContents(
574       content::WebContents* target_contents);
575
576   // Given the |prerender_contents|, find the iterator in active_prerenders_
577   // correponding to the given prerender.
578   ScopedVector<PrerenderData>::iterator
579       FindIteratorForPrerenderContents(PrerenderContents* prerender_contents);
580
581   bool DoesRateLimitAllowPrerender(Origin origin) const;
582
583   // Deletes old WebContents that have been replaced by prerendered ones.  This
584   // is needed because they're replaced in a callback from the old WebContents,
585   // so cannot immediately be deleted.
586   void DeleteOldWebContents();
587
588   // Cleans up old NavigationRecord's.
589   void CleanUpOldNavigations();
590
591   // Arrange for the given WebContents to be deleted asap. If deleter is not
592   // NULL, deletes that as well.
593   void ScheduleDeleteOldWebContents(content::WebContents* tab,
594                                     OnCloseWebContentsDeleter* deleter);
595
596   // Adds to the history list.
597   void AddToHistory(PrerenderContents* contents);
598
599   // Returns a new Value representing the pages currently being prerendered. The
600   // caller is responsible for delete'ing the return value.
601   base::Value* GetActivePrerendersAsValue() const;
602
603   // Destroys all pending prerenders using FinalStatus.  Also deletes them as
604   // well as any swapped out WebContents queued for destruction.
605   // Used both on destruction, and when clearing the browsing history.
606   void DestroyAllContents(FinalStatus final_status);
607
608   // Helper function to destroy a PrerenderContents with the specified
609   // final_status, while at the same time recording that for the MatchComplete
610   // case, that this prerender would have been used.
611   void DestroyAndMarkMatchCompleteAsUsed(PrerenderContents* prerender_contents,
612                                          FinalStatus final_status);
613
614   // Record a final status of a prerendered page in a histogram.
615   // This is a helper function which will ultimately call
616   // RecordFinalStatusWthMatchCompleteStatus, using MATCH_COMPLETE_DEFAULT.
617   void RecordFinalStatus(Origin origin,
618                          uint8 experiment_id,
619                          FinalStatus final_status) const;
620
621   // Returns whether prerendering is currently enabled for this manager.
622   // Must be called on the UI thread.
623   bool IsEnabled() const;
624
625   void CookieChanged(ChromeCookieDetails* details);
626   void CookieChangedAnyCookiesLeftLookupResult(const std::string& domain_key,
627                                                bool cookies_exist);
628   void LoggedInPredictorDataReceived(scoped_ptr<LoggedInStateMap> new_map);
629
630   void RecordEvent(PrerenderContents* contents, PrerenderEvent event) const;
631
632   // Swaps a prerender |prerender_data| for |url| into the tab, replacing
633   // |web_contents|.  Returns the new WebContents that was swapped in, or NULL
634   // if a swap-in was not possible.  If |should_replace_current_entry| is true,
635   // the current history entry in |web_contents| is replaced.
636   content::WebContents* SwapInternal(const GURL& url,
637                                      content::WebContents* web_contents,
638                                      PrerenderData* prerender_data,
639                                      bool should_replace_current_entry);
640
641   // The configuration.
642   Config config_;
643
644   // Specifies whether prerendering is currently enabled for this
645   // manager. The value can change dynamically during the lifetime
646   // of the PrerenderManager.
647   bool enabled_;
648
649   // The profile that owns this PrerenderManager.
650   Profile* profile_;
651
652   PrerenderTracker* prerender_tracker_;
653
654   // All running prerenders. Sorted by expiry time, in ascending order.
655   ScopedVector<PrerenderData> active_prerenders_;
656
657   // Prerenders awaiting deletion.
658   ScopedVector<PrerenderData> to_delete_prerenders_;
659
660   // List of recent navigations in this profile, sorted by ascending
661   // navigate_time_.
662   std::list<NavigationRecord> navigations_;
663
664   scoped_ptr<PrerenderContents::Factory> prerender_contents_factory_;
665
666   static PrerenderManagerMode mode_;
667
668   // A count of how many prerenders we do per session. Initialized to 0 then
669   // incremented and emitted to a histogram on each successful prerender.
670   static int prerenders_per_session_count_;
671
672   // RepeatingTimer to perform periodic cleanups of pending prerendered
673   // pages.
674   base::RepeatingTimer<PrerenderManager> repeating_timer_;
675
676   // Track time of last prerender to limit prerender spam.
677   base::TimeTicks last_prerender_start_time_;
678
679   std::list<content::WebContents*> old_web_contents_list_;
680
681   ScopedVector<OnCloseWebContentsDeleter> on_close_web_contents_deleters_;
682
683   scoped_ptr<PrerenderHistory> prerender_history_;
684
685   std::list<const PrerenderCondition*> prerender_conditions_;
686
687   scoped_ptr<PrerenderHistograms> histograms_;
688
689   scoped_ptr<PrerenderLocalPredictor> local_predictor_;
690
691   scoped_refptr<predictors::LoggedInPredictorTable> logged_in_predictor_table_;
692
693   // Here, we keep the logged in predictor state, but potentially a superset
694   // of its actual (database-backed) state, since we do not incorporate
695   // browser data deletion. We do not use this for actual lookups, but only
696   // to query cookie data for domains we know there was a login before.
697   // This is required to avoid a large number of cookie lookups on bulk
698   // deletion of cookies.
699   scoped_ptr<LoggedInStateMap> logged_in_state_;
700
701   content::NotificationRegistrar notification_registrar_;
702
703   CancelableRequestConsumer query_url_consumer_;
704
705   // The number of bytes transferred over the network for the profile this
706   // PrerenderManager is attached to.
707   int64 profile_network_bytes_;
708
709   // The value of profile_network_bytes_ that was last recorded.
710   int64 last_recorded_profile_network_bytes_;
711
712   DISALLOW_COPY_AND_ASSIGN(PrerenderManager);
713 };
714
715 }  // namespace prerender
716
717 #endif  // CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_