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.
5 #ifndef CHROME_BROWSER_PRERENDER_PRERENDER_TAB_HELPER_H_
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_TAB_HELPER_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/time/time.h"
11 #include "chrome/browser/prerender/prerender_histograms.h"
12 #include "chrome/browser/prerender/prerender_origin.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "content/public/browser/web_contents_user_data.h"
17 class PasswordManager
;
25 class PrerenderManager
;
27 // PrerenderTabHelper is responsible for recording perceived pageload times
28 // to compare PLT's with prerendering enabled and disabled.
29 class PrerenderTabHelper
30 : public content::WebContentsObserver
,
31 public content::WebContentsUserData
<PrerenderTabHelper
> {
34 EVENT_LOGGED_IN_TABLE_REQUESTED
= 0,
35 EVENT_LOGGED_IN_TABLE_PRESENT
= 1,
36 EVENT_MAINFRAME_CHANGE
= 2,
37 EVENT_MAINFRAME_CHANGE_DOMAIN_LOGGED_IN
= 3,
38 EVENT_MAINFRAME_COMMIT
= 4,
39 EVENT_MAINFRAME_COMMIT_DOMAIN_LOGGED_IN
= 5,
40 EVENT_LOGIN_ACTION_ADDED
= 6,
41 EVENT_LOGIN_ACTION_ADDED_PW_EMPTY
= 7,
45 static void CreateForWebContentsWithPasswordManager(
46 content::WebContents
* web_contents
,
47 PasswordManager
* password_manager
);
49 virtual ~PrerenderTabHelper();
51 // content::WebContentsObserver implementation.
52 virtual void ProvisionalChangeToMainFrameUrl(
54 content::RenderFrameHost
* render_frame_host
) OVERRIDE
;
55 virtual void DidStopLoading(
56 content::RenderViewHost
* render_view_host
) OVERRIDE
;
57 virtual void DidStartProvisionalLoadForFrame(
59 int64 parent_frame_id
,
61 const GURL
& validated_url
,
63 bool is_iframe_srcdoc
,
64 content::RenderViewHost
* render_view_host
) OVERRIDE
;
65 virtual void DidCommitProvisionalLoadForFrame(
67 const base::string16
& frame_unique_name
,
69 const GURL
& validated_url
,
70 content::PageTransition transition_type
,
71 content::RenderViewHost
* render_view_host
) OVERRIDE
;
73 // Called when a password form has been submitted.
74 void PasswordSubmitted(const autofill::PasswordForm
& form
);
76 // Called when this prerendered WebContents has just been swapped in.
77 void PrerenderSwappedIn();
79 // Called when a control prerender is resolved. Applies to the next load.
80 void WouldHavePrerenderedNextLoad(Origin origin
);
83 PrerenderTabHelper(content::WebContents
* web_contents
,
84 PasswordManager
* password_manager
);
85 friend class content::WebContentsUserData
<PrerenderTabHelper
>;
87 void RecordEvent(Event event
) const;
88 void RecordEventIfLoggedInURL(Event event
, const GURL
& url
);
89 void RecordEventIfLoggedInURLResult(Event event
, scoped_ptr
<bool> is_present
,
90 scoped_ptr
<bool> lookup_succeeded
);
92 void RecordPerceivedPageLoadTime(
93 base::TimeDelta perceived_page_load_time
,
94 double fraction_plt_elapsed_at_swap_in
);
96 // Retrieves the PrerenderManager, or NULL, if none was found.
97 PrerenderManager
* MaybeGetPrerenderManager() const;
99 // Returns whether the WebContents being observed is currently prerendering.
100 bool IsPrerendering();
102 // The type the current pending navigation, if there is one. If the tab is a
103 // prerender before swap, the value is always NAVIGATION_TYPE_PRERENDERED,
104 // even if the prerender is not currently loading.
105 NavigationType navigation_type_
;
107 // If |navigation_type_| is not NAVIGATION_TYPE_NORMAL, the origin of the
108 // relevant prerender. Otherwise, ORIGIN_NONE.
111 // True if the next load will be associated with a control prerender. This
112 // extra state is needed because control prerenders are resolved before the
113 // actual load begins. |next_load_origin_| gives the origin of the control
115 bool next_load_is_control_prerender_
;
116 Origin next_load_origin_
;
118 // System time at which the current load was started for the purpose of
119 // the perceived page load time (PPLT). If null, there is no current
121 base::TimeTicks pplt_load_start_
;
123 // System time at which the actual pageload started (pre-swapin), if
124 // a applicable (in cases when a prerender that was still loading was
126 base::TimeTicks actual_load_start_
;
128 // Current URL being loaded.
131 base::WeakPtrFactory
<PrerenderTabHelper
> weak_factory_
;
133 DISALLOW_COPY_AND_ASSIGN(PrerenderTabHelper
);
136 } // namespace prerender
138 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_TAB_HELPER_H_