| 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 | 69 |
| 70 return changed_paths; | 70 return changed_paths; |
| 71 } | 71 } |
| 72 | 72 |
| 73 } // namespace | 73 } // namespace |
| 74 | 74 |
| 75 // The core part of ArcDownloadsWatcherService to watch for file changes in | 75 // The core part of ArcDownloadsWatcherService to watch for file changes in |
| 76 // Downloads directory. | 76 // Downloads directory. |
| 77 class ArcDownloadsWatcherService::DownloadsWatcher { | 77 class ArcDownloadsWatcherService::DownloadsWatcher { |
| 78 public: | 78 public: |
| 79 using Callback = base::Callback<void(mojo::Array<mojo::String> paths)>; | 79 using Callback = |
| 80 base::Callback<void(const std::vector<base::FilePath>& paths)>; |
| 80 | 81 |
| 81 explicit DownloadsWatcher(const Callback& callback); | 82 explicit DownloadsWatcher(const Callback& callback); |
| 82 ~DownloadsWatcher(); | 83 ~DownloadsWatcher(); |
| 83 | 84 |
| 84 // Starts watching Downloads directory. | 85 // Starts watching Downloads directory. |
| 85 void Start(); | 86 void Start(); |
| 86 | 87 |
| 87 private: | 88 private: |
| 88 // Called by base::FilePathWatcher to notify file changes. | 89 // Called by base::FilePathWatcher to notify file changes. |
| 89 void OnFilePathChanged(const base::FilePath& path, bool error); | 90 void OnFilePathChanged(const base::FilePath& path, bool error); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 // as one given to FilePathWatcher::Watch(). | 140 // as one given to FilePathWatcher::Watch(). |
| 140 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 141 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 141 | 142 |
| 142 TimestampMap current_timestamp_map = BuildTimestampMap(); | 143 TimestampMap current_timestamp_map = BuildTimestampMap(); |
| 143 | 144 |
| 144 std::vector<base::FilePath> changed_paths = | 145 std::vector<base::FilePath> changed_paths = |
| 145 CollectChangedPaths(last_timestamp_map_, current_timestamp_map); | 146 CollectChangedPaths(last_timestamp_map_, current_timestamp_map); |
| 146 | 147 |
| 147 last_timestamp_map_ = std::move(current_timestamp_map); | 148 last_timestamp_map_ = std::move(current_timestamp_map); |
| 148 | 149 |
| 149 mojo::Array<mojo::String> mojo_paths(changed_paths.size()); | 150 callback_.Run(changed_paths); |
| 150 for (size_t i = 0; i < changed_paths.size(); ++i) { | |
| 151 mojo_paths[i] = changed_paths[i].value(); | |
| 152 } | |
| 153 BrowserThread::PostTask( | |
| 154 BrowserThread::UI, FROM_HERE, | |
| 155 base::Bind(callback_, base::Passed(std::move(mojo_paths)))); | |
| 156 } | 151 } |
| 157 | 152 |
| 158 TimestampMap ArcDownloadsWatcherService::DownloadsWatcher::BuildTimestampMap() | 153 TimestampMap ArcDownloadsWatcherService::DownloadsWatcher::BuildTimestampMap() |
| 159 const { | 154 const { |
| 160 DCHECK(!downloads_dir_.EndsWithSeparator()); | 155 DCHECK(!downloads_dir_.EndsWithSeparator()); |
| 161 TimestampMap timestamp_map; | 156 TimestampMap timestamp_map; |
| 162 | 157 |
| 163 // Enumerate normal files only; directories and symlinks are skipped. | 158 // Enumerate normal files only; directories and symlinks are skipped. |
| 164 base::FileEnumerator enumerator(downloads_dir_, true, | 159 base::FileEnumerator enumerator(downloads_dir_, true, |
| 165 base::FileEnumerator::FILES); | 160 base::FileEnumerator::FILES); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 | 210 |
| 216 void ArcDownloadsWatcherService::StopWatchingDownloads() { | 211 void ArcDownloadsWatcherService::StopWatchingDownloads() { |
| 217 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 212 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 218 if (watcher_.get()) { | 213 if (watcher_.get()) { |
| 219 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, | 214 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, |
| 220 watcher_.release()); | 215 watcher_.release()); |
| 221 } | 216 } |
| 222 } | 217 } |
| 223 | 218 |
| 224 void ArcDownloadsWatcherService::OnDownloadsChanged( | 219 void ArcDownloadsWatcherService::OnDownloadsChanged( |
| 225 mojo::Array<mojo::String> paths) { | 220 const std::vector<base::FilePath>& paths) { |
| 226 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 221 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 227 | 222 |
| 228 auto instance = arc_bridge_service()->file_system()->instance(); | 223 auto instance = arc_bridge_service()->file_system()->instance(); |
| 229 if (!instance) { | 224 if (!instance) { |
| 230 return; | 225 return; |
| 231 instance->RequestMediaScan(std::move(paths)); | 226 } |
| 227 |
| 228 mojo::Array<mojo::String> mojo_paths(paths.size()); |
| 229 for (size_t i = 0; i < paths.size(); ++i) { |
| 230 mojo_paths[i] = paths[i].value(); |
| 231 } |
| 232 instance->RequestMediaScan(std::move(mojo_paths)); |
| 232 } | 233 } |
| 233 | 234 |
| 234 } // namespace arc | 235 } // namespace arc |
| OLD | NEW |