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

Side by Side Diff: chrome/browser/chromeos/drive/file_cache.cc

Issue 15060002: drive: Rename FileCache methods in a blocking pool centric manner (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove virtual Created 7 years, 7 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/chromeos/drive/file_cache.h" 5 #include "chrome/browser/chromeos/drive/file_cache.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 void FileCache::AddObserver(FileCacheObserver* observer) { 242 void FileCache::AddObserver(FileCacheObserver* observer) {
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
244 observers_.AddObserver(observer); 244 observers_.AddObserver(observer);
245 } 245 }
246 246
247 void FileCache::RemoveObserver(FileCacheObserver* observer) { 247 void FileCache::RemoveObserver(FileCacheObserver* observer) {
248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
249 observers_.RemoveObserver(observer); 249 observers_.RemoveObserver(observer);
250 } 250 }
251 251
252 void FileCache::GetCacheEntry(const std::string& resource_id, 252 void FileCache::GetCacheEntryOnUIThread(const std::string& resource_id,
253 const std::string& md5, 253 const std::string& md5,
254 const GetCacheEntryCallback& callback) { 254 const GetCacheEntryCallback& callback) {
255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
256 DCHECK(!callback.is_null()); 256 DCHECK(!callback.is_null());
257 257
258 FileCacheEntry* cache_entry = new FileCacheEntry; 258 FileCacheEntry* cache_entry = new FileCacheEntry;
259 base::PostTaskAndReplyWithResult( 259 base::PostTaskAndReplyWithResult(
260 blocking_task_runner_, 260 blocking_task_runner_,
261 FROM_HERE, 261 FROM_HERE,
262 base::Bind(&FileCache::GetCacheEntryOnBlockingPool, 262 base::Bind(&FileCache::GetCacheEntry,
263 base::Unretained(this), resource_id, md5, cache_entry), 263 base::Unretained(this), resource_id, md5, cache_entry),
264 base::Bind(&RunGetCacheEntryCallback, 264 base::Bind(&RunGetCacheEntryCallback,
265 callback, base::Owned(cache_entry))); 265 callback, base::Owned(cache_entry)));
266 } 266 }
267 267
268 void FileCache::Iterate(const CacheIterateCallback& iteration_callback, 268 void FileCache::IterateOnUIThread(
269 const base::Closure& completion_callback) { 269 const CacheIterateCallback& iteration_callback,
270 const base::Closure& completion_callback) {
270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 271 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
271 DCHECK(!iteration_callback.is_null()); 272 DCHECK(!iteration_callback.is_null());
272 DCHECK(!completion_callback.is_null()); 273 DCHECK(!completion_callback.is_null());
273 274
274 blocking_task_runner_->PostTaskAndReply( 275 blocking_task_runner_->PostTaskAndReply(
275 FROM_HERE, 276 FROM_HERE,
276 base::Bind(&FileCache::IterateOnBlockingPool, 277 base::Bind(&FileCache::Iterate,
277 base::Unretained(this), 278 base::Unretained(this),
278 google_apis::CreateRelayCallback(iteration_callback)), 279 google_apis::CreateRelayCallback(iteration_callback)),
279 completion_callback); 280 completion_callback);
280 } 281 }
281 282
282 void FileCache::FreeDiskSpaceIfNeededFor( 283 void FileCache::FreeDiskSpaceIfNeededForOnUIThread(
283 int64 num_bytes, 284 int64 num_bytes,
284 const InitializeCacheCallback& callback) { 285 const InitializeCacheCallback& callback) {
285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
286 DCHECK(!callback.is_null()); 287 DCHECK(!callback.is_null());
287 288
288 base::PostTaskAndReplyWithResult( 289 base::PostTaskAndReplyWithResult(
289 blocking_task_runner_, 290 blocking_task_runner_,
290 FROM_HERE, 291 FROM_HERE,
291 base::Bind(&FileCache::FreeDiskSpaceOnBlockingPoolIfNeededFor, 292 base::Bind(&FileCache::FreeDiskSpaceIfNeededFor,
292 base::Unretained(this), 293 base::Unretained(this),
293 num_bytes), 294 num_bytes),
294 callback); 295 callback);
295 } 296 }
296 297
297 void FileCache::GetFile(const std::string& resource_id, 298 void FileCache::GetFileOnUIThread(const std::string& resource_id,
298 const std::string& md5, 299 const std::string& md5,
299 const GetFileFromCacheCallback& callback) { 300 const GetFileFromCacheCallback& callback) {
300 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 301 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
301 DCHECK(!callback.is_null()); 302 DCHECK(!callback.is_null());
302 303
303 base::PostTaskAndReplyWithResult( 304 base::PostTaskAndReplyWithResult(
304 blocking_task_runner_, 305 blocking_task_runner_,
305 FROM_HERE, 306 FROM_HERE,
306 base::Bind(&FileCache::GetFileOnBlockingPool, 307 base::Bind(&FileCache::GetFile,
307 base::Unretained(this), resource_id, md5), 308 base::Unretained(this), resource_id, md5),
308 base::Bind(&RunGetFileFromCacheCallback, callback)); 309 base::Bind(&RunGetFileFromCacheCallback, callback));
309 } 310 }
310 311
311 void FileCache::Store(const std::string& resource_id, 312 void FileCache::StoreOnUIThread(const std::string& resource_id,
312 const std::string& md5, 313 const std::string& md5,
313 const base::FilePath& source_path, 314 const base::FilePath& source_path,
314 FileOperationType file_operation_type, 315 FileOperationType file_operation_type,
315 const FileOperationCallback& callback) { 316 const FileOperationCallback& callback) {
316 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
317 DCHECK(!callback.is_null()); 318 DCHECK(!callback.is_null());
318 319
319 base::PostTaskAndReplyWithResult( 320 base::PostTaskAndReplyWithResult(
320 blocking_task_runner_, 321 blocking_task_runner_,
321 FROM_HERE, 322 FROM_HERE,
322 base::Bind(&FileCache::StoreOnBlockingPool, 323 base::Bind(&FileCache::Store,
323 base::Unretained(this), 324 base::Unretained(this),
324 resource_id, md5, source_path, file_operation_type, 325 resource_id, md5, source_path, file_operation_type,
325 CACHED_FILE_FROM_SERVER), 326 CACHED_FILE_FROM_SERVER),
326 callback); 327 callback);
327 } 328 }
328 329
329 void FileCache::StoreLocallyModified( 330 void FileCache::StoreLocallyModifiedOnUIThread(
330 const std::string& resource_id, 331 const std::string& resource_id,
331 const std::string& md5, 332 const std::string& md5,
332 const base::FilePath& source_path, 333 const base::FilePath& source_path,
333 FileOperationType file_operation_type, 334 FileOperationType file_operation_type,
334 const FileOperationCallback& callback) { 335 const FileOperationCallback& callback) {
335 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
336 DCHECK(!callback.is_null()); 337 DCHECK(!callback.is_null());
337 338
338 base::PostTaskAndReplyWithResult( 339 base::PostTaskAndReplyWithResult(
339 blocking_task_runner_, 340 blocking_task_runner_,
340 FROM_HERE, 341 FROM_HERE,
341 base::Bind(&FileCache::StoreOnBlockingPool, 342 base::Bind(&FileCache::Store,
342 base::Unretained(this), 343 base::Unretained(this),
343 resource_id, md5, source_path, file_operation_type, 344 resource_id, md5, source_path, file_operation_type,
344 CACHED_FILE_LOCALLY_MODIFIED), 345 CACHED_FILE_LOCALLY_MODIFIED),
345 base::Bind(&FileCache::OnCommitDirty, 346 base::Bind(&FileCache::OnCommitDirty,
346 weak_ptr_factory_.GetWeakPtr(), resource_id, callback)); 347 weak_ptr_factory_.GetWeakPtr(), resource_id, callback));
347 } 348 }
348 349
349 void FileCache::Pin(const std::string& resource_id, 350 void FileCache::PinOnUIThread(const std::string& resource_id,
350 const std::string& md5, 351 const std::string& md5,
351 const FileOperationCallback& callback) { 352 const FileOperationCallback& callback) {
352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 353 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
353 DCHECK(!callback.is_null()); 354 DCHECK(!callback.is_null());
354 355
355 base::PostTaskAndReplyWithResult( 356 base::PostTaskAndReplyWithResult(
356 blocking_task_runner_, 357 blocking_task_runner_,
357 FROM_HERE, 358 FROM_HERE,
358 base::Bind(&FileCache::PinOnBlockingPool, 359 base::Bind(&FileCache::Pin,
359 base::Unretained(this), resource_id, md5), 360 base::Unretained(this), resource_id, md5),
360 base::Bind(&FileCache::OnPinned, 361 base::Bind(&FileCache::OnPinned,
361 weak_ptr_factory_.GetWeakPtr(), resource_id, md5, callback)); 362 weak_ptr_factory_.GetWeakPtr(), resource_id, md5, callback));
362 } 363 }
363 364
364 void FileCache::Unpin(const std::string& resource_id, 365 void FileCache::UnpinOnUIThread(const std::string& resource_id,
365 const std::string& md5, 366 const std::string& md5,
366 const FileOperationCallback& callback) {
367 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
368 DCHECK(!callback.is_null());
369
370 base::PostTaskAndReplyWithResult(
371 blocking_task_runner_,
372 FROM_HERE,
373 base::Bind(&FileCache::UnpinOnBlockingPool,
374 base::Unretained(this), resource_id, md5),
375 base::Bind(&FileCache::OnUnpinned,
376 weak_ptr_factory_.GetWeakPtr(), resource_id, md5, callback));
377 }
378
379 void FileCache::MarkAsMounted(const std::string& resource_id,
380 const std::string& md5,
381 const GetFileFromCacheCallback& callback) {
382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
383 DCHECK(!callback.is_null());
384
385 base::PostTaskAndReplyWithResult(
386 blocking_task_runner_,
387 FROM_HERE,
388 base::Bind(&FileCache::MarkAsMountedOnBlockingPool,
389 base::Unretained(this), resource_id, md5),
390 base::Bind(RunGetFileFromCacheCallback, callback));
391 }
392
393 void FileCache::MarkAsUnmounted(const base::FilePath& file_path,
394 const FileOperationCallback& callback) { 367 const FileOperationCallback& callback) {
395 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 368 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
396 DCHECK(!callback.is_null()); 369 DCHECK(!callback.is_null());
397 370
398 base::PostTaskAndReplyWithResult( 371 base::PostTaskAndReplyWithResult(
399 blocking_task_runner_, 372 blocking_task_runner_,
400 FROM_HERE, 373 FROM_HERE,
401 base::Bind(&FileCache::MarkAsUnmountedOnBlockingPool, 374 base::Bind(&FileCache::Unpin,
402 base::Unretained(this), file_path), 375 base::Unretained(this), resource_id, md5),
403 callback); 376 base::Bind(&FileCache::OnUnpinned,
377 weak_ptr_factory_.GetWeakPtr(), resource_id, md5, callback));
404 } 378 }
405 379
406 void FileCache::MarkDirty(const std::string& resource_id, 380 void FileCache::MarkAsMountedOnUIThread(
407 const std::string& md5, 381 const std::string& resource_id,
408 const FileOperationCallback& callback) { 382 const std::string& md5,
383 const GetFileFromCacheCallback& callback) {
409 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 384 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
410 DCHECK(!callback.is_null()); 385 DCHECK(!callback.is_null());
411 386
412 base::PostTaskAndReplyWithResult( 387 base::PostTaskAndReplyWithResult(
413 blocking_task_runner_, 388 blocking_task_runner_,
414 FROM_HERE, 389 FROM_HERE,
415 base::Bind(&FileCache::MarkDirtyOnBlockingPool, 390 base::Bind(&FileCache::MarkAsMounted,
416 base::Unretained(this), resource_id, md5), 391 base::Unretained(this), resource_id, md5),
417 callback); 392 base::Bind(RunGetFileFromCacheCallback, callback));
418 } 393 }
419 394
420 void FileCache::CommitDirty(const std::string& resource_id, 395 void FileCache::MarkAsUnmountedOnUIThread(
421 const std::string& md5, 396 const base::FilePath& file_path,
422 const FileOperationCallback& callback) { 397 const FileOperationCallback& callback) {
423 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
424 DCHECK(!callback.is_null()); 399 DCHECK(!callback.is_null());
425 400
426 base::PostTaskAndReplyWithResult( 401 base::PostTaskAndReplyWithResult(
427 blocking_task_runner_, 402 blocking_task_runner_,
428 FROM_HERE, 403 FROM_HERE,
429 base::Bind(&FileCache::CommitDirtyOnBlockingPool, 404 base::Bind(&FileCache::MarkAsUnmounted,
430 base::Unretained(this), resource_id, md5), 405 base::Unretained(this), file_path),
431 base::Bind(&FileCache::OnCommitDirty, 406 callback);
432 weak_ptr_factory_.GetWeakPtr(), resource_id, callback));
433 } 407 }
434 408
435 void FileCache::ClearDirty(const std::string& resource_id, 409 void FileCache::MarkDirtyOnUIThread(const std::string& resource_id,
436 const std::string& md5, 410 const std::string& md5,
437 const FileOperationCallback& callback) { 411 const FileOperationCallback& callback) {
438 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 412 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
439 DCHECK(!callback.is_null()); 413 DCHECK(!callback.is_null());
440 414
441 base::PostTaskAndReplyWithResult( 415 base::PostTaskAndReplyWithResult(
442 blocking_task_runner_, 416 blocking_task_runner_,
443 FROM_HERE, 417 FROM_HERE,
444 base::Bind(&FileCache::ClearDirtyOnBlockingPool, 418 base::Bind(&FileCache::MarkDirty,
445 base::Unretained(this), resource_id, md5), 419 base::Unretained(this), resource_id, md5),
446 callback); 420 callback);
447 } 421 }
448 422
449 void FileCache::Remove(const std::string& resource_id, 423 void FileCache::CommitDirtyOnUIThread(const std::string& resource_id,
450 const FileOperationCallback& callback) { 424 const std::string& md5,
425 const FileOperationCallback& callback) {
451 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 426 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
452 DCHECK(!callback.is_null()); 427 DCHECK(!callback.is_null());
453 428
454 base::PostTaskAndReplyWithResult( 429 base::PostTaskAndReplyWithResult(
455 blocking_task_runner_, 430 blocking_task_runner_,
456 FROM_HERE, 431 FROM_HERE,
457 base::Bind(&FileCache::RemoveOnBlockingPool, 432 base::Bind(&FileCache::CommitDirty,
458 base::Unretained(this), resource_id), 433 base::Unretained(this), resource_id, md5),
459 callback); 434 base::Bind(&FileCache::OnCommitDirty,
435 weak_ptr_factory_.GetWeakPtr(), resource_id, callback));
460 } 436 }
461 437
462 void FileCache::ClearAll(const InitializeCacheCallback& callback) { 438 void FileCache::ClearDirtyOnUIThread(const std::string& resource_id,
439 const std::string& md5,
440 const FileOperationCallback& callback) {
463 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 441 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
464 DCHECK(!callback.is_null()); 442 DCHECK(!callback.is_null());
465 443
466 base::PostTaskAndReplyWithResult( 444 base::PostTaskAndReplyWithResult(
467 blocking_task_runner_, 445 blocking_task_runner_,
468 FROM_HERE, 446 FROM_HERE,
469 base::Bind(&FileCache::ClearAllOnBlockingPool, base::Unretained(this)), 447 base::Bind(&FileCache::ClearDirty,
448 base::Unretained(this), resource_id, md5),
470 callback); 449 callback);
471 } 450 }
472 451
452 void FileCache::RemoveOnUIThread(const std::string& resource_id,
453 const FileOperationCallback& callback) {
454 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
455 DCHECK(!callback.is_null());
456
457 base::PostTaskAndReplyWithResult(
458 blocking_task_runner_,
459 FROM_HERE,
460 base::Bind(&FileCache::Remove,
461 base::Unretained(this), resource_id),
462 callback);
463 }
464
465 void FileCache::ClearAllOnUIThread(const InitializeCacheCallback& callback) {
466 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
467 DCHECK(!callback.is_null());
468
469 base::PostTaskAndReplyWithResult(
470 blocking_task_runner_,
471 FROM_HERE,
472 base::Bind(&FileCache::ClearAll, base::Unretained(this)),
473 callback);
474 }
475
473 void FileCache::RequestInitialize(const InitializeCacheCallback& callback) { 476 void FileCache::RequestInitialize(const InitializeCacheCallback& callback) {
474 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
475 DCHECK(!callback.is_null()); 478 DCHECK(!callback.is_null());
476 479
477 base::PostTaskAndReplyWithResult( 480 base::PostTaskAndReplyWithResult(
478 blocking_task_runner_, 481 blocking_task_runner_,
479 FROM_HERE, 482 FROM_HERE,
480 base::Bind(&FileCache::InitializeOnBlockingPool, base::Unretained(this)), 483 base::Bind(&FileCache::InitializeOnBlockingPool, base::Unretained(this)),
481 callback); 484 callback);
482 } 485 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 metadata_ = FileCacheMetadata::CreateCacheMetadataForTesting( 525 metadata_ = FileCacheMetadata::CreateCacheMetadataForTesting(
523 blocking_task_runner_); 526 blocking_task_runner_);
524 metadata_->Initialize(cache_paths_); 527 metadata_->Initialize(cache_paths_);
525 } 528 }
526 529
527 void FileCache::DestroyOnBlockingPool() { 530 void FileCache::DestroyOnBlockingPool() {
528 AssertOnSequencedWorkerPool(); 531 AssertOnSequencedWorkerPool();
529 delete this; 532 delete this;
530 } 533 }
531 534
532 bool FileCache::GetCacheEntryOnBlockingPool(const std::string& resource_id, 535 bool FileCache::GetCacheEntry(const std::string& resource_id,
533 const std::string& md5, 536 const std::string& md5,
534 FileCacheEntry* entry) { 537 FileCacheEntry* entry) {
535 DCHECK(entry); 538 DCHECK(entry);
536 AssertOnSequencedWorkerPool(); 539 AssertOnSequencedWorkerPool();
537 return metadata_->GetCacheEntry(resource_id, md5, entry); 540 return metadata_->GetCacheEntry(resource_id, md5, entry);
538 } 541 }
539 542
540 void FileCache::IterateOnBlockingPool( 543 void FileCache::Iterate(const CacheIterateCallback& iteration_callback) {
541 const CacheIterateCallback& iteration_callback) {
542 AssertOnSequencedWorkerPool(); 544 AssertOnSequencedWorkerPool();
543 DCHECK(!iteration_callback.is_null()); 545 DCHECK(!iteration_callback.is_null());
544 546
545 metadata_->Iterate(iteration_callback); 547 metadata_->Iterate(iteration_callback);
546 } 548 }
547 549
548 bool FileCache::FreeDiskSpaceOnBlockingPoolIfNeededFor(int64 num_bytes) { 550 bool FileCache::FreeDiskSpaceIfNeededFor(int64 num_bytes) {
549 AssertOnSequencedWorkerPool(); 551 AssertOnSequencedWorkerPool();
550 552
551 // Do nothing and return if we have enough space. 553 // Do nothing and return if we have enough space.
552 if (HasEnoughSpaceFor(num_bytes, cache_root_path_)) 554 if (HasEnoughSpaceFor(num_bytes, cache_root_path_))
553 return true; 555 return true;
554 556
555 // Otherwise, try to free up the disk space. 557 // Otherwise, try to free up the disk space.
556 DVLOG(1) << "Freeing up disk space for " << num_bytes; 558 DVLOG(1) << "Freeing up disk space for " << num_bytes;
557 // First remove temporary files from the metadata. 559 // First remove temporary files from the metadata.
558 metadata_->RemoveTemporaryFiles(); 560 metadata_->RemoveTemporaryFiles();
559 // Then remove all files under "tmp" directory. 561 // Then remove all files under "tmp" directory.
560 RemoveAllFiles(GetCacheDirectoryPath(CACHE_TYPE_TMP)); 562 RemoveAllFiles(GetCacheDirectoryPath(CACHE_TYPE_TMP));
561 563
562 // Check the disk space again. 564 // Check the disk space again.
563 return HasEnoughSpaceFor(num_bytes, cache_root_path_); 565 return HasEnoughSpaceFor(num_bytes, cache_root_path_);
564 } 566 }
565 567
566 scoped_ptr<FileCache::GetFileResult> FileCache::GetFileOnBlockingPool( 568 scoped_ptr<FileCache::GetFileResult> FileCache::GetFile(
567 const std::string& resource_id, 569 const std::string& resource_id,
568 const std::string& md5) { 570 const std::string& md5) {
569 AssertOnSequencedWorkerPool(); 571 AssertOnSequencedWorkerPool();
570 572
571 scoped_ptr<GetFileResult> result(new GetFileResult); 573 scoped_ptr<GetFileResult> result(new GetFileResult);
572 574
573 FileCacheEntry cache_entry; 575 FileCacheEntry cache_entry;
574 if (!GetCacheEntryOnBlockingPool(resource_id, md5, &cache_entry) || 576 if (!GetCacheEntry(resource_id, md5, &cache_entry) ||
575 !cache_entry.is_present()) { 577 !cache_entry.is_present()) {
576 result->first = FILE_ERROR_NOT_FOUND; 578 result->first = FILE_ERROR_NOT_FOUND;
577 return result.Pass(); 579 return result.Pass();
578 } 580 }
579 581
580 CachedFileOrigin file_origin; 582 CachedFileOrigin file_origin;
581 if (cache_entry.is_mounted()) { 583 if (cache_entry.is_mounted()) {
582 file_origin = CACHED_FILE_MOUNTED; 584 file_origin = CACHED_FILE_MOUNTED;
583 } else if (cache_entry.is_dirty()) { 585 } else if (cache_entry.is_dirty()) {
584 file_origin = CACHED_FILE_LOCALLY_MODIFIED; 586 file_origin = CACHED_FILE_LOCALLY_MODIFIED;
585 } else { 587 } else {
586 file_origin = CACHED_FILE_FROM_SERVER; 588 file_origin = CACHED_FILE_FROM_SERVER;
587 } 589 }
588 result->first = FILE_ERROR_OK; 590 result->first = FILE_ERROR_OK;
589 result->second = GetCacheFilePath(resource_id, 591 result->second = GetCacheFilePath(resource_id,
590 md5, 592 md5,
591 GetSubDirectoryType(cache_entry), 593 GetSubDirectoryType(cache_entry),
592 file_origin); 594 file_origin);
593 return result.Pass(); 595 return result.Pass();
594 } 596 }
595 597
596 FileError FileCache::StoreOnBlockingPool( 598 FileError FileCache::Store(const std::string& resource_id,
597 const std::string& resource_id, 599 const std::string& md5,
598 const std::string& md5, 600 const base::FilePath& source_path,
599 const base::FilePath& source_path, 601 FileOperationType file_operation_type,
600 FileOperationType file_operation_type, 602 CachedFileOrigin origin) {
601 CachedFileOrigin origin) {
602 AssertOnSequencedWorkerPool(); 603 AssertOnSequencedWorkerPool();
603 604
604 int64 file_size = 0; 605 int64 file_size = 0;
605 if (file_operation_type == FILE_OPERATION_COPY) { 606 if (file_operation_type == FILE_OPERATION_COPY) {
606 if (!file_util::GetFileSize(source_path, &file_size)) { 607 if (!file_util::GetFileSize(source_path, &file_size)) {
607 LOG(WARNING) << "Couldn't get file size for: " << source_path.value(); 608 LOG(WARNING) << "Couldn't get file size for: " << source_path.value();
608 return FILE_ERROR_FAILED; 609 return FILE_ERROR_FAILED;
609 } 610 }
610 } 611 }
611 if (!FreeDiskSpaceOnBlockingPoolIfNeededFor(file_size)) 612 if (!FreeDiskSpaceIfNeededFor(file_size))
612 return FILE_ERROR_NO_SPACE; 613 return FILE_ERROR_NO_SPACE;
613 614
614 FileCacheEntry cache_entry; 615 FileCacheEntry cache_entry;
615 GetCacheEntryOnBlockingPool(resource_id, std::string(), &cache_entry); 616 GetCacheEntry(resource_id, std::string(), &cache_entry);
616 617
617 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP; 618 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP;
618 if (origin == CACHED_FILE_FROM_SERVER) { 619 if (origin == CACHED_FILE_FROM_SERVER) {
619 // If file is dirty or mounted, return error. 620 // If file is dirty or mounted, return error.
620 if (cache_entry.is_dirty() || cache_entry.is_mounted()) { 621 if (cache_entry.is_dirty() || cache_entry.is_mounted()) {
621 LOG(WARNING) << "Can't store a file to replace a " 622 LOG(WARNING) << "Can't store a file to replace a "
622 << (cache_entry.is_dirty() ? "dirty" : "mounted") 623 << (cache_entry.is_dirty() ? "dirty" : "mounted")
623 << " file: res_id=" << resource_id 624 << " file: res_id=" << resource_id
624 << ", md5=" << md5; 625 << ", md5=" << md5;
625 return FILE_ERROR_IN_USE; 626 return FILE_ERROR_IN_USE;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 if (success) { 672 if (success) {
672 // Now that file operations have completed, update metadata. 673 // Now that file operations have completed, update metadata.
673 cache_entry.set_md5(md5); 674 cache_entry.set_md5(md5);
674 cache_entry.set_is_present(true); 675 cache_entry.set_is_present(true);
675 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); 676 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT);
676 cache_entry.set_is_dirty(origin == CACHED_FILE_LOCALLY_MODIFIED); 677 cache_entry.set_is_dirty(origin == CACHED_FILE_LOCALLY_MODIFIED);
677 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); 678 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry);
678 679
679 // If storing a local modification, commit it. 680 // If storing a local modification, commit it.
680 if (origin == CACHED_FILE_LOCALLY_MODIFIED) 681 if (origin == CACHED_FILE_LOCALLY_MODIFIED)
681 CommitDirtyOnBlockingPool(resource_id, md5); 682 CommitDirty(resource_id, md5);
682 } 683 }
683 684
684 return success ? FILE_ERROR_OK : FILE_ERROR_FAILED; 685 return success ? FILE_ERROR_OK : FILE_ERROR_FAILED;
685 } 686 }
686 687
687 FileError FileCache::PinOnBlockingPool(const std::string& resource_id, 688 FileError FileCache::Pin(const std::string& resource_id,
688 const std::string& md5) { 689 const std::string& md5) {
689 AssertOnSequencedWorkerPool(); 690 AssertOnSequencedWorkerPool();
690 691
691 bool is_persistent = true; 692 bool is_persistent = true;
692 FileCacheEntry cache_entry; 693 FileCacheEntry cache_entry;
693 if (!GetCacheEntryOnBlockingPool(resource_id, md5, &cache_entry)) { 694 if (!GetCacheEntry(resource_id, md5, &cache_entry)) {
694 // The file will be first downloaded in 'tmp', then moved to 'persistent'. 695 // The file will be first downloaded in 'tmp', then moved to 'persistent'.
695 is_persistent = false; 696 is_persistent = false;
696 } else { // File exists in cache, determines destination path. 697 } else { // File exists in cache, determines destination path.
697 // Determine source and destination paths. 698 // Determine source and destination paths.
698 699
699 // If file is dirty or mounted, don't move it. 700 // If file is dirty or mounted, don't move it.
700 if (!cache_entry.is_dirty() && !cache_entry.is_mounted()) { 701 if (!cache_entry.is_dirty() && !cache_entry.is_mounted()) {
701 // If file was pinned before but actual file blob doesn't exist in cache: 702 // If file was pinned before but actual file blob doesn't exist in cache:
702 // - don't need to move the file. 703 // - don't need to move the file.
703 if (!cache_entry.is_present()) { 704 if (!cache_entry.is_present()) {
(...skipping 17 matching lines...) Expand all
721 } 722 }
722 723
723 // Now that file operations have completed, update metadata. 724 // Now that file operations have completed, update metadata.
724 cache_entry.set_md5(md5); 725 cache_entry.set_md5(md5);
725 cache_entry.set_is_pinned(true); 726 cache_entry.set_is_pinned(true);
726 cache_entry.set_is_persistent(is_persistent); 727 cache_entry.set_is_persistent(is_persistent);
727 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); 728 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry);
728 return FILE_ERROR_OK; 729 return FILE_ERROR_OK;
729 } 730 }
730 731
731 FileError FileCache::UnpinOnBlockingPool(const std::string& resource_id, 732 FileError FileCache::Unpin(const std::string& resource_id,
732 const std::string& md5) { 733 const std::string& md5) {
733 AssertOnSequencedWorkerPool(); 734 AssertOnSequencedWorkerPool();
734 735
735 // Unpinning a file means its entry must exist in cache. 736 // Unpinning a file means its entry must exist in cache.
736 FileCacheEntry cache_entry; 737 FileCacheEntry cache_entry;
737 if (!GetCacheEntryOnBlockingPool(resource_id, md5, &cache_entry)) { 738 if (!GetCacheEntry(resource_id, md5, &cache_entry)) {
738 LOG(WARNING) << "Can't unpin a file that wasn't pinned or cached: res_id=" 739 LOG(WARNING) << "Can't unpin a file that wasn't pinned or cached: res_id="
739 << resource_id 740 << resource_id
740 << ", md5=" << md5; 741 << ", md5=" << md5;
741 return FILE_ERROR_NOT_FOUND; 742 return FILE_ERROR_NOT_FOUND;
742 } 743 }
743 744
744 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP; 745 CacheSubDirectoryType sub_dir_type = CACHE_TYPE_TMP;
745 746
746 // If file is dirty or mounted, don't move it. 747 // If file is dirty or mounted, don't move it.
747 if (cache_entry.is_dirty() || cache_entry.is_mounted()) { 748 if (cache_entry.is_dirty() || cache_entry.is_mounted()) {
(...skipping 26 matching lines...) Expand all
774 cache_entry.set_is_pinned(false); 775 cache_entry.set_is_pinned(false);
775 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); 776 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT);
776 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); 777 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry);
777 } else { 778 } else {
778 // Remove the existing entry if we are unpinning a non-present file. 779 // Remove the existing entry if we are unpinning a non-present file.
779 metadata_->RemoveCacheEntry(resource_id); 780 metadata_->RemoveCacheEntry(resource_id);
780 } 781 }
781 return FILE_ERROR_OK; 782 return FILE_ERROR_OK;
782 } 783 }
783 784
784 scoped_ptr<FileCache::GetFileResult> FileCache::MarkAsMountedOnBlockingPool( 785 scoped_ptr<FileCache::GetFileResult> FileCache::MarkAsMounted(
785 const std::string& resource_id, 786 const std::string& resource_id,
786 const std::string& md5) { 787 const std::string& md5) {
787 AssertOnSequencedWorkerPool(); 788 AssertOnSequencedWorkerPool();
788 789
789 scoped_ptr<GetFileResult> result(new GetFileResult); 790 scoped_ptr<GetFileResult> result(new GetFileResult);
790 791
791 // Get cache entry associated with the resource_id and md5 792 // Get cache entry associated with the resource_id and md5
792 FileCacheEntry cache_entry; 793 FileCacheEntry cache_entry;
793 if (!GetCacheEntryOnBlockingPool(resource_id, md5, &cache_entry)) { 794 if (!GetCacheEntry(resource_id, md5, &cache_entry)) {
794 result->first = FILE_ERROR_NOT_FOUND; 795 result->first = FILE_ERROR_NOT_FOUND;
795 return result.Pass(); 796 return result.Pass();
796 } 797 }
797 798
798 if (cache_entry.is_mounted()) { 799 if (cache_entry.is_mounted()) {
799 result->first = FILE_ERROR_INVALID_OPERATION; 800 result->first = FILE_ERROR_INVALID_OPERATION;
800 return result.Pass(); 801 return result.Pass();
801 } 802 }
802 803
803 // Get the subdir type and path for the unmounted state. 804 // Get the subdir type and path for the unmounted state.
(...skipping 23 matching lines...) Expand all
827 cache_entry.set_md5(md5); 828 cache_entry.set_md5(md5);
828 cache_entry.set_is_mounted(true); 829 cache_entry.set_is_mounted(true);
829 cache_entry.set_is_persistent(true); 830 cache_entry.set_is_persistent(true);
830 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); 831 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry);
831 } 832 }
832 result->first = success ? FILE_ERROR_OK : FILE_ERROR_FAILED; 833 result->first = success ? FILE_ERROR_OK : FILE_ERROR_FAILED;
833 result->second = mounted_path; 834 result->second = mounted_path;
834 return result.Pass(); 835 return result.Pass();
835 } 836 }
836 837
837 FileError FileCache::MarkAsUnmountedOnBlockingPool( 838 FileError FileCache::MarkAsUnmounted(const base::FilePath& file_path) {
838 const base::FilePath& file_path) {
839 AssertOnSequencedWorkerPool(); 839 AssertOnSequencedWorkerPool();
840 DCHECK(IsUnderFileCacheDirectory(file_path)); 840 DCHECK(IsUnderFileCacheDirectory(file_path));
841 841
842 // Parse file path to obtain resource_id, md5 and extra_extension. 842 // Parse file path to obtain resource_id, md5 and extra_extension.
843 std::string resource_id; 843 std::string resource_id;
844 std::string md5; 844 std::string md5;
845 std::string extra_extension; 845 std::string extra_extension;
846 util::ParseCacheFilePath(file_path, &resource_id, &md5, &extra_extension); 846 util::ParseCacheFilePath(file_path, &resource_id, &md5, &extra_extension);
847 // The extra_extension shall be ".mounted" iff we're unmounting. 847 // The extra_extension shall be ".mounted" iff we're unmounting.
848 DCHECK_EQ(util::kMountedArchiveFileExtension, extra_extension); 848 DCHECK_EQ(util::kMountedArchiveFileExtension, extra_extension);
849 849
850 // Get cache entry associated with the resource_id and md5 850 // Get cache entry associated with the resource_id and md5
851 FileCacheEntry cache_entry; 851 FileCacheEntry cache_entry;
852 if (!GetCacheEntryOnBlockingPool(resource_id, md5, &cache_entry)) 852 if (!GetCacheEntry(resource_id, md5, &cache_entry))
853 return FILE_ERROR_NOT_FOUND; 853 return FILE_ERROR_NOT_FOUND;
854 854
855 if (!cache_entry.is_mounted()) 855 if (!cache_entry.is_mounted())
856 return FILE_ERROR_INVALID_OPERATION; 856 return FILE_ERROR_INVALID_OPERATION;
857 857
858 // Get the subdir type and path for the unmounted state. 858 // Get the subdir type and path for the unmounted state.
859 CacheSubDirectoryType unmounted_subdir = 859 CacheSubDirectoryType unmounted_subdir =
860 cache_entry.is_pinned() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; 860 cache_entry.is_pinned() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP;
861 base::FilePath unmounted_path = GetCacheFilePath( 861 base::FilePath unmounted_path = GetCacheFilePath(
862 resource_id, md5, unmounted_subdir, CACHED_FILE_FROM_SERVER); 862 resource_id, md5, unmounted_subdir, CACHED_FILE_FROM_SERVER);
863 863
864 // Get the subdir type and path for the mounted state. 864 // Get the subdir type and path for the mounted state.
865 CacheSubDirectoryType mounted_subdir = CACHE_TYPE_PERSISTENT; 865 CacheSubDirectoryType mounted_subdir = CACHE_TYPE_PERSISTENT;
866 base::FilePath mounted_path = GetCacheFilePath( 866 base::FilePath mounted_path = GetCacheFilePath(
867 resource_id, md5, mounted_subdir, CACHED_FILE_MOUNTED); 867 resource_id, md5, mounted_subdir, CACHED_FILE_MOUNTED);
868 868
869 // Move cache file. 869 // Move cache file.
870 if (!MoveFile(mounted_path, unmounted_path)) 870 if (!MoveFile(mounted_path, unmounted_path))
871 return FILE_ERROR_FAILED; 871 return FILE_ERROR_FAILED;
872 872
873 // Now that cache operation is complete, update metadata. 873 // Now that cache operation is complete, update metadata.
874 cache_entry.set_md5(md5); 874 cache_entry.set_md5(md5);
875 cache_entry.set_is_mounted(false); 875 cache_entry.set_is_mounted(false);
876 cache_entry.set_is_persistent(unmounted_subdir == CACHE_TYPE_PERSISTENT); 876 cache_entry.set_is_persistent(unmounted_subdir == CACHE_TYPE_PERSISTENT);
877 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); 877 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry);
878 return FILE_ERROR_OK; 878 return FILE_ERROR_OK;
879 } 879 }
880 880
881 FileError FileCache::MarkDirtyOnBlockingPool( 881 FileError FileCache::MarkDirty(const std::string& resource_id,
882 const std::string& resource_id, 882 const std::string& md5) {
883 const std::string& md5) {
884 AssertOnSequencedWorkerPool(); 883 AssertOnSequencedWorkerPool();
885 884
886 // If file has already been marked dirty in previous instance of chrome, we 885 // If file has already been marked dirty in previous instance of chrome, we
887 // would have lost the md5 info during cache initialization, because the file 886 // would have lost the md5 info during cache initialization, because the file
888 // would have been renamed to .local extension. 887 // would have been renamed to .local extension.
889 // So, search for entry in cache without comparing md5. 888 // So, search for entry in cache without comparing md5.
890 889
891 // Marking a file dirty means its entry and actual file blob must exist in 890 // Marking a file dirty means its entry and actual file blob must exist in
892 // cache. 891 // cache.
893 FileCacheEntry cache_entry; 892 FileCacheEntry cache_entry;
894 if (!GetCacheEntryOnBlockingPool(resource_id, std::string(), &cache_entry) || 893 if (!GetCacheEntry(resource_id, std::string(), &cache_entry) ||
895 !cache_entry.is_present()) { 894 !cache_entry.is_present()) {
896 LOG(WARNING) << "Can't mark dirty a file that wasn't cached: res_id=" 895 LOG(WARNING) << "Can't mark dirty a file that wasn't cached: res_id="
897 << resource_id 896 << resource_id
898 << ", md5=" << md5; 897 << ", md5=" << md5;
899 return FILE_ERROR_NOT_FOUND; 898 return FILE_ERROR_NOT_FOUND;
900 } 899 }
901 900
902 // If a file is already dirty (i.e. MarkDirtyInCache was called before), 901 // If a file is already dirty (i.e. MarkDirtyInCache was called before),
903 // delete outgoing symlink if it exists. 902 // delete outgoing symlink if it exists.
904 // TODO(benchan): We should only delete outgoing symlink if file is currently 903 // TODO(benchan): We should only delete outgoing symlink if file is currently
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
939 return FILE_ERROR_FAILED; 938 return FILE_ERROR_FAILED;
940 939
941 // Now that file operations have completed, update metadata. 940 // Now that file operations have completed, update metadata.
942 cache_entry.set_md5(md5); 941 cache_entry.set_md5(md5);
943 cache_entry.set_is_dirty(true); 942 cache_entry.set_is_dirty(true);
944 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); 943 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT);
945 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); 944 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry);
946 return FILE_ERROR_OK; 945 return FILE_ERROR_OK;
947 } 946 }
948 947
949 FileError FileCache::CommitDirtyOnBlockingPool( 948 FileError FileCache::CommitDirty(const std::string& resource_id,
950 const std::string& resource_id, 949 const std::string& md5) {
951 const std::string& md5) {
952 AssertOnSequencedWorkerPool(); 950 AssertOnSequencedWorkerPool();
953 951
954 // If file has already been marked dirty in previous instance of chrome, we 952 // If file has already been marked dirty in previous instance of chrome, we
955 // would have lost the md5 info during cache initialization, because the file 953 // would have lost the md5 info during cache initialization, because the file
956 // would have been renamed to .local extension. 954 // would have been renamed to .local extension.
957 // So, search for entry in cache without comparing md5. 955 // So, search for entry in cache without comparing md5.
958 956
959 // Committing a file dirty means its entry and actual file blob must exist in 957 // Committing a file dirty means its entry and actual file blob must exist in
960 // cache. 958 // cache.
961 FileCacheEntry cache_entry; 959 FileCacheEntry cache_entry;
962 if (!GetCacheEntryOnBlockingPool(resource_id, std::string(), &cache_entry) || 960 if (!GetCacheEntry(resource_id, std::string(), &cache_entry) ||
963 !cache_entry.is_present()) { 961 !cache_entry.is_present()) {
964 LOG(WARNING) << "Can't commit dirty a file that wasn't cached: res_id=" 962 LOG(WARNING) << "Can't commit dirty a file that wasn't cached: res_id="
965 << resource_id 963 << resource_id
966 << ", md5=" << md5; 964 << ", md5=" << md5;
967 return FILE_ERROR_NOT_FOUND; 965 return FILE_ERROR_NOT_FOUND;
968 } 966 }
969 967
970 // If a file is not dirty (it should have been marked dirty via 968 // If a file is not dirty (it should have been marked dirty via
971 // MarkDirtyInCache), committing it dirty is an invalid operation. 969 // MarkDirtyInCache), committing it dirty is an invalid operation.
972 if (!cache_entry.is_dirty()) { 970 if (!cache_entry.is_dirty()) {
(...skipping 16 matching lines...) Expand all
989 base::FilePath target_path = GetCacheFilePath( 987 base::FilePath target_path = GetCacheFilePath(
990 resource_id, 988 resource_id,
991 md5, 989 md5,
992 GetSubDirectoryType(cache_entry), 990 GetSubDirectoryType(cache_entry),
993 CACHED_FILE_LOCALLY_MODIFIED); 991 CACHED_FILE_LOCALLY_MODIFIED);
994 992
995 return CreateSymlink(target_path, symlink_path) ? 993 return CreateSymlink(target_path, symlink_path) ?
996 FILE_ERROR_OK : FILE_ERROR_FAILED; 994 FILE_ERROR_OK : FILE_ERROR_FAILED;
997 } 995 }
998 996
999 FileError FileCache::ClearDirtyOnBlockingPool( 997 FileError FileCache::ClearDirty(const std::string& resource_id,
1000 const std::string& resource_id, 998 const std::string& md5) {
1001 const std::string& md5) {
1002 AssertOnSequencedWorkerPool(); 999 AssertOnSequencedWorkerPool();
1003 1000
1004 // |md5| is the new .<md5> extension to rename the file to. 1001 // |md5| is the new .<md5> extension to rename the file to.
1005 // So, search for entry in cache without comparing md5. 1002 // So, search for entry in cache without comparing md5.
1006 FileCacheEntry cache_entry; 1003 FileCacheEntry cache_entry;
1007 1004
1008 // Clearing a dirty file means its entry and actual file blob must exist in 1005 // Clearing a dirty file means its entry and actual file blob must exist in
1009 // cache. 1006 // cache.
1010 if (!GetCacheEntryOnBlockingPool(resource_id, std::string(), &cache_entry) || 1007 if (!GetCacheEntry(resource_id, std::string(), &cache_entry) ||
1011 !cache_entry.is_present()) { 1008 !cache_entry.is_present()) {
1012 LOG(WARNING) << "Can't clear dirty state of a file that wasn't cached: " 1009 LOG(WARNING) << "Can't clear dirty state of a file that wasn't cached: "
1013 << "res_id=" << resource_id 1010 << "res_id=" << resource_id
1014 << ", md5=" << md5; 1011 << ", md5=" << md5;
1015 return FILE_ERROR_NOT_FOUND; 1012 return FILE_ERROR_NOT_FOUND;
1016 } 1013 }
1017 1014
1018 // If a file is not dirty (it should have been marked dirty via 1015 // If a file is not dirty (it should have been marked dirty via
1019 // MarkDirtyInCache), clearing its dirty state is an invalid operation. 1016 // MarkDirtyInCache), clearing its dirty state is an invalid operation.
1020 if (!cache_entry.is_dirty()) { 1017 if (!cache_entry.is_dirty()) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1055 DeleteSymlink(symlink_path); 1052 DeleteSymlink(symlink_path);
1056 1053
1057 // Now that file operations have completed, update metadata. 1054 // Now that file operations have completed, update metadata.
1058 cache_entry.set_md5(md5); 1055 cache_entry.set_md5(md5);
1059 cache_entry.set_is_dirty(false); 1056 cache_entry.set_is_dirty(false);
1060 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT); 1057 cache_entry.set_is_persistent(sub_dir_type == CACHE_TYPE_PERSISTENT);
1061 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry); 1058 metadata_->AddOrUpdateCacheEntry(resource_id, cache_entry);
1062 return FILE_ERROR_OK; 1059 return FILE_ERROR_OK;
1063 } 1060 }
1064 1061
1065 FileError FileCache::RemoveOnBlockingPool( 1062 FileError FileCache::Remove(const std::string& resource_id) {
1066 const std::string& resource_id) {
1067 AssertOnSequencedWorkerPool(); 1063 AssertOnSequencedWorkerPool();
1068 1064
1069 // MD5 is not passed into RemoveCacheEntry because we would delete all 1065 // MD5 is not passed into RemoveCacheEntry because we would delete all
1070 // cache files corresponding to <resource_id> regardless of the md5. 1066 // cache files corresponding to <resource_id> regardless of the md5.
1071 // So, search for entry in cache without taking md5 into account. 1067 // So, search for entry in cache without taking md5 into account.
1072 FileCacheEntry cache_entry; 1068 FileCacheEntry cache_entry;
1073 1069
1074 // If entry doesn't exist or is dirty or mounted in cache, nothing to do. 1070 // If entry doesn't exist or is dirty or mounted in cache, nothing to do.
1075 const bool entry_found = 1071 const bool entry_found =
1076 GetCacheEntryOnBlockingPool(resource_id, std::string(), &cache_entry); 1072 GetCacheEntry(resource_id, std::string(), &cache_entry);
1077 if (!entry_found || cache_entry.is_dirty() || cache_entry.is_mounted()) { 1073 if (!entry_found || cache_entry.is_dirty() || cache_entry.is_mounted()) {
1078 DVLOG(1) << "Entry is " 1074 DVLOG(1) << "Entry is "
1079 << (entry_found ? 1075 << (entry_found ?
1080 (cache_entry.is_dirty() ? "dirty" : "mounted") : 1076 (cache_entry.is_dirty() ? "dirty" : "mounted") :
1081 "non-existent") 1077 "non-existent")
1082 << " in cache, not removing"; 1078 << " in cache, not removing";
1083 return FILE_ERROR_OK; 1079 return FILE_ERROR_OK;
1084 } 1080 }
1085 1081
1086 // Determine paths to delete all cache versions of |resource_id| in 1082 // Determine paths to delete all cache versions of |resource_id| in
(...skipping 22 matching lines...) Expand all
1109 for (size_t i = 0; i < paths_to_delete.size(); ++i) { 1105 for (size_t i = 0; i < paths_to_delete.size(); ++i) {
1110 DeleteFilesSelectively(paths_to_delete[i], path_to_keep); 1106 DeleteFilesSelectively(paths_to_delete[i], path_to_keep);
1111 } 1107 }
1112 1108
1113 // Now that all file operations have completed, remove from metadata. 1109 // Now that all file operations have completed, remove from metadata.
1114 metadata_->RemoveCacheEntry(resource_id); 1110 metadata_->RemoveCacheEntry(resource_id);
1115 1111
1116 return FILE_ERROR_OK; 1112 return FILE_ERROR_OK;
1117 } 1113 }
1118 1114
1119 bool FileCache::ClearAllOnBlockingPool() { 1115 bool FileCache::ClearAll() {
1120 AssertOnSequencedWorkerPool(); 1116 AssertOnSequencedWorkerPool();
1121 1117
1122 if (!file_util::Delete(cache_root_path_, true)) { 1118 if (!file_util::Delete(cache_root_path_, true)) {
1123 LOG(WARNING) << "Failed to delete the cache directory"; 1119 LOG(WARNING) << "Failed to delete the cache directory";
1124 return false; 1120 return false;
1125 } 1121 }
1126 1122
1127 if (!InitializeOnBlockingPool()) { 1123 if (!InitializeOnBlockingPool()) {
1128 LOG(WARNING) << "Failed to initialize the cache"; 1124 LOG(WARNING) << "Failed to initialize the cache";
1129 return false; 1125 return false;
(...skipping 28 matching lines...) Expand all
1158 if (error == FILE_ERROR_OK) 1154 if (error == FILE_ERROR_OK)
1159 FOR_EACH_OBSERVER(FileCacheObserver, 1155 FOR_EACH_OBSERVER(FileCacheObserver,
1160 observers_, 1156 observers_,
1161 OnCacheUnpinned(resource_id, md5)); 1157 OnCacheUnpinned(resource_id, md5));
1162 1158
1163 // Now the file is moved from "persistent" to "tmp" directory. 1159 // Now the file is moved from "persistent" to "tmp" directory.
1164 // It's a chance to free up space if needed. 1160 // It's a chance to free up space if needed.
1165 blocking_task_runner_->PostTask( 1161 blocking_task_runner_->PostTask(
1166 FROM_HERE, 1162 FROM_HERE,
1167 base::Bind( 1163 base::Bind(
1168 base::IgnoreResult( 1164 base::IgnoreResult(&FileCache::FreeDiskSpaceIfNeededFor),
1169 &FileCache::FreeDiskSpaceOnBlockingPoolIfNeededFor),
1170 base::Unretained(this), 0)); 1165 base::Unretained(this), 0));
1171 } 1166 }
1172 1167
1173 void FileCache::OnCommitDirty(const std::string& resource_id, 1168 void FileCache::OnCommitDirty(const std::string& resource_id,
1174 const FileOperationCallback& callback, 1169 const FileOperationCallback& callback,
1175 FileError error) { 1170 FileError error) {
1176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1177 DCHECK(!callback.is_null()); 1172 DCHECK(!callback.is_null());
1178 1173
1179 callback.Run(error); 1174 callback.Run(error);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1232 } 1227 }
1233 1228
1234 // static 1229 // static
1235 FileCache::CacheSubDirectoryType FileCache::GetSubDirectoryType( 1230 FileCache::CacheSubDirectoryType FileCache::GetSubDirectoryType(
1236 const FileCacheEntry& cache_entry) { 1231 const FileCacheEntry& cache_entry) {
1237 return cache_entry.is_persistent() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP; 1232 return cache_entry.is_persistent() ? CACHE_TYPE_PERSISTENT : CACHE_TYPE_TMP;
1238 } 1233 }
1239 1234
1240 } // namespace internal 1235 } // namespace internal
1241 } // namespace drive 1236 } // namespace drive
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/drive/file_cache.h ('k') | chrome/browser/chromeos/drive/file_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698