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