OLD | NEW |
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 const char kHistogramPrefix[] = "PageLoad.Clients.AMP."; |
| 23 |
20 const char kHistogramAMPDOMContentLoadedEventFired[] = | 24 const char kHistogramAMPDOMContentLoadedEventFired[] = |
21 "PageLoad.Clients.AMPCache2.DocumentTiming." | 25 "DocumentTiming.NavigationToDOMContentLoadedEventFired"; |
22 "NavigationToDOMContentLoadedEventFired"; | |
23 const char kHistogramAMPFirstLayout[] = | 26 const char kHistogramAMPFirstLayout[] = |
24 "PageLoad.Clients.AMPCache2.DocumentTiming.NavigationToFirstLayout"; | 27 "DocumentTiming.NavigationToFirstLayout"; |
25 const char kHistogramAMPLoadEventFired[] = | 28 const char kHistogramAMPLoadEventFired[] = |
26 "PageLoad.Clients.AMPCache2.DocumentTiming.NavigationToLoadEventFired"; | 29 "DocumentTiming.NavigationToLoadEventFired"; |
27 const char kHistogramAMPFirstContentfulPaint[] = | 30 const char kHistogramAMPFirstContentfulPaint[] = |
28 "PageLoad.Clients.AMPCache2.PaintTiming." | 31 "PaintTiming.NavigationToFirstContentfulPaint"; |
29 "NavigationToFirstContentfulPaint"; | 32 const char kHistogramAMPParseStart[] = "ParseTiming.NavigationToParseStart"; |
30 const char kHistogramAMPParseStart[] = | |
31 "PageLoad.Clients.AMPCache2.ParseTiming.NavigationToParseStart"; | |
32 | 33 |
33 // Host pattern for AMP Cache URLs. | 34 // Host pattern for AMP Cache URLs. |
34 // See https://developers.google.com/amp/cache/overview#amp-cache-url-format | 35 // See https://developers.google.com/amp/cache/overview#amp-cache-url-format |
35 // for a definition of the format of AMP Cache URLs. | 36 // for a definition of the format of AMP Cache URLs. |
36 const char kAmpCacheHost[] = "cdn.ampproject.org"; | 37 const char kAmpCacheHostSuffix[] = "cdn.ampproject.org"; |
37 | 38 |
38 // Pattern for the path of Google AMP Viewer URLs. | 39 #define RECORD_HISTOGRAM_FOR_TYPE(name, amp_view_type, value) \ |
39 const char kGoogleAmpViewerPathPattern[] = "/amp/"; | 40 do { \ |
40 | 41 PAGE_LOAD_HISTOGRAM(std::string(kHistogramPrefix).append(name), value); \ |
41 bool IsAMPCacheURL(const GURL& url) { | 42 switch (amp_view_type) { \ |
42 return url.host() == kAmpCacheHost || | 43 case AMPViewType::AMP_CACHE: \ |
43 (google_util::IsGoogleDomainUrl( | 44 PAGE_LOAD_HISTOGRAM( \ |
44 url, google_util::DISALLOW_SUBDOMAIN, | 45 std::string(kHistogramPrefix).append("AmpCache.").append(name), \ |
45 google_util::DISALLOW_NON_STANDARD_PORTS) && | 46 value); \ |
46 base::StartsWith(url.path(), kGoogleAmpViewerPathPattern, | 47 break; \ |
47 base::CompareCase::SENSITIVE)); | 48 case AMPViewType::GOOGLE_SEARCH_AMP_VIEWER: \ |
48 } | 49 PAGE_LOAD_HISTOGRAM(std::string(kHistogramPrefix) \ |
| 50 .append("GoogleSearch.") \ |
| 51 .append(name), \ |
| 52 value); \ |
| 53 break; \ |
| 54 case AMPViewType::GOOGLE_NEWS_AMP_VIEWER: \ |
| 55 PAGE_LOAD_HISTOGRAM( \ |
| 56 std::string(kHistogramPrefix).append("GoogleNews.").append(name), \ |
| 57 value); \ |
| 58 break; \ |
| 59 case AMPViewType::NONE: \ |
| 60 NOTREACHED(); \ |
| 61 break; \ |
| 62 } \ |
| 63 } while (false) |
49 | 64 |
50 } // namespace | 65 } // namespace |
51 | 66 |
52 AMPPageLoadMetricsObserver::AMPPageLoadMetricsObserver() {} | 67 AMPPageLoadMetricsObserver::AMPPageLoadMetricsObserver() {} |
53 | 68 |
54 AMPPageLoadMetricsObserver::~AMPPageLoadMetricsObserver() {} | 69 AMPPageLoadMetricsObserver::~AMPPageLoadMetricsObserver() {} |
55 | 70 |
56 page_load_metrics::PageLoadMetricsObserver::ObservePolicy | 71 page_load_metrics::PageLoadMetricsObserver::ObservePolicy |
57 AMPPageLoadMetricsObserver::OnCommit( | 72 AMPPageLoadMetricsObserver::OnCommit( |
58 content::NavigationHandle* navigation_handle) { | 73 content::NavigationHandle* navigation_handle) { |
59 return IsAMPCacheURL(navigation_handle->GetURL()) ? CONTINUE_OBSERVING | 74 view_type_ = GetAMPViewType(navigation_handle->GetURL()); |
60 : STOP_OBSERVING; | 75 return (view_type_ != AMPViewType::NONE) ? CONTINUE_OBSERVING |
| 76 : STOP_OBSERVING; |
61 } | 77 } |
62 | 78 |
63 void AMPPageLoadMetricsObserver::OnDomContentLoadedEventStart( | 79 void AMPPageLoadMetricsObserver::OnDomContentLoadedEventStart( |
64 const page_load_metrics::PageLoadTiming& timing, | 80 const page_load_metrics::PageLoadTiming& timing, |
65 const page_load_metrics::PageLoadExtraInfo& info) { | 81 const page_load_metrics::PageLoadExtraInfo& info) { |
66 if (!WasStartedInForegroundOptionalEventInForeground( | 82 if (!WasStartedInForegroundOptionalEventInForeground( |
67 timing.document_timing.dom_content_loaded_event_start, info)) { | 83 timing.document_timing.dom_content_loaded_event_start, info)) { |
68 return; | 84 return; |
69 } | 85 } |
70 PAGE_LOAD_HISTOGRAM( | 86 RECORD_HISTOGRAM_FOR_TYPE( |
71 kHistogramAMPDOMContentLoadedEventFired, | 87 kHistogramAMPDOMContentLoadedEventFired, view_type_, |
72 timing.document_timing.dom_content_loaded_event_start.value()); | 88 timing.document_timing.dom_content_loaded_event_start.value()); |
73 } | 89 } |
74 | 90 |
75 void AMPPageLoadMetricsObserver::OnLoadEventStart( | 91 void AMPPageLoadMetricsObserver::OnLoadEventStart( |
76 const page_load_metrics::PageLoadTiming& timing, | 92 const page_load_metrics::PageLoadTiming& timing, |
77 const page_load_metrics::PageLoadExtraInfo& info) { | 93 const page_load_metrics::PageLoadExtraInfo& info) { |
78 if (!WasStartedInForegroundOptionalEventInForeground( | 94 if (!WasStartedInForegroundOptionalEventInForeground( |
79 timing.document_timing.load_event_start, info)) { | 95 timing.document_timing.load_event_start, info)) { |
80 return; | 96 return; |
81 } | 97 } |
82 PAGE_LOAD_HISTOGRAM(kHistogramAMPLoadEventFired, | 98 RECORD_HISTOGRAM_FOR_TYPE(kHistogramAMPLoadEventFired, view_type_, |
83 timing.document_timing.load_event_start.value()); | 99 timing.document_timing.load_event_start.value()); |
84 } | 100 } |
85 | 101 |
86 void AMPPageLoadMetricsObserver::OnFirstLayout( | 102 void AMPPageLoadMetricsObserver::OnFirstLayout( |
87 const page_load_metrics::PageLoadTiming& timing, | 103 const page_load_metrics::PageLoadTiming& timing, |
88 const page_load_metrics::PageLoadExtraInfo& info) { | 104 const page_load_metrics::PageLoadExtraInfo& info) { |
89 if (!WasStartedInForegroundOptionalEventInForeground( | 105 if (!WasStartedInForegroundOptionalEventInForeground( |
90 timing.document_timing.first_layout, info)) { | 106 timing.document_timing.first_layout, info)) { |
91 return; | 107 return; |
92 } | 108 } |
93 PAGE_LOAD_HISTOGRAM(kHistogramAMPFirstLayout, | 109 RECORD_HISTOGRAM_FOR_TYPE(kHistogramAMPFirstLayout, view_type_, |
94 timing.document_timing.first_layout.value()); | 110 timing.document_timing.first_layout.value()); |
95 } | 111 } |
96 | 112 |
97 void AMPPageLoadMetricsObserver::OnFirstContentfulPaintInPage( | 113 void AMPPageLoadMetricsObserver::OnFirstContentfulPaintInPage( |
98 const page_load_metrics::PageLoadTiming& timing, | 114 const page_load_metrics::PageLoadTiming& timing, |
99 const page_load_metrics::PageLoadExtraInfo& info) { | 115 const page_load_metrics::PageLoadExtraInfo& info) { |
100 if (!WasStartedInForegroundOptionalEventInForeground( | 116 if (!WasStartedInForegroundOptionalEventInForeground( |
101 timing.paint_timing.first_contentful_paint, info)) { | 117 timing.paint_timing.first_contentful_paint, info)) { |
102 return; | 118 return; |
103 } | 119 } |
104 PAGE_LOAD_HISTOGRAM(kHistogramAMPFirstContentfulPaint, | 120 RECORD_HISTOGRAM_FOR_TYPE(kHistogramAMPFirstContentfulPaint, view_type_, |
105 timing.paint_timing.first_contentful_paint.value()); | 121 timing.paint_timing.first_contentful_paint.value()); |
106 } | 122 } |
107 | 123 |
108 void AMPPageLoadMetricsObserver::OnParseStart( | 124 void AMPPageLoadMetricsObserver::OnParseStart( |
109 const page_load_metrics::PageLoadTiming& timing, | 125 const page_load_metrics::PageLoadTiming& timing, |
110 const page_load_metrics::PageLoadExtraInfo& info) { | 126 const page_load_metrics::PageLoadExtraInfo& info) { |
111 if (!WasStartedInForegroundOptionalEventInForeground( | 127 if (!WasStartedInForegroundOptionalEventInForeground( |
112 timing.parse_timing.parse_start, info)) { | 128 timing.parse_timing.parse_start, info)) { |
113 return; | 129 return; |
114 } | 130 } |
115 PAGE_LOAD_HISTOGRAM(kHistogramAMPParseStart, | 131 RECORD_HISTOGRAM_FOR_TYPE(kHistogramAMPParseStart, view_type_, |
116 timing.parse_timing.parse_start.value()); | 132 timing.parse_timing.parse_start.value()); |
117 } | 133 } |
| 134 |
| 135 // static |
| 136 AMPPageLoadMetricsObserver::AMPViewType |
| 137 AMPPageLoadMetricsObserver::GetAMPViewType(const GURL& url) { |
| 138 if (base::EndsWith(url.host(), kAmpCacheHostSuffix, |
| 139 base::CompareCase::INSENSITIVE_ASCII)) { |
| 140 return AMPViewType::AMP_CACHE; |
| 141 } |
| 142 |
| 143 base::Optional<std::string> google_hostname_prefix = |
| 144 page_load_metrics::GetGoogleHostnamePrefix(url); |
| 145 if (!google_hostname_prefix.has_value()) |
| 146 return AMPViewType::NONE; |
| 147 |
| 148 if (google_hostname_prefix.value() == "www" && |
| 149 base::StartsWith(url.path_piece(), "/amp/", |
| 150 base::CompareCase::SENSITIVE)) { |
| 151 return AMPViewType::GOOGLE_SEARCH_AMP_VIEWER; |
| 152 } |
| 153 |
| 154 if (google_hostname_prefix.value() == "news" && |
| 155 url.path_piece() == "/news/amp") { |
| 156 return AMPViewType::GOOGLE_NEWS_AMP_VIEWER; |
| 157 } |
| 158 return AMPViewType::NONE; |
| 159 } |
OLD | NEW |