OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_PAGE_LOAD_METRICS_BROWSER_PAGE_LOAD_METRICS_OBSERVER_H_ | 5 #ifndef COMPONENTS_PAGE_LOAD_METRICS_BROWSER_PAGE_LOAD_METRICS_OBSERVER_H_ |
6 #define COMPONENTS_PAGE_LOAD_METRICS_BROWSER_PAGE_LOAD_METRICS_OBSERVER_H_ | 6 #define COMPONENTS_PAGE_LOAD_METRICS_BROWSER_PAGE_LOAD_METRICS_OBSERVER_H_ |
7 | 7 |
8 #include "base/macros.h" | |
8 #include "components/page_load_metrics/common/page_load_timing.h" | 9 #include "components/page_load_metrics/common/page_load_timing.h" |
9 #include "content/public/browser/navigation_handle.h" | 10 #include "content/public/browser/navigation_handle.h" |
10 | 11 |
11 namespace page_load_metrics { | 12 namespace page_load_metrics { |
12 | 13 |
14 class PageLoadMetricsObservable; | |
15 | |
13 struct PageLoadExtraInfo { | 16 struct PageLoadExtraInfo { |
14 PageLoadExtraInfo(const base::TimeDelta& first_background_time, | 17 PageLoadExtraInfo(const base::TimeDelta& first_background_time, |
15 const base::TimeDelta& first_foreground_time, | 18 const base::TimeDelta& first_foreground_time, |
16 bool started_in_foreground); | 19 bool started_in_foreground, |
20 bool has_commit); | |
17 | 21 |
18 // Returns the time to first background if the page load started in the | 22 // Returns the time to first background if the page load started in the |
19 // foreground. If the page has not been backgrounded, or the page started in | 23 // foreground. If the page has not been backgrounded, or the page started in |
20 // the background, this will be base::TimeDelta(). | 24 // the background, this will be base::TimeDelta(). |
21 const base::TimeDelta first_background_time; | 25 const base::TimeDelta first_background_time; |
22 | 26 |
23 // Returns the time to first foreground if the page load started in the | 27 // Returns the time to first foreground if the page load started in the |
24 // background. If the page has not been foregrounded, or the page started in | 28 // background. If the page has not been foregrounded, or the page started in |
25 // the foreground, this will be base::TimeDelta(). | 29 // the foreground, this will be base::TimeDelta(). |
26 const base::TimeDelta first_foreground_time; | 30 const base::TimeDelta first_foreground_time; |
27 | 31 |
28 // True if the page load started in the foreground. | 32 // True if the page load started in the foreground. |
29 const bool started_in_foreground; | 33 const bool started_in_foreground; |
34 | |
35 // True if the page load committed and received its first bytes of data. | |
36 const bool has_commit; | |
30 }; | 37 }; |
31 | 38 |
32 // Interface for PageLoadMetrics observers. Note that it might be possible for | 39 // Interface for PageLoadMetrics observers. |
33 // OnCommit to be fired without a subsequent OnComplete (i.e. if an error | |
34 // occurs or we don't have any metrics to log). PageLoadMetricsObservers are | |
35 // required to stop observing (call PageLoadMetricsObservable::RemoveObserver) | |
36 // some time before the OnPageLoadMetricsGoingAway trigger finishes. If this | |
37 // class is destroyed before the PageLoadMetricsObservable, it should call | |
38 // RemoveObserver in its destructor. | |
39 class PageLoadMetricsObserver { | 40 class PageLoadMetricsObserver { |
40 public: | 41 public: |
41 virtual ~PageLoadMetricsObserver() {} | 42 // This constructor should be used if the derived class will not start |
43 // observing a PageLoadMetricsObservable at creation time. | |
44 PageLoadMetricsObserver(); | |
45 | |
46 // This constructor will add the object as an observer to the passed in | |
47 // observable. | |
48 explicit PageLoadMetricsObserver(PageLoadMetricsObservable* observable); | |
49 | |
50 // The page load started, with the given navigation handle. | |
51 virtual void OnStart(content::NavigationHandle* navigation_handle) {} | |
42 | 52 |
43 // OnRedirect is triggered when a page load redirects to another URL. | 53 // OnRedirect is triggered when a page load redirects to another URL. |
44 // The navigation handle holds relevant data for the navigation, but will | 54 // The navigation handle holds relevant data for the navigation, but will |
45 // be destroyed soon after this call. Don't hold a reference to it. | 55 // be destroyed soon after this call. Don't hold a reference to it. This can |
56 // be called multiple times. | |
46 virtual void OnRedirect(content::NavigationHandle* navigation_handle) {} | 57 virtual void OnRedirect(content::NavigationHandle* navigation_handle) {} |
47 | 58 |
48 // OnCommit is triggered when a page load commits, i.e. when we receive the | 59 // OnCommit is triggered when a page load commits, i.e. when we receive the |
49 // first data for the request. The navigation handle holds relevant data for | 60 // first data for the request. The navigation handle holds relevant data for |
50 // the navigation, but will be destroyed soon after this call. Don't hold a | 61 // the navigation, but will be destroyed soon after this call. Don't hold a |
51 // reference to it. | 62 // reference to it. |
52 virtual void OnCommit(content::NavigationHandle* navigation_handle) {} | 63 virtual void OnCommit(content::NavigationHandle* navigation_handle) {} |
53 | 64 |
54 // OnComplete is triggered when we are ready to record metrics for this page | 65 // OnComplete is triggered when we are ready to record metrics for this page |
55 // load. This will happen some time after commit, and will be triggered as | 66 // load. This will happen some time after commit. The PageLoadTiming struct |
56 // long as we've received some data about the page load. The | 67 // contains timing data and the PageLoadExtraInfo struct contains other useful |
57 // PageLoadTiming struct contains timing data and the PageLoadExtraInfo struct | 68 // data collected over the course of the page load. If the load did not |
58 // contains other useful information about the tab backgrounding/foregrounding | 69 // receive any timing information, |timing.IsEmpty()| will be true. |
59 // over the course of the page load. | 70 // OnPageLoadMetricsGoingAway will be called after OnComplete. |
60 virtual void OnComplete(const PageLoadTiming& timing, | 71 virtual void OnComplete(const PageLoadTiming& timing, |
61 const PageLoadExtraInfo& extra_info) {} | 72 const PageLoadExtraInfo& extra_info) {} |
62 | 73 |
63 // This is called when the WebContents we are observing is tearing down. No | 74 // This is called when the PageLoadMetricsObservable we are observing is |
64 // further callbacks will be triggered. | 75 // tearing down. No further callbacks will be triggered. If the observer has |
65 virtual void OnPageLoadMetricsGoingAway() {} | 76 // longer lifetime than the observable, it should override this method and |
77 // call RemoveObserver() at this point. | |
78 virtual void OnPageLoadMetricsGoingAway(); | |
79 | |
80 // Derived objects should use this method to destroy themselves. This will | |
81 // properly remove the observer from the observable. | |
82 void Destroy(); | |
83 | |
84 // Call these methods start and stop observing if the derived class does not | |
85 // share a lifetime with the PageLoadMetricsObservable. | |
86 void Observe(PageLoadMetricsObservable* observable); | |
Bryan McQuade
2015/11/30 19:18:48
I know WC exposes a similar method, but do we need
| |
87 void StopObserving(); | |
88 | |
89 protected: | |
90 virtual ~PageLoadMetricsObserver() {} | |
91 | |
92 private: | |
93 PageLoadMetricsObservable* observable_; | |
94 DISALLOW_COPY_AND_ASSIGN(PageLoadMetricsObserver); | |
66 }; | 95 }; |
67 | 96 |
68 // Class which handles notifying observers when loads commit and complete. It | 97 // Class which handles notifying observers when loads commit and complete. |
69 // must call OnPageLoadMetricsGoingAway in the destructor. | |
70 class PageLoadMetricsObservable { | 98 class PageLoadMetricsObservable { |
71 public: | 99 public: |
72 virtual ~PageLoadMetricsObservable() {} | 100 virtual ~PageLoadMetricsObservable() {} |
73 virtual void AddObserver(PageLoadMetricsObserver* observer) = 0; | 101 virtual void AddObserver(PageLoadMetricsObserver* observer) = 0; |
74 virtual void RemoveObserver(PageLoadMetricsObserver* observer) = 0; | 102 virtual void RemoveObserver(PageLoadMetricsObserver* observer) = 0; |
75 }; | 103 }; |
76 | 104 |
77 } // namespace page_load_metrics | 105 } // namespace page_load_metrics |
78 | 106 |
79 #endif // COMPONENTS_PAGE_LOAD_METRICS_BROWSER_PAGE_LOAD_METRICS_OBSERVER_H_ | 107 #endif // COMPONENTS_PAGE_LOAD_METRICS_BROWSER_PAGE_LOAD_METRICS_OBSERVER_H_ |
OLD | NEW |