OLD | NEW |
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 Loading... |
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) { |
60 if (success) { | 67 if (success) { |
61 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); | 68 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); |
62 (*retrieved)(success, key, data); | 69 if (retrieved != NULL) { |
| 70 (*retrieved)(success, key, data); |
| 71 } |
63 } else { | 72 } else { |
64 downloader_->Download(lookup_key_util_.GetUrlForKey(key), | 73 downloader_->Download(lookup_key_util_.GetUrlForKey(key), |
65 BuildCallback(this, &Retriever::OnDownloaded)); | 74 BuildCallback(this, &Retriever::OnDownloaded)); |
66 } | 75 } |
67 } | 76 } |
68 | 77 |
69 void Retriever::OnDownloaded(bool success, | 78 void Retriever::OnDownloaded(bool success, |
70 const std::string& url, | 79 const std::string& url, |
71 const std::string& data) { | 80 const std::string& data) { |
72 const std::string& key = lookup_key_util_.GetKeyForUrl(url); | 81 const std::string& key = lookup_key_util_.GetKeyForUrl(url); |
73 if (success) { | 82 if (success) { |
74 storage_->Put(key, data); | 83 storage_->Put(key, data); |
75 } | 84 } |
76 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); | 85 scoped_ptr<Callback> retrieved = GetCallbackForKey(key); |
77 (*retrieved)(success, key, success ? data : std::string()); | 86 if (retrieved != NULL) { |
| 87 (*retrieved)(success, key, success ? data : std::string()); |
| 88 } |
78 } | 89 } |
79 | 90 |
80 scoped_ptr<Retriever::Callback> Retriever::GetCallbackForKey( | 91 scoped_ptr<Retriever::Callback> Retriever::GetCallbackForKey( |
81 const std::string& key) { | 92 const std::string& key) { |
82 std::map<std::string, Callback*>::iterator iter = | 93 std::map<std::string, Callback*>::iterator iter = |
83 requests_.find(key); | 94 requests_.find(key); |
84 assert(iter != requests_.end()); | 95 if (iter == requests_.end()) { |
| 96 // An abandonened request. |
| 97 return scoped_ptr<Callback>(); |
| 98 } |
85 scoped_ptr<Callback> callback(iter->second); | 99 scoped_ptr<Callback> callback(iter->second); |
86 requests_.erase(iter); | 100 requests_.erase(iter); |
87 return callback.Pass(); | 101 return callback.Pass(); |
88 } | 102 } |
89 | 103 |
90 } // namespace addressinput | 104 } // namespace addressinput |
91 } // namespace i18n | 105 } // namespace i18n |
OLD | NEW |