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)); |
| 218 std::vector<base::FilePath> abs_sensitive_locations; |
| 219 for (size_t i = 0; i < sensitive_locations.size(); ++i) { |
| 220 base::FilePath path = base::MakeAbsoluteFilePath(sensitive_locations[i]); |
| 221 if (!path.empty()) |
| 222 abs_sensitive_locations.push_back(path); |
| 223 } |
217 // Count the number of scan results with the same parent directory. | 224 // Count the number of scan results with the same parent directory. |
218 typedef std::map<base::FilePath, int /*count*/> ContainerCandidates; | 225 typedef std::map<base::FilePath, int /*count*/> ContainerCandidates; |
219 ContainerCandidates candidates; | 226 ContainerCandidates candidates; |
220 for (MediaFolderFinder::MediaFolderFinderResults::const_iterator it = | 227 for (MediaFolderFinder::MediaFolderFinderResults::const_iterator it = |
221 found_folders.begin(); it != found_folders.end(); ++it) { | 228 found_folders.begin(); it != found_folders.end(); ++it) { |
222 base::FilePath parent_directory = it->first.DirName(); | 229 base::FilePath parent_directory = it->first.DirName(); |
| 230 |
| 231 // Skip sensitive folders and their ancestors. |
| 232 bool is_sensitive = false; |
| 233 base::FilePath abs_parent_directory = |
| 234 base::MakeAbsoluteFilePath(parent_directory); |
| 235 if (abs_parent_directory.empty()) |
| 236 continue; |
| 237 for (size_t i = 0; i < abs_sensitive_locations.size(); ++i) { |
| 238 if (abs_parent_directory == abs_sensitive_locations[i] || |
| 239 abs_parent_directory.IsParent(abs_sensitive_locations[i])) { |
| 240 is_sensitive = true; |
| 241 continue; |
| 242 } |
| 243 } |
| 244 if (is_sensitive) |
| 245 continue; |
| 246 |
223 ContainerCandidates::iterator existing = candidates.find(parent_directory); | 247 ContainerCandidates::iterator existing = candidates.find(parent_directory); |
224 if (existing == candidates.end()) { | 248 if (existing == candidates.end()) { |
225 candidates[parent_directory] = 1; | 249 candidates[parent_directory] = 1; |
226 } else { | 250 } else { |
227 existing->second++; | 251 existing->second++; |
228 } | 252 } |
229 } | 253 } |
230 | 254 |
231 // If a parent directory has more than one scan result, consider it. | 255 // If a parent directory has more than one scan result, consider it. |
232 MediaFolderFinder::MediaFolderFinderResults result; | 256 MediaFolderFinder::MediaFolderFinderResults result; |
(...skipping 12 matching lines...) Expand all Loading... |
245 name = dir_counter.Next()) { | 269 name = dir_counter.Next()) { |
246 if (!base::IsLink(name)) | 270 if (!base::IsLink(name)) |
247 count++; | 271 count++; |
248 } | 272 } |
249 if (it->second * 100 / count >= kContainerDirectoryMinimumPercent) | 273 if (it->second * 100 / count >= kContainerDirectoryMinimumPercent) |
250 result[it->first] = MediaGalleryScanResult(); | 274 result[it->first] = MediaGalleryScanResult(); |
251 } | 275 } |
252 return result; | 276 return result; |
253 } | 277 } |
254 | 278 |
255 void RemoveSensitiveLocations( | |
256 MediaFolderFinder::MediaFolderFinderResults* found_folders) { | |
257 // TODO(vandebo) Use the greylist from filesystem api. | |
258 } | |
259 | |
260 int CountScanResultsForExtension(MediaGalleriesPreferences* preferences, | 279 int CountScanResultsForExtension(MediaGalleriesPreferences* preferences, |
261 const extensions::Extension* extension, | 280 const extensions::Extension* extension, |
262 MediaGalleryScanResult* file_counts) { | 281 MediaGalleryScanResult* file_counts) { |
263 int gallery_count = 0; | 282 int gallery_count = 0; |
264 | 283 |
265 MediaGalleryPrefIdSet permitted_galleries = | 284 MediaGalleryPrefIdSet permitted_galleries = |
266 preferences->GalleriesForExtension(*extension); | 285 preferences->GalleriesForExtension(*extension); |
267 const MediaGalleriesPrefInfoMap& known_galleries = | 286 const MediaGalleriesPrefInfoMap& known_galleries = |
268 preferences->known_galleries(); | 287 preferences->known_galleries(); |
269 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); | 288 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 bool success, | 456 bool success, |
438 const MediaFolderFinder::MediaFolderFinderResults& found_folders) { | 457 const MediaFolderFinder::MediaFolderFinderResults& found_folders) { |
439 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 458 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
440 if (!folder_finder_ || !success) { | 459 if (!folder_finder_ || !success) { |
441 folder_finder_.reset(); | 460 folder_finder_.reset(); |
442 return; | 461 return; |
443 } | 462 } |
444 | 463 |
445 content::BrowserThread::PostTaskAndReplyWithResult( | 464 content::BrowserThread::PostTaskAndReplyWithResult( |
446 content::BrowserThread::FILE, FROM_HERE, | 465 content::BrowserThread::FILE, FROM_HERE, |
447 base::Bind(FindContainerScanResults, found_folders), | 466 base::Bind(FindContainerScanResults, |
| 467 found_folders, |
| 468 folder_finder_->graylisted_folders()), |
448 base::Bind(&MediaScanManager::OnFoundContainerDirectories, | 469 base::Bind(&MediaScanManager::OnFoundContainerDirectories, |
449 weak_factory_.GetWeakPtr(), found_folders)); | 470 weak_factory_.GetWeakPtr(), |
| 471 found_folders)); |
450 } | 472 } |
451 | 473 |
452 void MediaScanManager::OnFoundContainerDirectories( | 474 void MediaScanManager::OnFoundContainerDirectories( |
453 const MediaFolderFinder::MediaFolderFinderResults& found_folders, | 475 const MediaFolderFinder::MediaFolderFinderResults& found_folders, |
454 const MediaFolderFinder::MediaFolderFinderResults& container_folders) { | 476 const MediaFolderFinder::MediaFolderFinderResults& container_folders) { |
455 MediaFolderFinder::MediaFolderFinderResults folders; | 477 MediaFolderFinder::MediaFolderFinderResults folders; |
456 folders.insert(found_folders.begin(), found_folders.end()); | 478 folders.insert(found_folders.begin(), found_folders.end()); |
457 folders.insert(container_folders.begin(), container_folders.end()); | 479 folders.insert(container_folders.begin(), container_folders.end()); |
458 | 480 |
459 RemoveSensitiveLocations(&folders); | |
460 | |
461 for (ScanObserverMap::iterator scans_for_profile = observers_.begin(); | 481 for (ScanObserverMap::iterator scans_for_profile = observers_.begin(); |
462 scans_for_profile != observers_.end(); | 482 scans_for_profile != observers_.end(); |
463 ++scans_for_profile) { | 483 ++scans_for_profile) { |
464 if (scans_for_profile->second.scanning_extensions.empty()) | 484 if (scans_for_profile->second.scanning_extensions.empty()) |
465 continue; | 485 continue; |
466 Profile* profile = scans_for_profile->first; | 486 Profile* profile = scans_for_profile->first; |
467 MediaGalleriesPreferences* preferences = | 487 MediaGalleriesPreferences* preferences = |
468 MediaGalleriesPreferencesFactory::GetForProfile(profile); | 488 MediaGalleriesPreferencesFactory::GetForProfile(profile); |
469 ExtensionService* extension_service = | 489 ExtensionService* extension_service = |
470 extensions::ExtensionSystem::Get(profile)->extension_service(); | 490 extensions::ExtensionSystem::Get(profile)->extension_service(); |
(...skipping 18 matching lines...) Expand all Loading... |
489 gallery_count, | 509 gallery_count, |
490 file_counts); | 510 file_counts); |
491 } | 511 } |
492 } | 512 } |
493 scanning_extensions->clear(); | 513 scanning_extensions->clear(); |
494 preferences->SetLastScanCompletionTime(base::Time::Now()); | 514 preferences->SetLastScanCompletionTime(base::Time::Now()); |
495 } | 515 } |
496 registrar_.RemoveAll(); | 516 registrar_.RemoveAll(); |
497 folder_finder_.reset(); | 517 folder_finder_.reset(); |
498 } | 518 } |
OLD | NEW |