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

Side by Side Diff: chrome/browser/nacl_host/pnacl_translation_cache.cc

Issue 18640005: Rename PNaClTranslationCache->PnaclTranslationCache to match convention elsewhere (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/nacl_host/pnacl_translation_cache.h" 5 #include "chrome/browser/nacl_host/pnacl_translation_cache.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/threading/thread_checker.h" 11 #include "base/threading/thread_checker.h"
12 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
13 #include "net/base/io_buffer.h" 13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h" 14 #include "net/base/net_errors.h"
15 #include "net/disk_cache/disk_cache.h" 15 #include "net/disk_cache/disk_cache.h"
16 16
17 using content::BrowserThread; 17 using content::BrowserThread;
18 18
19 static const base::FilePath::CharType kDiskCacheDirectoryName[] = 19 static const base::FilePath::CharType kDiskCacheDirectoryName[] =
20 FILE_PATH_LITERAL("PNaClTranslationCache"); 20 FILE_PATH_LITERAL("PnaclTranslationCache");
21 21
22 namespace { 22 namespace {
23 23
24 void CloseDiskCacheEntry(disk_cache::Entry* entry) { entry->Close(); } 24 void CloseDiskCacheEntry(disk_cache::Entry* entry) { entry->Close(); }
25 25
26 } // namespace 26 } // namespace
27 27
28 namespace pnacl_cache { 28 namespace pnacl_cache {
29 // These are in pnacl_cache namespace instead of static so they can be used 29 // These are in pnacl_cache namespace instead of static so they can be used
30 // by the unit test. 30 // by the unit test.
31 const int kMaxDiskCacheSize = 1000 * 1024 * 1024; 31 const int kMaxDiskCacheSize = 1000 * 1024 * 1024;
32 const int kMaxMemCacheSize = 100 * 1024 * 1024; 32 const int kMaxMemCacheSize = 100 * 1024 * 1024;
33 33
34 ////////////////////////////////////////////////////////////////////// 34 //////////////////////////////////////////////////////////////////////
35 // Handle Reading/Writing to Cache. 35 // Handle Reading/Writing to Cache.
36 36
37 // PNaClTranslationCacheEntry is a shim that provides storage for the 37 // PnaclTranslationCacheEntry is a shim that provides storage for the
38 // 'key' and 'data' strings as the disk_cache is performing various async 38 // 'key' and 'data' strings as the disk_cache is performing various async
39 // operations. It also tracks the open disk_cache::Entry 39 // operations. It also tracks the open disk_cache::Entry
40 // and ensures that the entry is closed. 40 // and ensures that the entry is closed.
41 class PNaClTranslationCacheEntry 41 class PnaclTranslationCacheEntry
42 : public base::RefCounted<PNaClTranslationCacheEntry> { 42 : public base::RefCounted<PnaclTranslationCacheEntry> {
43 public: 43 public:
44 PNaClTranslationCacheEntry(base::WeakPtr<PNaClTranslationCache> cache, 44 PnaclTranslationCacheEntry(base::WeakPtr<PnaclTranslationCache> cache,
45 const std::string& key, 45 const std::string& key,
46 std::string* read_nexe, 46 std::string* read_nexe,
47 const std::string& write_nexe, 47 const std::string& write_nexe,
48 const CompletionCallback& callback, 48 const CompletionCallback& callback,
49 bool is_read); 49 bool is_read);
50 void Start(); 50 void Start();
51 51
52 // Writes: --- 52 // Writes: ---
53 // v | 53 // v |
54 // Start -> Open Existing --------------> Write ---> Close 54 // Start -> Open Existing --------------> Write ---> Close
55 // \ ^ 55 // \ ^
56 // \ / 56 // \ /
57 // --> Create -- 57 // --> Create --
58 // Reads: 58 // Reads:
59 // Start -> Open --------Read ----> Close 59 // Start -> Open --------Read ----> Close
60 // | ^ 60 // | ^
61 // |__| 61 // |__|
62 enum CacheStep { 62 enum CacheStep {
63 UNINITIALIZED, 63 UNINITIALIZED,
64 OPEN_ENTRY, 64 OPEN_ENTRY,
65 CREATE_ENTRY, 65 CREATE_ENTRY,
66 TRANSFER_ENTRY, 66 TRANSFER_ENTRY,
67 CLOSE_ENTRY 67 CLOSE_ENTRY
68 }; 68 };
69 69
70 private: 70 private:
71 friend class base::RefCounted<PNaClTranslationCacheEntry>; 71 friend class base::RefCounted<PnaclTranslationCacheEntry>;
72 ~PNaClTranslationCacheEntry(); 72 ~PnaclTranslationCacheEntry();
73 73
74 // Try to open an existing entry in the backend 74 // Try to open an existing entry in the backend
75 void OpenEntry(); 75 void OpenEntry();
76 // Create a new entry in the backend (for writes) 76 // Create a new entry in the backend (for writes)
77 void CreateEntry(); 77 void CreateEntry();
78 // Write |len| bytes to the backend, starting at |offset| 78 // Write |len| bytes to the backend, starting at |offset|
79 void WriteEntry(int offset, int len); 79 void WriteEntry(int offset, int len);
80 // Read |len| bytes from the backend, starting at |offset| 80 // Read |len| bytes from the backend, starting at |offset|
81 void ReadEntry(int offset, int len); 81 void ReadEntry(int offset, int len);
82 // If there was an error, doom the entry. Then post a task to the IO 82 // If there was an error, doom the entry. Then post a task to the IO
83 // thread to close (and delete) it. 83 // thread to close (and delete) it.
84 void CloseEntry(int rv); 84 void CloseEntry(int rv);
85 // Call the user callback, and signal to the cache to delete this. 85 // Call the user callback, and signal to the cache to delete this.
86 void Finish(int rv); 86 void Finish(int rv);
87 // Used as the callback for all operations to the backend. Handle state 87 // Used as the callback for all operations to the backend. Handle state
88 // transitions, track bytes transferred, and call the other helper methods. 88 // transitions, track bytes transferred, and call the other helper methods.
89 void DispatchNext(int rv); 89 void DispatchNext(int rv);
90 // Get the total transfer size. For reads, must be called after the backend 90 // Get the total transfer size. For reads, must be called after the backend
91 // entry has been opened. 91 // entry has been opened.
92 int GetTransferSize(); 92 int GetTransferSize();
93 93
94 base::WeakPtr<PNaClTranslationCache> cache_; 94 base::WeakPtr<PnaclTranslationCache> cache_;
95 95
96 std::string key_; 96 std::string key_;
97 std::string* read_nexe_; 97 std::string* read_nexe_;
98 std::string write_nexe_; 98 std::string write_nexe_;
99 disk_cache::Entry* entry_; 99 disk_cache::Entry* entry_;
100 CacheStep step_; 100 CacheStep step_;
101 bool is_read_; 101 bool is_read_;
102 int bytes_transferred_; 102 int bytes_transferred_;
103 int bytes_to_transfer_; 103 int bytes_to_transfer_;
104 scoped_refptr<net::IOBufferWithSize> read_buf_; 104 scoped_refptr<net::IOBufferWithSize> read_buf_;
105 CompletionCallback finish_callback_; 105 CompletionCallback finish_callback_;
106 base::ThreadChecker thread_checker_; 106 base::ThreadChecker thread_checker_;
107 DISALLOW_COPY_AND_ASSIGN(PNaClTranslationCacheEntry); 107 DISALLOW_COPY_AND_ASSIGN(PnaclTranslationCacheEntry);
108 }; 108 };
109 109
110 PNaClTranslationCacheEntry::PNaClTranslationCacheEntry( 110 PnaclTranslationCacheEntry::PnaclTranslationCacheEntry(
111 base::WeakPtr<PNaClTranslationCache> cache, 111 base::WeakPtr<PnaclTranslationCache> cache,
112 const std::string& key, 112 const std::string& key,
113 std::string* read_nexe, 113 std::string* read_nexe,
114 const std::string& write_nexe, 114 const std::string& write_nexe,
115 const CompletionCallback& callback, 115 const CompletionCallback& callback,
116 bool is_read) 116 bool is_read)
117 : cache_(cache), 117 : cache_(cache),
118 key_(key), 118 key_(key),
119 read_nexe_(read_nexe), 119 read_nexe_(read_nexe),
120 write_nexe_(write_nexe), 120 write_nexe_(write_nexe),
121 entry_(NULL), 121 entry_(NULL),
122 step_(UNINITIALIZED), 122 step_(UNINITIALIZED),
123 is_read_(is_read), 123 is_read_(is_read),
124 bytes_transferred_(0), 124 bytes_transferred_(0),
125 bytes_to_transfer_(-1), 125 bytes_to_transfer_(-1),
126 finish_callback_(callback) {} 126 finish_callback_(callback) {}
127 127
128 PNaClTranslationCacheEntry::~PNaClTranslationCacheEntry() { 128 PnaclTranslationCacheEntry::~PnaclTranslationCacheEntry() {
129 // Ensure we have called the user's callback 129 // Ensure we have called the user's callback
130 DCHECK(finish_callback_.is_null()); 130 DCHECK(finish_callback_.is_null());
131 } 131 }
132 132
133 void PNaClTranslationCacheEntry::Start() { 133 void PnaclTranslationCacheEntry::Start() {
134 DCHECK(thread_checker_.CalledOnValidThread()); 134 DCHECK(thread_checker_.CalledOnValidThread());
135 step_ = OPEN_ENTRY; 135 step_ = OPEN_ENTRY;
136 OpenEntry(); 136 OpenEntry();
137 } 137 }
138 138
139 // OpenEntry, CreateEntry, WriteEntry, ReadEntry and CloseEntry are only called 139 // OpenEntry, CreateEntry, WriteEntry, ReadEntry and CloseEntry are only called
140 // from DispatchNext, so they know that cache_ is still valid. 140 // from DispatchNext, so they know that cache_ is still valid.
141 void PNaClTranslationCacheEntry::OpenEntry() { 141 void PnaclTranslationCacheEntry::OpenEntry() {
142 int rv = cache_->backend() 142 int rv = cache_->backend()
143 ->OpenEntry(key_, 143 ->OpenEntry(key_,
144 &entry_, 144 &entry_,
145 base::Bind(&PNaClTranslationCacheEntry::DispatchNext, this)); 145 base::Bind(&PnaclTranslationCacheEntry::DispatchNext, this));
146 if (rv != net::ERR_IO_PENDING) 146 if (rv != net::ERR_IO_PENDING)
147 DispatchNext(rv); 147 DispatchNext(rv);
148 } 148 }
149 149
150 void PNaClTranslationCacheEntry::CreateEntry() { 150 void PnaclTranslationCacheEntry::CreateEntry() {
151 int rv = cache_->backend()->CreateEntry( 151 int rv = cache_->backend()->CreateEntry(
152 key_, 152 key_,
153 &entry_, 153 &entry_,
154 base::Bind(&PNaClTranslationCacheEntry::DispatchNext, this)); 154 base::Bind(&PnaclTranslationCacheEntry::DispatchNext, this));
155 if (rv != net::ERR_IO_PENDING) 155 if (rv != net::ERR_IO_PENDING)
156 DispatchNext(rv); 156 DispatchNext(rv);
157 } 157 }
158 158
159 void PNaClTranslationCacheEntry::WriteEntry(int offset, int len) { 159 void PnaclTranslationCacheEntry::WriteEntry(int offset, int len) {
160 scoped_refptr<net::StringIOBuffer> io_buf = 160 scoped_refptr<net::StringIOBuffer> io_buf =
161 new net::StringIOBuffer(write_nexe_.substr(offset, len)); 161 new net::StringIOBuffer(write_nexe_.substr(offset, len));
162 int rv = entry_->WriteData( 162 int rv = entry_->WriteData(
163 1, 163 1,
164 offset, 164 offset,
165 io_buf.get(), 165 io_buf.get(),
166 len, 166 len,
167 base::Bind(&PNaClTranslationCacheEntry::DispatchNext, this), 167 base::Bind(&PnaclTranslationCacheEntry::DispatchNext, this),
168 false); 168 false);
169 if (rv != net::ERR_IO_PENDING) 169 if (rv != net::ERR_IO_PENDING)
170 DispatchNext(rv); 170 DispatchNext(rv);
171 } 171 }
172 172
173 void PNaClTranslationCacheEntry::ReadEntry(int offset, int len) { 173 void PnaclTranslationCacheEntry::ReadEntry(int offset, int len) {
174 read_buf_ = new net::IOBufferWithSize(len); 174 read_buf_ = new net::IOBufferWithSize(len);
175 int rv = entry_->ReadData( 175 int rv = entry_->ReadData(
176 1, 176 1,
177 offset, 177 offset,
178 read_buf_.get(), 178 read_buf_.get(),
179 len, 179 len,
180 base::Bind(&PNaClTranslationCacheEntry::DispatchNext, this)); 180 base::Bind(&PnaclTranslationCacheEntry::DispatchNext, this));
181 if (rv != net::ERR_IO_PENDING) 181 if (rv != net::ERR_IO_PENDING)
182 DispatchNext(rv); 182 DispatchNext(rv);
183 } 183 }
184 184
185 int PNaClTranslationCacheEntry::GetTransferSize() { 185 int PnaclTranslationCacheEntry::GetTransferSize() {
186 if (is_read_) { 186 if (is_read_) {
187 DCHECK(entry_); 187 DCHECK(entry_);
188 return entry_->GetDataSize(1); 188 return entry_->GetDataSize(1);
189 } 189 }
190 return write_nexe_.size(); 190 return write_nexe_.size();
191 } 191 }
192 192
193 void PNaClTranslationCacheEntry::CloseEntry(int rv) { 193 void PnaclTranslationCacheEntry::CloseEntry(int rv) {
194 DCHECK(entry_); 194 DCHECK(entry_);
195 if (rv < 0) 195 if (rv < 0)
196 entry_->Doom(); 196 entry_->Doom();
197 BrowserThread::PostTask( 197 BrowserThread::PostTask(
198 BrowserThread::IO, FROM_HERE, base::Bind(&CloseDiskCacheEntry, entry_)); 198 BrowserThread::IO, FROM_HERE, base::Bind(&CloseDiskCacheEntry, entry_));
199 Finish(rv); 199 Finish(rv);
200 } 200 }
201 201
202 void PNaClTranslationCacheEntry::Finish(int rv) { 202 void PnaclTranslationCacheEntry::Finish(int rv) {
203 if (!finish_callback_.is_null()) { 203 if (!finish_callback_.is_null()) {
204 finish_callback_.Run(rv); 204 finish_callback_.Run(rv);
205 finish_callback_.Reset(); 205 finish_callback_.Reset();
206 } 206 }
207 cache_->OpComplete(this); 207 cache_->OpComplete(this);
208 } 208 }
209 209
210 void PNaClTranslationCacheEntry::DispatchNext(int rv) { 210 void PnaclTranslationCacheEntry::DispatchNext(int rv) {
211 DCHECK(thread_checker_.CalledOnValidThread()); 211 DCHECK(thread_checker_.CalledOnValidThread());
212 if (!cache_) 212 if (!cache_)
213 return; 213 return;
214 214
215 switch (step_) { 215 switch (step_) {
216 case UNINITIALIZED: 216 case UNINITIALIZED:
217 LOG(ERROR) << "Unexpected step in DispatchNext"; 217 LOG(ERROR) << "Unexpected step in DispatchNext";
218 break; 218 break;
219 219
220 case OPEN_ENTRY: 220 case OPEN_ENTRY:
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 CloseEntry(0); 273 CloseEntry(0);
274 break; 274 break;
275 275
276 case CLOSE_ENTRY: 276 case CLOSE_ENTRY:
277 step_ = UNINITIALIZED; 277 step_ = UNINITIALIZED;
278 break; 278 break;
279 } 279 }
280 } 280 }
281 281
282 ////////////////////////////////////////////////////////////////////// 282 //////////////////////////////////////////////////////////////////////
283 void PNaClTranslationCache::OpComplete(PNaClTranslationCacheEntry* entry) { 283 void PnaclTranslationCache::OpComplete(PnaclTranslationCacheEntry* entry) {
284 open_entries_.erase(entry); 284 open_entries_.erase(entry);
285 } 285 }
286 286
287 ////////////////////////////////////////////////////////////////////// 287 //////////////////////////////////////////////////////////////////////
288 // Construction and cache backend initialization 288 // Construction and cache backend initialization
289 PNaClTranslationCache::PNaClTranslationCache() 289 PnaclTranslationCache::PnaclTranslationCache()
290 : disk_cache_(NULL), in_memory_(false) {} 290 : disk_cache_(NULL), in_memory_(false) {}
291 291
292 PNaClTranslationCache::~PNaClTranslationCache() { delete disk_cache_; } 292 PnaclTranslationCache::~PnaclTranslationCache() { delete disk_cache_; }
293 293
294 int PNaClTranslationCache::InitWithDiskBackend( 294 int PnaclTranslationCache::InitWithDiskBackend(
295 const base::FilePath& cache_dir, 295 const base::FilePath& cache_dir,
296 int cache_size, 296 int cache_size,
297 const CompletionCallback& callback) { 297 const CompletionCallback& callback) {
298 return Init(net::DISK_CACHE, cache_dir, cache_size, callback); 298 return Init(net::DISK_CACHE, cache_dir, cache_size, callback);
299 } 299 }
300 300
301 int PNaClTranslationCache::InitWithMemBackend( 301 int PnaclTranslationCache::InitWithMemBackend(
302 int cache_size, 302 int cache_size,
303 const CompletionCallback& callback) { 303 const CompletionCallback& callback) {
304 return Init(net::MEMORY_CACHE, base::FilePath(), cache_size, callback); 304 return Init(net::MEMORY_CACHE, base::FilePath(), cache_size, callback);
305 } 305 }
306 306
307 int PNaClTranslationCache::Init(net::CacheType cache_type, 307 int PnaclTranslationCache::Init(net::CacheType cache_type,
308 const base::FilePath& cache_dir, 308 const base::FilePath& cache_dir,
309 int cache_size, 309 int cache_size,
310 const CompletionCallback& callback) { 310 const CompletionCallback& callback) {
311 int rv = disk_cache::CreateCacheBackend( 311 int rv = disk_cache::CreateCacheBackend(
312 cache_type, 312 cache_type,
313 net::CACHE_BACKEND_DEFAULT, 313 net::CACHE_BACKEND_DEFAULT,
314 cache_dir, 314 cache_dir,
315 cache_size, 315 cache_size,
316 true /* force_initialize */, 316 true /* force_initialize */,
317 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE).get(), 317 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE).get(),
318 NULL, /* dummy net log */ 318 NULL, /* dummy net log */
319 &disk_cache_, 319 &disk_cache_,
320 base::Bind(&PNaClTranslationCache::OnCreateBackendComplete, AsWeakPtr())); 320 base::Bind(&PnaclTranslationCache::OnCreateBackendComplete, AsWeakPtr()));
321 init_callback_ = callback; 321 init_callback_ = callback;
322 if (rv != net::ERR_IO_PENDING) { 322 if (rv != net::ERR_IO_PENDING) {
323 OnCreateBackendComplete(rv); 323 OnCreateBackendComplete(rv);
324 } 324 }
325 return rv; 325 return rv;
326 } 326 }
327 327
328 void PNaClTranslationCache::OnCreateBackendComplete(int rv) { 328 void PnaclTranslationCache::OnCreateBackendComplete(int rv) {
329 // Invoke our client's callback function. 329 // Invoke our client's callback function.
330 if (!init_callback_.is_null()) { 330 if (!init_callback_.is_null()) {
331 init_callback_.Run(rv); 331 init_callback_.Run(rv);
332 init_callback_.Reset(); 332 init_callback_.Reset();
333 } 333 }
334 } 334 }
335 335
336 ////////////////////////////////////////////////////////////////////// 336 //////////////////////////////////////////////////////////////////////
337 // High-level API 337 // High-level API
338 338
339 void PNaClTranslationCache::StoreNexe(const std::string& key, 339 void PnaclTranslationCache::StoreNexe(const std::string& key,
340 const std::string& nexe) { 340 const std::string& nexe) {
341 StoreNexe(key, nexe, CompletionCallback()); 341 StoreNexe(key, nexe, CompletionCallback());
342 } 342 }
343 343
344 void PNaClTranslationCache::StoreNexe(const std::string& key, 344 void PnaclTranslationCache::StoreNexe(const std::string& key,
345 const std::string& nexe, 345 const std::string& nexe,
346 const CompletionCallback& callback) { 346 const CompletionCallback& callback) {
347 PNaClTranslationCacheEntry* entry = new PNaClTranslationCacheEntry( 347 PnaclTranslationCacheEntry* entry = new PnaclTranslationCacheEntry(
348 AsWeakPtr(), key, NULL, nexe, callback, false); 348 AsWeakPtr(), key, NULL, nexe, callback, false);
349 open_entries_[entry] = entry; 349 open_entries_[entry] = entry;
350 entry->Start(); 350 entry->Start();
351 } 351 }
352 352
353 void PNaClTranslationCache::GetNexe(const std::string& key, 353 void PnaclTranslationCache::GetNexe(const std::string& key,
354 std::string* nexe, 354 std::string* nexe,
355 const CompletionCallback& callback) { 355 const CompletionCallback& callback) {
356 PNaClTranslationCacheEntry* entry = new PNaClTranslationCacheEntry( 356 PnaclTranslationCacheEntry* entry = new PnaclTranslationCacheEntry(
357 AsWeakPtr(), key, nexe, std::string(), callback, true); 357 AsWeakPtr(), key, nexe, std::string(), callback, true);
358 open_entries_[entry] = entry; 358 open_entries_[entry] = entry;
359 entry->Start(); 359 entry->Start();
360 } 360 }
361 361
362 int PNaClTranslationCache::InitCache(const base::FilePath& cache_directory, 362 int PnaclTranslationCache::InitCache(const base::FilePath& cache_directory,
363 bool in_memory, 363 bool in_memory,
364 const CompletionCallback& callback) { 364 const CompletionCallback& callback) {
365 int rv; 365 int rv;
366 in_memory_ = in_memory; 366 in_memory_ = in_memory;
367 if (in_memory_) { 367 if (in_memory_) {
368 rv = InitWithMemBackend(kMaxMemCacheSize, callback); 368 rv = InitWithMemBackend(kMaxMemCacheSize, callback);
369 } else { 369 } else {
370 rv = InitWithDiskBackend(cache_directory.Append(kDiskCacheDirectoryName), 370 rv = InitWithDiskBackend(cache_directory.Append(kDiskCacheDirectoryName),
371 kMaxDiskCacheSize, 371 kMaxDiskCacheSize,
372 callback); 372 callback);
373 } 373 }
374 374
375 return rv; 375 return rv;
376 } 376 }
377 377
378 int PNaClTranslationCache::Size() { 378 int PnaclTranslationCache::Size() {
379 if (!disk_cache_) 379 if (!disk_cache_)
380 return -1; 380 return -1;
381 return disk_cache_->GetEntryCount(); 381 return disk_cache_->GetEntryCount();
382 } 382 }
383 383
384 } // namespace pnacl_cache 384 } // namespace pnacl_cache
OLDNEW
« no previous file with comments | « chrome/browser/nacl_host/pnacl_translation_cache.h ('k') | chrome/browser/nacl_host/pnacl_translation_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698