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

Side by Side Diff: components/page_load_metrics/renderer/metrics_render_frame_observer.cc

Issue 1312213010: PageLoadMetrics renderer and browser implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Relax DCHECK constraints slightly for tests Created 5 years, 3 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/page_load_metrics/renderer/metrics_render_frame_observer.h"
6
7 #include "base/time/time.h"
8 #include "base/timer/timer.h"
9 #include "components/page_load_metrics/renderer/page_timing_metrics_sender.h"
10 #include "content/public/renderer/render_frame.h"
11 #include "third_party/WebKit/public/platform/WebURLResponse.h"
12 #include "third_party/WebKit/public/web/WebDataSource.h"
13 #include "third_party/WebKit/public/web/WebDocument.h"
14 #include "third_party/WebKit/public/web/WebLocalFrame.h"
15 #include "third_party/WebKit/public/web/WebPerformance.h"
16 #include "url/gurl.h"
17
18 namespace page_load_metrics {
19
20 namespace {
21
22 base::TimeDelta ClampDelta(double event_ms, double start_ms) {
23 double delta = event_ms - start_ms;
24 return base::TimeDelta::FromMillisecondsD(delta < 0 ? 0 : delta);
25 }
26
27 } // namespace
28
29 MetricsRenderFrameObserver::MetricsRenderFrameObserver(
30 content::RenderFrame* render_frame)
31 : content::RenderFrameObserver(render_frame) {}
32
33 MetricsRenderFrameObserver::~MetricsRenderFrameObserver() {}
34
35 void MetricsRenderFrameObserver::DidChangePerformanceTiming() {
36 SendMetrics();
37 }
38
39 void MetricsRenderFrameObserver::DidCommitProvisionalLoad(
40 bool is_new_navigation,
41 bool is_same_page_navigation) {
42 // Same-page navigations (e.g. an in-document navigation from a fragment
43 // link) aren't full page loads, since they don't go to network to load the
44 // main HTML resource. DidStartProvisionalLoad doesn't get invoked for same
45 // page navigations, so we may still have an active
46 // page_timing_metrics_sender_ at this point.
47 if (is_same_page_navigation)
48 return;
49
50 // Make sure to release the sender for a previous navigation, if we have one.
51 page_timing_metrics_sender_.reset();
52
53 // We only create a PageTimingMetricsSender if the page meets the criteria for
54 // sending and recording metrics. Once page_timing_metrics_sender_ is
55 // non-null, we will send metrics for the current page at some later time, as
56 // those metrics become available.
57 if (ShouldSendMetrics()) {
58 page_timing_metrics_sender_.reset(
59 new PageTimingMetricsSender(this, routing_id(), CreateTimer()));
60 }
61 }
62
63 void MetricsRenderFrameObserver::SendMetrics() {
64 if (!page_timing_metrics_sender_)
65 return;
66
67 PageLoadTiming timing(GetTiming());
68 page_timing_metrics_sender_->Send(timing);
69 }
70
71 bool MetricsRenderFrameObserver::ShouldSendMetrics() const {
72 const blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
73 // We only generate historgrams for main frames.
74 if (frame->parent())
75 return false;
76
77 const blink::WebDocument& document = frame->document();
78 // Ignore non-HTTP schemes (e.g. chrome://).
79 const GURL& url = document.url();
80 if (!url.SchemeIsHTTPOrHTTPS())
81 return false;
82
83 const blink::WebURLResponse& url_response = frame->dataSource()->response();
84 // Ignore multipart responses (e.g. MHTML).
85 if (url_response.isMultipartPayload())
86 return false;
87
88 // Ignore non-HTML documents (e.g. SVG). Note that images are treated by
89 // Blink as HTML documents, so to exclude images, we must perform
90 // additional mime type checking below.
91 if (!document.isHTMLDocument() && !document.isXHTMLDocument())
92 return false;
93
94 // Ignore non-HTML mime types (e.g. images).
95 std::string mime_type = url_response.mimeType().utf8();
96 if (mime_type != "text/html" && mime_type != "application/xhtml+xml")
97 return false;
98
99 return true;
100 }
101
102 PageLoadTiming MetricsRenderFrameObserver::GetTiming() const {
103 const blink::WebPerformance& perf =
104 render_frame()->GetWebFrame()->performance();
105
106 PageLoadTiming timing;
107 double start = perf.navigationStart();
108 timing.navigation_start = base::Time::FromDoubleT(start);
109 timing.response_start = ClampDelta(perf.responseStart(), start);
110 timing.dom_content_loaded_event_start =
111 ClampDelta(perf.domContentLoadedEventStart(), start);
112 timing.load_event_start = ClampDelta(perf.loadEventStart(), start);
113 timing.first_layout = ClampDelta(perf.firstLayout(), start);
114 return timing;
115 }
116
117 scoped_ptr<base::Timer> MetricsRenderFrameObserver::CreateTimer() const {
118 return scoped_ptr<base::Timer>(
119 new base::OneShotTimer<PageTimingMetricsSender>());
120 }
121
122 } // namespace page_load_metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698