OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/tools/flip_server/mem_cache.h" | 5 #include "net/tools/flip_server/mem_cache.h" |
6 | 6 |
7 #include <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <stdio.h> | 10 #include <stdio.h> |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 headers_.reset(new BalsaHeaders); | 58 headers_.reset(new BalsaHeaders); |
59 headers_->CopyFrom(*headers); | 59 headers_->CopyFrom(*headers); |
60 } | 60 } |
61 } | 61 } |
62 | 62 |
63 FileData::FileData() {} | 63 FileData::FileData() {} |
64 | 64 |
65 FileData::~FileData() {} | 65 FileData::~FileData() {} |
66 | 66 |
67 MemoryCache::MemoryCache() : cwd_(FLAGS_cache_base_dir) {} | 67 MemoryCache::MemoryCache() : cwd_(FLAGS_cache_base_dir) {} |
68 | 68 MemoryCache::~MemoryCache() {} |
69 MemoryCache::~MemoryCache() { ClearFiles(); } | |
70 | |
71 void MemoryCache::CloneFrom(const MemoryCache& mc) { | |
72 DCHECK_NE(this, &mc); | |
73 ClearFiles(); | |
74 files_ = mc.files_; | |
75 cwd_ = mc.cwd_; | |
76 } | |
77 | 69 |
78 void MemoryCache::AddFiles() { | 70 void MemoryCache::AddFiles() { |
79 std::deque<std::string> paths; | 71 std::deque<std::string> paths; |
80 paths.push_back(cwd_ + "/GET_"); | 72 paths.push_back(cwd_ + "/GET_"); |
81 DIR* current_dir = NULL; | 73 DIR* current_dir = NULL; |
82 while (!paths.empty()) { | 74 while (!paths.empty()) { |
83 while (current_dir == NULL && !paths.empty()) { | 75 while (current_dir == NULL && !paths.empty()) { |
84 std::string current_dir_name = paths.front(); | 76 std::string current_dir_name = paths.front(); |
85 VLOG(1) << "Attempting to open dir: \"" << current_dir_name << "\""; | 77 VLOG(1) << "Attempting to open dir: \"" << current_dir_name << "\""; |
86 current_dir = opendir(current_dir_name.c_str()); | 78 current_dir = opendir(current_dir_name.c_str()); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 Files::iterator fi = files_.end(); | 198 Files::iterator fi = files_.end(); |
207 if (base::EndsWith(filename, ".html", base::CompareCase::SENSITIVE)) { | 199 if (base::EndsWith(filename, ".html", base::CompareCase::SENSITIVE)) { |
208 fi = files_.find(filename.substr(0, filename.size() - 5) + ".http"); | 200 fi = files_.find(filename.substr(0, filename.size() - 5) + ".http"); |
209 } | 201 } |
210 if (fi == files_.end()) | 202 if (fi == files_.end()) |
211 fi = files_.find(filename); | 203 fi = files_.find(filename); |
212 | 204 |
213 if (fi == files_.end()) { | 205 if (fi == files_.end()) { |
214 return NULL; | 206 return NULL; |
215 } | 207 } |
216 return fi->second; | 208 return fi->second.get(); |
| 209 ; |
217 } | 210 } |
218 | 211 |
219 bool MemoryCache::AssignFileData(const std::string& filename, | 212 bool MemoryCache::AssignFileData(const std::string& filename, |
220 MemCacheIter* mci) { | 213 MemCacheIter* mci) { |
221 mci->file_data = GetFileData(filename); | 214 mci->file_data = GetFileData(filename); |
222 if (mci->file_data == NULL) { | 215 if (mci->file_data == NULL) { |
223 LOG(ERROR) << "Could not find file data for " << filename; | 216 LOG(ERROR) << "Could not find file data for " << filename; |
224 return false; | 217 return false; |
225 } | 218 } |
226 return true; | 219 return true; |
227 } | 220 } |
228 | 221 |
229 void MemoryCache::InsertFile(const BalsaHeaders* headers, | 222 void MemoryCache::InsertFile(const BalsaHeaders* headers, |
230 const std::string& filename, | 223 const std::string& filename, |
231 const std::string& body) { | 224 const std::string& body) { |
232 InsertFile(new FileData(headers, filename, body)); | 225 InsertFile(new FileData(headers, filename, body)); |
233 } | 226 } |
234 | 227 |
235 void MemoryCache::InsertFile(FileData* file_data) { | 228 void MemoryCache::InsertFile(FileData* file_data) { |
236 Files::iterator it = files_.find(file_data->filename()); | 229 Files::iterator it = files_.find(file_data->filename()); |
237 if (it != files_.end()) { | 230 if (it != files_.end()) { |
238 delete it->second; | 231 it->second.reset(file_data); |
239 it->second = file_data; | |
240 } else { | 232 } else { |
241 files_.insert(std::make_pair(file_data->filename(), file_data)); | 233 files_.insert( |
| 234 std::make_pair(file_data->filename(), make_scoped_ptr(file_data))); |
242 } | 235 } |
243 } | 236 } |
244 | 237 |
245 void MemoryCache::ClearFiles() { | |
246 for (Files::const_iterator i = files_.begin(); i != files_.end(); ++i) { | |
247 delete i->second; | |
248 } | |
249 files_.clear(); | |
250 } | |
251 | |
252 } // namespace net | 238 } // namespace net |
OLD | NEW |