no_state_prefetch_utils.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (c) 2011 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. #include "components/no_state_prefetch/browser/no_state_prefetch_utils.h"
  5. #include "base/metrics/histogram_macros.h"
  6. #include "base/time/time.h"
  7. #include "components/google/core/common/google_util.h"
  8. #include "components/no_state_prefetch/browser/no_state_prefetch_manager.h"
  9. #include "content/public/browser/navigation_handle.h"
  10. #include "content/public/browser/web_contents.h"
  11. #include "services/metrics/public/cpp/metrics_utils.h"
  12. #include "services/metrics/public/cpp/ukm_builders.h"
  13. #include "services/metrics/public/cpp/ukm_recorder.h"
  14. #include "url/gurl.h"
  15. #include "url/url_constants.h"
  16. namespace prerender {
  17. bool IsGoogleOriginURL(const GURL& origin_url) {
  18. // ALLOW_NON_STANDARD_PORTS for integration tests with the embedded server.
  19. if (!google_util::IsGoogleDomainUrl(origin_url,
  20. google_util::DISALLOW_SUBDOMAIN,
  21. google_util::ALLOW_NON_STANDARD_PORTS)) {
  22. return false;
  23. }
  24. return (origin_url.path_piece() == "/") ||
  25. google_util::IsGoogleSearchUrl(origin_url);
  26. }
  27. void RecordNoStatePrefetchMetrics(
  28. content::NavigationHandle* navigation_handle,
  29. ukm::SourceId source_id,
  30. NoStatePrefetchManager* no_state_prefetch_manager) {
  31. DCHECK(no_state_prefetch_manager);
  32. const std::vector<GURL>& redirects = navigation_handle->GetRedirectChain();
  33. base::TimeDelta prefetch_age;
  34. FinalStatus final_status;
  35. Origin prefetch_origin;
  36. bool nostate_prefetch_entry_found =
  37. no_state_prefetch_manager->GetPrefetchInformation(
  38. navigation_handle->GetURL(), &prefetch_age, &final_status,
  39. &prefetch_origin);
  40. // Try the URLs from the redirect chain.
  41. if (!nostate_prefetch_entry_found) {
  42. for (const auto& url : redirects) {
  43. nostate_prefetch_entry_found =
  44. no_state_prefetch_manager->GetPrefetchInformation(
  45. url, &prefetch_age, &final_status, &prefetch_origin);
  46. if (nostate_prefetch_entry_found)
  47. break;
  48. }
  49. }
  50. if (!nostate_prefetch_entry_found)
  51. return;
  52. ukm::builders::NoStatePrefetch builder(source_id);
  53. builder.SetPrefetchedRecently_PrefetchAge(
  54. ukm::GetExponentialBucketMinForUserTiming(prefetch_age.InMilliseconds()));
  55. builder.SetPrefetchedRecently_FinalStatus(final_status);
  56. builder.SetPrefetchedRecently_Origin(prefetch_origin);
  57. builder.Record(ukm::UkmRecorder::Get());
  58. }
  59. } // namespace prerender