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