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