| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/file_system_provider/provided_file_system.h" | 5 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 return base::Bind( | 356 return base::Bind( |
| 357 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 357 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 358 } | 358 } |
| 359 | 359 |
| 360 ProvidedFileSystem::AbortCallback ProvidedFileSystem::ObserveDirectory( | 360 ProvidedFileSystem::AbortCallback ProvidedFileSystem::ObserveDirectory( |
| 361 const base::FilePath& directory_path, | 361 const base::FilePath& directory_path, |
| 362 bool recursive, | 362 bool recursive, |
| 363 const storage::AsyncFileUtil::StatusCallback& callback) { | 363 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 364 // TODO(mtomasz): Wrap the entire method body with an asynchronous queue to | 364 // TODO(mtomasz): Wrap the entire method body with an asynchronous queue to |
| 365 // avoid races. | 365 // avoid races. |
| 366 const ObservedEntries::const_iterator it = | 366 const ObservedEntryKey key(directory_path, recursive); |
| 367 observed_entries_.find(directory_path); | 367 const ObservedEntries::const_iterator it = observed_entries_.find(key); |
| 368 if (it != observed_entries_.end()) { | 368 if (it != observed_entries_.end()) { |
| 369 if (!recursive || it->second.recursive) { | 369 OnObserveDirectoryCompleted( |
| 370 callback.Run(base::File::FILE_ERROR_EXISTS); | 370 directory_path, recursive, callback, base::File::FILE_ERROR_EXISTS); |
| 371 return AbortCallback(); | 371 return AbortCallback(); |
| 372 } | |
| 373 } | 372 } |
| 374 | 373 |
| 375 const int request_id = request_manager_->CreateRequest( | 374 const int request_id = request_manager_->CreateRequest( |
| 376 OBSERVE_DIRECTORY, | 375 OBSERVE_DIRECTORY, |
| 377 scoped_ptr<RequestManager::HandlerInterface>( | 376 scoped_ptr<RequestManager::HandlerInterface>( |
| 378 new operations::ObserveDirectory( | 377 new operations::ObserveDirectory( |
| 379 event_router_, | 378 event_router_, |
| 380 file_system_info_, | 379 file_system_info_, |
| 381 directory_path, | 380 directory_path, |
| 382 recursive, | 381 recursive, |
| 383 base::Bind(&ProvidedFileSystem::OnObserveDirectoryCompleted, | 382 base::Bind(&ProvidedFileSystem::OnObserveDirectoryCompleted, |
| 384 weak_ptr_factory_.GetWeakPtr(), | 383 weak_ptr_factory_.GetWeakPtr(), |
| 385 directory_path, | 384 directory_path, |
| 386 recursive, | 385 recursive, |
| 387 callback)))); | 386 callback)))); |
| 388 if (!request_id) { | 387 if (!request_id) { |
| 389 callback.Run(base::File::FILE_ERROR_SECURITY); | 388 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 390 return AbortCallback(); | 389 return AbortCallback(); |
| 391 } | 390 } |
| 392 | 391 |
| 393 return base::Bind( | 392 return base::Bind( |
| 394 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); | 393 &ProvidedFileSystem::Abort, weak_ptr_factory_.GetWeakPtr(), request_id); |
| 395 } | 394 } |
| 396 | 395 |
| 397 void ProvidedFileSystem::UnobserveEntry( | 396 void ProvidedFileSystem::UnobserveEntry( |
| 398 const base::FilePath& entry_path, | 397 const base::FilePath& entry_path, |
| 398 bool recursive, |
| 399 const storage::AsyncFileUtil::StatusCallback& callback) { | 399 const storage::AsyncFileUtil::StatusCallback& callback) { |
| 400 const ObservedEntries::const_iterator it = observed_entries_.find(entry_path); | 400 const ObservedEntryKey key(entry_path, recursive); |
| 401 const ObservedEntries::const_iterator it = observed_entries_.find(key); |
| 401 if (it == observed_entries_.end()) { | 402 if (it == observed_entries_.end()) { |
| 402 callback.Run(base::File::FILE_ERROR_NOT_FOUND); | 403 callback.Run(base::File::FILE_ERROR_NOT_FOUND); |
| 403 return; | 404 return; |
| 404 } | 405 } |
| 405 | 406 |
| 406 // Delete the watcher in advance since the list of observed entries is owned | 407 // Delete the watcher in advance since the list of observed entries is owned |
| 407 // by the C++ layer, not by the extension. | 408 // by the C++ layer, not by the extension. |
| 408 observed_entries_.erase(it); | 409 observed_entries_.erase(it); |
| 409 | 410 |
| 410 FOR_EACH_OBSERVER( | 411 FOR_EACH_OBSERVER( |
| 411 ProvidedFileSystemObserver, | 412 ProvidedFileSystemObserver, |
| 412 observers_, | 413 observers_, |
| 413 OnObservedEntryListChanged(file_system_info_, observed_entries_)); | 414 OnObservedEntryListChanged(file_system_info_, observed_entries_)); |
| 414 | 415 |
| 415 // TODO(mtomasz): Consider returning always an OK error code, since for the | 416 // TODO(mtomasz): Consider returning always an OK error code, since for the |
| 416 // callers it's important that the entry is not watched anymore. The watcher | 417 // callers it's important that the entry is not watched anymore. The watcher |
| 417 // is removed even if the extension returns an error. | 418 // is removed even if the extension returns an error. |
| 418 const int request_id = request_manager_->CreateRequest( | 419 const int request_id = request_manager_->CreateRequest( |
| 419 UNOBSERVE_ENTRY, | 420 UNOBSERVE_ENTRY, |
| 420 scoped_ptr<RequestManager::HandlerInterface>( | 421 scoped_ptr<RequestManager::HandlerInterface>( |
| 421 new operations::UnobserveEntry( | 422 new operations::UnobserveEntry(event_router_, |
| 422 event_router_, file_system_info_, entry_path, callback))); | 423 file_system_info_, |
| 424 entry_path, |
| 425 recursive, |
| 426 callback))); |
| 423 if (!request_id) | 427 if (!request_id) |
| 424 callback.Run(base::File::FILE_ERROR_SECURITY); | 428 callback.Run(base::File::FILE_ERROR_SECURITY); |
| 425 } | 429 } |
| 426 | 430 |
| 427 const ProvidedFileSystemInfo& ProvidedFileSystem::GetFileSystemInfo() const { | 431 const ProvidedFileSystemInfo& ProvidedFileSystem::GetFileSystemInfo() const { |
| 428 return file_system_info_; | 432 return file_system_info_; |
| 429 } | 433 } |
| 430 | 434 |
| 431 RequestManager* ProvidedFileSystem::GetRequestManager() { | 435 RequestManager* ProvidedFileSystem::GetRequestManager() { |
| 432 return request_manager_.get(); | 436 return request_manager_.get(); |
| 433 } | 437 } |
| 434 | 438 |
| 435 ObservedEntries* ProvidedFileSystem::GetObservedEntries() { | 439 ObservedEntries* ProvidedFileSystem::GetObservedEntries() { |
| 436 return &observed_entries_; | 440 return &observed_entries_; |
| 437 } | 441 } |
| 438 | 442 |
| 439 void ProvidedFileSystem::AddObserver(ProvidedFileSystemObserver* observer) { | 443 void ProvidedFileSystem::AddObserver(ProvidedFileSystemObserver* observer) { |
| 440 DCHECK(observer); | 444 DCHECK(observer); |
| 441 observers_.AddObserver(observer); | 445 observers_.AddObserver(observer); |
| 442 } | 446 } |
| 443 | 447 |
| 444 void ProvidedFileSystem::RemoveObserver(ProvidedFileSystemObserver* observer) { | 448 void ProvidedFileSystem::RemoveObserver(ProvidedFileSystemObserver* observer) { |
| 445 DCHECK(observer); | 449 DCHECK(observer); |
| 446 observers_.RemoveObserver(observer); | 450 observers_.RemoveObserver(observer); |
| 447 } | 451 } |
| 448 | 452 |
| 449 bool ProvidedFileSystem::Notify( | 453 bool ProvidedFileSystem::Notify( |
| 450 const base::FilePath& observed_path, | 454 const base::FilePath& observed_path, |
| 455 bool recursive, |
| 451 ProvidedFileSystemObserver::ChangeType change_type, | 456 ProvidedFileSystemObserver::ChangeType change_type, |
| 452 scoped_ptr<ProvidedFileSystemObserver::ChildChanges> child_changes, | 457 scoped_ptr<ProvidedFileSystemObserver::Changes> changes, |
| 453 const std::string& tag) { | 458 const std::string& tag) { |
| 454 const ObservedEntries::iterator it = observed_entries_.find(observed_path); | 459 const ObservedEntryKey key(observed_path, recursive); |
| 460 const ObservedEntries::iterator it = observed_entries_.find(key); |
| 455 if (it == observed_entries_.end()) | 461 if (it == observed_entries_.end()) |
| 456 return false; | 462 return false; |
| 457 | 463 |
| 458 // The tag must be provided if and only if it's explicitly supported. | 464 // The tag must be provided if and only if it's explicitly supported. |
| 459 if (file_system_info_.supports_notify_tag() == tag.empty()) | 465 if (file_system_info_.supports_notify_tag() == tag.empty()) |
| 460 return false; | 466 return false; |
| 461 | 467 |
| 462 // The object is owned by AutoUpdated, so the reference is valid as long as | 468 // The object is owned by AutoUpdated, so the reference is valid as long as |
| 463 // callbacks created with AutoUpdater::CreateCallback(). | 469 // callbacks created with AutoUpdater::CreateCallback(). |
| 464 const ProvidedFileSystemObserver::ChildChanges& child_changes_ref = | 470 const ProvidedFileSystemObserver::Changes& changes_ref = *changes.get(); |
| 465 *child_changes.get(); | |
| 466 | 471 |
| 467 scoped_refptr<AutoUpdater> auto_updater( | 472 scoped_refptr<AutoUpdater> auto_updater( |
| 468 new AutoUpdater(base::Bind(&ProvidedFileSystem::OnNotifyCompleted, | 473 new AutoUpdater(base::Bind(&ProvidedFileSystem::OnNotifyCompleted, |
| 469 weak_ptr_factory_.GetWeakPtr(), | 474 weak_ptr_factory_.GetWeakPtr(), |
| 470 observed_path, | 475 observed_path, |
| 476 recursive, |
| 471 change_type, | 477 change_type, |
| 472 base::Passed(&child_changes), | 478 base::Passed(&changes), |
| 473 it->second.last_tag, | 479 it->second.last_tag, |
| 474 tag))); | 480 tag))); |
| 475 | 481 |
| 476 FOR_EACH_OBSERVER(ProvidedFileSystemObserver, | 482 FOR_EACH_OBSERVER(ProvidedFileSystemObserver, |
| 477 observers_, | 483 observers_, |
| 478 OnObservedEntryChanged(file_system_info_, | 484 OnObservedEntryChanged(file_system_info_, |
| 479 it->second, | 485 it->second, |
| 480 change_type, | 486 change_type, |
| 481 child_changes_ref, | 487 changes_ref, |
| 482 auto_updater->CreateCallback())); | 488 auto_updater->CreateCallback())); |
| 483 | 489 |
| 484 return true; | 490 return true; |
| 485 } | 491 } |
| 486 | 492 |
| 487 base::WeakPtr<ProvidedFileSystemInterface> ProvidedFileSystem::GetWeakPtr() { | 493 base::WeakPtr<ProvidedFileSystemInterface> ProvidedFileSystem::GetWeakPtr() { |
| 488 return weak_ptr_factory_.GetWeakPtr(); | 494 return weak_ptr_factory_.GetWeakPtr(); |
| 489 } | 495 } |
| 490 | 496 |
| 491 void ProvidedFileSystem::Abort( | 497 void ProvidedFileSystem::Abort( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 508 void ProvidedFileSystem::OnObserveDirectoryCompleted( | 514 void ProvidedFileSystem::OnObserveDirectoryCompleted( |
| 509 const base::FilePath& directory_path, | 515 const base::FilePath& directory_path, |
| 510 bool recursive, | 516 bool recursive, |
| 511 const storage::AsyncFileUtil::StatusCallback& callback, | 517 const storage::AsyncFileUtil::StatusCallback& callback, |
| 512 base::File::Error result) { | 518 base::File::Error result) { |
| 513 if (result != base::File::FILE_OK) { | 519 if (result != base::File::FILE_OK) { |
| 514 callback.Run(result); | 520 callback.Run(result); |
| 515 return; | 521 return; |
| 516 } | 522 } |
| 517 | 523 |
| 518 observed_entries_[directory_path].entry_path = directory_path; | 524 const ObservedEntryKey key(directory_path, recursive); |
| 519 observed_entries_[directory_path].recursive |= recursive; | 525 const ObservedEntries::iterator it = observed_entries_.find(key); |
| 526 if (it != observed_entries_.end()) { |
| 527 callback.Run(base::File::FILE_OK); |
| 528 return; |
| 529 } |
| 530 |
| 531 observed_entries_[key].entry_path = directory_path; |
| 532 observed_entries_[key].recursive = recursive; |
| 520 | 533 |
| 521 FOR_EACH_OBSERVER( | 534 FOR_EACH_OBSERVER( |
| 522 ProvidedFileSystemObserver, | 535 ProvidedFileSystemObserver, |
| 523 observers_, | 536 observers_, |
| 524 OnObservedEntryListChanged(file_system_info_, observed_entries_)); | 537 OnObservedEntryListChanged(file_system_info_, observed_entries_)); |
| 525 | 538 |
| 526 callback.Run(result); | 539 callback.Run(base::File::FILE_OK); |
| 527 } | 540 } |
| 528 | 541 |
| 529 void ProvidedFileSystem::OnNotifyCompleted( | 542 void ProvidedFileSystem::OnNotifyCompleted( |
| 530 const base::FilePath& observed_path, | 543 const base::FilePath& observed_path, |
| 544 bool recursive, |
| 531 ProvidedFileSystemObserver::ChangeType change_type, | 545 ProvidedFileSystemObserver::ChangeType change_type, |
| 532 scoped_ptr<ProvidedFileSystemObserver::ChildChanges> /* child_changes */, | 546 scoped_ptr<ProvidedFileSystemObserver::Changes> /* changes */, |
| 533 const std::string& last_tag, | 547 const std::string& last_tag, |
| 534 const std::string& tag) { | 548 const std::string& tag) { |
| 535 const ObservedEntries::iterator it = observed_entries_.find(observed_path); | 549 const ObservedEntryKey key(observed_path, recursive); |
| 550 const ObservedEntries::iterator it = observed_entries_.find(key); |
| 536 // Check if the entry is still observed. | 551 // Check if the entry is still observed. |
| 537 if (it == observed_entries_.end()) | 552 if (it == observed_entries_.end()) |
| 538 return; | 553 return; |
| 539 | 554 |
| 540 // Another following notification finished earlier. | 555 // Another following notification finished earlier. |
| 541 if (it->second.last_tag != last_tag) | 556 if (it->second.last_tag != last_tag) |
| 542 return; | 557 return; |
| 543 | 558 |
| 544 // It's illegal to provide a tag which is not unique. As for now only an error | 559 // It's illegal to provide a tag which is not unique. As for now only an error |
| 545 // message is printed, but we may want to pass the error to the providing | 560 // message is printed, but we may want to pass the error to the providing |
| 546 // extension. TODO(mtomasz): Consider it. | 561 // extension. TODO(mtomasz): Consider it. |
| 547 if (!tag.empty() && tag == it->second.last_tag) | 562 if (!tag.empty() && tag == it->second.last_tag) |
| 548 LOG(ERROR) << "Tag specified, but same as the previous one."; | 563 LOG(ERROR) << "Tag specified, but same as the previous one."; |
| 549 | 564 |
| 550 it->second.last_tag = tag; | 565 it->second.last_tag = tag; |
| 551 | 566 |
| 552 FOR_EACH_OBSERVER(ProvidedFileSystemObserver, | 567 FOR_EACH_OBSERVER(ProvidedFileSystemObserver, |
| 553 observers_, | 568 observers_, |
| 554 OnObservedEntryTagUpdated(file_system_info_, it->second)); | 569 OnObservedEntryTagUpdated(file_system_info_, it->second)); |
| 555 | 570 |
| 556 // If the observed entry is deleted, then unobserve it. | 571 // If the observed entry is deleted, then unobserve it. |
| 557 if (change_type == ProvidedFileSystemObserver::DELETED) | 572 if (change_type == ProvidedFileSystemObserver::DELETED) |
| 558 UnobserveEntry(observed_path, base::Bind(&EmptyStatusCallback)); | 573 UnobserveEntry(observed_path, recursive, base::Bind(&EmptyStatusCallback)); |
| 559 } | 574 } |
| 560 | 575 |
| 561 } // namespace file_system_provider | 576 } // namespace file_system_provider |
| 562 } // namespace chromeos | 577 } // namespace chromeos |
| OLD | NEW |