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 // For deletion of a directory, onFileChanged gets different changed_files. | |
718 // We solve the difference in this method. | |
719 // | |
720 // /a/b is watched, and /a is deleted from Drive (e.g. from Web). | |
721 // 1. /a/b DELETE:DIRECTORY | |
722 // 2. /a DELETE:DIRECTORY | |
723 // | |
724 // /a/b is watched, and /a is deleted from Files.app. | |
725 // 1. /a DELETE:DIRECTORY | |
714 typedef std::map<base::FilePath, drive::FileChange> FileChangeMap; | 726 typedef std::map<base::FilePath, drive::FileChange> FileChangeMap; |
727 typedef drive::FileChange::ChangeList::List FileChangeList; | |
715 | 728 |
716 FileChangeMap map; | 729 FileChangeMap map; |
717 const drive::FileChange::Map& changed_file_map = changed_files.map(); | 730 const drive::FileChange::Map& changed_file_map = changed_files.map(); |
718 for (drive::FileChange::Map::const_iterator it = changed_file_map.begin(); | 731 for (drive::FileChange::Map::const_iterator file_it = |
mtomasz
2014/10/21 02:13:52
nit: I was just told that we can use some cool C++
yawano
2014/10/21 05:48:48
Done.
| |
719 it != changed_file_map.end(); | 732 changed_file_map.begin(); |
720 it++) { | 733 file_it != changed_file_map.end(); file_it++) { |
721 const base::FilePath& path = it->first; | 734 bool contains_directory_deletion = false; |
722 map[path.DirName()].Update(path, it->second); | 735 const FileChangeList list = file_it->second.list(); |
736 for (FileChangeList::const_iterator change_it = list.begin(); | |
737 change_it != list.end(); ++change_it) { | |
738 if (change_it->IsDirectory() && change_it->IsDelete()) { | |
739 contains_directory_deletion = true; | |
740 break; | |
741 } | |
742 } | |
743 | |
744 const base::FilePath& path = file_it->first; | |
745 map[path.DirName()].Update(path, file_it->second); | |
mtomasz
2014/10/21 02:13:52
I don't really understand this logic, including th
yawano
2014/10/21 05:48:48
No, file change itself is not changed. HandleFileW
| |
746 | |
747 // If the change is a deletion of a directory, it may have deleted a | |
748 // watched directory. | |
749 // e.g. When /a is deleted, it means /a/b is also deleted. | |
750 if (contains_directory_deletion) { | |
751 // Expand the deleted directory path with watched paths. | |
752 for (WatcherMap::const_iterator file_watchers_it = | |
753 file_watchers_.lower_bound(path); | |
754 file_watchers_it != file_watchers_.end() && | |
755 file_watchers_it->first.value().find(path.value()) == 0; | |
756 ++file_watchers_it) { | |
757 // Set an empty change list. | |
758 drive::FileChange::ChangeList change_list; | |
759 map[file_watchers_it->first].Update(file_watchers_it->first, | |
760 change_list); | |
761 } | |
762 } | |
723 } | 763 } |
724 | 764 |
725 for (FileChangeMap::const_iterator it = map.begin(); it != map.end(); it++) { | 765 for (FileChangeMap::const_iterator it = map.begin(); it != map.end(); it++) { |
726 HandleFileWatchNotification(&(it->second), it->first, false); | 766 HandleFileWatchNotification(&(it->second), it->first, false); |
727 } | 767 } |
728 } | 768 } |
729 | 769 |
730 void EventRouter::OnDriveSyncError(drive::file_system::DriveSyncErrorType type, | 770 void EventRouter::OnDriveSyncError(drive::file_system::DriveSyncErrorType type, |
731 const base::FilePath& drive_path) { | 771 const base::FilePath& drive_path) { |
732 file_manager_private::DriveSyncErrorEvent event; | 772 file_manager_private::DriveSyncErrorEvent event; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
784 list, | 824 list, |
785 got_error, | 825 got_error, |
786 iter->second->GetExtensionIds()); | 826 iter->second->GetExtensionIds()); |
787 } | 827 } |
788 | 828 |
789 void EventRouter::DispatchDirectoryChangeEvent( | 829 void EventRouter::DispatchDirectoryChangeEvent( |
790 const base::FilePath& virtual_path, | 830 const base::FilePath& virtual_path, |
791 const drive::FileChange* list, | 831 const drive::FileChange* list, |
792 bool got_error, | 832 bool got_error, |
793 const std::vector<std::string>& extension_ids) { | 833 const std::vector<std::string>& extension_ids) { |
834 dispatch_directory_change_event_impl_.Run(virtual_path, list, got_error, | |
835 extension_ids); | |
836 } | |
837 | |
838 void EventRouter::DispatchDirectoryChangeEventImpl( | |
839 const base::FilePath& virtual_path, const drive::FileChange* list, | |
840 bool got_error, const std::vector<std::string>& extension_ids) { | |
794 if (!profile_) { | 841 if (!profile_) { |
795 NOTREACHED(); | 842 NOTREACHED(); |
796 return; | 843 return; |
797 } | 844 } |
798 linked_ptr<drive::FileChange> changes; | 845 linked_ptr<drive::FileChange> changes; |
799 if (list) | 846 if (list) |
800 changes.reset(new drive::FileChange(*list)); // Copy | 847 changes.reset(new drive::FileChange(*list)); // Copy |
801 | 848 |
802 for (size_t i = 0; i < extension_ids.size(); ++i) { | 849 for (size_t i = 0; i < extension_ids.size(); ++i) { |
803 std::string* extension_id = new std::string(extension_ids[i]); | 850 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, | 1007 void EventRouter::Observe(int type, |
961 const content::NotificationSource& source, | 1008 const content::NotificationSource& source, |
962 const content::NotificationDetails& details) { | 1009 const content::NotificationDetails& details) { |
963 if (type == chrome::NOTIFICATION_PROFILE_ADDED) { | 1010 if (type == chrome::NOTIFICATION_PROFILE_ADDED) { |
964 Profile* const added_profile = content::Source<Profile>(source).ptr(); | 1011 Profile* const added_profile = content::Source<Profile>(source).ptr(); |
965 if (!added_profile->IsOffTheRecord()) | 1012 if (!added_profile->IsOffTheRecord()) |
966 GrantAccessForAddedProfileToRunningInstance(added_profile, profile_); | 1013 GrantAccessForAddedProfileToRunningInstance(added_profile, profile_); |
967 } | 1014 } |
968 } | 1015 } |
969 | 1016 |
1017 void EventRouter::SetDispatchDirectoryChangeEventImplForTesting( | |
1018 const DispatchDirectoryChangeEventImplCallback& callback) { | |
1019 dispatch_directory_change_event_impl_ = callback; | |
1020 } | |
1021 | |
970 } // namespace file_manager | 1022 } // namespace file_manager |
OLD | NEW |