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

Side by Side Diff: components/page_load_metrics/renderer/page_load_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: Instrumented NavigationHandle for IsSamePage, fixed broken logic and 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 "base/time/time.h"
6 #include "base/timer/timer.h"
7 #include "components/page_load_metrics/renderer/page_load_metrics_render_frame_o bserver.h"
8 #include "components/page_load_metrics/renderer/page_timing_metrics_sender.h"
9 #include "content/public/renderer/render_frame.h"
10 #include "third_party/WebKit/public/platform/WebURLResponse.h"
11 #include "third_party/WebKit/public/web/WebDataSource.h"
12 #include "third_party/WebKit/public/web/WebDocument.h"
13 #include "third_party/WebKit/public/web/WebLocalFrame.h"
14 #include "third_party/WebKit/public/web/WebPerformance.h"
15 #include "url/gurl.h"
16
17 namespace page_load_metrics {
18
19 MainRenderFrameObserver::MainRenderFrameObserver(
20 content::RenderFrame* render_frame)
21 : content::RenderFrameObserver(render_frame) {}
22
23 MainRenderFrameObserver::~MainRenderFrameObserver() {}
24
25 void MainRenderFrameObserver::OnStop() {
26 SendMetrics();
27 }
28
29 // TODO(bmcquade): use DidChangePerformanceTiming instead, once available.
30 // CL 1289053003
31 void MainRenderFrameObserver::DidFinishDocumentLoad() {
32 SendMetrics();
33 }
34
35 // TODO(bmcquade): use DidChangePerformanceTiming instead, once available.
36 // CL 1289053003
37 void MainRenderFrameObserver::DidFinishLoad() {
38 SendMetrics();
39 }
40
41 void MainRenderFrameObserver::DidFailProvisionalLoad(
42 const blink::WebURLError& e) {
43 SendMetrics();
44 }
45
46 void MainRenderFrameObserver::DidCommitProvisionalLoad(
47 bool is_new_navigation,
48 bool is_same_page_navigation) {
49 // Same-page navigations (e.g. an in-document navigation from a fragment
50 // link) aren't full page loads, since they don't go to network to load the
51 // main HTML resource. DidStartProvisionalLoad doesn't get invoked for same
52 // page navigations, so we may still have an active
53 // page_timing_metrics_sender_ at this point.
54 if (is_same_page_navigation)
55 return;
56
57 // We only create a PageTimingMetricsSender if the page meets the criteria for
58 // sending and recording metrics. Once page_timing_metrics_sender_ is
59 // non-null, we will send metrics for the current page at some later time, as
60 // those metrics become available.
61 if (ShouldSendMetrics()) {
Bryan McQuade 2015/09/04 20:53:18 can get rid of the braces here
Charlie Harrison 2015/09/08 23:05:15 Is this in the style guide? I think braces are goo
Bryan McQuade 2015/09/09 16:26:25 Ah, you're right - I had assumed any single-statem
62 page_timing_metrics_sender_.reset(
63 new PageTimingMetricsSender(this, routing_id(), CreateTimer()));
64 }
65 }
66
67 void MainRenderFrameObserver::SendMetrics() {
68 if (!page_timing_metrics_sender_)
69 return;
70
71 PageLoadTiming timing(GetTiming());
72 page_timing_metrics_sender_->Send(timing);
73 }
74
75 bool MainRenderFrameObserver::ShouldSendMetrics() const {
76 const blink::WebLocalFrame& frame = *render_frame()->GetWebFrame();
77 // We only generate historgrams for main frames.
78 if (frame.parent())
79 return false;
80
81 const blink::WebDocument& document = frame.document();
82 // Ignore non-HTTP schemes (e.g. chrome://).
83 const GURL& url = document.url();
84 if (!url.SchemeIsHTTPOrHTTPS())
85 return false;
86
87 const blink::WebURLResponse& url_response = frame.dataSource()->response();
88 // Ignore multipart responses (e.g. MHTML).
89 if (url_response.isMultipartPayload())
90 return false;
91
92 // Ignore non-HTML documents (e.g. SVG). Note that images are treated by
93 // Blink as HTML documents, so to exclude images, we must perform
94 // additional mime type checking below.
95 if (!document.isHTMLDocument() && !document.isXHTMLDocument())
96 return false;
97
98 // Ignore non-HTML mime types (e.g. images).
99 std::string mime_type = url_response.mimeType().utf8();
100 if (mime_type != "text/html" && mime_type != "application/xhtml+xml")
101 return false;
102
103 return true;
104 }
105
106 PageLoadTiming MainRenderFrameObserver::GetTiming() const {
107 blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
108 if (frame) {
Bryan McQuade 2015/09/04 20:53:18 once you fold the line below, can get rid of brace
Charlie Harrison 2015/09/08 23:05:15 Done.
109 blink::WebPerformance performance = frame->performance();
Bryan McQuade 2015/09/04 20:53:18 can get rid of this line and just fold it into the
Charlie Harrison 2015/09/08 23:05:15 Done.
110 return PageLoadTiming(performance);
111 }
112 return PageLoadTiming();
113 }
114
115 scoped_ptr<base::Timer> MainRenderFrameObserver::CreateTimer() const {
116 return scoped_ptr<base::Timer>(
117 new base::OneShotTimer<PageTimingMetricsSender>());
Bryan McQuade 2015/09/04 20:53:18 I never fully understood how the template paramete
Charlie Harrison 2015/09/08 23:05:15 This is the right way to do it, we need the class
118 }
119
120 } // namespace page_load_metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698