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

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

Issue 676403003: Revert of Changed api to notify when watched directory is deleted. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month 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))),
392 weak_factory_(this) { 389 weak_factory_(this) {
393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 390 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
394 ObserveEvents(); 391 ObserveEvents();
395 } 392 }
396 393
397 EventRouter::~EventRouter() { 394 EventRouter::~EventRouter() {
398 } 395 }
399 396
400 void EventRouter::Shutdown() { 397 void EventRouter::Shutdown() {
401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 BroadcastEvent(profile_, 704 BroadcastEvent(profile_,
708 file_manager_private::OnFileTransfersUpdated::kEventName, 705 file_manager_private::OnFileTransfersUpdated::kEventName,
709 file_manager_private::OnFileTransfersUpdated::Create(status)); 706 file_manager_private::OnFileTransfersUpdated::Create(status));
710 } 707 }
711 708
712 void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path) { 709 void EventRouter::OnDirectoryChanged(const base::FilePath& drive_path) {
713 HandleFileWatchNotification(NULL, drive_path, false); 710 HandleFileWatchNotification(NULL, drive_path, false);
714 } 711 }
715 712
716 void EventRouter::OnFileChanged(const drive::FileChange& changed_files) { 713 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.
727 typedef std::map<base::FilePath, drive::FileChange> FileChangeMap; 714 typedef std::map<base::FilePath, drive::FileChange> FileChangeMap;
728 typedef drive::FileChange::ChangeList::List FileChangeList;
729 715
730 FileChangeMap map; 716 FileChangeMap map;
731 const drive::FileChange::Map& changed_file_map = changed_files.map(); 717 const drive::FileChange::Map& changed_file_map = changed_files.map();
732 for (auto const& file_change_key_value : changed_file_map) { 718 for (drive::FileChange::Map::const_iterator it = changed_file_map.begin();
733 // Check whether the FileChangeList contains directory deletion. 719 it != changed_file_map.end();
734 bool contains_directory_deletion = false; 720 it++) {
735 const FileChangeList list = file_change_key_value.second.list(); 721 const base::FilePath& path = it->first;
736 for (drive::FileChange::Change const& change : list) { 722 map[path.DirName()].Update(path, it->second);
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 }
769 } 723 }
770 724
771 for (auto const& file_change_key_value : map) { 725 for (FileChangeMap::const_iterator it = map.begin(); it != map.end(); it++) {
772 HandleFileWatchNotification(&(file_change_key_value.second), 726 HandleFileWatchNotification(&(it->second), it->first, false);
773 file_change_key_value.first, false);
774 } 727 }
775 } 728 }
776 729
777 void EventRouter::OnDriveSyncError(drive::file_system::DriveSyncErrorType type, 730 void EventRouter::OnDriveSyncError(drive::file_system::DriveSyncErrorType type,
778 const base::FilePath& drive_path) { 731 const base::FilePath& drive_path) {
779 file_manager_private::DriveSyncErrorEvent event; 732 file_manager_private::DriveSyncErrorEvent event;
780 switch (type) { 733 switch (type) {
781 case drive::file_system::DRIVE_SYNC_ERROR_DELETE_WITHOUT_PERMISSION: 734 case drive::file_system::DRIVE_SYNC_ERROR_DELETE_WITHOUT_PERMISSION:
782 event.type = 735 event.type =
783 file_manager_private::DRIVE_SYNC_ERROR_TYPE_DELETE_WITHOUT_PERMISSION; 736 file_manager_private::DRIVE_SYNC_ERROR_TYPE_DELETE_WITHOUT_PERMISSION;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 list, 784 list,
832 got_error, 785 got_error,
833 iter->second->GetExtensionIds()); 786 iter->second->GetExtensionIds());
834 } 787 }
835 788
836 void EventRouter::DispatchDirectoryChangeEvent( 789 void EventRouter::DispatchDirectoryChangeEvent(
837 const base::FilePath& virtual_path, 790 const base::FilePath& virtual_path,
838 const drive::FileChange* list, 791 const drive::FileChange* list,
839 bool got_error, 792 bool got_error,
840 const std::vector<std::string>& extension_ids) { 793 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) {
850 if (!profile_) { 794 if (!profile_) {
851 NOTREACHED(); 795 NOTREACHED();
852 return; 796 return;
853 } 797 }
854 linked_ptr<drive::FileChange> changes; 798 linked_ptr<drive::FileChange> changes;
855 if (list) 799 if (list)
856 changes.reset(new drive::FileChange(*list)); // Copy 800 changes.reset(new drive::FileChange(*list)); // Copy
857 801
858 for (size_t i = 0; i < extension_ids.size(); ++i) { 802 for (size_t i = 0; i < extension_ids.size(); ++i) {
859 std::string* extension_id = new std::string(extension_ids[i]); 803 std::string* extension_id = new std::string(extension_ids[i]);
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
1016 void EventRouter::Observe(int type, 960 void EventRouter::Observe(int type,
1017 const content::NotificationSource& source, 961 const content::NotificationSource& source,
1018 const content::NotificationDetails& details) { 962 const content::NotificationDetails& details) {
1019 if (type == chrome::NOTIFICATION_PROFILE_ADDED) { 963 if (type == chrome::NOTIFICATION_PROFILE_ADDED) {
1020 Profile* const added_profile = content::Source<Profile>(source).ptr(); 964 Profile* const added_profile = content::Source<Profile>(source).ptr();
1021 if (!added_profile->IsOffTheRecord()) 965 if (!added_profile->IsOffTheRecord())
1022 GrantAccessForAddedProfileToRunningInstance(added_profile, profile_); 966 GrantAccessForAddedProfileToRunningInstance(added_profile, profile_);
1023 } 967 }
1024 } 968 }
1025 969
1026 void EventRouter::SetDispatchDirectoryChangeEventImplForTesting(
1027 const DispatchDirectoryChangeEventImplCallback& callback) {
1028 dispatch_directory_change_event_impl_ = callback;
1029 }
1030
1031 } // namespace file_manager 970 } // namespace file_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698