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

Side by Side Diff: chrome/browser/page_load_metrics/observers/amp_page_load_metrics_observer.cc

Issue 2880323003: Various cleaups for AMP page load metrics. (Closed)
Patch Set: improve tests Created 3 years, 7 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/page_load_metrics/observers/amp_page_load_metrics_obser ver.h" 5 #include "chrome/browser/page_load_metrics/observers/amp_page_load_metrics_obser ver.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/optional.h" 9 #include "base/optional.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/time/time.h" 11 #include "base/time/time.h"
12 #include "chrome/browser/page_load_metrics/page_load_metrics_util.h" 12 #include "chrome/browser/page_load_metrics/page_load_metrics_util.h"
13 #include "chrome/common/page_load_metrics/page_load_timing.h" 13 #include "chrome/common/page_load_metrics/page_load_timing.h"
14 #include "components/google/core/browser/google_util.h" 14 #include "components/google/core/browser/google_util.h"
15 #include "content/public/browser/navigation_handle.h" 15 #include "content/public/browser/navigation_handle.h"
16 #include "url/gurl.h" 16 #include "url/gurl.h"
17 17
18 namespace { 18 namespace {
19 19
20 using AMPViewType = AMPPageLoadMetricsObserver::AMPViewType;
21
22 #define RECORD_HISTOGRAM_FOR_TYPE(name, amp_view_type, value) \
23 do { \
24 PAGE_LOAD_HISTOGRAM(name, value); \
25 switch (amp_view_type) { \
26 case AMPViewType::AMP_CACHE: \
27 PAGE_LOAD_HISTOGRAM(std::string(name).append(".AmpCache"), value); \
28 break; \
29 case AMPViewType::GOOGLE_SEARCH_AMP_VIEWER: \
30 PAGE_LOAD_HISTOGRAM(std::string(name).append(".GoogleSearch"), value); \
31 break; \
32 case AMPViewType::GOOGLE_NEWS_AMP_VIEWER: \
33 PAGE_LOAD_HISTOGRAM(std::string(name).append(".GoogleNews"), value); \
34 break; \
35 case AMPViewType::NONE: \
36 NOTREACHED(); \
37 PAGE_LOAD_HISTOGRAM(std::string(name).append(".None"), value); \
RyanSturm 2017/05/15 20:42:06 Remove the PAGE_LOAD_HISTOGRAM from this case.
Bryan McQuade 2017/05/16 03:59:14 Done
38 break; \
39 } \
40 } while (false)
41
20 const char kHistogramAMPDOMContentLoadedEventFired[] = 42 const char kHistogramAMPDOMContentLoadedEventFired[] =
21 "PageLoad.Clients.AMPCache2.DocumentTiming." 43 "PageLoad.Clients.AMP.DocumentTiming."
22 "NavigationToDOMContentLoadedEventFired"; 44 "NavigationToDOMContentLoadedEventFired";
23 const char kHistogramAMPFirstLayout[] = 45 const char kHistogramAMPFirstLayout[] =
24 "PageLoad.Clients.AMPCache2.DocumentTiming.NavigationToFirstLayout"; 46 "PageLoad.Clients.AMP.DocumentTiming.NavigationToFirstLayout";
25 const char kHistogramAMPLoadEventFired[] = 47 const char kHistogramAMPLoadEventFired[] =
26 "PageLoad.Clients.AMPCache2.DocumentTiming.NavigationToLoadEventFired"; 48 "PageLoad.Clients.AMP.DocumentTiming.NavigationToLoadEventFired";
27 const char kHistogramAMPFirstContentfulPaint[] = 49 const char kHistogramAMPFirstContentfulPaint[] =
28 "PageLoad.Clients.AMPCache2.PaintTiming." 50 "PageLoad.Clients.AMP.PaintTiming."
29 "NavigationToFirstContentfulPaint"; 51 "NavigationToFirstContentfulPaint";
30 const char kHistogramAMPParseStart[] = 52 const char kHistogramAMPParseStart[] =
31 "PageLoad.Clients.AMPCache2.ParseTiming.NavigationToParseStart"; 53 "PageLoad.Clients.AMP.ParseTiming.NavigationToParseStart";
32 54
33 // Host pattern for AMP Cache URLs. 55 // Host pattern for AMP Cache URLs.
34 // See https://developers.google.com/amp/cache/overview#amp-cache-url-format 56 // See https://developers.google.com/amp/cache/overview#amp-cache-url-format
35 // for a definition of the format of AMP Cache URLs. 57 // for a definition of the format of AMP Cache URLs.
36 const char kAmpCacheHost[] = "cdn.ampproject.org"; 58 const char kAmpCacheHostSuffix[] = "cdn.ampproject.org";
37 59
38 // Pattern for the path of Google AMP Viewer URLs. 60 // Pattern for the path of Google AMP Viewer URLs.
39 const char kGoogleAmpViewerPathPattern[] = "/amp/"; 61 const char kGoogleSearchAmpViewerPathPrefix[] = "/amp/";
40 62 const char kGoogleNewsAmpViewerPath[] = "/news/amp";
41 bool IsAMPCacheURL(const GURL& url) {
42 return url.host() == kAmpCacheHost ||
43 (google_util::IsGoogleDomainUrl(
44 url, google_util::DISALLOW_SUBDOMAIN,
45 google_util::DISALLOW_NON_STANDARD_PORTS) &&
46 base::StartsWith(url.path(), kGoogleAmpViewerPathPattern,
47 base::CompareCase::SENSITIVE));
48 }
49 63
50 } // namespace 64 } // namespace
51 65
52 AMPPageLoadMetricsObserver::AMPPageLoadMetricsObserver() {} 66 AMPPageLoadMetricsObserver::AMPPageLoadMetricsObserver() {}
53 67
54 AMPPageLoadMetricsObserver::~AMPPageLoadMetricsObserver() {} 68 AMPPageLoadMetricsObserver::~AMPPageLoadMetricsObserver() {}
55 69
56 page_load_metrics::PageLoadMetricsObserver::ObservePolicy 70 page_load_metrics::PageLoadMetricsObserver::ObservePolicy
57 AMPPageLoadMetricsObserver::OnCommit( 71 AMPPageLoadMetricsObserver::OnCommit(
58 content::NavigationHandle* navigation_handle) { 72 content::NavigationHandle* navigation_handle) {
59 return IsAMPCacheURL(navigation_handle->GetURL()) ? CONTINUE_OBSERVING 73 view_type_ = GetAMPViewType(navigation_handle->GetURL());
60 : STOP_OBSERVING; 74 return (view_type_ != AMPViewType::NONE) ? CONTINUE_OBSERVING
75 : STOP_OBSERVING;
61 } 76 }
62 77
63 void AMPPageLoadMetricsObserver::OnDomContentLoadedEventStart( 78 void AMPPageLoadMetricsObserver::OnDomContentLoadedEventStart(
64 const page_load_metrics::PageLoadTiming& timing, 79 const page_load_metrics::PageLoadTiming& timing,
65 const page_load_metrics::PageLoadExtraInfo& info) { 80 const page_load_metrics::PageLoadExtraInfo& info) {
66 if (!WasStartedInForegroundOptionalEventInForeground( 81 if (!WasStartedInForegroundOptionalEventInForeground(
67 timing.document_timing.dom_content_loaded_event_start, info)) { 82 timing.document_timing.dom_content_loaded_event_start, info)) {
68 return; 83 return;
69 } 84 }
70 PAGE_LOAD_HISTOGRAM( 85 RECORD_HISTOGRAM_FOR_TYPE(
71 kHistogramAMPDOMContentLoadedEventFired, 86 kHistogramAMPDOMContentLoadedEventFired, view_type_,
72 timing.document_timing.dom_content_loaded_event_start.value()); 87 timing.document_timing.dom_content_loaded_event_start.value());
73 } 88 }
74 89
75 void AMPPageLoadMetricsObserver::OnLoadEventStart( 90 void AMPPageLoadMetricsObserver::OnLoadEventStart(
76 const page_load_metrics::PageLoadTiming& timing, 91 const page_load_metrics::PageLoadTiming& timing,
77 const page_load_metrics::PageLoadExtraInfo& info) { 92 const page_load_metrics::PageLoadExtraInfo& info) {
78 if (!WasStartedInForegroundOptionalEventInForeground( 93 if (!WasStartedInForegroundOptionalEventInForeground(
79 timing.document_timing.load_event_start, info)) { 94 timing.document_timing.load_event_start, info)) {
80 return; 95 return;
81 } 96 }
82 PAGE_LOAD_HISTOGRAM(kHistogramAMPLoadEventFired, 97 RECORD_HISTOGRAM_FOR_TYPE(kHistogramAMPLoadEventFired, view_type_,
83 timing.document_timing.load_event_start.value()); 98 timing.document_timing.load_event_start.value());
84 } 99 }
85 100
86 void AMPPageLoadMetricsObserver::OnFirstLayout( 101 void AMPPageLoadMetricsObserver::OnFirstLayout(
87 const page_load_metrics::PageLoadTiming& timing, 102 const page_load_metrics::PageLoadTiming& timing,
88 const page_load_metrics::PageLoadExtraInfo& info) { 103 const page_load_metrics::PageLoadExtraInfo& info) {
89 if (!WasStartedInForegroundOptionalEventInForeground( 104 if (!WasStartedInForegroundOptionalEventInForeground(
90 timing.document_timing.first_layout, info)) { 105 timing.document_timing.first_layout, info)) {
91 return; 106 return;
92 } 107 }
93 PAGE_LOAD_HISTOGRAM(kHistogramAMPFirstLayout, 108 RECORD_HISTOGRAM_FOR_TYPE(kHistogramAMPFirstLayout, view_type_,
94 timing.document_timing.first_layout.value()); 109 timing.document_timing.first_layout.value());
95 } 110 }
96 111
97 void AMPPageLoadMetricsObserver::OnFirstContentfulPaintInPage( 112 void AMPPageLoadMetricsObserver::OnFirstContentfulPaintInPage(
98 const page_load_metrics::PageLoadTiming& timing, 113 const page_load_metrics::PageLoadTiming& timing,
99 const page_load_metrics::PageLoadExtraInfo& info) { 114 const page_load_metrics::PageLoadExtraInfo& info) {
100 if (!WasStartedInForegroundOptionalEventInForeground( 115 if (!WasStartedInForegroundOptionalEventInForeground(
101 timing.paint_timing.first_contentful_paint, info)) { 116 timing.paint_timing.first_contentful_paint, info)) {
102 return; 117 return;
103 } 118 }
104 PAGE_LOAD_HISTOGRAM(kHistogramAMPFirstContentfulPaint, 119 RECORD_HISTOGRAM_FOR_TYPE(kHistogramAMPFirstContentfulPaint, view_type_,
105 timing.paint_timing.first_contentful_paint.value()); 120 timing.paint_timing.first_contentful_paint.value());
106 } 121 }
107 122
108 void AMPPageLoadMetricsObserver::OnParseStart( 123 void AMPPageLoadMetricsObserver::OnParseStart(
109 const page_load_metrics::PageLoadTiming& timing, 124 const page_load_metrics::PageLoadTiming& timing,
110 const page_load_metrics::PageLoadExtraInfo& info) { 125 const page_load_metrics::PageLoadExtraInfo& info) {
111 if (!WasStartedInForegroundOptionalEventInForeground( 126 if (!WasStartedInForegroundOptionalEventInForeground(
112 timing.parse_timing.parse_start, info)) { 127 timing.parse_timing.parse_start, info)) {
113 return; 128 return;
114 } 129 }
115 PAGE_LOAD_HISTOGRAM(kHistogramAMPParseStart, 130 RECORD_HISTOGRAM_FOR_TYPE(kHistogramAMPParseStart, view_type_,
116 timing.parse_timing.parse_start.value()); 131 timing.parse_timing.parse_start.value());
117 } 132 }
133
134 // static
135 AMPPageLoadMetricsObserver::AMPViewType
136 AMPPageLoadMetricsObserver::GetAMPViewType(const GURL& url) {
137 if (base::EndsWith(url.host(), kAmpCacheHostSuffix,
138 base::CompareCase::INSENSITIVE_ASCII)) {
139 return AMPViewType::AMP_CACHE;
140 }
141
142 base::StringPiece google_hostname_prefix;
143 if (!page_load_metrics::IsGoogleHostnameAndGetPrefix(
144 url, &google_hostname_prefix)) {
145 return AMPViewType::NONE;
146 }
147
148 if (google_hostname_prefix == "www" &&
RyanSturm 2017/05/15 20:42:06 As a note: it's a little strange that "www" is inl
Bryan McQuade 2017/05/16 03:59:15 Done
149 base::StartsWith(url.path_piece(), kGoogleSearchAmpViewerPathPrefix,
150 base::CompareCase::SENSITIVE)) {
RyanSturm 2017/05/15 20:42:06 Not sure whether this should be case sensitive or
Bryan McQuade 2017/05/16 03:59:14 My understanding is that hostnames are not case se
151 return AMPViewType::GOOGLE_SEARCH_AMP_VIEWER;
152 }
153
154 if (google_hostname_prefix == "news" &&
155 url.path_piece() == kGoogleNewsAmpViewerPath) {
156 return AMPViewType::GOOGLE_NEWS_AMP_VIEWER;
157 }
158 return AMPViewType::NONE;
159 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698