OLD | NEW |
| (Empty) |
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 | |
5 #include "components/translate/core/browser/ranker_model.h" | |
6 | |
7 #include "base/memory/ptr_util.h" | |
8 #include "base/time/time.h" | |
9 #include "components/translate/core/browser/proto/ranker_model.pb.h" | |
10 | |
11 namespace chrome_intelligence { | |
12 | |
13 RankerModel::RankerModel() : proto_(base::MakeUnique<RankerModelProto>()) {} | |
14 | |
15 RankerModel::~RankerModel() {} | |
16 | |
17 // static | |
18 std::unique_ptr<RankerModel> RankerModel::FromString(const std::string& data) { | |
19 auto model = base::MakeUnique<RankerModel>(); | |
20 if (!model->mutable_proto()->ParseFromString(data)) | |
21 return nullptr; | |
22 return model; | |
23 } | |
24 | |
25 bool RankerModel::IsExpired() const { | |
26 if (!proto().has_metadata()) | |
27 return true; | |
28 | |
29 const auto& metadata = proto().metadata(); | |
30 | |
31 // If the age of the model cannot be determined, presume it to be expired. | |
32 if (!metadata.has_last_modified_sec()) | |
33 return true; | |
34 | |
35 // If the model has no set cache duration, then it never expires. | |
36 if (!metadata.has_cache_duration_sec() || metadata.cache_duration_sec() == 0) | |
37 return false; | |
38 | |
39 // Otherwise, a model is expired if its age exceeds the cache duration. | |
40 base::Time last_modified = | |
41 base::Time() + base::TimeDelta::FromSeconds(metadata.last_modified_sec()); | |
42 base::TimeDelta age = base::Time::Now() - last_modified; | |
43 return age > base::TimeDelta::FromSeconds(metadata.cache_duration_sec()); | |
44 } | |
45 | |
46 const std::string& RankerModel::GetSourceURL() const { | |
47 return proto_->metadata().source(); | |
48 } | |
49 | |
50 std::string RankerModel::SerializeAsString() const { | |
51 return proto_->SerializeAsString(); | |
52 } | |
53 | |
54 } // namespace chrome_intelligence | |
OLD | NEW |