| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/page_load_metrics_util.h" | 5 #include "chrome/common/page_load_metrics/page_load_metrics_util.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "chrome/common/page_load_metrics/page_load_timing.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 10 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 11 #include "url/gurl.h" | |
| 12 | 11 |
| 13 namespace page_load_metrics { | 12 namespace page_load_metrics { |
| 14 | 13 |
| 15 namespace { | |
| 16 | |
| 17 bool IsBackgroundAbort(const PageLoadExtraInfo& info) { | |
| 18 if (!info.started_in_foreground || !info.first_background_time) | |
| 19 return false; | |
| 20 | |
| 21 if (!info.page_end_time) | |
| 22 return true; | |
| 23 | |
| 24 return info.first_background_time <= info.page_end_time; | |
| 25 } | |
| 26 | |
| 27 PageAbortReason GetAbortReasonForEndReason(PageEndReason end_reason) { | |
| 28 switch (end_reason) { | |
| 29 case END_RELOAD: | |
| 30 return ABORT_RELOAD; | |
| 31 case END_FORWARD_BACK: | |
| 32 return ABORT_FORWARD_BACK; | |
| 33 case END_NEW_NAVIGATION: | |
| 34 return ABORT_NEW_NAVIGATION; | |
| 35 case END_STOP: | |
| 36 return ABORT_STOP; | |
| 37 case END_CLOSE: | |
| 38 return ABORT_CLOSE; | |
| 39 case END_OTHER: | |
| 40 return ABORT_OTHER; | |
| 41 default: | |
| 42 return ABORT_NONE; | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 base::Optional<std::string> GetGoogleHostnamePrefix(const GURL& url) { | 14 base::Optional<std::string> GetGoogleHostnamePrefix(const GURL& url) { |
| 49 const size_t registry_length = | 15 const size_t registry_length = |
| 50 net::registry_controlled_domains::GetRegistryLength( | 16 net::registry_controlled_domains::GetRegistryLength( |
| 51 url, | 17 url, |
| 52 | 18 |
| 53 // Do not include unknown registries (registries that don't have any | 19 // Do not include unknown registries (registries that don't have any |
| 54 // matches in effective TLD names). | 20 // matches in effective TLD names). |
| 55 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, | 21 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
| 56 | 22 |
| 57 // Do not include private registries, such as appspot.com. We don't | 23 // Do not include private registries, such as appspot.com. We don't |
| (...skipping 19 matching lines...) Expand all Loading... |
| 77 } | 43 } |
| 78 | 44 |
| 79 return std::string(hostname_minus_registry.substr( | 45 return std::string(hostname_minus_registry.substr( |
| 80 0, hostname_minus_registry.length() - strlen(".google"))); | 46 0, hostname_minus_registry.length() - strlen(".google"))); |
| 81 } | 47 } |
| 82 | 48 |
| 83 bool IsGoogleHostname(const GURL& url) { | 49 bool IsGoogleHostname(const GURL& url) { |
| 84 return GetGoogleHostnamePrefix(url).has_value(); | 50 return GetGoogleHostnamePrefix(url).has_value(); |
| 85 } | 51 } |
| 86 | 52 |
| 87 bool WasStartedInForegroundOptionalEventInForeground( | |
| 88 const base::Optional<base::TimeDelta>& event, | |
| 89 const PageLoadExtraInfo& info) { | |
| 90 return info.started_in_foreground && event && | |
| 91 (!info.first_background_time || | |
| 92 event.value() <= info.first_background_time.value()); | |
| 93 } | |
| 94 | |
| 95 PageAbortInfo GetPageAbortInfo(const PageLoadExtraInfo& info) { | |
| 96 if (IsBackgroundAbort(info)) { | |
| 97 // Though most cases where a tab is backgrounded are user initiated, we | |
| 98 // can't be certain that we were backgrounded due to a user action. For | |
| 99 // example, on Android, the screen times out after a period of inactivity, | |
| 100 // resulting in a non-user-initiated backgrounding. | |
| 101 return {ABORT_BACKGROUND, UserInitiatedInfo::NotUserInitiated(), | |
| 102 info.first_background_time.value()}; | |
| 103 } | |
| 104 | |
| 105 PageAbortReason abort_reason = | |
| 106 GetAbortReasonForEndReason(info.page_end_reason); | |
| 107 if (abort_reason == ABORT_NONE) | |
| 108 return PageAbortInfo(); | |
| 109 | |
| 110 return {abort_reason, info.page_end_user_initiated_info, | |
| 111 info.page_end_time.value()}; | |
| 112 } | |
| 113 | |
| 114 base::Optional<base::TimeDelta> GetInitialForegroundDuration( | |
| 115 const PageLoadExtraInfo& info, | |
| 116 base::TimeTicks app_background_time) { | |
| 117 if (!info.started_in_foreground) | |
| 118 return base::Optional<base::TimeDelta>(); | |
| 119 | |
| 120 base::Optional<base::TimeDelta> time_on_page = | |
| 121 OptionalMin(info.first_background_time, info.page_end_time); | |
| 122 | |
| 123 // If we don't have a time_on_page value yet, and we have an app background | |
| 124 // time, use the app background time as our end time. This addresses cases | |
| 125 // where the Chrome app is backgrounded before the page load is complete, on | |
| 126 // platforms where Chrome may be killed once it goes into the background | |
| 127 // (Android). In these cases, we use the app background time as the 'end | |
| 128 // time'. | |
| 129 if (!time_on_page && !app_background_time.is_null()) { | |
| 130 time_on_page = app_background_time - info.navigation_start; | |
| 131 } | |
| 132 return time_on_page; | |
| 133 } | |
| 134 | |
| 135 base::Optional<base::TimeDelta> OptionalMin( | 53 base::Optional<base::TimeDelta> OptionalMin( |
| 136 const base::Optional<base::TimeDelta>& a, | 54 const base::Optional<base::TimeDelta>& a, |
| 137 const base::Optional<base::TimeDelta>& b) { | 55 const base::Optional<base::TimeDelta>& b) { |
| 138 if (a && !b) | 56 if (a && !b) |
| 139 return a; | 57 return a; |
| 140 if (b && !a) | 58 if (b && !a) |
| 141 return b; | 59 return b; |
| 142 if (!a && !b) | 60 if (!a && !b) |
| 143 return a; // doesn't matter which | 61 return a; // doesn't matter which |
| 144 return base::Optional<base::TimeDelta>(std::min(a.value(), b.value())); | 62 return base::Optional<base::TimeDelta>(std::min(a.value(), b.value())); |
| 145 } | 63 } |
| 146 | 64 |
| 147 bool DidObserveLoadingBehaviorInAnyFrame( | |
| 148 const page_load_metrics::PageLoadExtraInfo& info, | |
| 149 blink::WebLoadingBehaviorFlag behavior) { | |
| 150 const int all_frame_loading_behavior_flags = | |
| 151 info.main_frame_metadata.behavior_flags | | |
| 152 info.subframe_metadata.behavior_flags; | |
| 153 | |
| 154 return (all_frame_loading_behavior_flags & behavior) != 0; | |
| 155 } | |
| 156 | |
| 157 } // namespace page_load_metrics | 65 } // namespace page_load_metrics |
| OLD | NEW |