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

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: Add DidFinishNavigation handler and remove unneeded DCHECK 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 // TODO(bmcquade): use DidChangePerformanceTiming instead, once available.
26 // CL 1289053003
27 void MainRenderFrameObserver::DidFinishDocumentLoad() {
28 SendMetrics();
29 }
30
31 // TODO(bmcquade): use DidChangePerformanceTiming instead, once available.
32 // CL 1289053003
33 void MainRenderFrameObserver::DidFinishLoad() {
34 SendMetrics();
35 }
36
37 void MainRenderFrameObserver::DidCommitProvisionalLoad(
38 bool is_new_navigation,
39 bool is_same_page_navigation) {
40 // Same-page navigations (e.g. an in-document navigation from a fragment
41 // link) aren't full page loads, since they don't go to network to load the
42 // main HTML resource. DidStartProvisionalLoad doesn't get invoked for same
43 // page navigations, so we may still have an active
44 // page_timing_metrics_sender_ at this point.
45 if (is_same_page_navigation)
46 return;
47
48 // We only create a PageTimingMetricsSender if the page meets the criteria for
49 // sending and recording metrics. Once page_timing_metrics_sender_ is
50 // non-null, we will send metrics for the current page at some later time, as
51 // those metrics become available.
52 if (ShouldSendMetrics())
53 page_timing_metrics_sender_.reset(
54 new PageTimingMetricsSender(this, routing_id(), CreateTimer()));
55 }
56
57 void MainRenderFrameObserver::SendMetrics() {
58 if (!page_timing_metrics_sender_)
59 return;
60
61 PageLoadTiming timing(GetTiming());
62 page_timing_metrics_sender_->Send(timing);
63 }
64
65 bool MainRenderFrameObserver::ShouldSendMetrics() const {
66 const blink::WebLocalFrame& frame = *render_frame()->GetWebFrame();
67 // We only generate historgrams for main frames.
68 if (frame.parent())
69 return false;
70
71 const blink::WebDocument& document = frame.document();
72 // Ignore non-HTTP schemes (e.g. chrome://).
73 const GURL& url = document.url();
74 if (!url.SchemeIsHTTPOrHTTPS())
75 return false;
76
77 const blink::WebURLResponse& url_response = frame.dataSource()->response();
78 // Ignore multipart responses (e.g. MHTML).
79 if (url_response.isMultipartPayload())
80 return false;
81
82 // Ignore non-HTML documents (e.g. SVG). Note that images are treated by
83 // Blink as HTML documents, so to exclude images, we must perform
84 // additional mime type checking below.
85 if (!document.isHTMLDocument() && !document.isXHTMLDocument())
86 return false;
87
88 // Ignore non-HTML mime types (e.g. images).
89 std::string mime_type = url_response.mimeType().utf8();
90 if (mime_type != "text/html" && mime_type != "application/xhtml+xml")
91 return false;
92
93 return true;
94 }
95
96 PageLoadTiming MainRenderFrameObserver::GetTiming() const {
97 blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
98 if (frame) {
99 blink::WebPerformance performance = frame->performance();
100 return PageLoadTiming(performance);
101 }
102 return PageLoadTiming();
103 }
104
105 scoped_ptr<base::Timer> MainRenderFrameObserver::CreateTimer() const {
106 return scoped_ptr<base::Timer>(
107 new base::OneShotTimer<PageTimingMetricsSender>());
108 }
109
110 } // namespace page_load_metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698