Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: chrome/browser/page_load_metrics/metrics_web_contents_observer.h

Issue 2847513002: Break page load metrics test dependency on IPC. (Closed)
Patch Set: add expectation in wait method Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 CHROME_BROWSER_PAGE_LOAD_METRICS_METRICS_WEB_CONTENTS_OBSERVER_H_ 5 #ifndef CHROME_BROWSER_PAGE_LOAD_METRICS_METRICS_WEB_CONTENTS_OBSERVER_H_
6 #define CHROME_BROWSER_PAGE_LOAD_METRICS_METRICS_WEB_CONTENTS_OBSERVER_H_ 6 #define CHROME_BROWSER_PAGE_LOAD_METRICS_METRICS_WEB_CONTENTS_OBSERVER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/observer_list.h"
13 #include "base/time/time.h" 14 #include "base/time/time.h"
14 #include "chrome/browser/page_load_metrics/page_load_metrics_observer.h" 15 #include "chrome/browser/page_load_metrics/page_load_metrics_observer.h"
15 #include "chrome/common/page_load_metrics/page_load_timing.h" 16 #include "chrome/common/page_load_metrics/page_load_timing.h"
16 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data .h" 17 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data .h"
17 #include "content/public/browser/render_widget_host.h" 18 #include "content/public/browser/render_widget_host.h"
18 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
19 #include "content/public/browser/web_contents_observer.h" 20 #include "content/public/browser/web_contents_observer.h"
20 #include "content/public/browser/web_contents_user_data.h" 21 #include "content/public/browser/web_contents_user_data.h"
21 #include "content/public/common/resource_type.h" 22 #include "content/public/common/resource_type.h"
22 #include "third_party/WebKit/public/platform/WebInputEvent.h" 23 #include "third_party/WebKit/public/platform/WebInputEvent.h"
(...skipping 13 matching lines...) Expand all
36 class PageLoadTracker; 37 class PageLoadTracker;
37 38
38 // MetricsWebContentsObserver tracks page loads and loading metrics 39 // MetricsWebContentsObserver tracks page loads and loading metrics
39 // related data based on IPC messages received from a 40 // related data based on IPC messages received from a
40 // MetricsRenderFrameObserver. 41 // MetricsRenderFrameObserver.
41 class MetricsWebContentsObserver 42 class MetricsWebContentsObserver
42 : public content::WebContentsObserver, 43 : public content::WebContentsObserver,
43 public content::WebContentsUserData<MetricsWebContentsObserver>, 44 public content::WebContentsUserData<MetricsWebContentsObserver>,
44 public content::RenderWidgetHost::InputEventObserver { 45 public content::RenderWidgetHost::InputEventObserver {
45 public: 46 public:
47 // TestingObserver allows tests to observe MetricsWebContentsObserver state
48 // changes. Tests may use TestingObserver to wait until certain state changes,
49 // such as the arrivial of PageLoadTiming messages from the render process,
50 // have been observed.
51 class TestingObserver {
52 public:
53 explicit TestingObserver(content::WebContents* web_contents);
54 virtual ~TestingObserver();
55
56 void OnGoingAway();
57
58 // Invoked when a new PageLoadTiming update has been received and processed.
59 virtual void OnTimingUpdated(const PageLoadTiming& timing,
60 const PageLoadMetadata& metadata) {}
61
62 private:
63 page_load_metrics::MetricsWebContentsObserver* observer_;
64
65 DISALLOW_COPY_AND_ASSIGN(TestingObserver);
66 };
67
46 // Note that the returned metrics is owned by the web contents. 68 // Note that the returned metrics is owned by the web contents.
47 static MetricsWebContentsObserver* CreateForWebContents( 69 static MetricsWebContentsObserver* CreateForWebContents(
48 content::WebContents* web_contents, 70 content::WebContents* web_contents,
49 std::unique_ptr<PageLoadMetricsEmbedderInterface> embedder_interface); 71 std::unique_ptr<PageLoadMetricsEmbedderInterface> embedder_interface);
50 MetricsWebContentsObserver( 72 MetricsWebContentsObserver(
51 content::WebContents* web_contents, 73 content::WebContents* web_contents,
52 std::unique_ptr<PageLoadMetricsEmbedderInterface> embedder_interface); 74 std::unique_ptr<PageLoadMetricsEmbedderInterface> embedder_interface);
53 ~MetricsWebContentsObserver() override; 75 ~MetricsWebContentsObserver() override;
54 76
55 // content::WebContentsObserver implementation: 77 // content::WebContentsObserver implementation:
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 118
97 // Flush any buffered metrics, as part of the metrics subsystem persisting 119 // Flush any buffered metrics, as part of the metrics subsystem persisting
98 // metrics as the application goes into the background. The application may be 120 // metrics as the application goes into the background. The application may be
99 // killed at any time after this method is invoked without further 121 // killed at any time after this method is invoked without further
100 // notification. 122 // notification.
101 void FlushMetricsOnAppEnterBackground(); 123 void FlushMetricsOnAppEnterBackground();
102 124
103 // This getter function is required for testing. 125 // This getter function is required for testing.
104 const PageLoadExtraInfo GetPageLoadExtraInfoForCommittedLoad(); 126 const PageLoadExtraInfo GetPageLoadExtraInfoForCommittedLoad();
105 127
128 // Register / unregister TestingObservers. Should only be called from tests.
129 void AddTestingObserver(TestingObserver* observer);
130 void RemoveTestingObserver(TestingObserver* observer);
131
132 // public only for testing
133 void OnTimingUpdated(content::RenderFrameHost* render_frame_host,
134 const PageLoadTiming& timing,
135 const PageLoadMetadata& metadata);
136
106 private: 137 private:
107 friend class content::WebContentsUserData<MetricsWebContentsObserver>; 138 friend class content::WebContentsUserData<MetricsWebContentsObserver>;
108 139
109 void HandleFailedNavigationForTrackedLoad( 140 void HandleFailedNavigationForTrackedLoad(
110 content::NavigationHandle* navigation_handle, 141 content::NavigationHandle* navigation_handle,
111 std::unique_ptr<PageLoadTracker> tracker); 142 std::unique_ptr<PageLoadTracker> tracker);
112 143
113 void HandleCommittedNavigationForTrackedLoad( 144 void HandleCommittedNavigationForTrackedLoad(
114 content::NavigationHandle* navigation_handle, 145 content::NavigationHandle* navigation_handle,
115 std::unique_ptr<PageLoadTracker> tracker); 146 std::unique_ptr<PageLoadTracker> tracker);
(...skipping 20 matching lines...) Expand all
136 void UnregisterInputEventObserver(content::RenderViewHost* host); 167 void UnregisterInputEventObserver(content::RenderViewHost* host);
137 168
138 // Notify aborted provisional loads that a new navigation occurred. This is 169 // Notify aborted provisional loads that a new navigation occurred. This is
139 // used for more consistent attribution tracking for aborted provisional 170 // used for more consistent attribution tracking for aborted provisional
140 // loads. This method returns the provisional load that was likely aborted 171 // loads. This method returns the provisional load that was likely aborted
141 // by this navigation, to help instantiate the new PageLoadTracker. 172 // by this navigation, to help instantiate the new PageLoadTracker.
142 std::unique_ptr<PageLoadTracker> NotifyAbortedProvisionalLoadsNewNavigation( 173 std::unique_ptr<PageLoadTracker> NotifyAbortedProvisionalLoadsNewNavigation(
143 content::NavigationHandle* new_navigation, 174 content::NavigationHandle* new_navigation,
144 UserInitiatedInfo user_initiated_info); 175 UserInitiatedInfo user_initiated_info);
145 176
146 void OnTimingUpdated(content::RenderFrameHost*,
147 const PageLoadTiming& timing,
148 const PageLoadMetadata& metadata);
149
150 bool ShouldTrackNavigation( 177 bool ShouldTrackNavigation(
151 content::NavigationHandle* navigation_handle) const; 178 content::NavigationHandle* navigation_handle) const;
152 179
153 // True if the web contents is currently in the foreground. 180 // True if the web contents is currently in the foreground.
154 bool in_foreground_; 181 bool in_foreground_;
155 182
156 // The PageLoadTrackers must be deleted before the |embedder_interface_|, 183 // The PageLoadTrackers must be deleted before the |embedder_interface_|,
157 // because they hold a pointer to the |embedder_interface_|. 184 // because they hold a pointer to the |embedder_interface_|.
158 std::unique_ptr<PageLoadMetricsEmbedderInterface> embedder_interface_; 185 std::unique_ptr<PageLoadMetricsEmbedderInterface> embedder_interface_;
159 186
160 // This map tracks all of the navigations ongoing that are not committed 187 // This map tracks all of the navigations ongoing that are not committed
161 // yet. Once a navigation is committed, it moves from the map to 188 // yet. Once a navigation is committed, it moves from the map to
162 // committed_load_. Note that a PageLoadTrackers NavigationHandle is only 189 // committed_load_. Note that a PageLoadTrackers NavigationHandle is only
163 // valid until commit time, when we remove it from the map. 190 // valid until commit time, when we remove it from the map.
164 std::map<content::NavigationHandle*, std::unique_ptr<PageLoadTracker>> 191 std::map<content::NavigationHandle*, std::unique_ptr<PageLoadTracker>>
165 provisional_loads_; 192 provisional_loads_;
166 193
167 // Tracks aborted provisional loads for a little bit longer than usual (one 194 // Tracks aborted provisional loads for a little bit longer than usual (one
168 // more navigation commit at the max), in order to better understand how the 195 // more navigation commit at the max), in order to better understand how the
169 // navigation failed. This is because most provisional loads are destroyed 196 // navigation failed. This is because most provisional loads are destroyed
170 // and vanish before we get signal about what caused the abort (new 197 // and vanish before we get signal about what caused the abort (new
171 // navigation, stop button, etc.). 198 // navigation, stop button, etc.).
172 std::vector<std::unique_ptr<PageLoadTracker>> aborted_provisional_loads_; 199 std::vector<std::unique_ptr<PageLoadTracker>> aborted_provisional_loads_;
173 200
174 std::unique_ptr<PageLoadTracker> committed_load_; 201 std::unique_ptr<PageLoadTracker> committed_load_;
175 202
176 // Has the MWCO observed at least one navigation? 203 // Has the MWCO observed at least one navigation?
177 bool has_navigated_; 204 bool has_navigated_;
178 205
206 base::ObserverList<TestingObserver> testing_observers_;
207
179 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserver); 208 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserver);
180 }; 209 };
181 210
182 } // namespace page_load_metrics 211 } // namespace page_load_metrics
183 212
184 #endif // CHROME_BROWSER_PAGE_LOAD_METRICS_METRICS_WEB_CONTENTS_OBSERVER_H_ 213 #endif // CHROME_BROWSER_PAGE_LOAD_METRICS_METRICS_WEB_CONTENTS_OBSERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698