optimization_guide_util.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2019 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. #ifndef COMPONENTS_OPTIMIZATION_GUIDE_CORE_OPTIMIZATION_GUIDE_UTIL_H_
  5. #define COMPONENTS_OPTIMIZATION_GUIDE_CORE_OPTIMIZATION_GUIDE_UTIL_H_
  6. #include <string>
  7. #include "base/strings/string_split.h"
  8. #include "base/time/time.h"
  9. #include "components/optimization_guide/core/optimization_guide_enums.h"
  10. #include "components/optimization_guide/proto/common_types.pb.h"
  11. #include "components/optimization_guide/proto/models.pb.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. #define OPTIMIZATION_GUIDE_LOG(log_source, optimization_guide_logger, message) \
  14. do { \
  15. if (optimization_guide_logger && \
  16. optimization_guide_logger->ShouldEnableDebugLogs()) { \
  17. optimization_guide_logger->OnLogMessageAdded( \
  18. base::Time::Now(), log_source, __FILE__, __LINE__, message); \
  19. } \
  20. if (optimization_guide::switches::IsDebugLogsEnabled()) \
  21. DVLOG(0) << message; \
  22. } while (0)
  23. namespace optimization_guide {
  24. enum class OptimizationGuideDecision;
  25. // Returns false if the host is an IP address, localhosts, or an invalid
  26. // host that is not supported by the remote optimization guide.
  27. bool IsHostValidToFetchFromRemoteOptimizationGuide(const std::string& host);
  28. // Validates that the metadata stored in |any_metadata_| is of the same type
  29. // and is parseable as |T|. Will return metadata if all checks pass.
  30. template <class T,
  31. class = typename std::enable_if<
  32. std::is_convertible<T*, google::protobuf::MessageLite*>{}>::type>
  33. absl::optional<T> ParsedAnyMetadata(const proto::Any& any_metadata) {
  34. // Verify type is the same - the Any type URL should be wrapped as:
  35. // "type.googleapis.com/com.foo.Name".
  36. std::vector<std::string> any_type_parts =
  37. base::SplitString(any_metadata.type_url(), ".", base::TRIM_WHITESPACE,
  38. base::SPLIT_WANT_NONEMPTY);
  39. if (any_type_parts.empty())
  40. return absl::nullopt;
  41. T metadata;
  42. std::vector<std::string> type_parts =
  43. base::SplitString(metadata.GetTypeName(), ".", base::TRIM_WHITESPACE,
  44. base::SPLIT_WANT_NONEMPTY);
  45. if (type_parts.empty())
  46. return absl::nullopt;
  47. std::string any_type_name = any_type_parts.back();
  48. std::string type_name = type_parts.back();
  49. if (type_name != any_type_name)
  50. return absl::nullopt;
  51. // Return metadata if parseable.
  52. if (metadata.ParseFromString(any_metadata.value()))
  53. return metadata;
  54. return absl::nullopt;
  55. }
  56. // Returns a debug string for OptimizationGuideDecision.
  57. std::string GetStringForOptimizationGuideDecision(
  58. OptimizationGuideDecision decision);
  59. // Returns client's origin info, including platform and milestone.
  60. proto::OriginInfo GetClientOriginInfo();
  61. } // namespace optimization_guide
  62. #endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_OPTIMIZATION_GUIDE_UTIL_H_