Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(642)

Unified Diff: chrome/browser/chromeos/arc/arc_downloads_watcher_service.cc

Issue 2310833002: Don't block FILE thread for media scanning (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/chromeos/arc/arc_downloads_watcher_service.cc
diff --git a/chrome/browser/chromeos/arc/arc_downloads_watcher_service.cc b/chrome/browser/chromeos/arc/arc_downloads_watcher_service.cc
index cf63bf4dfe3933669f078652c92a5bcaa14a56a4..343dd5aba7c1c4f78d11884b6df1a057608d7e9a 100644
--- a/chrome/browser/chromeos/arc/arc_downloads_watcher_service.cc
+++ b/chrome/browser/chromeos/arc/arc_downloads_watcher_service.cc
@@ -30,6 +30,10 @@ namespace arc {
namespace {
+// ID used to ensure sequential execution of HandleFilePathChanged.
+// This is sequential to prevent two instances from updating last_update_time_.
+const char kSequenceId[] = "arc_downloads_watcher";
+
const base::FilePath::CharType kAndroidDownloadDir[] =
FILE_PATH_LITERAL("/storage/emulated/0/Download");
@@ -87,8 +91,15 @@ class ArcDownloadsWatcherService::DownloadsWatcher {
private:
// Called by base::FilePathWatcher to notify file changes.
+ // Wrapper around HandleFilePathChanged to pass the notification timestamp
+ // and move the work off the FILE thread.
void OnFilePathChanged(const base::FilePath& path, bool error);
+ // If there has not been a full scan of updated files since notify_time,
+ // update the last_timestamp_map_ and send any new/changed files to the
+ // android media scanner.
+ void HandleFilePathChanged(base::Time notify_time);
+
// Scans files under |downloads_dir_| recursively and builds a map from file
// paths (in Android filesystem) to last modified timestamps.
TimestampMap BuildTimestampMap() const;
@@ -97,6 +108,7 @@ class ArcDownloadsWatcherService::DownloadsWatcher {
base::FilePath downloads_dir_;
std::unique_ptr<base::FilePathWatcher> watcher_;
TimestampMap last_timestamp_map_;
+ base::Time last_update_time_;
// Note: This should remain the last member so it'll be destroyed and
// invalidate the weak pointers before any other members are destroyed.
@@ -107,7 +119,9 @@ class ArcDownloadsWatcherService::DownloadsWatcher {
ArcDownloadsWatcherService::DownloadsWatcher::DownloadsWatcher(
const Callback& callback)
- : callback_(callback), weak_ptr_factory_(this) {
+ : callback_(callback),
+ last_update_time_(base::Time()),
+ weak_ptr_factory_(this) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
downloads_dir_ = DownloadPrefs(ProfileManager::GetActiveUserProfile())
@@ -139,6 +153,18 @@ void ArcDownloadsWatcherService::DownloadsWatcher::OnFilePathChanged(
// On Linux, |error| is always false. Also, |path| is always the same path
// as one given to FilePathWatcher::Watch().
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
+ BrowserThread::PostBlockingPoolSequencedTask(
+ kSequenceId, FROM_HERE,
+ base::Bind(&DownloadsWatcher::HandleFilePathChanged,
+ weak_ptr_factory_.GetWeakPtr(), base::Time::Now()));
Shuhei Takahashi 2016/09/05 09:28:23 Weak pointers can be dereferenced only on FILE thr
dspaid 2016/09/07 01:16:56 Thanks for the reminder. I couldn't really find a
+}
+
+void ArcDownloadsWatcherService::DownloadsWatcher::HandleFilePathChanged(
+ base::Time notify_time) {
+ if (notify_time < last_update_time_) {
Shuhei Takahashi 2016/09/05 09:28:23 Please add a comment about the purpose of this che
dspaid 2016/09/07 01:16:56 Done.
+ return;
+ }
+ last_update_time_ = base::Time::Now();
TimestampMap current_timestamp_map = BuildTimestampMap();
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698