| 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 for (size_t i = 0; i < sensitive_locations.size(); ++i) { |
| 227 if (parent_directory == sensitive_locations[i] || |
| 228 parent_directory.IsParent(sensitive_locations[i])) { |
| 229 continue; |
| 230 } |
| 231 } |
| 232 |
| 223 ContainerCandidates::iterator existing = candidates.find(parent_directory); | 233 ContainerCandidates::iterator existing = candidates.find(parent_directory); |
| 224 if (existing == candidates.end()) { | 234 if (existing == candidates.end()) { |
| 225 candidates[parent_directory] = 1; | 235 candidates[parent_directory] = 1; |
| 226 } else { | 236 } else { |
| 227 existing->second++; | 237 existing->second++; |
| 228 } | 238 } |
| 229 } | 239 } |
| 230 | 240 |
| 231 // If a parent directory has more than one scan result, consider it. | 241 // If a parent directory has more than one scan result, consider it. |
| 232 MediaFolderFinder::MediaFolderFinderResults result; | 242 MediaFolderFinder::MediaFolderFinderResults result; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 245 name = dir_counter.Next()) { | 255 name = dir_counter.Next()) { |
| 246 if (!base::IsLink(name)) | 256 if (!base::IsLink(name)) |
| 247 count++; | 257 count++; |
| 248 } | 258 } |
| 249 if (it->second * 100 / count >= kContainerDirectoryMinimumPercent) | 259 if (it->second * 100 / count >= kContainerDirectoryMinimumPercent) |
| 250 result[it->first] = MediaGalleryScanResult(); | 260 result[it->first] = MediaGalleryScanResult(); |
| 251 } | 261 } |
| 252 return result; | 262 return result; |
| 253 } | 263 } |
| 254 | 264 |
| 255 void RemoveSensitiveLocations( | |
| 256 MediaFolderFinder::MediaFolderFinderResults* found_folders) { | |
| 257 // TODO(vandebo) Use the greylist from filesystem api. | |
| 258 } | |
| 259 | |
| 260 int CountScanResultsForExtension(MediaGalleriesPreferences* preferences, | 265 int CountScanResultsForExtension(MediaGalleriesPreferences* preferences, |
| 261 const extensions::Extension* extension, | 266 const extensions::Extension* extension, |
| 262 MediaGalleryScanResult* file_counts) { | 267 MediaGalleryScanResult* file_counts) { |
| 263 int gallery_count = 0; | 268 int gallery_count = 0; |
| 264 | 269 |
| 265 MediaGalleryPrefIdSet permitted_galleries = | 270 MediaGalleryPrefIdSet permitted_galleries = |
| 266 preferences->GalleriesForExtension(*extension); | 271 preferences->GalleriesForExtension(*extension); |
| 267 const MediaGalleriesPrefInfoMap& known_galleries = | 272 const MediaGalleriesPrefInfoMap& known_galleries = |
| 268 preferences->known_galleries(); | 273 preferences->known_galleries(); |
| 269 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); | 274 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 it != observers_.end(); | 433 it != observers_.end(); |
| 429 ++it) { | 434 ++it) { |
| 430 if (!it->second.scanning_extensions.empty()) | 435 if (!it->second.scanning_extensions.empty()) |
| 431 return true; | 436 return true; |
| 432 } | 437 } |
| 433 return false; | 438 return false; |
| 434 } | 439 } |
| 435 | 440 |
| 436 void MediaScanManager::OnScanCompleted( | 441 void MediaScanManager::OnScanCompleted( |
| 437 bool success, | 442 bool success, |
| 438 const MediaFolderFinder::MediaFolderFinderResults& found_folders) { | 443 const MediaFolderFinder::MediaFolderFinderResults& found_folders, |
| 444 const std::vector<base::FilePath>& sensitive_locations) { |
| 439 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 445 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 440 if (!folder_finder_ || !success) { | 446 if (!folder_finder_ || !success) { |
| 441 folder_finder_.reset(); | 447 folder_finder_.reset(); |
| 442 return; | 448 return; |
| 443 } | 449 } |
| 444 | 450 |
| 445 content::BrowserThread::PostTaskAndReplyWithResult( | 451 content::BrowserThread::PostTaskAndReplyWithResult( |
| 446 content::BrowserThread::FILE, FROM_HERE, | 452 content::BrowserThread::FILE, FROM_HERE, |
| 447 base::Bind(FindContainerScanResults, found_folders), | 453 base::Bind(FindContainerScanResults, |
| 454 found_folders, |
| 455 sensitive_locations), |
| 448 base::Bind(&MediaScanManager::OnFoundContainerDirectories, | 456 base::Bind(&MediaScanManager::OnFoundContainerDirectories, |
| 449 weak_factory_.GetWeakPtr(), found_folders)); | 457 weak_factory_.GetWeakPtr(), |
| 458 found_folders)); |
| 450 } | 459 } |
| 451 | 460 |
| 452 void MediaScanManager::OnFoundContainerDirectories( | 461 void MediaScanManager::OnFoundContainerDirectories( |
| 453 const MediaFolderFinder::MediaFolderFinderResults& found_folders, | 462 const MediaFolderFinder::MediaFolderFinderResults& found_folders, |
| 454 const MediaFolderFinder::MediaFolderFinderResults& container_folders) { | 463 const MediaFolderFinder::MediaFolderFinderResults& container_folders) { |
| 455 MediaFolderFinder::MediaFolderFinderResults folders; | 464 MediaFolderFinder::MediaFolderFinderResults folders; |
| 456 folders.insert(found_folders.begin(), found_folders.end()); | 465 folders.insert(found_folders.begin(), found_folders.end()); |
| 457 folders.insert(container_folders.begin(), container_folders.end()); | 466 folders.insert(container_folders.begin(), container_folders.end()); |
| 458 | 467 |
| 459 RemoveSensitiveLocations(&folders); | |
| 460 | |
| 461 for (ScanObserverMap::iterator scans_for_profile = observers_.begin(); | 468 for (ScanObserverMap::iterator scans_for_profile = observers_.begin(); |
| 462 scans_for_profile != observers_.end(); | 469 scans_for_profile != observers_.end(); |
| 463 ++scans_for_profile) { | 470 ++scans_for_profile) { |
| 464 if (scans_for_profile->second.scanning_extensions.empty()) | 471 if (scans_for_profile->second.scanning_extensions.empty()) |
| 465 continue; | 472 continue; |
| 466 Profile* profile = scans_for_profile->first; | 473 Profile* profile = scans_for_profile->first; |
| 467 MediaGalleriesPreferences* preferences = | 474 MediaGalleriesPreferences* preferences = |
| 468 MediaGalleriesPreferencesFactory::GetForProfile(profile); | 475 MediaGalleriesPreferencesFactory::GetForProfile(profile); |
| 469 ExtensionService* extension_service = | 476 ExtensionService* extension_service = |
| 470 extensions::ExtensionSystem::Get(profile)->extension_service(); | 477 extensions::ExtensionSystem::Get(profile)->extension_service(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 489 gallery_count, | 496 gallery_count, |
| 490 file_counts); | 497 file_counts); |
| 491 } | 498 } |
| 492 } | 499 } |
| 493 scanning_extensions->clear(); | 500 scanning_extensions->clear(); |
| 494 preferences->SetLastScanCompletionTime(base::Time::Now()); | 501 preferences->SetLastScanCompletionTime(base::Time::Now()); |
| 495 } | 502 } |
| 496 registrar_.RemoveAll(); | 503 registrar_.RemoveAll(); |
| 497 folder_finder_.reset(); | 504 folder_finder_.reset(); |
| 498 } | 505 } |
| OLD | NEW |