OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/page_load_metrics/observers/amp_page_load_metrics_obser
ver.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/optional.h" |
| 10 #include "base/strings/string_util.h" |
| 11 #include "base/time/time.h" |
| 12 #include "chrome/browser/page_load_metrics/page_load_metrics_util.h" |
| 13 #include "chrome/common/page_load_metrics/page_load_timing.h" |
| 14 #include "components/google/core/browser/google_util.h" |
| 15 #include "content/public/browser/navigation_handle.h" |
| 16 #include "url/gurl.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 const char kHistogramAMPDOMContentLoadedEventFired[] = |
| 21 "PageLoad.Clients.AMPCache.DocumentTiming." |
| 22 "NavigationToDOMContentLoadedEventFired"; |
| 23 const char kHistogramAMPFirstLayout[] = |
| 24 "PageLoad.Clients.AMPCache.DocumentTiming.NavigationToFirstLayout"; |
| 25 const char kHistogramAMPLoadEventFired[] = |
| 26 "PageLoad.Clients.AMPCache.DocumentTiming.NavigationToLoadEventFired"; |
| 27 const char kHistogramAMPFirstContentfulPaint[] = |
| 28 "PageLoad.Clients.AMPCache.PaintTiming.NavigationToFirstContentfulPaint"; |
| 29 const char kHistogramAMPParseStart[] = |
| 30 "PageLoad.Clients.AMPCache.ParseTiming.NavigationToParseStart"; |
| 31 |
| 32 // Host pattern for AMP Cache URLs. |
| 33 // See https://developers.google.com/amp/cache/overview#amp-cache-url-format |
| 34 // for a definition of the format of AMP Cache URLs. |
| 35 const char kAmpCacheHost[] = "cdn.ampproject.org"; |
| 36 |
| 37 // Pattern for the path of Google AMP Viewer URLs. |
| 38 const char kGoogleAmpViewerPathPattern[] = "/amp/"; |
| 39 |
| 40 bool IsAMPCacheURL(const GURL& url) { |
| 41 return url.host() == kAmpCacheHost || |
| 42 (google_util::IsGoogleDomainUrl( |
| 43 url, google_util::DISALLOW_SUBDOMAIN, |
| 44 google_util::DISALLOW_NON_STANDARD_PORTS) && |
| 45 base::StartsWith(url.path(), kGoogleAmpViewerPathPattern, |
| 46 base::CompareCase::SENSITIVE)); |
| 47 } |
| 48 |
| 49 } // namespace |
| 50 |
| 51 AMPPageLoadMetricsObserver::AMPPageLoadMetricsObserver() {} |
| 52 |
| 53 AMPPageLoadMetricsObserver::~AMPPageLoadMetricsObserver() {} |
| 54 |
| 55 page_load_metrics::PageLoadMetricsObserver::ObservePolicy |
| 56 AMPPageLoadMetricsObserver::OnCommit( |
| 57 content::NavigationHandle* navigation_handle) { |
| 58 return IsAMPCacheURL(navigation_handle->GetURL()) ? CONTINUE_OBSERVING |
| 59 : STOP_OBSERVING; |
| 60 } |
| 61 |
| 62 void AMPPageLoadMetricsObserver::OnDomContentLoadedEventStart( |
| 63 const page_load_metrics::PageLoadTiming& timing, |
| 64 const page_load_metrics::PageLoadExtraInfo& info) { |
| 65 if (!WasStartedInForegroundOptionalEventInForeground( |
| 66 timing.dom_content_loaded_event_start, info)) { |
| 67 return; |
| 68 } |
| 69 PAGE_LOAD_HISTOGRAM(kHistogramAMPDOMContentLoadedEventFired, |
| 70 timing.dom_content_loaded_event_start.value()); |
| 71 } |
| 72 |
| 73 void AMPPageLoadMetricsObserver::OnLoadEventStart( |
| 74 const page_load_metrics::PageLoadTiming& timing, |
| 75 const page_load_metrics::PageLoadExtraInfo& info) { |
| 76 if (!WasStartedInForegroundOptionalEventInForeground( |
| 77 timing.dom_content_loaded_event_start, info)) { |
| 78 return; |
| 79 } |
| 80 PAGE_LOAD_HISTOGRAM(kHistogramAMPLoadEventFired, |
| 81 timing.load_event_start.value()); |
| 82 } |
| 83 |
| 84 void AMPPageLoadMetricsObserver::OnFirstLayout( |
| 85 const page_load_metrics::PageLoadTiming& timing, |
| 86 const page_load_metrics::PageLoadExtraInfo& info) { |
| 87 if (!WasStartedInForegroundOptionalEventInForeground( |
| 88 timing.dom_content_loaded_event_start, info)) { |
| 89 return; |
| 90 } |
| 91 PAGE_LOAD_HISTOGRAM(kHistogramAMPFirstLayout, timing.first_layout.value()); |
| 92 } |
| 93 |
| 94 void AMPPageLoadMetricsObserver::OnFirstContentfulPaint( |
| 95 const page_load_metrics::PageLoadTiming& timing, |
| 96 const page_load_metrics::PageLoadExtraInfo& info) { |
| 97 if (!WasStartedInForegroundOptionalEventInForeground( |
| 98 timing.dom_content_loaded_event_start, info)) { |
| 99 return; |
| 100 } |
| 101 PAGE_LOAD_HISTOGRAM(kHistogramAMPFirstContentfulPaint, |
| 102 timing.first_contentful_paint.value()); |
| 103 } |
| 104 |
| 105 void AMPPageLoadMetricsObserver::OnParseStart( |
| 106 const page_load_metrics::PageLoadTiming& timing, |
| 107 const page_load_metrics::PageLoadExtraInfo& info) { |
| 108 if (!WasStartedInForegroundOptionalEventInForeground( |
| 109 timing.dom_content_loaded_event_start, info)) { |
| 110 return; |
| 111 } |
| 112 PAGE_LOAD_HISTOGRAM(kHistogramAMPParseStart, timing.parse_start.value()); |
| 113 } |
OLD | NEW |