| Index: components/translate/core/browser/ranker_model.cc
|
| diff --git a/components/translate/core/browser/ranker_model.cc b/components/translate/core/browser/ranker_model.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..de907f0a21d694752a38660f3561fefa5f11be41
|
| --- /dev/null
|
| +++ b/components/translate/core/browser/ranker_model.cc
|
| @@ -0,0 +1,54 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "components/translate/core/browser/ranker_model.h"
|
| +
|
| +#include "base/memory/ptr_util.h"
|
| +#include "base/time/time.h"
|
| +#include "components/translate/core/browser/proto/ranker_model.pb.h"
|
| +
|
| +namespace chrome_intelligence {
|
| +
|
| +RankerModel::RankerModel() : proto_(base::MakeUnique<RankerModelProto>()) {}
|
| +
|
| +RankerModel::~RankerModel() {}
|
| +
|
| +// static
|
| +std::unique_ptr<RankerModel> RankerModel::FromString(const std::string& data) {
|
| + auto model = base::MakeUnique<RankerModel>();
|
| + if (!model->mutable_proto()->ParseFromString(data))
|
| + return nullptr;
|
| + return model;
|
| +}
|
| +
|
| +bool RankerModel::IsExpired() const {
|
| + if (!proto().has_metadata())
|
| + return true;
|
| +
|
| + const auto& metadata = proto().metadata();
|
| +
|
| + // If the age of the model cannot be determined, presume it to be expired.
|
| + if (!metadata.has_last_modified_sec())
|
| + return true;
|
| +
|
| + // If the model has no set cache duration, then it never expires.
|
| + if (!metadata.has_cache_duration_sec() || metadata.cache_duration_sec() == 0)
|
| + return false;
|
| +
|
| + // Otherwise, a model is expired if its age exceeds the cache duration.
|
| + base::Time last_modified =
|
| + base::Time() + base::TimeDelta::FromSeconds(metadata.last_modified_sec());
|
| + base::TimeDelta age = base::Time::Now() - last_modified;
|
| + return age > base::TimeDelta::FromSeconds(metadata.cache_duration_sec());
|
| +}
|
| +
|
| +const std::string& RankerModel::GetSourceURL() const {
|
| + return proto_->metadata().source();
|
| +}
|
| +
|
| +std::string RankerModel::SerializeAsString() const {
|
| + return proto_->SerializeAsString();
|
| +}
|
| +
|
| +} // namespace chrome_intelligence
|
|
|