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

Side by Side Diff: webkit/browser/fileapi/syncable/local_file_sync_context.cc

Issue 18668003: SyncFS: Introduce SyncFileSystemBackend (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: lazy initialization 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 "webkit/browser/fileapi/syncable/local_file_sync_context.h" 5 #include "webkit/browser/fileapi/syncable/local_file_sync_context.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/platform_file.h" 9 #include "base/platform_file.h"
10 #include "base/single_thread_task_runner.h" 10 #include "base/single_thread_task_runner.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/task_runner_util.h" 12 #include "base/task_runner_util.h"
13 #include "webkit/browser/fileapi/file_system_context.h" 13 #include "webkit/browser/fileapi/file_system_context.h"
14 #include "webkit/browser/fileapi/file_system_file_util.h" 14 #include "webkit/browser/fileapi/file_system_file_util.h"
15 #include "webkit/browser/fileapi/file_system_operation_context.h" 15 #include "webkit/browser/fileapi/file_system_operation_context.h"
16 #include "webkit/browser/fileapi/file_system_operation_runner.h" 16 #include "webkit/browser/fileapi/file_system_operation_runner.h"
17 #include "webkit/browser/fileapi/file_system_task_runners.h" 17 #include "webkit/browser/fileapi/file_system_task_runners.h"
18 #include "webkit/browser/fileapi/syncable/file_change.h" 18 #include "webkit/browser/fileapi/syncable/file_change.h"
19 #include "webkit/browser/fileapi/syncable/local_file_change_tracker.h" 19 #include "webkit/browser/fileapi/syncable/local_file_change_tracker.h"
20 #include "webkit/browser/fileapi/syncable/local_origin_change_observer.h" 20 #include "webkit/browser/fileapi/syncable/local_origin_change_observer.h"
21 #include "webkit/browser/fileapi/syncable/sync_file_metadata.h" 21 #include "webkit/browser/fileapi/syncable/sync_file_metadata.h"
22 #include "webkit/browser/fileapi/syncable/sync_file_system_backend.h"
22 #include "webkit/browser/fileapi/syncable/syncable_file_operation_runner.h" 23 #include "webkit/browser/fileapi/syncable/syncable_file_operation_runner.h"
23 #include "webkit/browser/fileapi/syncable/syncable_file_system_util.h" 24 #include "webkit/browser/fileapi/syncable/syncable_file_system_util.h"
24 #include "webkit/common/fileapi/file_system_util.h" 25 #include "webkit/common/fileapi/file_system_util.h"
25 26
26 using fileapi::FileSystemContext; 27 using fileapi::FileSystemContext;
27 using fileapi::FileSystemFileUtil; 28 using fileapi::FileSystemFileUtil;
28 using fileapi::FileSystemOperation; 29 using fileapi::FileSystemOperation;
29 using fileapi::FileSystemOperationContext; 30 using fileapi::FileSystemOperationContext;
30 using fileapi::FileSystemURL; 31 using fileapi::FileSystemURL;
31 using fileapi::LocalFileSystemOperation; 32 using fileapi::LocalFileSystemOperation;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 if (!file_system_context->task_runners()->file_task_runner()-> 113 if (!file_system_context->task_runners()->file_task_runner()->
113 RunsTasksOnCurrentThread()) { 114 RunsTasksOnCurrentThread()) {
114 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 115 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
115 file_system_context->task_runners()->file_task_runner()->PostTask( 116 file_system_context->task_runners()->file_task_runner()->PostTask(
116 FROM_HERE, 117 FROM_HERE,
117 base::Bind(&LocalFileSyncContext::ClearChangesForURL, 118 base::Bind(&LocalFileSyncContext::ClearChangesForURL,
118 this, make_scoped_refptr(file_system_context), 119 this, make_scoped_refptr(file_system_context),
119 url, done_callback)); 120 url, done_callback));
120 return; 121 return;
121 } 122 }
122 DCHECK(file_system_context->change_tracker()); 123
123 file_system_context->change_tracker()->ClearChangesForURL(url); 124 SyncFileSystemBackend* backend =
125 SyncFileSystemBackend::GetBackend(file_system_context);
126 DCHECK(backend);
127 DCHECK(backend->change_tracker());
128 backend->change_tracker()->ClearChangesForURL(url);
124 129
125 // Call the completion callback on UI thread. 130 // Call the completion callback on UI thread.
126 ui_task_runner_->PostTask(FROM_HERE, done_callback); 131 ui_task_runner_->PostTask(FROM_HERE, done_callback);
127 } 132 }
128 133
129 void LocalFileSyncContext::ClearSyncFlagForURL(const FileSystemURL& url) { 134 void LocalFileSyncContext::ClearSyncFlagForURL(const FileSystemURL& url) {
130 // This is initially called on UI thread and to be relayed to IO thread. 135 // This is initially called on UI thread and to be relayed to IO thread.
131 io_task_runner_->PostTask( 136 io_task_runner_->PostTask(
132 FROM_HERE, 137 FROM_HERE,
133 base::Bind(&LocalFileSyncContext::EnableWritingOnIOThread, 138 base::Bind(&LocalFileSyncContext::EnableWritingOnIOThread,
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 RunsTasksOnCurrentThread()) { 301 RunsTasksOnCurrentThread()) {
297 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 302 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
298 file_system_context->task_runners()->file_task_runner()->PostTask( 303 file_system_context->task_runners()->file_task_runner()->PostTask(
299 FROM_HERE, 304 FROM_HERE,
300 base::Bind(&LocalFileSyncContext::RecordFakeLocalChange, 305 base::Bind(&LocalFileSyncContext::RecordFakeLocalChange,
301 this, make_scoped_refptr(file_system_context), 306 this, make_scoped_refptr(file_system_context),
302 url, change, callback)); 307 url, change, callback));
303 return; 308 return;
304 } 309 }
305 310
306 DCHECK(file_system_context->change_tracker()); 311 SyncFileSystemBackend* backend =
307 file_system_context->change_tracker()->MarkDirtyOnDatabase(url); 312 SyncFileSystemBackend::GetBackend(file_system_context);
308 file_system_context->change_tracker()->RecordChange(url, change); 313 DCHECK(backend);
314 DCHECK(backend->change_tracker());
315 backend->change_tracker()->MarkDirtyOnDatabase(url);
316 backend->change_tracker()->RecordChange(url, change);
309 317
310 // Fire the callback on UI thread. 318 // Fire the callback on UI thread.
311 ui_task_runner_->PostTask(FROM_HERE, 319 ui_task_runner_->PostTask(FROM_HERE,
312 base::Bind(callback, 320 base::Bind(callback,
313 SYNC_STATUS_OK)); 321 SYNC_STATUS_OK));
314 } 322 }
315 323
316 void LocalFileSyncContext::GetFileMetadata( 324 void LocalFileSyncContext::GetFileMetadata(
317 FileSystemContext* file_system_context, 325 FileSystemContext* file_system_context,
318 const FileSystemURL& url, 326 const FileSystemURL& url,
(...skipping 26 matching lines...) Expand all
345 RunsTasksOnCurrentThread()) { 353 RunsTasksOnCurrentThread()) {
346 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 354 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
347 file_system_context->task_runners()->file_task_runner()->PostTask( 355 file_system_context->task_runners()->file_task_runner()->PostTask(
348 FROM_HERE, 356 FROM_HERE,
349 base::Bind(&LocalFileSyncContext::HasPendingLocalChanges, 357 base::Bind(&LocalFileSyncContext::HasPendingLocalChanges,
350 this, make_scoped_refptr(file_system_context), 358 this, make_scoped_refptr(file_system_context),
351 url, callback)); 359 url, callback));
352 return; 360 return;
353 } 361 }
354 362
355 DCHECK(file_system_context->change_tracker()); 363 SyncFileSystemBackend* backend =
364 SyncFileSystemBackend::GetBackend(file_system_context);
365 DCHECK(backend);
366 DCHECK(backend->change_tracker());
356 FileChangeList changes; 367 FileChangeList changes;
357 file_system_context->change_tracker()->GetChangesForURL(url, &changes); 368 backend->change_tracker()->GetChangesForURL(url, &changes);
358 369
359 // Fire the callback on UI thread. 370 // Fire the callback on UI thread.
360 ui_task_runner_->PostTask(FROM_HERE, 371 ui_task_runner_->PostTask(FROM_HERE,
361 base::Bind(callback, 372 base::Bind(callback,
362 SYNC_STATUS_OK, 373 SYNC_STATUS_OK,
363 !changes.empty())); 374 !changes.empty()));
364 } 375 }
365 376
366 void LocalFileSyncContext::AddOriginChangeObserver( 377 void LocalFileSyncContext::AddOriginChangeObserver(
367 LocalOriginChangeObserver* observer) { 378 LocalOriginChangeObserver* observer) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 operation_runner_.reset(); 450 operation_runner_.reset();
440 sync_status_.reset(); 451 sync_status_.reset();
441 timer_on_io_.reset(); 452 timer_on_io_.reset();
442 } 453 }
443 454
444 void LocalFileSyncContext::InitializeFileSystemContextOnIOThread( 455 void LocalFileSyncContext::InitializeFileSystemContextOnIOThread(
445 const GURL& source_url, 456 const GURL& source_url,
446 FileSystemContext* file_system_context) { 457 FileSystemContext* file_system_context) {
447 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); 458 DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
448 DCHECK(file_system_context); 459 DCHECK(file_system_context);
449 if (!file_system_context->change_tracker()) { 460 SyncFileSystemBackend* backend =
461 SyncFileSystemBackend::GetBackend(file_system_context);
462 DCHECK(backend);
463 if (!backend->change_tracker()) {
450 // First registers the service name. 464 // First registers the service name.
451 RegisterSyncableFileSystem(); 465 RegisterSyncableFileSystem();
452 // Create and initialize LocalFileChangeTracker and call back this method 466 // Create and initialize LocalFileChangeTracker and call back this method
453 // later again. 467 // later again.
454 std::set<GURL>* origins_with_changes = new std::set<GURL>; 468 std::set<GURL>* origins_with_changes = new std::set<GURL>;
455 scoped_ptr<LocalFileChangeTracker>* tracker_ptr( 469 scoped_ptr<LocalFileChangeTracker>* tracker_ptr(
456 new scoped_ptr<LocalFileChangeTracker>); 470 new scoped_ptr<LocalFileChangeTracker>);
457 base::PostTaskAndReplyWithResult( 471 base::PostTaskAndReplyWithResult(
458 file_system_context->task_runners()->file_task_runner(), 472 file_system_context->task_runners()->file_task_runner(),
459 FROM_HERE, 473 FROM_HERE,
(...skipping 11 matching lines...) Expand all
471 if (!operation_runner_) { 485 if (!operation_runner_) {
472 DCHECK(!sync_status_); 486 DCHECK(!sync_status_);
473 DCHECK(!timer_on_io_); 487 DCHECK(!timer_on_io_);
474 sync_status_.reset(new LocalFileSyncStatus); 488 sync_status_.reset(new LocalFileSyncStatus);
475 timer_on_io_.reset(new base::OneShotTimer<LocalFileSyncContext>); 489 timer_on_io_.reset(new base::OneShotTimer<LocalFileSyncContext>);
476 operation_runner_.reset(new SyncableFileOperationRunner( 490 operation_runner_.reset(new SyncableFileOperationRunner(
477 kMaxConcurrentSyncableOperation, 491 kMaxConcurrentSyncableOperation,
478 sync_status_.get())); 492 sync_status_.get()));
479 sync_status_->AddObserver(this); 493 sync_status_->AddObserver(this);
480 } 494 }
481 file_system_context->set_sync_context(this); 495 backend->set_sync_context(this);
482 DidInitialize(source_url, file_system_context, 496 DidInitialize(source_url, file_system_context,
483 SYNC_STATUS_OK); 497 SYNC_STATUS_OK);
484 } 498 }
485 499
486 SyncStatusCode LocalFileSyncContext::InitializeChangeTrackerOnFileThread( 500 SyncStatusCode LocalFileSyncContext::InitializeChangeTrackerOnFileThread(
487 scoped_ptr<LocalFileChangeTracker>* tracker_ptr, 501 scoped_ptr<LocalFileChangeTracker>* tracker_ptr,
488 FileSystemContext* file_system_context, 502 FileSystemContext* file_system_context,
489 std::set<GURL>* origins_with_changes) { 503 std::set<GURL>* origins_with_changes) {
490 DCHECK(file_system_context); 504 DCHECK(file_system_context);
491 DCHECK(tracker_ptr); 505 DCHECK(tracker_ptr);
(...skipping 21 matching lines...) Expand all
513 FileSystemContext* file_system_context, 527 FileSystemContext* file_system_context,
514 std::set<GURL>* origins_with_changes, 528 std::set<GURL>* origins_with_changes,
515 SyncStatusCode status) { 529 SyncStatusCode status) {
516 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); 530 DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
517 DCHECK(file_system_context); 531 DCHECK(file_system_context);
518 DCHECK(origins_with_changes); 532 DCHECK(origins_with_changes);
519 if (status != SYNC_STATUS_OK) { 533 if (status != SYNC_STATUS_OK) {
520 DidInitialize(source_url, file_system_context, status); 534 DidInitialize(source_url, file_system_context, status);
521 return; 535 return;
522 } 536 }
523 file_system_context->SetLocalFileChangeTracker(tracker_ptr->Pass()); 537
538 SyncFileSystemBackend* backend =
539 SyncFileSystemBackend::GetBackend(file_system_context);
540 DCHECK(backend);
541 backend->set_change_tracker(tracker_ptr->Pass());
524 542
525 origins_with_pending_changes_.insert(origins_with_changes->begin(), 543 origins_with_pending_changes_.insert(origins_with_changes->begin(),
526 origins_with_changes->end()); 544 origins_with_changes->end());
527 ScheduleNotifyChangesUpdatedOnIOThread(); 545 ScheduleNotifyChangesUpdatedOnIOThread();
528 546
529 InitializeFileSystemContextOnIOThread(source_url, file_system_context); 547 InitializeFileSystemContextOnIOThread(source_url, file_system_context);
530 } 548 }
531 549
532 void LocalFileSyncContext::DidInitialize( 550 void LocalFileSyncContext::DidInitialize(
533 const GURL& source_url, 551 const GURL& source_url,
534 FileSystemContext* file_system_context, 552 FileSystemContext* file_system_context,
535 SyncStatusCode status) { 553 SyncStatusCode status) {
536 if (!ui_task_runner_->RunsTasksOnCurrentThread()) { 554 if (!ui_task_runner_->RunsTasksOnCurrentThread()) {
537 ui_task_runner_->PostTask( 555 ui_task_runner_->PostTask(
538 FROM_HERE, 556 FROM_HERE,
539 base::Bind(&LocalFileSyncContext::DidInitialize, 557 base::Bind(&LocalFileSyncContext::DidInitialize,
540 this, source_url, 558 this, source_url,
541 make_scoped_refptr(file_system_context), status)); 559 make_scoped_refptr(file_system_context), status));
542 return; 560 return;
543 } 561 }
544 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 562 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
545 DCHECK(!ContainsKey(file_system_contexts_, file_system_context)); 563 DCHECK(!ContainsKey(file_system_contexts_, file_system_context));
546 DCHECK(ContainsKey(pending_initialize_callbacks_, file_system_context)); 564 DCHECK(ContainsKey(pending_initialize_callbacks_, file_system_context));
547 DCHECK(file_system_context->change_tracker()); 565
566
567 SyncFileSystemBackend* backend =
568 SyncFileSystemBackend::GetBackend(file_system_context);
569 DCHECK(backend);
570 DCHECK(backend->change_tracker());
548 571
549 file_system_contexts_.insert(file_system_context); 572 file_system_contexts_.insert(file_system_context);
550 573
551 StatusCallbackQueue& callback_queue = 574 StatusCallbackQueue& callback_queue =
552 pending_initialize_callbacks_[file_system_context]; 575 pending_initialize_callbacks_[file_system_context];
553 for (StatusCallbackQueue::iterator iter = callback_queue.begin(); 576 for (StatusCallbackQueue::iterator iter = callback_queue.begin();
554 iter != callback_queue.end(); ++iter) { 577 iter != callback_queue.end(); ++iter) {
555 ui_task_runner_->PostTask(FROM_HERE, base::Bind(*iter, status)); 578 ui_task_runner_->PostTask(FROM_HERE, base::Bind(*iter, status));
556 } 579 }
557 pending_initialize_callbacks_.erase(file_system_context); 580 pending_initialize_callbacks_.erase(file_system_context);
558 } 581 }
559 582
560 void LocalFileSyncContext::GetNextURLsForSyncOnFileThread( 583 void LocalFileSyncContext::GetNextURLsForSyncOnFileThread(
561 FileSystemContext* file_system_context, 584 FileSystemContext* file_system_context,
562 std::deque<FileSystemURL>* urls) { 585 std::deque<FileSystemURL>* urls) {
563 DCHECK(file_system_context); 586 DCHECK(file_system_context);
564 DCHECK(file_system_context->task_runners()->file_task_runner()-> 587 DCHECK(file_system_context->task_runners()->file_task_runner()->
565 RunsTasksOnCurrentThread()); 588 RunsTasksOnCurrentThread());
566 DCHECK(file_system_context->change_tracker()); 589
567 file_system_context->change_tracker()->GetNextChangedURLs( 590 SyncFileSystemBackend* backend =
591 SyncFileSystemBackend::GetBackend(file_system_context);
592 DCHECK(backend);
593 DCHECK(backend->change_tracker());
594 backend->change_tracker()->GetNextChangedURLs(
568 urls, kMaxURLsToFetchForLocalSync); 595 urls, kMaxURLsToFetchForLocalSync);
569 } 596 }
570 597
571 void LocalFileSyncContext::TryPrepareForLocalSync( 598 void LocalFileSyncContext::TryPrepareForLocalSync(
572 FileSystemContext* file_system_context, 599 FileSystemContext* file_system_context,
573 std::deque<FileSystemURL>* urls, 600 std::deque<FileSystemURL>* urls,
574 const LocalFileSyncInfoCallback& callback) { 601 const LocalFileSyncInfoCallback& callback) {
575 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 602 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
576 DCHECK(urls); 603 DCHECK(urls);
577 604
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 return; 655 return;
629 } 656 }
630 file_system_context->task_runners()->file_task_runner()->PostTask( 657 file_system_context->task_runners()->file_task_runner()->PostTask(
631 FROM_HERE, 658 FROM_HERE,
632 base::Bind(&LocalFileSyncContext::DidGetWritingStatusForSync, 659 base::Bind(&LocalFileSyncContext::DidGetWritingStatusForSync,
633 this, make_scoped_refptr(file_system_context), 660 this, make_scoped_refptr(file_system_context),
634 status, url, callback)); 661 status, url, callback));
635 return; 662 return;
636 } 663 }
637 664
638 DCHECK(file_system_context->change_tracker()); 665 SyncFileSystemBackend* backend =
666 SyncFileSystemBackend::GetBackend(file_system_context);
667 DCHECK(backend);
668 DCHECK(backend->change_tracker());
639 FileChangeList changes; 669 FileChangeList changes;
640 file_system_context->change_tracker()->GetChangesForURL(url, &changes); 670 backend->change_tracker()->GetChangesForURL(url, &changes);
641 671
642 base::FilePath platform_path; 672 base::FilePath platform_path;
643 base::PlatformFileInfo file_info; 673 base::PlatformFileInfo file_info;
644 FileSystemFileUtil* file_util = file_system_context->GetFileUtil(url.type()); 674 FileSystemFileUtil* file_util = file_system_context->GetFileUtil(url.type());
645 DCHECK(file_util); 675 DCHECK(file_util);
646 base::PlatformFileError file_error = file_util->GetFileInfo( 676 base::PlatformFileError file_error = file_util->GetFileInfo(
647 make_scoped_ptr( 677 make_scoped_ptr(
648 new FileSystemOperationContext(file_system_context)).get(), 678 new FileSystemOperationContext(file_system_context)).get(),
649 url, 679 url,
650 &file_info, 680 &file_info,
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 return; 765 return;
736 } 766 }
737 767
738 FileSystemURL url_for_sync = CreateSyncableFileSystemURLForSync( 768 FileSystemURL url_for_sync = CreateSyncableFileSystemURLForSync(
739 file_system_context, dest_url); 769 file_system_context, dest_url);
740 file_system_context->operation_runner()->CopyInForeignFile( 770 file_system_context->operation_runner()->CopyInForeignFile(
741 local_path, url_for_sync, callback); 771 local_path, url_for_sync, callback);
742 } 772 }
743 773
744 } // namespace sync_file_system 774 } // namespace sync_file_system
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698