OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/media_galleries/media_folder_finder.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include <algorithm> | |
11 #include <set> | |
12 | |
13 #include "base/files/file_enumerator.h" | |
14 #include "base/files/file_util.h" | |
15 #include "base/macros.h" | |
16 #include "base/path_service.h" | |
17 #include "base/sequence_checker.h" | |
18 #include "base/stl_util.h" | |
19 #include "base/strings/string_util.h" | |
20 #include "base/task_runner_util.h" | |
21 #include "base/threading/sequenced_worker_pool.h" | |
22 #include "build/build_config.h" | |
23 #include "chrome/browser/extensions/api/file_system/file_system_api.h" | |
24 #include "chrome/browser/media_galleries/fileapi/media_path_filter.h" | |
25 #include "chrome/common/chrome_paths.h" | |
26 #include "components/storage_monitor/storage_monitor.h" | |
27 #include "content/public/browser/browser_thread.h" | |
28 | |
29 #if defined(OS_CHROMEOS) | |
30 #include "chrome/common/chrome_paths.h" | |
31 #include "chromeos/dbus/cros_disks_client.h" | |
32 #endif | |
33 | |
34 using storage_monitor::StorageInfo; | |
35 using storage_monitor::StorageMonitor; | |
36 | |
37 typedef base::Callback<void(const std::vector<base::FilePath>& /*roots*/)> | |
38 DefaultScanRootsCallback; | |
39 using content::BrowserThread; | |
40 | |
41 namespace { | |
42 | |
43 const int64_t kMinimumImageSize = 200 * 1024; // 200 KB | |
44 const int64_t kMinimumAudioSize = 500 * 1024; // 500 KB | |
45 const int64_t kMinimumVideoSize = 1024 * 1024; // 1 MB | |
46 | |
47 const int kPrunedPaths[] = { | |
48 #if defined(OS_WIN) | |
49 base::DIR_IE_INTERNET_CACHE, | |
50 base::DIR_PROGRAM_FILES, | |
51 base::DIR_PROGRAM_FILESX86, | |
52 base::DIR_WINDOWS, | |
53 #endif | |
54 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
55 chrome::DIR_USER_APPLICATIONS, | |
56 chrome::DIR_USER_LIBRARY, | |
57 #endif | |
58 #if defined(OS_LINUX) | |
59 base::DIR_CACHE, | |
60 #endif | |
61 #if defined(OS_WIN) || defined(OS_LINUX) | |
62 base::DIR_TEMP, | |
63 #endif | |
64 }; | |
65 | |
66 bool IsValidScanPath(const base::FilePath& path) { | |
67 return !path.empty() && path.IsAbsolute(); | |
68 } | |
69 | |
70 void CountScanResult(MediaGalleryScanFileType type, | |
71 MediaGalleryScanResult* scan_result) { | |
72 if (type & MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE) | |
73 scan_result->image_count += 1; | |
74 if (type & MEDIA_GALLERY_SCAN_FILE_TYPE_AUDIO) | |
75 scan_result->audio_count += 1; | |
76 if (type & MEDIA_GALLERY_SCAN_FILE_TYPE_VIDEO) | |
77 scan_result->video_count += 1; | |
78 } | |
79 | |
80 bool FileMeetsSizeRequirement(MediaGalleryScanFileType type, int64_t size) { | |
81 if (type & MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE) | |
82 if (size >= kMinimumImageSize) | |
83 return true; | |
84 if (type & MEDIA_GALLERY_SCAN_FILE_TYPE_AUDIO) | |
85 if (size >= kMinimumAudioSize) | |
86 return true; | |
87 if (type & MEDIA_GALLERY_SCAN_FILE_TYPE_VIDEO) | |
88 if (size >= kMinimumVideoSize) | |
89 return true; | |
90 return false; | |
91 } | |
92 | |
93 // Return true if |path| should not be considered as the starting point for a | |
94 // media scan. | |
95 bool ShouldIgnoreScanRoot(const base::FilePath& path) { | |
96 #if defined(OS_MACOSX) | |
97 // Scanning root is of little value. | |
98 return (path.value() == "/"); | |
99 #elif defined(OS_CHROMEOS) | |
100 // Sanity check to make sure mount points are where they should be. | |
101 base::FilePath mount_point = | |
102 chromeos::CrosDisksClient::GetRemovableDiskMountPoint(); | |
103 return mount_point.IsParent(path); | |
104 #elif defined(OS_LINUX) | |
105 // /media and /mnt are likely the only places with interesting mount points. | |
106 if (base::StartsWith(path.value(), "/media", base::CompareCase::SENSITIVE) || | |
107 base::StartsWith(path.value(), "/mnt", base::CompareCase::SENSITIVE)) { | |
108 return false; | |
109 } | |
110 return true; | |
111 #elif defined(OS_WIN) | |
112 return false; | |
113 #else | |
114 NOTIMPLEMENTED(); | |
115 return false; | |
116 #endif | |
117 } | |
118 | |
119 // Return a location that is likely to have user data to scan, if any. | |
120 base::FilePath GetPlatformSpecificDefaultScanRoot() { | |
121 base::FilePath root; | |
122 #if defined(OS_CHROMEOS) | |
123 PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &root); | |
124 #elif defined(OS_MACOSX) || defined(OS_LINUX) | |
125 PathService::Get(base::DIR_HOME, &root); | |
126 #elif defined(OS_WIN) | |
127 // Nothing to add. | |
128 #else | |
129 NOTIMPLEMENTED(); | |
130 #endif | |
131 return root; | |
132 } | |
133 | |
134 // Find the likely locations with user media files and pass them to | |
135 // |callback|. Locations are platform specific. | |
136 void GetDefaultScanRoots(const DefaultScanRootsCallback& callback, | |
137 bool has_override, | |
138 const std::vector<base::FilePath>& override_paths) { | |
139 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
140 | |
141 if (has_override) { | |
142 callback.Run(override_paths); | |
143 return; | |
144 } | |
145 | |
146 StorageMonitor* monitor = StorageMonitor::GetInstance(); | |
147 DCHECK(monitor->IsInitialized()); | |
148 | |
149 std::vector<base::FilePath> roots; | |
150 std::vector<StorageInfo> storages = monitor->GetAllAvailableStorages(); | |
151 for (size_t i = 0; i < storages.size(); ++i) { | |
152 StorageInfo::Type type; | |
153 if (!StorageInfo::CrackDeviceId(storages[i].device_id(), &type, NULL) || | |
154 (type != StorageInfo::FIXED_MASS_STORAGE && | |
155 type != StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM)) { | |
156 continue; | |
157 } | |
158 base::FilePath path(storages[i].location()); | |
159 if (ShouldIgnoreScanRoot(path)) | |
160 continue; | |
161 roots.push_back(path); | |
162 } | |
163 | |
164 base::FilePath platform_root = GetPlatformSpecificDefaultScanRoot(); | |
165 if (!platform_root.empty()) | |
166 roots.push_back(platform_root); | |
167 callback.Run(roots); | |
168 } | |
169 | |
170 } // namespace | |
171 | |
172 MediaFolderFinder::WorkerReply::WorkerReply() {} | |
173 | |
174 MediaFolderFinder::WorkerReply::~WorkerReply() {} | |
175 | |
176 // The Worker is created on the UI thread, but does all its work on a blocking | |
177 // SequencedTaskRunner. | |
178 class MediaFolderFinder::Worker { | |
179 public: | |
180 explicit Worker(const std::vector<base::FilePath>& graylisted_folders); | |
181 ~Worker(); | |
182 | |
183 // Scans |path| and return the results. | |
184 WorkerReply ScanFolder(const base::FilePath& path); | |
185 | |
186 private: | |
187 void MakeFolderPathsAbsolute(); | |
188 | |
189 bool folder_paths_are_absolute_; | |
190 std::vector<base::FilePath> graylisted_folders_; | |
191 std::vector<base::FilePath> pruned_folders_; | |
192 | |
193 scoped_ptr<MediaPathFilter> filter_; | |
194 | |
195 base::SequenceChecker sequence_checker_; | |
196 | |
197 DISALLOW_COPY_AND_ASSIGN(Worker); | |
198 }; | |
199 | |
200 MediaFolderFinder::Worker::Worker( | |
201 const std::vector<base::FilePath>& graylisted_folders) | |
202 : folder_paths_are_absolute_(false), | |
203 graylisted_folders_(graylisted_folders), | |
204 filter_(new MediaPathFilter) { | |
205 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
206 | |
207 for (size_t i = 0; i < arraysize(kPrunedPaths); ++i) { | |
208 base::FilePath path; | |
209 if (PathService::Get(kPrunedPaths[i], &path)) | |
210 pruned_folders_.push_back(path); | |
211 } | |
212 | |
213 sequence_checker_.DetachFromSequence(); | |
214 } | |
215 | |
216 MediaFolderFinder::Worker::~Worker() { | |
217 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
218 } | |
219 | |
220 MediaFolderFinder::WorkerReply MediaFolderFinder::Worker::ScanFolder( | |
221 const base::FilePath& path) { | |
222 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
223 CHECK(IsValidScanPath(path)); | |
224 | |
225 if (!folder_paths_are_absolute_) | |
226 MakeFolderPathsAbsolute(); | |
227 | |
228 WorkerReply reply; | |
229 bool folder_meets_size_requirement = false; | |
230 bool is_graylisted_folder = false; | |
231 base::FilePath abspath = base::MakeAbsoluteFilePath(path); | |
232 if (abspath.empty()) | |
233 return reply; | |
234 | |
235 for (size_t i = 0; i < graylisted_folders_.size(); ++i) { | |
236 if (abspath == graylisted_folders_[i] || | |
237 abspath.IsParent(graylisted_folders_[i])) { | |
238 is_graylisted_folder = true; | |
239 break; | |
240 } | |
241 } | |
242 | |
243 base::FileEnumerator enumerator( | |
244 path, | |
245 false, /* recursive? */ | |
246 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES | |
247 #if defined(OS_POSIX) | |
248 | base::FileEnumerator::SHOW_SYM_LINKS // show symlinks, not follow. | |
249 #endif | |
250 ); // NOLINT | |
251 while (!enumerator.Next().empty()) { | |
252 base::FileEnumerator::FileInfo file_info = enumerator.GetInfo(); | |
253 base::FilePath full_path = path.Append(file_info.GetName()); | |
254 if (MediaPathFilter::ShouldSkip(full_path)) | |
255 continue; | |
256 | |
257 // Enumerating a directory. | |
258 if (file_info.IsDirectory()) { | |
259 bool is_pruned_folder = false; | |
260 base::FilePath abs_full_path = base::MakeAbsoluteFilePath(full_path); | |
261 if (abs_full_path.empty()) | |
262 continue; | |
263 for (size_t i = 0; i < pruned_folders_.size(); ++i) { | |
264 if (abs_full_path == pruned_folders_[i]) { | |
265 is_pruned_folder = true; | |
266 break; | |
267 } | |
268 } | |
269 | |
270 if (!is_pruned_folder) | |
271 reply.new_folders.push_back(full_path); | |
272 continue; | |
273 } | |
274 | |
275 // Enumerating a file. | |
276 // | |
277 // Do not include scan results for graylisted folders. | |
278 if (is_graylisted_folder) | |
279 continue; | |
280 | |
281 MediaGalleryScanFileType type = filter_->GetType(full_path); | |
282 if (type == MEDIA_GALLERY_SCAN_FILE_TYPE_UNKNOWN) | |
283 continue; | |
284 | |
285 CountScanResult(type, &reply.scan_result); | |
286 if (!folder_meets_size_requirement) { | |
287 folder_meets_size_requirement = | |
288 FileMeetsSizeRequirement(type, file_info.GetSize()); | |
289 } | |
290 } | |
291 // Make sure there is at least 1 file above a size threshold. | |
292 if (!folder_meets_size_requirement) | |
293 reply.scan_result = MediaGalleryScanResult(); | |
294 return reply; | |
295 } | |
296 | |
297 void MediaFolderFinder::Worker::MakeFolderPathsAbsolute() { | |
298 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
299 DCHECK(!folder_paths_are_absolute_); | |
300 folder_paths_are_absolute_ = true; | |
301 | |
302 std::vector<base::FilePath> abs_paths; | |
303 for (size_t i = 0; i < graylisted_folders_.size(); ++i) { | |
304 base::FilePath path = base::MakeAbsoluteFilePath(graylisted_folders_[i]); | |
305 if (!path.empty()) | |
306 abs_paths.push_back(path); | |
307 } | |
308 graylisted_folders_ = abs_paths; | |
309 abs_paths.clear(); | |
310 for (size_t i = 0; i < pruned_folders_.size(); ++i) { | |
311 base::FilePath path = base::MakeAbsoluteFilePath(pruned_folders_[i]); | |
312 if (!path.empty()) | |
313 abs_paths.push_back(path); | |
314 } | |
315 pruned_folders_ = abs_paths; | |
316 } | |
317 | |
318 MediaFolderFinder::MediaFolderFinder( | |
319 const MediaFolderFinderResultsCallback& callback) | |
320 : results_callback_(callback), | |
321 graylisted_folders_( | |
322 extensions::file_system_api::GetGrayListedDirectories()), | |
323 scan_state_(SCAN_STATE_NOT_STARTED), | |
324 worker_(new Worker(graylisted_folders_)), | |
325 has_roots_for_testing_(false), | |
326 weak_factory_(this) { | |
327 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
328 | |
329 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | |
330 worker_task_runner_ = pool->GetSequencedTaskRunner(pool->GetSequenceToken()); | |
331 } | |
332 | |
333 MediaFolderFinder::~MediaFolderFinder() { | |
334 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
335 | |
336 worker_task_runner_->DeleteSoon(FROM_HERE, worker_); | |
337 | |
338 if (scan_state_ == SCAN_STATE_FINISHED) | |
339 return; | |
340 | |
341 MediaFolderFinderResults empty_results; | |
342 results_callback_.Run(false /* success? */, empty_results); | |
343 } | |
344 | |
345 void MediaFolderFinder::StartScan() { | |
346 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
347 | |
348 if (scan_state_ != SCAN_STATE_NOT_STARTED) | |
349 return; | |
350 | |
351 scan_state_ = SCAN_STATE_STARTED; | |
352 GetDefaultScanRoots( | |
353 base::Bind(&MediaFolderFinder::OnInitialized, weak_factory_.GetWeakPtr()), | |
354 has_roots_for_testing_, | |
355 roots_for_testing_); | |
356 } | |
357 | |
358 const std::vector<base::FilePath>& | |
359 MediaFolderFinder::graylisted_folders() const { | |
360 return graylisted_folders_; | |
361 } | |
362 | |
363 void MediaFolderFinder::SetRootsForTesting( | |
364 const std::vector<base::FilePath>& roots) { | |
365 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
366 DCHECK_EQ(SCAN_STATE_NOT_STARTED, scan_state_); | |
367 | |
368 has_roots_for_testing_ = true; | |
369 roots_for_testing_ = roots; | |
370 } | |
371 | |
372 void MediaFolderFinder::OnInitialized( | |
373 const std::vector<base::FilePath>& roots) { | |
374 DCHECK_EQ(SCAN_STATE_STARTED, scan_state_); | |
375 | |
376 std::set<base::FilePath> valid_roots; | |
377 for (size_t i = 0; i < roots.size(); ++i) { | |
378 // Skip if |path| is invalid or redundant. | |
379 const base::FilePath& path = roots[i]; | |
380 if (!IsValidScanPath(path)) | |
381 continue; | |
382 if (ContainsKey(valid_roots, path)) | |
383 continue; | |
384 | |
385 // Check for overlap. | |
386 bool valid_roots_contains_path = false; | |
387 std::vector<base::FilePath> overlapping_paths_to_remove; | |
388 for (std::set<base::FilePath>::iterator it = valid_roots.begin(); | |
389 it != valid_roots.end(); ++it) { | |
390 if (it->IsParent(path)) { | |
391 valid_roots_contains_path = true; | |
392 break; | |
393 } | |
394 const base::FilePath& other_path = *it; | |
395 if (path.IsParent(other_path)) | |
396 overlapping_paths_to_remove.push_back(other_path); | |
397 } | |
398 if (valid_roots_contains_path) | |
399 continue; | |
400 // Remove anything |path| overlaps from |valid_roots|. | |
401 for (size_t i = 0; i < overlapping_paths_to_remove.size(); ++i) | |
402 valid_roots.erase(overlapping_paths_to_remove[i]); | |
403 | |
404 valid_roots.insert(path); | |
405 } | |
406 | |
407 std::copy(valid_roots.begin(), valid_roots.end(), | |
408 std::back_inserter(folders_to_scan_)); | |
409 ScanFolder(); | |
410 } | |
411 | |
412 void MediaFolderFinder::ScanFolder() { | |
413 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
414 DCHECK_EQ(SCAN_STATE_STARTED, scan_state_); | |
415 | |
416 if (folders_to_scan_.empty()) { | |
417 scan_state_ = SCAN_STATE_FINISHED; | |
418 results_callback_.Run(true /* success? */, results_); | |
419 return; | |
420 } | |
421 | |
422 base::FilePath folder_to_scan = folders_to_scan_.back(); | |
423 folders_to_scan_.pop_back(); | |
424 base::PostTaskAndReplyWithResult( | |
425 worker_task_runner_.get(), | |
426 FROM_HERE, | |
427 base::Bind( | |
428 &Worker::ScanFolder, base::Unretained(worker_), folder_to_scan), | |
429 base::Bind(&MediaFolderFinder::GotScanResults, | |
430 weak_factory_.GetWeakPtr(), | |
431 folder_to_scan)); | |
432 } | |
433 | |
434 void MediaFolderFinder::GotScanResults(const base::FilePath& path, | |
435 const WorkerReply& reply) { | |
436 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
437 DCHECK_EQ(SCAN_STATE_STARTED, scan_state_); | |
438 DCHECK(!path.empty()); | |
439 CHECK(!ContainsKey(results_, path)); | |
440 | |
441 if (!IsEmptyScanResult(reply.scan_result)) | |
442 results_[path] = reply.scan_result; | |
443 | |
444 // Push new folders to the |folders_to_scan_| in reverse order. | |
445 std::copy(reply.new_folders.rbegin(), reply.new_folders.rend(), | |
446 std::back_inserter(folders_to_scan_)); | |
447 | |
448 ScanFolder(); | |
449 } | |
OLD | NEW |