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