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/extensions/file_manager/event_router.h" | 5 #include "chrome/browser/chromeos/extensions/file_manager/event_router.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/prefs/pref_change_registrar.h" | 10 #include "base/prefs/pref_change_registrar.h" |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 | 379 |
380 EventRouter::DriveJobInfoWithStatus::DriveJobInfoWithStatus( | 380 EventRouter::DriveJobInfoWithStatus::DriveJobInfoWithStatus( |
381 const drive::JobInfo& info, const std::string& status) | 381 const drive::JobInfo& info, const std::string& status) |
382 : job_info(info), status(status) { | 382 : job_info(info), status(status) { |
383 } | 383 } |
384 | 384 |
385 EventRouter::EventRouter(Profile* profile) | 385 EventRouter::EventRouter(Profile* profile) |
386 : pref_change_registrar_(new PrefChangeRegistrar), | 386 : pref_change_registrar_(new PrefChangeRegistrar), |
387 profile_(profile), | 387 profile_(profile), |
388 device_event_router_(new DeviceEventRouterImpl(profile)), | 388 device_event_router_(new DeviceEventRouterImpl(profile)), |
| 389 dispatch_directory_change_event_impl_( |
| 390 base::Bind(&EventRouter::DispatchDirectoryChangeEventImpl, |
| 391 base::Unretained(this))), |
389 weak_factory_(this) { | 392 weak_factory_(this) { |
390 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
391 ObserveEvents(); | 394 ObserveEvents(); |
392 } | 395 } |
393 | 396 |
394 EventRouter::~EventRouter() { | 397 EventRouter::~EventRouter() { |
395 } | 398 } |
396 | 399 |
397 void EventRouter::Shutdown() { | 400 void EventRouter::Shutdown() { |
398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 BroadcastEvent(profile_, | 707 BroadcastEvent(profile_, |
705 file_manager_private::OnFileTransfersUpdated::kEventName, | 708 file_manager_private::OnFileTransfersUpdated::kEventName, |
706 file_manager_private::OnFileTransfersUpdated::Create(status)); | 709 file_manager_private::OnFileTransfersUpdated::Create(status)); |
707 } | 710 } |
708 | 711 |
709 void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path) { | 712 void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path) { |
710 HandleFileWatchNotification(NULL, drive_path, false); | 713 HandleFileWatchNotification(NULL, drive_path, false); |
711 } | 714 } |
712 | 715 |
713 void EventRouter::OnFileChanged(const drive::FileChange& changed_files) { | 716 void EventRouter::OnFileChanged(const drive::FileChange& changed_files) { |
| 717 // In this method, we convert changed_files to a map which can be handled by |
| 718 // HandleFileWatchNotification. |
| 719 // |
| 720 // e.g. |
| 721 // /a/b DIRECTORY:DELETE |
| 722 // |
| 723 // map[/a] = /a/b DIRECTORY:DELETE |
| 724 // map[/a/b] = /a/b DIRECTORY:DELETE |
| 725 // |
| 726 // We used the key of map to match the watched directories of file watchers. |
714 typedef std::map<base::FilePath, drive::FileChange> FileChangeMap; | 727 typedef std::map<base::FilePath, drive::FileChange> FileChangeMap; |
| 728 typedef drive::FileChange::ChangeList::List FileChangeList; |
715 | 729 |
716 FileChangeMap map; | 730 FileChangeMap map; |
717 const drive::FileChange::Map& changed_file_map = changed_files.map(); | 731 const drive::FileChange::Map& changed_file_map = changed_files.map(); |
718 for (drive::FileChange::Map::const_iterator it = changed_file_map.begin(); | 732 for (auto const& file_change_key_value : changed_file_map) { |
719 it != changed_file_map.end(); | 733 // Check whether the FileChangeList contains directory deletion. |
720 it++) { | 734 bool contains_directory_deletion = false; |
721 const base::FilePath& path = it->first; | 735 const FileChangeList list = file_change_key_value.second.list(); |
722 map[path.DirName()].Update(path, it->second); | 736 for (drive::FileChange::Change const& change : list) { |
| 737 if (change.IsDirectory() && change.IsDelete()) { |
| 738 contains_directory_deletion = true; |
| 739 break; |
| 740 } |
| 741 } |
| 742 |
| 743 const base::FilePath& path = file_change_key_value.first; |
| 744 map[path.DirName()].Update(path, file_change_key_value.second); |
| 745 |
| 746 // For deletion of a directory, onFileChanged gets different changed_files. |
| 747 // We solve the difference here. |
| 748 // |
| 749 // /a/b is watched, and /a is deleted from Drive (e.g. from Web). |
| 750 // 1. /a/b DELETE:DIRECTORY |
| 751 // 2. /a DELETE:DIRECTORY |
| 752 // |
| 753 // /a/b is watched, and /a is deleted from Files.app. |
| 754 // 1. /a DELETE:DIRECTORY |
| 755 if (contains_directory_deletion) { |
| 756 // Expand the deleted directory path with watched paths. |
| 757 for (WatcherMap::const_iterator file_watchers_it = |
| 758 file_watchers_.lower_bound(path); |
| 759 file_watchers_it != file_watchers_.end(); ++file_watchers_it) { |
| 760 if (path == file_watchers_it->first || |
| 761 path.IsParent(file_watchers_it->first)) { |
| 762 map[file_watchers_it->first].Update( |
| 763 file_watchers_it->first, |
| 764 drive::FileChange::FileType::FILE_TYPE_DIRECTORY, |
| 765 drive::FileChange::ChangeType::DELETE); |
| 766 } |
| 767 } |
| 768 } |
723 } | 769 } |
724 | 770 |
725 for (FileChangeMap::const_iterator it = map.begin(); it != map.end(); it++) { | 771 for (auto const& file_change_key_value : map) { |
726 HandleFileWatchNotification(&(it->second), it->first, false); | 772 HandleFileWatchNotification(&(file_change_key_value.second), |
| 773 file_change_key_value.first, false); |
727 } | 774 } |
728 } | 775 } |
729 | 776 |
730 void EventRouter::OnDriveSyncError(drive::file_system::DriveSyncErrorType type, | 777 void EventRouter::OnDriveSyncError(drive::file_system::DriveSyncErrorType type, |
731 const base::FilePath& drive_path) { | 778 const base::FilePath& drive_path) { |
732 file_manager_private::DriveSyncErrorEvent event; | 779 file_manager_private::DriveSyncErrorEvent event; |
733 switch (type) { | 780 switch (type) { |
734 case drive::file_system::DRIVE_SYNC_ERROR_DELETE_WITHOUT_PERMISSION: | 781 case drive::file_system::DRIVE_SYNC_ERROR_DELETE_WITHOUT_PERMISSION: |
735 event.type = | 782 event.type = |
736 file_manager_private::DRIVE_SYNC_ERROR_TYPE_DELETE_WITHOUT_PERMISSION; | 783 file_manager_private::DRIVE_SYNC_ERROR_TYPE_DELETE_WITHOUT_PERMISSION; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
784 list, | 831 list, |
785 got_error, | 832 got_error, |
786 iter->second->GetExtensionIds()); | 833 iter->second->GetExtensionIds()); |
787 } | 834 } |
788 | 835 |
789 void EventRouter::DispatchDirectoryChangeEvent( | 836 void EventRouter::DispatchDirectoryChangeEvent( |
790 const base::FilePath& virtual_path, | 837 const base::FilePath& virtual_path, |
791 const drive::FileChange* list, | 838 const drive::FileChange* list, |
792 bool got_error, | 839 bool got_error, |
793 const std::vector<std::string>& extension_ids) { | 840 const std::vector<std::string>& extension_ids) { |
| 841 dispatch_directory_change_event_impl_.Run(virtual_path, list, got_error, |
| 842 extension_ids); |
| 843 } |
| 844 |
| 845 void EventRouter::DispatchDirectoryChangeEventImpl( |
| 846 const base::FilePath& virtual_path, |
| 847 const drive::FileChange* list, |
| 848 bool got_error, |
| 849 const std::vector<std::string>& extension_ids) { |
794 if (!profile_) { | 850 if (!profile_) { |
795 NOTREACHED(); | 851 NOTREACHED(); |
796 return; | 852 return; |
797 } | 853 } |
798 linked_ptr<drive::FileChange> changes; | 854 linked_ptr<drive::FileChange> changes; |
799 if (list) | 855 if (list) |
800 changes.reset(new drive::FileChange(*list)); // Copy | 856 changes.reset(new drive::FileChange(*list)); // Copy |
801 | 857 |
802 for (size_t i = 0; i < extension_ids.size(); ++i) { | 858 for (size_t i = 0; i < extension_ids.size(); ++i) { |
803 std::string* extension_id = new std::string(extension_ids[i]); | 859 std::string* extension_id = new std::string(extension_ids[i]); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
960 void EventRouter::Observe(int type, | 1016 void EventRouter::Observe(int type, |
961 const content::NotificationSource& source, | 1017 const content::NotificationSource& source, |
962 const content::NotificationDetails& details) { | 1018 const content::NotificationDetails& details) { |
963 if (type == chrome::NOTIFICATION_PROFILE_ADDED) { | 1019 if (type == chrome::NOTIFICATION_PROFILE_ADDED) { |
964 Profile* const added_profile = content::Source<Profile>(source).ptr(); | 1020 Profile* const added_profile = content::Source<Profile>(source).ptr(); |
965 if (!added_profile->IsOffTheRecord()) | 1021 if (!added_profile->IsOffTheRecord()) |
966 GrantAccessForAddedProfileToRunningInstance(added_profile, profile_); | 1022 GrantAccessForAddedProfileToRunningInstance(added_profile, profile_); |
967 } | 1023 } |
968 } | 1024 } |
969 | 1025 |
| 1026 void EventRouter::SetDispatchDirectoryChangeEventImplForTesting( |
| 1027 const DispatchDirectoryChangeEventImplCallback& callback) { |
| 1028 dispatch_directory_change_event_impl_ = callback; |
| 1029 } |
| 1030 |
970 } // namespace file_manager | 1031 } // namespace file_manager |
OLD | NEW |