OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/media_galleries/media_scan_manager.h" | 5 #include "chrome/browser/media_galleries/media_scan_manager.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/file_enumerator.h" | 8 #include "base/files/file_enumerator.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 gallery.last_attach_time, file_counts.audio_count, | 205 gallery.last_attach_time, file_counts.audio_count, |
206 file_counts.image_count, file_counts.video_count); | 206 file_counts.image_count, file_counts.video_count); |
207 } | 207 } |
208 } | 208 } |
209 | 209 |
210 // A single directory may contain many folders with media in them, without | 210 // A single directory may contain many folders with media in them, without |
211 // containing any media itself. In fact, the primary purpose of that directory | 211 // containing any media itself. In fact, the primary purpose of that directory |
212 // may be to contain media directories. This function tries to find those | 212 // may be to contain media directories. This function tries to find those |
213 // immediate container directories. | 213 // immediate container directories. |
214 MediaFolderFinder::MediaFolderFinderResults FindContainerScanResults( | 214 MediaFolderFinder::MediaFolderFinderResults FindContainerScanResults( |
215 const MediaFolderFinder::MediaFolderFinderResults& found_folders) { | 215 const MediaFolderFinder::MediaFolderFinderResults& found_folders, |
216 const std::vector<base::FilePath> sensitive_locations) { | |
216 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 217 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
217 // Count the number of scan results with the same parent directory. | 218 // Count the number of scan results with the same parent directory. |
218 typedef std::map<base::FilePath, int /*count*/> ContainerCandidates; | 219 typedef std::map<base::FilePath, int /*count*/> ContainerCandidates; |
219 ContainerCandidates candidates; | 220 ContainerCandidates candidates; |
220 for (MediaFolderFinder::MediaFolderFinderResults::const_iterator it = | 221 for (MediaFolderFinder::MediaFolderFinderResults::const_iterator it = |
221 found_folders.begin(); it != found_folders.end(); ++it) { | 222 found_folders.begin(); it != found_folders.end(); ++it) { |
222 base::FilePath parent_directory = it->first.DirName(); | 223 base::FilePath parent_directory = it->first.DirName(); |
224 | |
225 // Skip sensitive folders and their ancestors. | |
226 bool is_sensitive = false; | |
227 for (size_t i = 0; i < sensitive_locations.size(); ++i) { | |
228 if (parent_directory == sensitive_locations[i] || | |
229 parent_directory.IsParent(sensitive_locations[i])) { | |
230 is_sensitive = true; | |
231 continue; | |
vandebo (ex-Chrome)
2014/02/25 05:06:55
nit: shouldn't this be a break?
Lei Zhang
2014/02/26 00:58:54
Done.
| |
232 } | |
233 } | |
234 if (is_sensitive) | |
235 continue; | |
236 | |
223 ContainerCandidates::iterator existing = candidates.find(parent_directory); | 237 ContainerCandidates::iterator existing = candidates.find(parent_directory); |
224 if (existing == candidates.end()) { | 238 if (existing == candidates.end()) { |
225 candidates[parent_directory] = 1; | 239 candidates[parent_directory] = 1; |
226 } else { | 240 } else { |
227 existing->second++; | 241 existing->second++; |
228 } | 242 } |
229 } | 243 } |
230 | 244 |
231 // If a parent directory has more than one scan result, consider it. | 245 // If a parent directory has more than one scan result, consider it. |
232 MediaFolderFinder::MediaFolderFinderResults result; | 246 MediaFolderFinder::MediaFolderFinderResults result; |
(...skipping 12 matching lines...) Expand all Loading... | |
245 name = dir_counter.Next()) { | 259 name = dir_counter.Next()) { |
246 if (!base::IsLink(name)) | 260 if (!base::IsLink(name)) |
247 count++; | 261 count++; |
248 } | 262 } |
249 if (it->second * 100 / count >= kContainerDirectoryMinimumPercent) | 263 if (it->second * 100 / count >= kContainerDirectoryMinimumPercent) |
250 result[it->first] = MediaGalleryScanResult(); | 264 result[it->first] = MediaGalleryScanResult(); |
251 } | 265 } |
252 return result; | 266 return result; |
253 } | 267 } |
254 | 268 |
255 void RemoveSensitiveLocations( | |
256 MediaFolderFinder::MediaFolderFinderResults* found_folders) { | |
257 // TODO(vandebo) Use the greylist from filesystem api. | |
258 } | |
259 | |
260 int CountScanResultsForExtension(MediaGalleriesPreferences* preferences, | 269 int CountScanResultsForExtension(MediaGalleriesPreferences* preferences, |
261 const extensions::Extension* extension, | 270 const extensions::Extension* extension, |
262 MediaGalleryScanResult* file_counts) { | 271 MediaGalleryScanResult* file_counts) { |
263 int gallery_count = 0; | 272 int gallery_count = 0; |
264 | 273 |
265 MediaGalleryPrefIdSet permitted_galleries = | 274 MediaGalleryPrefIdSet permitted_galleries = |
266 preferences->GalleriesForExtension(*extension); | 275 preferences->GalleriesForExtension(*extension); |
267 const MediaGalleriesPrefInfoMap& known_galleries = | 276 const MediaGalleriesPrefInfoMap& known_galleries = |
268 preferences->known_galleries(); | 277 preferences->known_galleries(); |
269 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); | 278 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
437 bool success, | 446 bool success, |
438 const MediaFolderFinder::MediaFolderFinderResults& found_folders) { | 447 const MediaFolderFinder::MediaFolderFinderResults& found_folders) { |
439 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 448 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
440 if (!folder_finder_ || !success) { | 449 if (!folder_finder_ || !success) { |
441 folder_finder_.reset(); | 450 folder_finder_.reset(); |
442 return; | 451 return; |
443 } | 452 } |
444 | 453 |
445 content::BrowserThread::PostTaskAndReplyWithResult( | 454 content::BrowserThread::PostTaskAndReplyWithResult( |
446 content::BrowserThread::FILE, FROM_HERE, | 455 content::BrowserThread::FILE, FROM_HERE, |
447 base::Bind(FindContainerScanResults, found_folders), | 456 base::Bind(FindContainerScanResults, |
457 found_folders, | |
458 folder_finder_->graylisted_folders()), | |
448 base::Bind(&MediaScanManager::OnFoundContainerDirectories, | 459 base::Bind(&MediaScanManager::OnFoundContainerDirectories, |
449 weak_factory_.GetWeakPtr(), found_folders)); | 460 weak_factory_.GetWeakPtr(), |
461 found_folders)); | |
450 } | 462 } |
451 | 463 |
452 void MediaScanManager::OnFoundContainerDirectories( | 464 void MediaScanManager::OnFoundContainerDirectories( |
453 const MediaFolderFinder::MediaFolderFinderResults& found_folders, | 465 const MediaFolderFinder::MediaFolderFinderResults& found_folders, |
454 const MediaFolderFinder::MediaFolderFinderResults& container_folders) { | 466 const MediaFolderFinder::MediaFolderFinderResults& container_folders) { |
455 MediaFolderFinder::MediaFolderFinderResults folders; | 467 MediaFolderFinder::MediaFolderFinderResults folders; |
456 folders.insert(found_folders.begin(), found_folders.end()); | 468 folders.insert(found_folders.begin(), found_folders.end()); |
457 folders.insert(container_folders.begin(), container_folders.end()); | 469 folders.insert(container_folders.begin(), container_folders.end()); |
458 | 470 |
459 RemoveSensitiveLocations(&folders); | |
460 | |
461 for (ScanObserverMap::iterator scans_for_profile = observers_.begin(); | 471 for (ScanObserverMap::iterator scans_for_profile = observers_.begin(); |
462 scans_for_profile != observers_.end(); | 472 scans_for_profile != observers_.end(); |
463 ++scans_for_profile) { | 473 ++scans_for_profile) { |
464 if (scans_for_profile->second.scanning_extensions.empty()) | 474 if (scans_for_profile->second.scanning_extensions.empty()) |
465 continue; | 475 continue; |
466 Profile* profile = scans_for_profile->first; | 476 Profile* profile = scans_for_profile->first; |
467 MediaGalleriesPreferences* preferences = | 477 MediaGalleriesPreferences* preferences = |
468 MediaGalleriesPreferencesFactory::GetForProfile(profile); | 478 MediaGalleriesPreferencesFactory::GetForProfile(profile); |
469 ExtensionService* extension_service = | 479 ExtensionService* extension_service = |
470 extensions::ExtensionSystem::Get(profile)->extension_service(); | 480 extensions::ExtensionSystem::Get(profile)->extension_service(); |
(...skipping 18 matching lines...) Expand all Loading... | |
489 gallery_count, | 499 gallery_count, |
490 file_counts); | 500 file_counts); |
491 } | 501 } |
492 } | 502 } |
493 scanning_extensions->clear(); | 503 scanning_extensions->clear(); |
494 preferences->SetLastScanCompletionTime(base::Time::Now()); | 504 preferences->SetLastScanCompletionTime(base::Time::Now()); |
495 } | 505 } |
496 registrar_.RemoveAll(); | 506 registrar_.RemoveAll(); |
497 folder_finder_.reset(); | 507 folder_finder_.reset(); |
498 } | 508 } |
OLD | NEW |