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

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

Issue 109323011: [rac] Download all rules for a country code in libaddressinput. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add a TODO. 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 30 matching lines...) Expand all
41 assert(storage_ != NULL); 41 assert(storage_ != NULL);
42 assert(downloader_ != NULL); 42 assert(downloader_ != NULL);
43 } 43 }
44 44
45 Retriever::~Retriever() { 45 Retriever::~Retriever() {
46 STLDeleteValues(&requests_); 46 STLDeleteValues(&requests_);
47 } 47 }
48 48
49 void Retriever::Retrieve(const std::string& key, 49 void Retriever::Retrieve(const std::string& key,
50 scoped_ptr<Callback> retrieved) { 50 scoped_ptr<Callback> retrieved) {
51 assert(requests_.find(key) == requests_.end()); 51 std::map<std::string, Callback*>::iterator request_it =
52 requests_.insert(std::make_pair(key, retrieved.release())); 52 requests_.find(key);
53 if (request_it != requests_.end()) {
54 // Abandon a previous request.
55 delete request_it->second;
56 requests_.erase(request_it);
57 }
58
59 requests_[key] = retrieved.release();
53 storage_->Get(key, 60 storage_->Get(key,
54 BuildCallback(this, &Retriever::OnDataRetrievedFromStorage)); 61 BuildCallback(this, &Retriever::OnDataRetrievedFromStorage));
55 } 62 }
56 63
57 void Retriever::OnDataRetrievedFromStorage(bool success, 64 void Retriever::OnDataRetrievedFromStorage(bool success,
58 const std::string& key, 65 const std::string& key,
59 const std::string& data) { 66 const std::string& data) {
67 // TODO(rouslan): Add validation for data integrity and freshness. If a
68 // download fails, then it's OK to use stale data.
60 if (success) { 69 if (success) {
61 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); 70 scoped_ptr<Callback> retrieved = GetCallbackForKey(key);
62 (*retrieved)(success, key, data); 71 if (retrieved != NULL) {
72 (*retrieved)(success, key, data);
73 }
63 } else { 74 } else {
64 downloader_->Download(lookup_key_util_.GetUrlForKey(key), 75 downloader_->Download(lookup_key_util_.GetUrlForKey(key),
65 BuildCallback(this, &Retriever::OnDownloaded)); 76 BuildCallback(this, &Retriever::OnDownloaded));
66 } 77 }
67 } 78 }
68 79
69 void Retriever::OnDownloaded(bool success, 80 void Retriever::OnDownloaded(bool success,
70 const std::string& url, 81 const std::string& url,
71 const std::string& data) { 82 const std::string& data) {
72 const std::string& key = lookup_key_util_.GetKeyForUrl(url); 83 const std::string& key = lookup_key_util_.GetKeyForUrl(url);
73 if (success) { 84 if (success) {
74 storage_->Put(key, data); 85 storage_->Put(key, data);
75 } 86 }
76 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); 87 scoped_ptr<Callback> retrieved = GetCallbackForKey(key);
77 (*retrieved)(success, key, success ? data : std::string()); 88 if (retrieved != NULL) {
89 (*retrieved)(success, key, success ? data : std::string());
90 }
78 } 91 }
79 92
80 scoped_ptr<Retriever::Callback> Retriever::GetCallbackForKey( 93 scoped_ptr<Retriever::Callback> Retriever::GetCallbackForKey(
81 const std::string& key) { 94 const std::string& key) {
82 std::map<std::string, Callback*>::iterator iter = 95 std::map<std::string, Callback*>::iterator iter =
83 requests_.find(key); 96 requests_.find(key);
84 assert(iter != requests_.end()); 97 if (iter == requests_.end()) {
98 // An abandonened request.
99 return scoped_ptr<Callback>();
100 }
85 scoped_ptr<Callback> callback(iter->second); 101 scoped_ptr<Callback> callback(iter->second);
86 requests_.erase(iter); 102 requests_.erase(iter);
87 return callback.Pass(); 103 return callback.Pass();
88 } 104 }
89 105
90 } // namespace addressinput 106 } // namespace addressinput
91 } // namespace i18n 107 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698