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/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 <string> | |
| 14 #include <utility> | 13 #include <utility> |
| 15 #include <vector> | |
| 16 | 14 |
| 17 #include "base/callback.h" | 15 #include "base/callback.h" |
| 18 #include "base/files/file_enumerator.h" | 16 #include "base/files/file_enumerator.h" |
| 19 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 20 #include "base/files/file_path_watcher.h" | 18 #include "base/files/file_path_watcher.h" |
| 21 #include "base/memory/ptr_util.h" | 19 #include "base/memory/ptr_util.h" |
| 22 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 23 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 24 #include "chrome/browser/download/download_prefs.h" | 22 #include "chrome/browser/download/download_prefs.h" |
| 25 #include "chrome/browser/profiles/profile_manager.h" | 23 #include "chrome/browser/profiles/profile_manager.h" |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 std::end(kAndroidSupportedMediaExtensions), | 177 std::end(kAndroidSupportedMediaExtensions), |
| 180 extension.c_str(), less_comparator); | 178 extension.c_str(), less_comparator); |
| 181 return iter != std::end(kAndroidSupportedMediaExtensions) && | 179 return iter != std::end(kAndroidSupportedMediaExtensions) && |
| 182 strcmp(*iter, extension.c_str()) == 0; | 180 strcmp(*iter, extension.c_str()) == 0; |
| 183 } | 181 } |
| 184 | 182 |
| 185 // The core part of ArcDownloadsWatcherService to watch for file changes in | 183 // The core part of ArcDownloadsWatcherService to watch for file changes in |
| 186 // Downloads directory. | 184 // Downloads directory. |
| 187 class ArcDownloadsWatcherService::DownloadsWatcher { | 185 class ArcDownloadsWatcherService::DownloadsWatcher { |
| 188 public: | 186 public: |
| 189 using Callback = base::Callback<void(mojo::Array<mojo::String> paths)>; | 187 using Callback = base::Callback<void(const std::vector<std::string>& paths)>; |
| 190 | 188 |
| 191 explicit DownloadsWatcher(const Callback& callback); | 189 explicit DownloadsWatcher(const Callback& callback); |
| 192 ~DownloadsWatcher(); | 190 ~DownloadsWatcher(); |
| 193 | 191 |
| 194 // Starts watching Downloads directory. | 192 // Starts watching Downloads directory. |
| 195 void Start(); | 193 void Start(); |
| 196 | 194 |
| 197 private: | 195 private: |
| 198 // Called by base::FilePathWatcher to notify file changes. | 196 // Called by base::FilePathWatcher to notify file changes. |
| 199 // Kicks off the update of last_timestamp_map_ if one is not already in | 197 // Kicks off the update of last_timestamp_map_ if one is not already in |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 std::pair<base::TimeTicks, TimestampMap> timestamp_and_map) { | 287 std::pair<base::TimeTicks, TimestampMap> timestamp_and_map) { |
| 290 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 288 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 291 DCHECK(outstanding_task_); | 289 DCHECK(outstanding_task_); |
| 292 base::TimeTicks snapshot_time = timestamp_and_map.first; | 290 base::TimeTicks snapshot_time = timestamp_and_map.first; |
| 293 TimestampMap current_timestamp_map = std::move(timestamp_and_map.second); | 291 TimestampMap current_timestamp_map = std::move(timestamp_and_map.second); |
| 294 std::vector<base::FilePath> changed_paths = | 292 std::vector<base::FilePath> changed_paths = |
| 295 CollectChangedPaths(last_timestamp_map_, current_timestamp_map); | 293 CollectChangedPaths(last_timestamp_map_, current_timestamp_map); |
| 296 | 294 |
| 297 last_timestamp_map_ = std::move(current_timestamp_map); | 295 last_timestamp_map_ = std::move(current_timestamp_map); |
| 298 | 296 |
| 299 mojo::Array<mojo::String> mojo_paths(changed_paths.size()); | 297 std::vector<std::string> mojo_paths(changed_paths.size()); |
|
Yusuke Sato
2016/11/11 22:12:08
nit: s/mojo_// or s/mojo_/string_/ ?
Luis Héctor Chávez
2016/11/11 22:19:21
Chose the latter.
| |
| 300 for (size_t i = 0; i < changed_paths.size(); ++i) { | 298 for (size_t i = 0; i < changed_paths.size(); ++i) { |
| 301 mojo_paths[i] = changed_paths[i].value(); | 299 mojo_paths[i] = changed_paths[i].value(); |
| 302 } | 300 } |
| 303 BrowserThread::PostTask( | 301 BrowserThread::PostTask( |
| 304 BrowserThread::UI, FROM_HERE, | 302 BrowserThread::UI, FROM_HERE, |
| 305 base::Bind(callback_, base::Passed(std::move(mojo_paths)))); | 303 base::Bind(callback_, base::Passed(std::move(mojo_paths)))); |
| 306 if (last_notify_time_ > snapshot_time) { | 304 if (last_notify_time_ > snapshot_time) { |
| 307 base::PostTaskAndReplyWithResult( | 305 base::PostTaskAndReplyWithResult( |
| 308 BrowserThread::GetBlockingPool(), FROM_HERE, | 306 BrowserThread::GetBlockingPool(), FROM_HERE, |
| 309 base::Bind(&BuildTimestampMapCallback, downloads_dir_), | 307 base::Bind(&BuildTimestampMapCallback, downloads_dir_), |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 352 | 350 |
| 353 void ArcDownloadsWatcherService::StopWatchingDownloads() { | 351 void ArcDownloadsWatcherService::StopWatchingDownloads() { |
| 354 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 352 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 355 if (watcher_) { | 353 if (watcher_) { |
| 356 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, | 354 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, |
| 357 watcher_.release()); | 355 watcher_.release()); |
| 358 } | 356 } |
| 359 } | 357 } |
| 360 | 358 |
| 361 void ArcDownloadsWatcherService::OnDownloadsChanged( | 359 void ArcDownloadsWatcherService::OnDownloadsChanged( |
| 362 mojo::Array<mojo::String> paths) { | 360 const std::vector<std::string>& paths) { |
| 363 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 361 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 364 | 362 |
| 365 auto* instance = arc_bridge_service()->file_system()->GetInstanceForMethod( | 363 auto* instance = arc_bridge_service()->file_system()->GetInstanceForMethod( |
| 366 "RequestMediaScan"); | 364 "RequestMediaScan"); |
| 367 if (!instance) | 365 if (!instance) |
| 368 return; | 366 return; |
| 369 instance->RequestMediaScan(std::move(paths)); | 367 instance->RequestMediaScan(paths); |
| 370 } | 368 } |
| 371 | 369 |
| 372 } // namespace arc | 370 } // namespace arc |
| OLD | NEW |