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 will add the object as an observer to the passed in |
| 43 // observable. |
| 44 explicit PageLoadMetricsObserver(PageLoadMetricsObservable* observable); |
| 45 |
| 46 // The page load started, with the given navigation handle. |
| 47 virtual void OnStart(content::NavigationHandle* navigation_handle) {} |
42 | 48 |
43 // OnRedirect is triggered when a page load redirects to another URL. | 49 // OnRedirect is triggered when a page load redirects to another URL. |
44 // The navigation handle holds relevant data for the navigation, but will | 50 // 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. | 51 // be destroyed soon after this call. Don't hold a reference to it. This can |
| 52 // be called multiple times. |
46 virtual void OnRedirect(content::NavigationHandle* navigation_handle) {} | 53 virtual void OnRedirect(content::NavigationHandle* navigation_handle) {} |
47 | 54 |
48 // OnCommit is triggered when a page load commits, i.e. when we receive the | 55 // 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 | 56 // 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 | 57 // the navigation, but will be destroyed soon after this call. Don't hold a |
51 // reference to it. | 58 // reference to it. |
52 virtual void OnCommit(content::NavigationHandle* navigation_handle) {} | 59 virtual void OnCommit(content::NavigationHandle* navigation_handle) {} |
53 | 60 |
54 // OnComplete is triggered when we are ready to record metrics for this page | 61 // 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 | 62 // load. This will happen some time after commit. The PageLoadTiming struct |
56 // long as we've received some data about the page load. The | 63 // contains timing data and the PageLoadExtraInfo struct contains other useful |
57 // PageLoadTiming struct contains timing data and the PageLoadExtraInfo struct | 64 // data collected over the course of the page load. If the load did not |
58 // contains other useful information about the tab backgrounding/foregrounding | 65 // receive any timing information, |timing.IsEmpty()| will be true. |
59 // over the course of the page load. | 66 // OnPageLoadMetricsGoingAway will be called after OnComplete. |
60 virtual void OnComplete(const PageLoadTiming& timing, | 67 virtual void OnComplete(const PageLoadTiming& timing, |
61 const PageLoadExtraInfo& extra_info) {} | 68 const PageLoadExtraInfo& extra_info) {} |
62 | 69 |
63 // This is called when the WebContents we are observing is tearing down. No | 70 // This is called when the PageLoadMetricsObservable we are observing is |
64 // further callbacks will be triggered. | 71 // tearing down. No further callbacks will be triggered. If the observer has |
65 virtual void OnPageLoadMetricsGoingAway() {} | 72 // longer lifetime than the observable, it should override this method and |
| 73 // call RemoveObserver() at this point. |
| 74 virtual void OnPageLoadMetricsGoingAway(); |
| 75 |
| 76 // Derived objects should use this method to destroy themselves. This will |
| 77 // properly remove the observer from the observable. |
| 78 void Destroy(); |
| 79 |
| 80 // Call these methods start and stop observing if the derived class does not |
| 81 // share a lifetime with the PageLoadMetricsObservable. |
| 82 void Observe(PageLoadMetricsObservable* observable); |
| 83 void StopObserving(); |
| 84 |
| 85 protected: |
| 86 virtual ~PageLoadMetricsObserver() {} |
| 87 |
| 88 private: |
| 89 PageLoadMetricsObservable* observable_; |
| 90 DISALLOW_COPY_AND_ASSIGN(PageLoadMetricsObserver); |
66 }; | 91 }; |
67 | 92 |
68 // Class which handles notifying observers when loads commit and complete. It | 93 // Class which handles notifying observers when loads commit and complete. |
69 // must call OnPageLoadMetricsGoingAway in the destructor. | |
70 class PageLoadMetricsObservable { | 94 class PageLoadMetricsObservable { |
71 public: | 95 public: |
72 virtual ~PageLoadMetricsObservable() {} | 96 virtual ~PageLoadMetricsObservable() {} |
73 virtual void AddObserver(PageLoadMetricsObserver* observer) = 0; | 97 virtual void AddObserver(PageLoadMetricsObserver* observer) = 0; |
74 virtual void RemoveObserver(PageLoadMetricsObserver* observer) = 0; | 98 virtual void RemoveObserver(PageLoadMetricsObserver* observer) = 0; |
75 }; | 99 }; |
76 | 100 |
77 } // namespace page_load_metrics | 101 } // namespace page_load_metrics |
78 | 102 |
79 #endif // COMPONENTS_PAGE_LOAD_METRICS_BROWSER_PAGE_LOAD_METRICS_OBSERVER_H_ | 103 #endif // COMPONENTS_PAGE_LOAD_METRICS_BROWSER_PAGE_LOAD_METRICS_OBSERVER_H_ |
OLD | NEW |