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 "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::DidStartProvisionalLoad() { |
| 38 // A provisional page load has started. Shut down metrics reporting until the |
| 39 // next page load is committed. Note that a provisoinal load may fail. We |
| 40 // simply stop reporting metrics in that case, until the next successful page |
| 41 // load is committed. |
| 42 page_timing_metrics_sender_.reset(); |
| 43 } |
| 44 |
| 45 void MainRenderFrameObserver::DidCommitProvisionalLoad( |
| 46 bool is_new_navigation, |
| 47 bool is_same_page_navigation) { |
| 48 // Same-page navigations (e.g. an in-document navigation from a fragment |
| 49 // link) aren't full page loads, since they don't go to network to load the |
| 50 // main HTML resource. DidStartProvisionalLoad doesn't get invoked for same |
| 51 // page navigations, so we may still have an active |
| 52 // page_timing_metrics_sender_ at this point. |
| 53 if (is_same_page_navigation) |
| 54 return; |
| 55 |
| 56 DCHECK(!page_timing_metrics_sender_); |
| 57 |
| 58 // We only create a PageTimingMetricsSender if the page meets the criteria for |
| 59 // sending and recording metrics. Once page_timing_metrics_sender_ is |
| 60 // non-null, we will send metrics for the current page at some later time, as |
| 61 // those metrics become available. |
| 62 if (ShouldSendMetrics()) |
| 63 page_timing_metrics_sender_.reset( |
| 64 new PageTimingMetricsSender(this, routing_id(), CreateTimer())); |
| 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) { |
| 109 blink::WebPerformance performance = frame->performance(); |
| 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>()); |
| 118 } |
| 119 |
| 120 } // namespace page_load_metrics |
OLD | NEW |