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

Unified Diff: components/page_load_metrics/renderer/page_load_metrics_render_frame_observer.cc

Issue 1282303002: PageLoadMetrics renderer and browser implementation (deprecated) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
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..6f85ce96e4bf341f63e1fd591559ea0986d22b3b
--- /dev/null
+++ b/components/page_load_metrics/renderer/page_load_metrics_render_frame_observer.cc
@@ -0,0 +1,225 @@
+// 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 "components/page_load_metrics/common/page_load_metrics_messages.h"
+#include "components/page_load_metrics/renderer/page_load_metrics_render_frame_observer.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"
+
+using base::Time;
+
+namespace page_load_metrics {
+
+namespace {
+
+// PerformanceTiming is the concrete implementation of the Timing interface
+// that gets its timing data from a blink::WebPerformance instance.
+class PerformanceTiming : public PageLoadMetricsRenderFrameObserver::Timing {
+ public:
+ explicit PerformanceTiming(const blink::WebPerformance& performance)
+ : performance_(performance) {}
+
+ Time GetNavigationStart() const override {
+ return Time::FromDoubleT(performance_.navigationStart());
+ }
+
+ Time GetDOMContentLoadedEvent() const override {
+ return Time::FromDoubleT(performance_.domContentLoadedEventStart());
+ }
+
+ Time GetLoadEvent() const override {
+ return Time::FromDoubleT(performance_.loadEventStart());
+ }
+
+ Time GetFirstLayout() const override {
+ return Time::FromDoubleT(performance_.firstLayout());
+ }
+
+ private:
+ blink::WebPerformance performance_;
+
+ DISALLOW_COPY_AND_ASSIGN(PerformanceTiming);
+};
+
+} // namespace
+
+// PageMetricsSender is a helper class responsible for sending page load
+// metrics over the lifetime of a webpage. A PageMetricsSender instance is
+// active from the time a provisional load is committed, until the time the
+// provisional load for the next page is started.
+class PageLoadMetricsRenderFrameObserver::PageMetricsSender {
+ public:
+ explicit PageMetricsSender(PageLoadMetricsRenderFrameObserver* parent);
+ void MaybeSendMetrics(const Timing& timing);
+
+ private:
+ void Send(IPC::Message* message) { parent_->Send(message); }
+ int routing_id() const { return parent_->routing_id(); }
+
+ void MaybeSendFirstLayout(const Timing& timing);
+ void MaybeSendDOMContentLoadedEvent(const Timing& timing);
+ void MaybeSendLoadEvent(const Timing& timing);
+
+ PageLoadMetricsRenderFrameObserver* const parent_;
nasko 2015/08/20 00:22:18 Parent has a very well defined meaning with frames
Bryan McQuade 2015/08/25 13:53:36 This got refactored so we no longer have a parent_
+ bool did_send_first_layout_;
+ bool did_send_dom_content_loaded_event_;
+ bool did_send_load_event_;
+};
+
+PageLoadMetricsRenderFrameObserver::PageLoadMetricsRenderFrameObserver(
+ content::RenderFrame* render_frame)
+ : content::RenderFrameObserver(render_frame) {}
+
+PageLoadMetricsRenderFrameObserver::~PageLoadMetricsRenderFrameObserver() {}
+
+// TODO(bmcquade): use DidChangePerformanceTiming instead, once available.
+void PageLoadMetricsRenderFrameObserver::DidFinishDocumentLoad() {
+ MaybeSendMetrics();
+}
+
+// TODO(bmcquade): use DidChangePerformanceTiming instead, once available.
+void PageLoadMetricsRenderFrameObserver::DidFinishLoad() {
+ MaybeSendMetrics();
+}
+
+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_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
nasko 2015/08/20 00:22:18 Do we only care about network loads? If so, we nee
Bryan McQuade 2015/08/25 13:53:36 Ah, interesting. Are you saying that DidStartProvi
+ // page navigations, so we may still have an active page_metrics_sender_ at
+ // this point.
+ if (is_same_page_navigation)
+ return;
+
+ DCHECK(!page_metrics_sender_);
+
+ // We only create a PageMetricsSender if the page meets the criteria for
+ // sending and recording metrics. Once page_metrics_sender_ is non-null,
+ // we will send metrics for the current page at some later time, as those
+ // metrics become available.
+ if (ShouldSendMetrics())
+ page_metrics_sender_.reset(new PageMetricsSender(this));
+}
+
+void PageLoadMetricsRenderFrameObserver::MaybeSendMetrics() {
+ if (!page_metrics_sender_)
+ return;
+
+ scoped_ptr<Timing> timing(CreateTiming());
+ page_metrics_sender_->MaybeSendMetrics(*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;
+}
+
+scoped_ptr<PageLoadMetricsRenderFrameObserver::Timing>
+PageLoadMetricsRenderFrameObserver::CreateTiming() {
+ blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
+ scoped_ptr<Timing> timing(new PerformanceTiming(
+ frame ? frame->performance() : blink::WebPerformance()));
+ return timing.Pass();
+}
+
+PageLoadMetricsRenderFrameObserver::PageMetricsSender::PageMetricsSender(
+ PageLoadMetricsRenderFrameObserver* parent)
+ : parent_(parent),
+ did_send_first_layout_(false),
+ did_send_dom_content_loaded_event_(false),
+ did_send_load_event_(false) {}
+
+void PageLoadMetricsRenderFrameObserver::PageMetricsSender::MaybeSendMetrics(
+ const Timing& timing) {
+ MaybeSendFirstLayout(timing);
+ MaybeSendDOMContentLoadedEvent(timing);
+ MaybeSendLoadEvent(timing);
+}
+
+void PageLoadMetricsRenderFrameObserver::PageMetricsSender::
+ MaybeSendFirstLayout(const Timing& timing) {
+ if (did_send_first_layout_)
+ return;
+
+ if (timing.GetNavigationStart().is_null() ||
+ timing.GetFirstLayout().is_null())
+ return;
+
+ Send(new PageLoadMetricsMsg_FirstLayout(
+ routing_id(),
+ timing.GetFirstLayout() - timing.GetNavigationStart()));
+ did_send_first_layout_ = true;
+}
+
+void PageLoadMetricsRenderFrameObserver::PageMetricsSender::
+ MaybeSendDOMContentLoadedEvent(const Timing& timing) {
+ if (did_send_dom_content_loaded_event_)
+ return;
+
+ if (timing.GetNavigationStart().is_null() ||
+ timing.GetDOMContentLoadedEvent().is_null())
+ return;
+
+ Send(new PageLoadMetricsMsg_DOMContentLoadedEvent(
+ routing_id(),
+ timing.GetDOMContentLoadedEvent() - timing.GetNavigationStart()));
+ did_send_dom_content_loaded_event_ = true;
+}
+
+void PageLoadMetricsRenderFrameObserver::PageMetricsSender::MaybeSendLoadEvent(
+ const Timing& timing) {
+ if (did_send_load_event_)
+ return;
+
+ if (timing.GetNavigationStart().is_null() || timing.GetLoadEvent().is_null())
+ return;
+
+ Send(new PageLoadMetricsMsg_LoadEvent(
+ routing_id(),
+ timing.GetLoadEvent() - timing.GetNavigationStart()));
+ did_send_load_event_ = true;
+}
+
+} // namespace page_load_metrics

Powered by Google App Engine
This is Rietveld 408576698