Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/arc/arc_downloads_watcher_service.h" | 5 #include "chrome/browser/chromeos/arc/arc_downloads_watcher_service.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 // Mapping from Android file paths to last modified timestamps. | 26 // Mapping from Android file paths to last modified timestamps. |
| 27 using TimestampMap = std::map<base::FilePath, base::Time>; | 27 using TimestampMap = std::map<base::FilePath, base::Time>; |
| 28 | 28 |
| 29 namespace arc { | 29 namespace arc { |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 const base::FilePath::CharType kAndroidDownloadDir[] = | 33 const base::FilePath::CharType kAndroidDownloadDir[] = |
| 34 FILE_PATH_LITERAL("/storage/emulated/0/Download"); | 34 FILE_PATH_LITERAL("/storage/emulated/0/Download"); |
| 35 | 35 |
| 36 // How long to wait for new inotify events before building the updated timestamp | |
| 37 // map. | |
| 38 const base::TimeDelta kBuildTimestampMapDelay = | |
| 39 base::TimeDelta::FromMilliseconds(1000); | |
| 40 | |
| 36 // Compares two TimestampMaps and returns the list of file paths added/removed | 41 // Compares two TimestampMaps and returns the list of file paths added/removed |
| 37 // or whose timestamp have changed. | 42 // or whose timestamp have changed. |
| 38 std::vector<base::FilePath> CollectChangedPaths( | 43 std::vector<base::FilePath> CollectChangedPaths( |
| 39 const TimestampMap& timestamp_map_a, | 44 const TimestampMap& timestamp_map_a, |
| 40 const TimestampMap& timestamp_map_b) { | 45 const TimestampMap& timestamp_map_b) { |
| 41 std::vector<base::FilePath> changed_paths; | 46 std::vector<base::FilePath> changed_paths; |
| 42 | 47 |
| 43 TimestampMap::const_iterator iter_a = timestamp_map_a.begin(); | 48 TimestampMap::const_iterator iter_a = timestamp_map_a.begin(); |
| 44 TimestampMap::const_iterator iter_b = timestamp_map_b.begin(); | 49 TimestampMap::const_iterator iter_b = timestamp_map_b.begin(); |
| 45 while (iter_a != timestamp_map_a.end() && iter_b != timestamp_map_b.end()) { | 50 while (iter_a != timestamp_map_a.end() && iter_b != timestamp_map_b.end()) { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 63 ++iter_a; | 68 ++iter_a; |
| 64 } | 69 } |
| 65 while (iter_b != timestamp_map_b.end()) { | 70 while (iter_b != timestamp_map_b.end()) { |
| 66 changed_paths.emplace_back(iter_b->first); | 71 changed_paths.emplace_back(iter_b->first); |
| 67 ++iter_b; | 72 ++iter_b; |
| 68 } | 73 } |
| 69 | 74 |
| 70 return changed_paths; | 75 return changed_paths; |
| 71 } | 76 } |
| 72 | 77 |
| 78 // Scans files under |downloads_dir| recursively and builds a map from file | |
| 79 // paths (in Android filesystem) to last modified timestamps. | |
| 80 TimestampMap BuildTimestampMap(base::FilePath downloads_dir) { | |
| 81 DCHECK(!downloads_dir.EndsWithSeparator()); | |
| 82 TimestampMap timestamp_map; | |
| 83 | |
| 84 // Enumerate normal files only; directories and symlinks are skipped. | |
| 85 base::FileEnumerator enumerator(downloads_dir, true, | |
| 86 base::FileEnumerator::FILES); | |
| 87 for (base::FilePath cros_path = enumerator.Next(); !cros_path.empty(); | |
| 88 cros_path = enumerator.Next()) { | |
| 89 // Android file path can be obtained by replacing |downloads_dir| prefix | |
| 90 // with |kAndroidDownloadDir|. | |
| 91 const base::FilePath& android_path = | |
| 92 base::FilePath(kAndroidDownloadDir) | |
| 93 .AppendRelativePath( | |
| 94 cros_path.value().substr(downloads_dir.value().length() + 1)); | |
|
hidehiko
2016/09/13 07:33:56
Something went wrong here, I think?
dspaid
2016/09/15 01:58:12
Not sure what you're thinking of, but the previous
hidehiko
2016/09/15 02:26:18
Sorry for poor explanation.
I meant;
FilePath and
| |
| 95 const base::FileEnumerator::FileInfo& info = enumerator.GetInfo(); | |
| 96 timestamp_map[android_path] = info.GetLastModifiedTime(); | |
| 97 } | |
| 98 return timestamp_map; | |
| 99 } | |
| 100 | |
| 101 std::pair<base::TimeTicks, TimestampMap> BuildTimestampMapCallback( | |
| 102 base::FilePath downloads_dir) { | |
| 103 // The TimestampMap may include chnages form after snapshot_time. | |
|
hidehiko
2016/09/13 07:33:56
nit: changes
dspaid
2016/09/15 01:58:12
Done.
| |
| 104 // We must take the snapshot_time before we build the TimestampMap since | |
| 105 // changes that occur while building the map may not be captured. | |
| 106 base::TimeTicks snapshot_time = base::TimeTicks::Now(); | |
| 107 TimestampMap current_timestamp_map = BuildTimestampMap(downloads_dir); | |
| 108 return std::make_pair(snapshot_time, std::move(current_timestamp_map)); | |
| 109 } | |
| 110 | |
| 73 } // namespace | 111 } // namespace |
| 74 | 112 |
| 75 // The core part of ArcDownloadsWatcherService to watch for file changes in | 113 // The core part of ArcDownloadsWatcherService to watch for file changes in |
| 76 // Downloads directory. | 114 // Downloads directory. |
| 77 class ArcDownloadsWatcherService::DownloadsWatcher { | 115 class ArcDownloadsWatcherService::DownloadsWatcher { |
| 78 public: | 116 public: |
| 79 using Callback = base::Callback<void(mojo::Array<mojo::String> paths)>; | 117 using Callback = base::Callback<void(mojo::Array<mojo::String> paths)>; |
| 80 | 118 |
| 81 explicit DownloadsWatcher(const Callback& callback); | 119 explicit DownloadsWatcher(const Callback& callback); |
| 82 ~DownloadsWatcher(); | 120 ~DownloadsWatcher(); |
| 83 | 121 |
| 84 // Starts watching Downloads directory. | 122 // Starts watching Downloads directory. |
| 85 void Start(); | 123 void Start(); |
| 86 | 124 |
| 87 private: | 125 private: |
| 88 // Called by base::FilePathWatcher to notify file changes. | 126 // Called by base::FilePathWatcher to notify file changes. |
| 127 // Kicks off the update of last_timestamp_map_ if one is not already in | |
| 128 // progress. | |
| 89 void OnFilePathChanged(const base::FilePath& path, bool error); | 129 void OnFilePathChanged(const base::FilePath& path, bool error); |
| 90 | 130 |
| 91 // Scans files under |downloads_dir_| recursively and builds a map from file | 131 // Called with a delay to allow additional inotify events for the same user |
| 92 // paths (in Android filesystem) to last modified timestamps. | 132 // action to queue up so that they can be dealt with in batch. |
| 93 TimestampMap BuildTimestampMap() const; | 133 void DelayBuildTimestampMap(); |
| 134 | |
| 135 // Called after a new timestamp map has been created and causes any recently | |
| 136 // modified files to be sent to the media scanner. | |
| 137 void OnBuildTimestampMap( | |
| 138 std::pair<base::TimeTicks, TimestampMap> timestamp_and_map); | |
| 94 | 139 |
| 95 Callback callback_; | 140 Callback callback_; |
| 96 base::FilePath downloads_dir_; | 141 base::FilePath downloads_dir_; |
| 97 std::unique_ptr<base::FilePathWatcher> watcher_; | 142 std::unique_ptr<base::FilePathWatcher> watcher_; |
| 98 TimestampMap last_timestamp_map_; | 143 TimestampMap last_timestamp_map_; |
| 144 // The timestamp of the last OnFilePathChanged callback received. | |
| 145 base::TimeTicks last_notify_time_; | |
| 146 // Whether or not there is an outstanding task to update last_timestamp_map_. | |
| 147 bool outstanding_task_; | |
| 99 | 148 |
| 100 // Note: This should remain the last member so it'll be destroyed and | 149 // Note: This should remain the last member so it'll be destroyed and |
| 101 // invalidate the weak pointers before any other members are destroyed. | 150 // invalidate the weak pointers before any other members are destroyed. |
| 102 base::WeakPtrFactory<DownloadsWatcher> weak_ptr_factory_; | 151 base::WeakPtrFactory<DownloadsWatcher> weak_ptr_factory_; |
| 103 | 152 |
| 104 DISALLOW_COPY_AND_ASSIGN(DownloadsWatcher); | 153 DISALLOW_COPY_AND_ASSIGN(DownloadsWatcher); |
| 105 }; | 154 }; |
| 106 | 155 |
| 107 ArcDownloadsWatcherService::DownloadsWatcher::DownloadsWatcher( | 156 ArcDownloadsWatcherService::DownloadsWatcher::DownloadsWatcher( |
| 108 const Callback& callback) | 157 const Callback& callback) |
| 109 : callback_(callback), weak_ptr_factory_(this) { | 158 : callback_(callback), |
| 159 last_notify_time_(base::TimeTicks()), | |
| 160 outstanding_task_(false), | |
| 161 weak_ptr_factory_(this) { | |
| 110 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 162 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 111 | 163 |
| 112 downloads_dir_ = DownloadPrefs(ProfileManager::GetActiveUserProfile()) | 164 downloads_dir_ = DownloadPrefs(ProfileManager::GetActiveUserProfile()) |
| 113 .GetDefaultDownloadDirectoryForProfile() | 165 .GetDefaultDownloadDirectoryForProfile() |
| 114 .StripTrailingSeparators(); | 166 .StripTrailingSeparators(); |
| 115 } | 167 } |
| 116 | 168 |
| 117 ArcDownloadsWatcherService::DownloadsWatcher::~DownloadsWatcher() { | 169 ArcDownloadsWatcherService::DownloadsWatcher::~DownloadsWatcher() { |
| 118 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 170 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 119 } | 171 } |
| 120 | 172 |
| 121 void ArcDownloadsWatcherService::DownloadsWatcher::Start() { | 173 void ArcDownloadsWatcherService::DownloadsWatcher::Start() { |
| 122 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 174 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 123 | 175 |
| 124 // Initialize with the current timestamp map and avoid initial notification. | 176 // Initialize with the current timestamp map and avoid initial notification. |
| 125 // It is not needed since MediaProvider scans whole storage area on boot. | 177 // It is not needed since MediaProvider scans whole storage area on boot. |
| 126 last_timestamp_map_ = BuildTimestampMap(); | 178 last_notify_time_ = base::TimeTicks::Now(); |
| 179 last_timestamp_map_ = BuildTimestampMap(downloads_dir_); | |
| 127 | 180 |
| 128 watcher_ = base::MakeUnique<base::FilePathWatcher>(); | 181 watcher_ = base::MakeUnique<base::FilePathWatcher>(); |
| 129 // On Linux, base::FilePathWatcher::Watch() always returns true. | 182 // On Linux, base::FilePathWatcher::Watch() always returns true. |
| 130 watcher_->Watch(downloads_dir_, true, | 183 watcher_->Watch(downloads_dir_, true, |
| 131 base::Bind(&DownloadsWatcher::OnFilePathChanged, | 184 base::Bind(&DownloadsWatcher::OnFilePathChanged, |
| 132 weak_ptr_factory_.GetWeakPtr())); | 185 weak_ptr_factory_.GetWeakPtr())); |
| 133 } | 186 } |
| 134 | 187 |
| 135 void ArcDownloadsWatcherService::DownloadsWatcher::OnFilePathChanged( | 188 void ArcDownloadsWatcherService::DownloadsWatcher::OnFilePathChanged( |
| 136 const base::FilePath& path, | 189 const base::FilePath& path, |
| 137 bool error) { | 190 bool error) { |
| 138 // On Linux, |error| is always false. Also, |path| is always the same path | 191 // On Linux, |error| is always false. Also, |path| is always the same path |
| 139 // as one given to FilePathWatcher::Watch(). | 192 // as one given to FilePathWatcher::Watch(). |
| 140 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 193 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 194 if (!outstanding_task_) { | |
| 195 outstanding_task_ = true; | |
| 196 BrowserThread::PostDelayedTask( | |
| 197 BrowserThread::FILE, FROM_HERE, | |
| 198 base::Bind(&DownloadsWatcher::DelayBuildTimestampMap, | |
| 199 weak_ptr_factory_.GetWeakPtr()), | |
| 200 kBuildTimestampMapDelay); | |
| 201 } else { | |
| 202 last_notify_time_ = base::TimeTicks::Now(); | |
| 203 } | |
| 204 } | |
| 141 | 205 |
| 142 TimestampMap current_timestamp_map = BuildTimestampMap(); | 206 void ArcDownloadsWatcherService::DownloadsWatcher::DelayBuildTimestampMap() { |
| 207 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 208 DCHECK(outstanding_task_); | |
| 209 base::PostTaskAndReplyWithResult( | |
| 210 BrowserThread::GetBlockingPool(), FROM_HERE, | |
| 211 base::Bind(&BuildTimestampMapCallback, downloads_dir_), | |
| 212 base::Bind(&DownloadsWatcher::OnBuildTimestampMap, | |
| 213 weak_ptr_factory_.GetWeakPtr())); | |
| 214 } | |
| 143 | 215 |
| 216 void ArcDownloadsWatcherService::DownloadsWatcher::OnBuildTimestampMap( | |
| 217 std::pair<base::TimeTicks, TimestampMap> timestamp_and_map) { | |
| 218 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 219 DCHECK(outstanding_task_); | |
| 220 base::TimeTicks snapshot_time = timestamp_and_map.first; | |
| 221 TimestampMap current_timestamp_map = std::move(timestamp_and_map.second); | |
| 144 std::vector<base::FilePath> changed_paths = | 222 std::vector<base::FilePath> changed_paths = |
| 145 CollectChangedPaths(last_timestamp_map_, current_timestamp_map); | 223 CollectChangedPaths(last_timestamp_map_, current_timestamp_map); |
| 146 | 224 |
| 147 last_timestamp_map_ = std::move(current_timestamp_map); | 225 last_timestamp_map_ = std::move(current_timestamp_map); |
| 148 | 226 |
| 149 mojo::Array<mojo::String> mojo_paths(changed_paths.size()); | 227 mojo::Array<mojo::String> mojo_paths(changed_paths.size()); |
| 150 for (size_t i = 0; i < changed_paths.size(); ++i) { | 228 for (size_t i = 0; i < changed_paths.size(); ++i) { |
| 151 mojo_paths[i] = changed_paths[i].value(); | 229 mojo_paths[i] = changed_paths[i].value(); |
| 152 } | 230 } |
| 153 BrowserThread::PostTask( | 231 BrowserThread::PostTask( |
| 154 BrowserThread::UI, FROM_HERE, | 232 BrowserThread::UI, FROM_HERE, |
| 155 base::Bind(callback_, base::Passed(std::move(mojo_paths)))); | 233 base::Bind(callback_, base::Passed(std::move(mojo_paths)))); |
| 156 } | 234 if (last_notify_time_ > snapshot_time) { |
| 157 | 235 base::PostTaskAndReplyWithResult( |
| 158 TimestampMap ArcDownloadsWatcherService::DownloadsWatcher::BuildTimestampMap() | 236 BrowserThread::GetBlockingPool(), FROM_HERE, |
| 159 const { | 237 base::Bind(&BuildTimestampMapCallback, downloads_dir_), |
| 160 DCHECK(!downloads_dir_.EndsWithSeparator()); | 238 base::Bind(&DownloadsWatcher::OnBuildTimestampMap, |
| 161 TimestampMap timestamp_map; | 239 weak_ptr_factory_.GetWeakPtr())); |
| 162 | 240 } else { |
| 163 // Enumerate normal files only; directories and symlinks are skipped. | 241 outstanding_task_ = false; |
| 164 base::FileEnumerator enumerator(downloads_dir_, true, | |
| 165 base::FileEnumerator::FILES); | |
| 166 for (base::FilePath cros_path = enumerator.Next(); !cros_path.empty(); | |
| 167 cros_path = enumerator.Next()) { | |
| 168 // Android file path can be obtained by replacing |downloads_dir_| prefix | |
| 169 // with |kAndroidDownloadDir|. | |
| 170 const base::FilePath& android_path = | |
| 171 base::FilePath(kAndroidDownloadDir) | |
| 172 .Append( | |
| 173 cros_path.value().substr(downloads_dir_.value().length() + 1)); | |
| 174 const base::FileEnumerator::FileInfo& info = enumerator.GetInfo(); | |
| 175 timestamp_map[android_path] = info.GetLastModifiedTime(); | |
| 176 } | 242 } |
| 177 return timestamp_map; | |
| 178 } | 243 } |
| 179 | 244 |
| 180 ArcDownloadsWatcherService::ArcDownloadsWatcherService( | 245 ArcDownloadsWatcherService::ArcDownloadsWatcherService( |
| 181 ArcBridgeService* bridge_service) | 246 ArcBridgeService* bridge_service) |
| 182 : ArcService(bridge_service), weak_ptr_factory_(this) { | 247 : ArcService(bridge_service), weak_ptr_factory_(this) { |
| 183 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 248 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 184 arc_bridge_service()->file_system()->AddObserver(this); | 249 arc_bridge_service()->file_system()->AddObserver(this); |
| 185 } | 250 } |
| 186 | 251 |
| 187 ArcDownloadsWatcherService::~ArcDownloadsWatcherService() { | 252 ArcDownloadsWatcherService::~ArcDownloadsWatcherService() { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 mojo::Array<mojo::String> paths) { | 290 mojo::Array<mojo::String> paths) { |
| 226 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 291 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 227 | 292 |
| 228 auto* instance = arc_bridge_service()->file_system()->instance(); | 293 auto* instance = arc_bridge_service()->file_system()->instance(); |
| 229 if (!instance) | 294 if (!instance) |
| 230 return; | 295 return; |
| 231 instance->RequestMediaScan(std::move(paths)); | 296 instance->RequestMediaScan(std::move(paths)); |
| 232 } | 297 } |
| 233 | 298 |
| 234 } // namespace arc | 299 } // namespace arc |
| OLD | NEW |