Upstream version 11.40.277.0
[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 "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"
15 #include "url/gurl.h"
16
17 namespace autofill {
18 struct PasswordForm;
19 }
20
21 namespace password_manager {
22 class PasswordManager;
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       password_manager::PasswordManager* password_manager);
50
51   ~PrerenderTabHelper() override;
52
53   // content::WebContentsObserver implementation.
54   void DidGetRedirectForResourceRequest(
55       content::RenderViewHost* render_view_host,
56       const content::ResourceRedirectDetails& details) override;
57   void DidStopLoading(content::RenderViewHost* render_view_host) override;
58   void DidStartProvisionalLoadForFrame(
59       content::RenderFrameHost* render_frame_host,
60       const GURL& validated_url,
61       bool is_error_page,
62       bool is_iframe_srcdoc) override;
63   void DidCommitProvisionalLoadForFrame(
64       content::RenderFrameHost* render_frame_host,
65       const GURL& validated_url,
66       ui::PageTransition transition_type) override;
67
68   // Called when the URL of the main frame changed, either when the load
69   // commits, or a redirect happens.
70   void MainFrameUrlDidChange(const GURL& url);
71
72   // Called when a password form has been submitted.
73   void PasswordSubmitted(const autofill::PasswordForm& form);
74
75   // Called when this prerendered WebContents has just been swapped in.
76   void PrerenderSwappedIn();
77
78   // Called when a control prerender is resolved. Applies to the next load.
79   void WouldHavePrerenderedNextLoad(Origin origin);
80
81  private:
82   PrerenderTabHelper(content::WebContents* web_contents,
83                      password_manager::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
91   void RecordPerceivedPageLoadTime(
92       base::TimeDelta perceived_page_load_time,
93       double fraction_plt_elapsed_at_swap_in);
94
95   // Retrieves the PrerenderManager, or NULL, if none was found.
96   PrerenderManager* MaybeGetPrerenderManager() const;
97
98   // Returns whether the WebContents being observed is currently prerendering.
99   bool IsPrerendering();
100
101   // The type the current pending navigation, if there is one. If the tab is a
102   // prerender before swap, the value is always NAVIGATION_TYPE_PRERENDERED,
103   // even if the prerender is not currently loading.
104   NavigationType navigation_type_;
105
106   // If |navigation_type_| is not NAVIGATION_TYPE_NORMAL, the origin of the
107   // relevant prerender. Otherwise, ORIGIN_NONE.
108   Origin origin_;
109
110   // True if the next load will be associated with a control prerender. This
111   // extra state is needed because control prerenders are resolved before the
112   // actual load begins. |next_load_origin_| gives the origin of the control
113   // prerender.
114   bool next_load_is_control_prerender_;
115   Origin next_load_origin_;
116
117   // System time at which the current load was started for the purpose of
118   // the perceived page load time (PPLT). If null, there is no current
119   // load.
120   base::TimeTicks pplt_load_start_;
121
122   // System time at which the actual pageload started (pre-swapin), if
123   // a applicable (in cases when a prerender that was still loading was
124   // swapped in).
125   base::TimeTicks actual_load_start_;
126
127   // Current URL being loaded.
128   GURL url_;
129
130   base::WeakPtrFactory<PrerenderTabHelper> weak_factory_;
131
132   DISALLOW_COPY_AND_ASSIGN(PrerenderTabHelper);
133 };
134
135 }  // namespace prerender
136
137 #endif  // CHROME_BROWSER_PRERENDER_PRERENDER_TAB_HELPER_H_