Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: third_party/libaddressinput/chromium/cpp/src/retriever.cc

Issue 144353002: [rac] Use stale libaddressinput data if download fails (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (C) 2013 Google Inc. 1 // Copyright (C) 2013 Google Inc.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
(...skipping 10 matching lines...) Expand all
21 #include <libaddressinput/util/scoped_ptr.h> 21 #include <libaddressinput/util/scoped_ptr.h>
22 22
23 #include <cassert> 23 #include <cassert>
24 #include <cstddef> 24 #include <cstddef>
25 #include <map> 25 #include <map>
26 #include <string> 26 #include <string>
27 #include <utility> 27 #include <utility>
28 28
29 #include "fallback_data_store.h" 29 #include "fallback_data_store.h"
30 #include "util/stl_util.h" 30 #include "util/stl_util.h"
31 #include "wrapper.h"
31 32
32 namespace i18n { 33 namespace i18n {
33 namespace addressinput { 34 namespace addressinput {
34 35
36 namespace {
37
38 // The number of seconds after which data is considered stale. The staleness
39 // threshold is 30 days:
40 // 30 days *
41 // 24 hours per day *
42 // 60 minutes per hour *
43 // 60 seconds per minute.
44 static const double kStaleDataAgeInSeconds = 30.0 * 24.0 * 60.0 * 60.0;
45
46 } // namespace
47
35 Retriever::Retriever(const std::string& validation_data_url, 48 Retriever::Retriever(const std::string& validation_data_url,
36 scoped_ptr<Downloader> downloader, 49 scoped_ptr<Downloader> downloader,
37 scoped_ptr<Storage> storage) 50 scoped_ptr<Storage> storage)
38 : validation_data_url_(validation_data_url), 51 : validation_data_url_(validation_data_url),
39 downloader_(downloader.Pass()), 52 downloader_(downloader.Pass()),
40 storage_(storage.Pass()) { 53 storage_(storage.Pass()),
54 stale_data_() {
41 assert(validation_data_url_.length() > 0); 55 assert(validation_data_url_.length() > 0);
42 assert(validation_data_url_[validation_data_url_.length() - 1] == '/'); 56 assert(validation_data_url_[validation_data_url_.length() - 1] == '/');
43 assert(storage_ != NULL); 57 assert(storage_ != NULL);
44 assert(downloader_ != NULL); 58 assert(downloader_ != NULL);
45 } 59 }
46 60
47 Retriever::~Retriever() { 61 Retriever::~Retriever() {
48 STLDeleteValues(&requests_); 62 STLDeleteValues(&requests_);
49 } 63 }
50 64
51 void Retriever::Retrieve(const std::string& key, 65 void Retriever::Retrieve(const std::string& key,
52 scoped_ptr<Callback> retrieved) { 66 scoped_ptr<Callback> retrieved) {
53 std::map<std::string, Callback*>::iterator request_it = 67 std::map<std::string, Callback*>::iterator request_it =
54 requests_.find(key); 68 requests_.find(key);
55 if (request_it != requests_.end()) { 69 if (request_it != requests_.end()) {
56 // Abandon a previous request. 70 // Abandon a previous request.
57 delete request_it->second; 71 delete request_it->second;
58 requests_.erase(request_it); 72 requests_.erase(request_it);
59 } 73 }
60 74
61 requests_[key] = retrieved.release(); 75 requests_[key] = retrieved.release();
62 storage_->Get(key, 76 storage_->Get(key,
63 BuildCallback(this, &Retriever::OnDataRetrievedFromStorage)); 77 BuildCallback(this, &Retriever::OnDataRetrievedFromStorage));
64 } 78 }
65 79
66 void Retriever::OnDataRetrievedFromStorage(bool success, 80 void Retriever::OnDataRetrievedFromStorage(bool success,
67 const std::string& key, 81 const std::string& key,
68 const std::string& stored_data) { 82 const std::string& stored_data) {
69 // TODO(rouslan): Add validation for data integrity and freshness. If a 83 std::string unwrapped = stored_data;
70 // download fails, then it's OK to use stale data. 84 double age_in_seconds = 0.0;
71 if (success) { 85 bool valid_format = Wrapper::Unwrap(&unwrapped, &age_in_seconds);
86 if (success && valid_format && age_in_seconds < kStaleDataAgeInSeconds) {
72 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); 87 scoped_ptr<Callback> retrieved = GetCallbackForKey(key);
73 if (retrieved != NULL) { 88 if (retrieved != NULL) {
74 (*retrieved)(success, key, stored_data); 89 (*retrieved)(success, key, unwrapped);
75 } 90 }
76 } else { 91 } else {
92 if (success && valid_format) {
93 stale_data_[key] = unwrapped;
94 }
77 downloader_->Download(GetUrlForKey(key), 95 downloader_->Download(GetUrlForKey(key),
78 BuildCallback(this, &Retriever::OnDownloaded)); 96 BuildCallback(this, &Retriever::OnDownloaded));
79 } 97 }
80 } 98 }
81 99
82 void Retriever::OnDownloaded(bool success, 100 void Retriever::OnDownloaded(bool success,
83 const std::string& url, 101 const std::string& url,
84 const std::string& downloaded_data) { 102 const std::string& downloaded_data) {
85 const std::string& key = GetKeyForUrl(url); 103 const std::string& key = GetKeyForUrl(url);
86 std::string response; 104 std::string response;
105 std::map<std::string, std::string>::iterator stale_data_it =
106 stale_data_.find(key);
107
87 if (success) { 108 if (success) {
88 storage_->Put(key, downloaded_data); 109 storage_->Put(key, Wrapper::Wrap(downloaded_data));
89 response = downloaded_data; 110 response = downloaded_data;
111 } else if (stale_data_it != stale_data_.end()) {
112 success = true;
113 response = stale_data_it->second;
90 } else { 114 } else {
91 success = FallbackDataStore::Get(key, &response); 115 success = FallbackDataStore::Get(key, &response);
92 } 116 }
93 117
118 if (stale_data_it != stale_data_.end()) {
119 stale_data_.erase(stale_data_it);
120 }
121
94 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); 122 scoped_ptr<Callback> retrieved = GetCallbackForKey(key);
95 if (retrieved != NULL) { 123 if (retrieved != NULL) {
96 (*retrieved)(success, key, response); 124 (*retrieved)(success, key, response);
97 } 125 }
98 } 126 }
99 127
100 std::string Retriever::GetUrlForKey(const std::string& key) const { 128 std::string Retriever::GetUrlForKey(const std::string& key) const {
101 return validation_data_url_ + key; 129 return validation_data_url_ + key;
102 } 130 }
103 131
(...skipping 17 matching lines...) Expand all
121 // An abandonened request. 149 // An abandonened request.
122 return scoped_ptr<Callback>(); 150 return scoped_ptr<Callback>();
123 } 151 }
124 scoped_ptr<Callback> callback(iter->second); 152 scoped_ptr<Callback> callback(iter->second);
125 requests_.erase(iter); 153 requests_.erase(iter);
126 return callback.Pass(); 154 return callback.Pass();
127 } 155 }
128 156
129 } // namespace addressinput 157 } // namespace addressinput
130 } // namespace i18n 158 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698