ranker_model.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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
  3. // found in the LICENSE file.
  4. #include "components/assist_ranker/ranker_model.h"
  5. #include <memory>
  6. #include "base/time/time.h"
  7. #include "components/assist_ranker/proto/ranker_model.pb.h"
  8. namespace assist_ranker {
  9. RankerModel::RankerModel() : proto_(std::make_unique<RankerModelProto>()) {}
  10. RankerModel::~RankerModel() {}
  11. // static
  12. std::unique_ptr<RankerModel> RankerModel::FromString(const std::string& data) {
  13. auto model = std::make_unique<RankerModel>();
  14. if (!model->mutable_proto()->ParseFromString(data))
  15. return nullptr;
  16. return model;
  17. }
  18. bool RankerModel::IsExpired() const {
  19. if (!proto().has_metadata())
  20. return true;
  21. const auto& metadata = proto().metadata();
  22. // If the age of the model cannot be determined, presume it to be expired.
  23. if (!metadata.has_last_modified_sec())
  24. return true;
  25. // If the model has no set cache duration, then it never expires.
  26. if (!metadata.has_cache_duration_sec() || metadata.cache_duration_sec() == 0)
  27. return false;
  28. // Otherwise, a model is expired if its age exceeds the cache duration.
  29. base::Time last_modified =
  30. base::Time() + base::Seconds(metadata.last_modified_sec());
  31. base::TimeDelta age = base::Time::Now() - last_modified;
  32. return age > base::Seconds(metadata.cache_duration_sec());
  33. }
  34. const std::string& RankerModel::GetSourceURL() const {
  35. return proto_->metadata().source();
  36. }
  37. std::string RankerModel::SerializeAsString() const {
  38. return proto_->SerializeAsString();
  39. }
  40. } // namespace assist_ranker