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