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

Side by Side Diff: chrome/browser/chromeos/extensions/file_manager/event_router.cc

Issue 658013002: Changed api to notify when watched directory is deleted. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changed FileChange of deletion of a directory. Created 6 years, 2 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
OLDNEW
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
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
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 bool contains_directory_deletion = false;
mtomasz 2014/10/21 07:16:56 nit: Please add a comment what actually 'contains_
yawano 2014/10/21 08:40:16 Done.
720 it++) { 734 const FileChangeList list = file_change_key_value.second.list();
721 const base::FilePath& path = it->first; 735 for (drive::FileChange::Change const& change : list) {
722 map[path.DirName()].Update(path, it->second); 736 if (change.IsDirectory() && change.IsDelete()) {
737 contains_directory_deletion = true;
738 break;
739 }
740 }
741
742 const base::FilePath& path = file_change_key_value.first;
743 map[path.DirName()].Update(path, file_change_key_value.second);
744
745 // For deletion of a directory, onFileChanged gets different changed_files.
746 // We solve the difference here.
747 //
748 // /a/b is watched, and /a is deleted from Drive (e.g. from Web).
749 // 1. /a/b DELETE:DIRECTORY
750 // 2. /a DELETE:DIRECTORY
751 //
752 // /a/b is watched, and /a is deleted from Files.app.
753 // 1. /a DELETE:DIRECTORY
754 if (contains_directory_deletion) {
755 // Expand the deleted directory path with watched paths.
756 for (WatcherMap::const_iterator file_watchers_it =
757 file_watchers_.lower_bound(path);
758 file_watchers_it != file_watchers_.end() &&
mtomasz 2014/10/21 07:16:56 What if we have watchers on /a and /aaa. Will /aaa
yawano 2014/10/21 08:40:16 This was a error case in the previous CL. I added
759 file_watchers_it->first.value().find(path.value()) == 0;
760 ++file_watchers_it) {
761 map[file_watchers_it->first].Update(
762 file_watchers_it->first,
763 drive::FileChange::FileType::FILE_TYPE_DIRECTORY,
764 drive::FileChange::ChangeType::DELETE);
765 }
766 }
723 } 767 }
724 768
725 for (FileChangeMap::const_iterator it = map.begin(); it != map.end(); it++) { 769 for (auto const& file_change_key_value : map) {
726 HandleFileWatchNotification(&(it->second), it->first, false); 770 HandleFileWatchNotification(&(file_change_key_value.second),
771 file_change_key_value.first, false);
727 } 772 }
728 } 773 }
729 774
730 void EventRouter::OnDriveSyncError(drive::file_system::DriveSyncErrorType type, 775 void EventRouter::OnDriveSyncError(drive::file_system::DriveSyncErrorType type,
731 const base::FilePath& drive_path) { 776 const base::FilePath& drive_path) {
732 file_manager_private::DriveSyncErrorEvent event; 777 file_manager_private::DriveSyncErrorEvent event;
733 switch (type) { 778 switch (type) {
734 case drive::file_system::DRIVE_SYNC_ERROR_DELETE_WITHOUT_PERMISSION: 779 case drive::file_system::DRIVE_SYNC_ERROR_DELETE_WITHOUT_PERMISSION:
735 event.type = 780 event.type =
736 file_manager_private::DRIVE_SYNC_ERROR_TYPE_DELETE_WITHOUT_PERMISSION; 781 file_manager_private::DRIVE_SYNC_ERROR_TYPE_DELETE_WITHOUT_PERMISSION;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
784 list, 829 list,
785 got_error, 830 got_error,
786 iter->second->GetExtensionIds()); 831 iter->second->GetExtensionIds());
787 } 832 }
788 833
789 void EventRouter::DispatchDirectoryChangeEvent( 834 void EventRouter::DispatchDirectoryChangeEvent(
790 const base::FilePath& virtual_path, 835 const base::FilePath& virtual_path,
791 const drive::FileChange* list, 836 const drive::FileChange* list,
792 bool got_error, 837 bool got_error,
793 const std::vector<std::string>& extension_ids) { 838 const std::vector<std::string>& extension_ids) {
839 dispatch_directory_change_event_impl_.Run(virtual_path, list, got_error,
840 extension_ids);
841 }
842
843 void EventRouter::DispatchDirectoryChangeEventImpl(
844 const base::FilePath& virtual_path, const drive::FileChange* list,
845 bool got_error, const std::vector<std::string>& extension_ids) {
794 if (!profile_) { 846 if (!profile_) {
795 NOTREACHED(); 847 NOTREACHED();
796 return; 848 return;
797 } 849 }
798 linked_ptr<drive::FileChange> changes; 850 linked_ptr<drive::FileChange> changes;
799 if (list) 851 if (list)
800 changes.reset(new drive::FileChange(*list)); // Copy 852 changes.reset(new drive::FileChange(*list)); // Copy
801 853
802 for (size_t i = 0; i < extension_ids.size(); ++i) { 854 for (size_t i = 0; i < extension_ids.size(); ++i) {
803 std::string* extension_id = new std::string(extension_ids[i]); 855 std::string* extension_id = new std::string(extension_ids[i]);
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
960 void EventRouter::Observe(int type, 1012 void EventRouter::Observe(int type,
961 const content::NotificationSource& source, 1013 const content::NotificationSource& source,
962 const content::NotificationDetails& details) { 1014 const content::NotificationDetails& details) {
963 if (type == chrome::NOTIFICATION_PROFILE_ADDED) { 1015 if (type == chrome::NOTIFICATION_PROFILE_ADDED) {
964 Profile* const added_profile = content::Source<Profile>(source).ptr(); 1016 Profile* const added_profile = content::Source<Profile>(source).ptr();
965 if (!added_profile->IsOffTheRecord()) 1017 if (!added_profile->IsOffTheRecord())
966 GrantAccessForAddedProfileToRunningInstance(added_profile, profile_); 1018 GrantAccessForAddedProfileToRunningInstance(added_profile, profile_);
967 } 1019 }
968 } 1020 }
969 1021
1022 void EventRouter::SetDispatchDirectoryChangeEventImplForTesting(
1023 const DispatchDirectoryChangeEventImplCallback& callback) {
1024 dispatch_directory_change_event_impl_ = callback;
1025 }
1026
970 } // namespace file_manager 1027 } // namespace file_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698