Chromium Code Reviews| Index: components/page_load_metrics/renderer/page_load_metrics_render_frame_observer.cc |
| diff --git a/components/page_load_metrics/renderer/page_load_metrics_render_frame_observer.cc b/components/page_load_metrics/renderer/page_load_metrics_render_frame_observer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9169a335e370cb5eefecbd63c1a8141b95b9c640 |
| --- /dev/null |
| +++ b/components/page_load_metrics/renderer/page_load_metrics_render_frame_observer.cc |
| @@ -0,0 +1,128 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/time/time.h" |
| +#include "base/timer/timer.h" |
| +#include "components/page_load_metrics/renderer/page_load_metrics_render_frame_observer.h" |
| +#include "components/page_load_metrics/renderer/page_timing_metrics_sender.h" |
| +#include "content/public/renderer/render_frame.h" |
| +#include "third_party/WebKit/public/platform/WebURLResponse.h" |
| +#include "third_party/WebKit/public/web/WebDataSource.h" |
| +#include "third_party/WebKit/public/web/WebDocument.h" |
| +#include "third_party/WebKit/public/web/WebLocalFrame.h" |
| +#include "third_party/WebKit/public/web/WebPerformance.h" |
| +#include "url/gurl.h" |
| + |
| +namespace page_load_metrics { |
| + |
| +PageLoadMetricsRenderFrameObserver::PageLoadMetricsRenderFrameObserver( |
| + content::RenderFrame* render_frame) |
| + : content::RenderFrameObserver(render_frame) {} |
| + |
| +PageLoadMetricsRenderFrameObserver::~PageLoadMetricsRenderFrameObserver() {} |
| + |
| +// TODO(bmcquade): use DidChangePerformanceTiming instead, once available. |
| +void PageLoadMetricsRenderFrameObserver::DidFinishDocumentLoad() { |
| + SendMetrics(); |
| +} |
| + |
| +// TODO(bmcquade): use DidChangePerformanceTiming instead, once available. |
| +void PageLoadMetricsRenderFrameObserver::DidFinishLoad() { |
| + SendMetrics(); |
| +} |
| + |
| +void PageLoadMetricsRenderFrameObserver::DidStartProvisionalLoad() { |
| + // A provisional page load has started. Shut down metrics reporting until the |
| + // next page load is committed. Note that a provisoinal load may fail. We |
| + // simply stop reporting metrics in that case, until the next successful page |
| + // load is committed. |
| + page_timing_metrics_sender_.reset(); |
| +} |
| + |
| +void PageLoadMetricsRenderFrameObserver::DidCommitProvisionalLoad( |
| + bool is_new_navigation, bool is_same_page_navigation) { |
| + // Same-page navigations (e.g. an in-document navigation from a fragment |
| + // link) aren't full page loads, since they don't go to network to load the |
| + // main HTML resource. DidStartProvisionalLoad doesn't get invoked for same |
| + // page navigations, so we may still have an active |
| + // page_timing_metrics_sender_ at this point. |
| + if (is_same_page_navigation) |
| + return; |
| + |
| + // TODO(bmcquade): what about data: and blob: URLs? |
| + DCHECK(!page_timing_metrics_sender_); |
| + |
| + // We only create a PageTimingMetricsSender if the page meets the criteria for |
| + // sending and recording metrics. Once page_timing_metrics_sender_ is |
| + // non-null, we will send metrics for the current page at some later time, as |
| + // 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
|
| + if (ShouldSendMetrics()) |
| + page_timing_metrics_sender_.reset( |
| + new PageTimingMetricsSender(this, routing_id(), CreateTimer())); |
| +} |
| + |
| +void PageLoadMetricsRenderFrameObserver::SendMetrics() { |
| + if (!page_timing_metrics_sender_) |
| + return; |
| + |
| + PageLoadTiming timing(GetTiming()); |
| + page_timing_metrics_sender_->Send(timing); |
| +} |
| + |
| +bool PageLoadMetricsRenderFrameObserver::ShouldSendMetrics() const { |
| + const blink::WebLocalFrame& frame = *render_frame()->GetWebFrame(); |
| + // We only generate historgrams for main frames. |
| + if (frame.parent()) |
| + return false; |
| + |
| + const blink::WebDocument& document = frame.document(); |
| + // Ignore non-HTTP schemes (e.g. chrome://). |
| + const GURL& url = document.url(); |
| + if (!url.SchemeIsHTTPOrHTTPS()) |
| + return false; |
| + |
| + const blink::WebURLResponse& url_response = frame.dataSource()->response(); |
| + // Ignore multipart responses (e.g. MHTML). |
| + if (url_response.isMultipartPayload()) |
| + return false; |
| + |
| + // Ignore non-HTML documents (e.g. SVG). Note that images are treated by |
| + // Blink as HTML documents, so to exclude images, we must perform |
| + // additional mime type checking below. |
| + if (!document.isHTMLDocument() && !document.isXHTMLDocument()) |
| + return false; |
| + |
| + // Ignore non-HTML mime types (e.g. images). |
| + std::string mime_type = url_response.mimeType().utf8(); |
| + if (mime_type != "text/html" && mime_type != "application/xhtml+xml") |
| + return false; |
| + |
| + return true; |
| +} |
| + |
| +PageLoadTiming PageLoadMetricsRenderFrameObserver::GetTiming() const { |
| + PageLoadTiming timing; |
| + blink::WebLocalFrame* frame = render_frame()->GetWebFrame(); |
| + if (frame) { |
| + blink::WebPerformance performance = frame->performance(); |
| + 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
|
| + base::Time::FromDoubleT(performance.navigationStart()); |
| + timing.response_start = |
| + base::Time::FromDoubleT(performance.responseStart()); |
| + timing.dom_content_loaded_event_start = |
| + base::Time::FromDoubleT(performance.domContentLoadedEventStart()); |
| + timing.load_event_start = |
| + base::Time::FromDoubleT(performance.loadEventStart()); |
| + timing.first_layout = base::Time::FromDoubleT(performance.firstLayout()); |
| + } |
| + return timing; |
| +} |
| + |
| +scoped_ptr<base::Timer> |
| +PageLoadMetricsRenderFrameObserver::CreateTimer() const { |
| + return scoped_ptr<base::Timer>( |
| + new base::OneShotTimer<PageTimingMetricsSender>()); |
| +} |
| + |
| +} // namespace page_load_metrics |