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

Side by Side Diff: chrome/browser/chromeos/arc/arc_downloads_watcher_service.cc

Issue 2310833002: Don't block FILE thread for media scanning (Closed)
Patch Set: Move logic back to FILE thread 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 using Callback = base::Callback<void(mojo::Array<mojo::String> paths)>; 79 using Callback = base::Callback<void(mojo::Array<mojo::String> paths)>;
80 80
81 explicit DownloadsWatcher(const Callback& callback); 81 explicit DownloadsWatcher(const Callback& callback);
82 ~DownloadsWatcher(); 82 ~DownloadsWatcher();
83 83
84 // Starts watching Downloads directory. 84 // Starts watching Downloads directory.
85 void Start(); 85 void Start();
86 86
87 private: 87 private:
88 // Called by base::FilePathWatcher to notify file changes. 88 // Called by base::FilePathWatcher to notify file changes.
89 // Wrapper around HandleFilePathChanged to pass the notification timestamp.
89 void OnFilePathChanged(const base::FilePath& path, bool error); 90 void OnFilePathChanged(const base::FilePath& path, bool error);
90 91
92 // If there has not been a full scan of updated files since notify_time,
93 // update the last_timestamp_map_ and send any new/changed files to the
94 // android media scanner.
95 void HandleFilePathChanged(base::Time notify_time);
96
91 // Scans files under |downloads_dir_| recursively and builds a map from file 97 // Scans files under |downloads_dir_| recursively and builds a map from file
92 // paths (in Android filesystem) to last modified timestamps. 98 // paths (in Android filesystem) to last modified timestamps.
93 TimestampMap BuildTimestampMap() const; 99 TimestampMap BuildTimestampMap() const;
94 100
95 Callback callback_; 101 Callback callback_;
96 base::FilePath downloads_dir_; 102 base::FilePath downloads_dir_;
97 std::unique_ptr<base::FilePathWatcher> watcher_; 103 std::unique_ptr<base::FilePathWatcher> watcher_;
98 TimestampMap last_timestamp_map_; 104 TimestampMap last_timestamp_map_;
105 base::Time last_update_time_;
99 106
100 // Note: This should remain the last member so it'll be destroyed and 107 // Note: This should remain the last member so it'll be destroyed and
101 // invalidate the weak pointers before any other members are destroyed. 108 // invalidate the weak pointers before any other members are destroyed.
102 base::WeakPtrFactory<DownloadsWatcher> weak_ptr_factory_; 109 base::WeakPtrFactory<DownloadsWatcher> weak_ptr_factory_;
103 110
104 DISALLOW_COPY_AND_ASSIGN(DownloadsWatcher); 111 DISALLOW_COPY_AND_ASSIGN(DownloadsWatcher);
105 }; 112 };
106 113
107 ArcDownloadsWatcherService::DownloadsWatcher::DownloadsWatcher( 114 ArcDownloadsWatcherService::DownloadsWatcher::DownloadsWatcher(
108 const Callback& callback) 115 const Callback& callback)
109 : callback_(callback), weak_ptr_factory_(this) { 116 : callback_(callback),
117 last_update_time_(base::Time()),
118 weak_ptr_factory_(this) {
110 DCHECK_CURRENTLY_ON(BrowserThread::UI); 119 DCHECK_CURRENTLY_ON(BrowserThread::UI);
111 120
112 downloads_dir_ = DownloadPrefs(ProfileManager::GetActiveUserProfile()) 121 downloads_dir_ = DownloadPrefs(ProfileManager::GetActiveUserProfile())
113 .GetDefaultDownloadDirectoryForProfile() 122 .GetDefaultDownloadDirectoryForProfile()
114 .StripTrailingSeparators(); 123 .StripTrailingSeparators();
115 } 124 }
116 125
117 ArcDownloadsWatcherService::DownloadsWatcher::~DownloadsWatcher() { 126 ArcDownloadsWatcherService::DownloadsWatcher::~DownloadsWatcher() {
118 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 127 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
119 } 128 }
(...skipping 11 matching lines...) Expand all
131 base::Bind(&DownloadsWatcher::OnFilePathChanged, 140 base::Bind(&DownloadsWatcher::OnFilePathChanged,
132 weak_ptr_factory_.GetWeakPtr())); 141 weak_ptr_factory_.GetWeakPtr()));
133 } 142 }
134 143
135 void ArcDownloadsWatcherService::DownloadsWatcher::OnFilePathChanged( 144 void ArcDownloadsWatcherService::DownloadsWatcher::OnFilePathChanged(
136 const base::FilePath& path, 145 const base::FilePath& path,
137 bool error) { 146 bool error) {
138 // On Linux, |error| is always false. Also, |path| is always the same path 147 // On Linux, |error| is always false. Also, |path| is always the same path
139 // as one given to FilePathWatcher::Watch(). 148 // as one given to FilePathWatcher::Watch().
140 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 149 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
150 BrowserThread::PostTask(
151 FROM_HERE, base::Bind(&DownloadsWatcher::HandleFilePathChanged,
152 weak_ptr_factory_.GetWeakPtr(), base::Time::Now()));
153 }
154
155 void ArcDownloadsWatcherService::DownloadsWatcher::HandleFilePathChanged(
156 base::Time notify_time) {
157 // Because this logic handles all updates and not just the update that
158 // triggered the callback we can return early if we have already processed
159 // updates to the directory after this callback was created.
160 if (notify_time < last_update_time_) {
161 return;
162 }
163 last_update_time_ = base::Time::Now();
141 164
142 TimestampMap current_timestamp_map = BuildTimestampMap(); 165 TimestampMap current_timestamp_map = BuildTimestampMap();
hidehiko 2016/09/07 01:44:32 For the record. As we chatted offline, because th
143 166
144 std::vector<base::FilePath> changed_paths = 167 std::vector<base::FilePath> changed_paths =
145 CollectChangedPaths(last_timestamp_map_, current_timestamp_map); 168 CollectChangedPaths(last_timestamp_map_, current_timestamp_map);
146 169
147 last_timestamp_map_ = std::move(current_timestamp_map); 170 last_timestamp_map_ = std::move(current_timestamp_map);
148 171
149 mojo::Array<mojo::String> mojo_paths(changed_paths.size()); 172 mojo::Array<mojo::String> mojo_paths(changed_paths.size());
150 for (size_t i = 0; i < changed_paths.size(); ++i) { 173 for (size_t i = 0; i < changed_paths.size(); ++i) {
151 mojo_paths[i] = changed_paths[i].value(); 174 mojo_paths[i] = changed_paths[i].value();
152 } 175 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 mojo::Array<mojo::String> paths) { 248 mojo::Array<mojo::String> paths) {
226 DCHECK_CURRENTLY_ON(BrowserThread::UI); 249 DCHECK_CURRENTLY_ON(BrowserThread::UI);
227 250
228 auto* instance = arc_bridge_service()->file_system()->instance(); 251 auto* instance = arc_bridge_service()->file_system()->instance();
229 if (!instance) 252 if (!instance)
230 return; 253 return;
231 instance->RequestMediaScan(std::move(paths)); 254 instance->RequestMediaScan(std::move(paths));
232 } 255 }
233 256
234 } // namespace arc 257 } // namespace arc
OLDNEW
« 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