Home | History | Annotate | Download | only in prerender
      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_TAB_HELPER_H_
      6 #define CHROME_BROWSER_PRERENDER_PRERENDER_TAB_HELPER_H_
      7 
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/memory/weak_ptr.h"
     10 #include "base/time/time.h"
     11 #include "content/public/browser/web_contents_observer.h"
     12 #include "content/public/browser/web_contents_user_data.h"
     13 #include "url/gurl.h"
     14 
     15 namespace predictors {
     16 class LoggedInPredictorTable;
     17 }
     18 
     19 namespace prerender {
     20 
     21 class PrerenderManager;
     22 
     23 // PrerenderTabHelper is responsible for recording perceived pageload times
     24 // to compare PLT's with prerendering enabled and disabled.
     25 class PrerenderTabHelper
     26     : public content::WebContentsObserver,
     27       public content::WebContentsUserData<PrerenderTabHelper> {
     28  public:
     29   enum Event {
     30     EVENT_LOGGED_IN_TABLE_REQUESTED = 0,
     31     EVENT_LOGGED_IN_TABLE_PRESENT = 1,
     32     EVENT_MAINFRAME_CHANGE = 2,
     33     EVENT_MAINFRAME_CHANGE_DOMAIN_LOGGED_IN = 3,
     34     EVENT_MAINFRAME_COMMIT = 4,
     35     EVENT_MAINFRAME_COMMIT_DOMAIN_LOGGED_IN = 5,
     36     EVENT_LOGIN_ACTION_ADDED = 6,
     37     EVENT_LOGIN_ACTION_ADDED_MAINFRAME = 7,
     38     EVENT_LOGIN_ACTION_ADDED_MAINFRAME_PW_EMPTY = 8,
     39     EVENT_LOGIN_ACTION_ADDED_SUBFRAME = 9,
     40     EVENT_LOGIN_ACTION_ADDED_SUBFRAME_PW_EMPTY = 10,
     41     EVENT_MAX_VALUE
     42   };
     43 
     44   virtual ~PrerenderTabHelper();
     45 
     46   // content::WebContentsObserver implementation.
     47   virtual void ProvisionalChangeToMainFrameUrl(
     48       const GURL& url,
     49       content::RenderViewHost* render_view_host) OVERRIDE;
     50   virtual void DidStopLoading(
     51       content::RenderViewHost* render_view_host) OVERRIDE;
     52   virtual void DidStartProvisionalLoadForFrame(
     53       int64 frame_id,
     54       int64 parent_frame_id,
     55       bool is_main_frame,
     56       const GURL& validated_url,
     57       bool is_error_page,
     58       bool is_iframe_srcdoc,
     59       content::RenderViewHost* render_view_host) OVERRIDE;
     60   virtual void DidCommitProvisionalLoadForFrame(
     61       int64 frame_id,
     62       bool is_main_frame,
     63       const GURL& validated_url,
     64       content::PageTransition transition_type,
     65       content::RenderViewHost* render_view_host) OVERRIDE;
     66   virtual void DidNavigateAnyFrame(
     67       const content::LoadCommittedDetails& details,
     68       const content::FrameNavigateParams& params) OVERRIDE;
     69 
     70   // Called when this prerendered WebContents has just been swapped in.
     71   void PrerenderSwappedIn();
     72 
     73  private:
     74   explicit PrerenderTabHelper(content::WebContents* web_contents);
     75   friend class content::WebContentsUserData<PrerenderTabHelper>;
     76 
     77   void RecordEvent(Event event) const;
     78   void RecordEventIfLoggedInURL(Event event, const GURL& url);
     79   void RecordEventIfLoggedInURLResult(Event event, scoped_ptr<bool> is_present,
     80                                       scoped_ptr<bool> lookup_succeeded);
     81   // Helper class to compute pixel-based stats on the paint progress
     82   // between when a prerendered page is swapped in and when the onload event
     83   // fires.
     84   class PixelStats;
     85   scoped_ptr<PixelStats> pixel_stats_;
     86 
     87   // Retrieves the PrerenderManager, or NULL, if none was found.
     88   PrerenderManager* MaybeGetPrerenderManager() const;
     89 
     90   // Returns whether the WebContents being observed is currently prerendering.
     91   bool IsPrerendering();
     92 
     93   // Returns whether the WebContents being observed was prerendered.
     94   bool IsPrerendered();
     95 
     96   // System time at which the current load was started for the purpose of
     97   // the perceived page load time (PPLT).
     98   base::TimeTicks pplt_load_start_;
     99 
    100   // System time at which the actual pageload started (pre-swapin), if
    101   // a applicable (in cases when a prerender that was still loading was
    102   // swapped in).
    103   base::TimeTicks actual_load_start_;
    104 
    105   // Current URL being loaded.
    106   GURL url_;
    107 
    108   base::WeakPtrFactory<PrerenderTabHelper> weak_factory_;
    109 
    110   DISALLOW_COPY_AND_ASSIGN(PrerenderTabHelper);
    111 };
    112 
    113 }  // namespace prerender
    114 
    115 #endif  // CHROME_BROWSER_PRERENDER_PRERENDER_TAB_HELPER_H_
    116