| 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/downloads_watcher/arc_downloads_watcher_se
rvice.h" | 5 #include "chrome/browser/chromeos/arc/downloads_watcher/arc_downloads_watcher_se
rvice.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <iterator> | 10 #include <iterator> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <utility> | 13 #include <utility> |
| 14 | 14 |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/files/file_enumerator.h" | 16 #include "base/files/file_enumerator.h" |
| 17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 18 #include "base/files/file_path_watcher.h" | 18 #include "base/files/file_path_watcher.h" |
| 19 #include "base/memory/ptr_util.h" | 19 #include "base/memory/ptr_util.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/task_scheduler/post_task.h" |
| 21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 22 #include "chrome/browser/download/download_prefs.h" | 23 #include "chrome/browser/download/download_prefs.h" |
| 23 #include "chrome/browser/profiles/profile_manager.h" | 24 #include "chrome/browser/profiles/profile_manager.h" |
| 24 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
| 25 #include "components/arc/arc_bridge_service.h" | 26 #include "components/arc/arc_bridge_service.h" |
| 26 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 27 | 28 |
| 28 using content::BrowserThread; | 29 using content::BrowserThread; |
| 29 | 30 |
| 30 // Mapping from Android file paths to last modified timestamps. | 31 // Mapping from Android file paths to last modified timestamps. |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 weak_ptr_factory_.GetWeakPtr()), | 270 weak_ptr_factory_.GetWeakPtr()), |
| 270 kBuildTimestampMapDelay); | 271 kBuildTimestampMapDelay); |
| 271 } else { | 272 } else { |
| 272 last_notify_time_ = base::TimeTicks::Now(); | 273 last_notify_time_ = base::TimeTicks::Now(); |
| 273 } | 274 } |
| 274 } | 275 } |
| 275 | 276 |
| 276 void ArcDownloadsWatcherService::DownloadsWatcher::DelayBuildTimestampMap() { | 277 void ArcDownloadsWatcherService::DownloadsWatcher::DelayBuildTimestampMap() { |
| 277 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 278 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 278 DCHECK(outstanding_task_); | 279 DCHECK(outstanding_task_); |
| 279 base::PostTaskAndReplyWithResult( | 280 base::PostTaskWithTraitsAndReplyWithResult( |
| 280 BrowserThread::GetBlockingPool(), FROM_HERE, | 281 FROM_HERE, base::TaskTraits().MayBlock(), |
| 281 base::Bind(&BuildTimestampMapCallback, downloads_dir_), | 282 base::Bind(&BuildTimestampMapCallback, downloads_dir_), |
| 282 base::Bind(&DownloadsWatcher::OnBuildTimestampMap, | 283 base::Bind(&DownloadsWatcher::OnBuildTimestampMap, |
| 283 weak_ptr_factory_.GetWeakPtr())); | 284 weak_ptr_factory_.GetWeakPtr())); |
| 284 } | 285 } |
| 285 | 286 |
| 286 void ArcDownloadsWatcherService::DownloadsWatcher::OnBuildTimestampMap( | 287 void ArcDownloadsWatcherService::DownloadsWatcher::OnBuildTimestampMap( |
| 287 std::pair<base::TimeTicks, TimestampMap> timestamp_and_map) { | 288 std::pair<base::TimeTicks, TimestampMap> timestamp_and_map) { |
| 288 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 289 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 289 DCHECK(outstanding_task_); | 290 DCHECK(outstanding_task_); |
| 290 base::TimeTicks snapshot_time = timestamp_and_map.first; | 291 base::TimeTicks snapshot_time = timestamp_and_map.first; |
| 291 TimestampMap current_timestamp_map = std::move(timestamp_and_map.second); | 292 TimestampMap current_timestamp_map = std::move(timestamp_and_map.second); |
| 292 std::vector<base::FilePath> changed_paths = | 293 std::vector<base::FilePath> changed_paths = |
| 293 CollectChangedPaths(last_timestamp_map_, current_timestamp_map); | 294 CollectChangedPaths(last_timestamp_map_, current_timestamp_map); |
| 294 | 295 |
| 295 last_timestamp_map_ = std::move(current_timestamp_map); | 296 last_timestamp_map_ = std::move(current_timestamp_map); |
| 296 | 297 |
| 297 std::vector<std::string> string_paths(changed_paths.size()); | 298 std::vector<std::string> string_paths(changed_paths.size()); |
| 298 for (size_t i = 0; i < changed_paths.size(); ++i) { | 299 for (size_t i = 0; i < changed_paths.size(); ++i) { |
| 299 string_paths[i] = changed_paths[i].value(); | 300 string_paths[i] = changed_paths[i].value(); |
| 300 } | 301 } |
| 301 BrowserThread::PostTask( | 302 BrowserThread::PostTask( |
| 302 BrowserThread::UI, FROM_HERE, | 303 BrowserThread::UI, FROM_HERE, |
| 303 base::Bind(callback_, base::Passed(std::move(string_paths)))); | 304 base::Bind(callback_, base::Passed(std::move(string_paths)))); |
| 304 if (last_notify_time_ > snapshot_time) { | 305 if (last_notify_time_ > snapshot_time) |
| 305 base::PostTaskAndReplyWithResult( | 306 DelayBuildTimestampMap(); |
| 306 BrowserThread::GetBlockingPool(), FROM_HERE, | 307 else |
| 307 base::Bind(&BuildTimestampMapCallback, downloads_dir_), | |
| 308 base::Bind(&DownloadsWatcher::OnBuildTimestampMap, | |
| 309 weak_ptr_factory_.GetWeakPtr())); | |
| 310 } else { | |
| 311 outstanding_task_ = false; | 308 outstanding_task_ = false; |
| 312 } | |
| 313 } | 309 } |
| 314 | 310 |
| 315 ArcDownloadsWatcherService::ArcDownloadsWatcherService( | 311 ArcDownloadsWatcherService::ArcDownloadsWatcherService( |
| 316 ArcBridgeService* bridge_service) | 312 ArcBridgeService* bridge_service) |
| 317 : ArcService(bridge_service), weak_ptr_factory_(this) { | 313 : ArcService(bridge_service), weak_ptr_factory_(this) { |
| 318 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 314 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 319 arc_bridge_service()->file_system()->AddObserver(this); | 315 arc_bridge_service()->file_system()->AddObserver(this); |
| 320 } | 316 } |
| 321 | 317 |
| 322 ArcDownloadsWatcherService::~ArcDownloadsWatcherService() { | 318 ArcDownloadsWatcherService::~ArcDownloadsWatcherService() { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 357 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 362 | 358 |
| 363 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( | 359 auto* instance = ARC_GET_INSTANCE_FOR_METHOD( |
| 364 arc_bridge_service()->file_system(), RequestMediaScan); | 360 arc_bridge_service()->file_system(), RequestMediaScan); |
| 365 if (!instance) | 361 if (!instance) |
| 366 return; | 362 return; |
| 367 instance->RequestMediaScan(paths); | 363 instance->RequestMediaScan(paths); |
| 368 } | 364 } |
| 369 | 365 |
| 370 } // namespace arc | 366 } // namespace arc |
| OLD | NEW |