Chromium Code Reviews| 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 PageLoadMetricsRenderFrameObserver::PageLoadMetricsRenderFrameObserver( | |
| 20 content::RenderFrame* render_frame) | |
| 21 : content::RenderFrameObserver(render_frame) {} | |
| 22 | |
| 23 PageLoadMetricsRenderFrameObserver::~PageLoadMetricsRenderFrameObserver() {} | |
| 24 | |
| 25 // TODO(bmcquade): use DidChangePerformanceTiming instead, once available. | |
| 26 void PageLoadMetricsRenderFrameObserver::DidFinishDocumentLoad() { | |
| 27 SendMetrics(); | |
| 28 } | |
| 29 | |
| 30 // TODO(bmcquade): use DidChangePerformanceTiming instead, once available. | |
| 31 void PageLoadMetricsRenderFrameObserver::DidFinishLoad() { | |
| 32 SendMetrics(); | |
| 33 } | |
| 34 | |
| 35 void PageLoadMetricsRenderFrameObserver::DidStartProvisionalLoad() { | |
| 36 // A provisional page load has started. Shut down metrics reporting until the | |
| 37 // next page load is committed. Note that a provisoinal load may fail. We | |
| 38 // simply stop reporting metrics in that case, until the next successful page | |
| 39 // load is committed. | |
| 40 page_timing_metrics_sender_.reset(); | |
| 41 } | |
| 42 | |
| 43 void PageLoadMetricsRenderFrameObserver::DidCommitProvisionalLoad( | |
| 44 bool is_new_navigation, bool is_same_page_navigation) { | |
| 45 // Same-page navigations (e.g. an in-document navigation from a fragment | |
| 46 // link) aren't full page loads, since they don't go to network to load the | |
| 47 // main HTML resource. DidStartProvisionalLoad doesn't get invoked for same | |
| 48 // page navigations, so we may still have an active | |
| 49 // page_timing_metrics_sender_ at this point. | |
| 50 if (is_same_page_navigation) | |
| 51 return; | |
| 52 | |
| 53 // TODO(bmcquade): what about data: and blob: URLs? | |
| 54 DCHECK(!page_timing_metrics_sender_); | |
| 55 | |
| 56 // We only create a PageTimingMetricsSender if the page meets the criteria for | |
| 57 // sending and recording metrics. Once page_timing_metrics_sender_ is | |
| 58 // non-null, we will send metrics for the current page at some later time, as | |
| 59 // those metrics become available. | |
|
nasko
2015/08/26 17:27:24
You won't get all the events in 1 second after the
Bryan McQuade
2015/08/26 18:28:00
Though we use a OneShotTimer, the timer gets resta
| |
| 60 if (ShouldSendMetrics()) | |
| 61 page_timing_metrics_sender_.reset( | |
| 62 new PageTimingMetricsSender(this, routing_id(), CreateTimer())); | |
| 63 } | |
| 64 | |
| 65 void PageLoadMetricsRenderFrameObserver::SendMetrics() { | |
| 66 if (!page_timing_metrics_sender_) | |
| 67 return; | |
| 68 | |
| 69 PageLoadTiming timing(GetTiming()); | |
| 70 page_timing_metrics_sender_->Send(timing); | |
| 71 } | |
| 72 | |
| 73 bool PageLoadMetricsRenderFrameObserver::ShouldSendMetrics() const { | |
| 74 const blink::WebLocalFrame& frame = *render_frame()->GetWebFrame(); | |
| 75 // We only generate historgrams for main frames. | |
| 76 if (frame.parent()) | |
| 77 return false; | |
| 78 | |
| 79 const blink::WebDocument& document = frame.document(); | |
| 80 // Ignore non-HTTP schemes (e.g. chrome://). | |
| 81 const GURL& url = document.url(); | |
| 82 if (!url.SchemeIsHTTPOrHTTPS()) | |
| 83 return false; | |
| 84 | |
| 85 const blink::WebURLResponse& url_response = frame.dataSource()->response(); | |
| 86 // Ignore multipart responses (e.g. MHTML). | |
| 87 if (url_response.isMultipartPayload()) | |
| 88 return false; | |
| 89 | |
| 90 // Ignore non-HTML documents (e.g. SVG). Note that images are treated by | |
| 91 // Blink as HTML documents, so to exclude images, we must perform | |
| 92 // additional mime type checking below. | |
| 93 if (!document.isHTMLDocument() && !document.isXHTMLDocument()) | |
| 94 return false; | |
| 95 | |
| 96 // Ignore non-HTML mime types (e.g. images). | |
| 97 std::string mime_type = url_response.mimeType().utf8(); | |
| 98 if (mime_type != "text/html" && mime_type != "application/xhtml+xml") | |
| 99 return false; | |
| 100 | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 PageLoadTiming PageLoadMetricsRenderFrameObserver::GetTiming() const { | |
| 105 PageLoadTiming timing; | |
| 106 blink::WebLocalFrame* frame = render_frame()->GetWebFrame(); | |
| 107 if (frame) { | |
| 108 blink::WebPerformance performance = frame->performance(); | |
| 109 timing.navigation_start = | |
|
nasko
2015/08/26 17:27:24
Why not create a copy constructor for PageLoadTimi
Bryan McQuade
2015/08/26 18:28:00
+1 to copy constructor that takes a WebPerformance
| |
| 110 base::Time::FromDoubleT(performance.navigationStart()); | |
| 111 timing.response_start = | |
| 112 base::Time::FromDoubleT(performance.responseStart()); | |
| 113 timing.dom_content_loaded_event_start = | |
| 114 base::Time::FromDoubleT(performance.domContentLoadedEventStart()); | |
| 115 timing.load_event_start = | |
| 116 base::Time::FromDoubleT(performance.loadEventStart()); | |
| 117 timing.first_layout = base::Time::FromDoubleT(performance.firstLayout()); | |
| 118 } | |
| 119 return timing; | |
| 120 } | |
| 121 | |
| 122 scoped_ptr<base::Timer> | |
| 123 PageLoadMetricsRenderFrameObserver::CreateTimer() const { | |
| 124 return scoped_ptr<base::Timer>( | |
| 125 new base::OneShotTimer<PageTimingMetricsSender>()); | |
| 126 } | |
| 127 | |
| 128 } // namespace page_load_metrics | |
| OLD | NEW |