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 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FOLDER_FINDER_H_ 6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FOLDER_FINDER_H_ 7 8 #include <map> 9 #include <vector> 10 11 #include "base/callback.h" 12 #include "base/files/file_path.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/sequenced_task_runner.h" 16 #include "chrome/browser/media_galleries/media_scan_types.h" 17 18 // MediaFolderFinder scans local hard drives and look for folders that contain 19 // media files. 20 class MediaFolderFinder { 21 public: 22 // Key: path to a folder 23 // Value: scan results for that folder, non-recursive. 24 typedef std::map<base::FilePath, MediaGalleryScanResult> 25 MediaFolderFinderResults; 26 27 // |results| never contains entries for |graylisted_folders_| or parent 28 // directories of |graylisted_folders_|. 29 typedef base::Callback<void(bool /*success*/, 30 const MediaFolderFinderResults& /*results*/)> 31 MediaFolderFinderResultsCallback; 32 33 // |callback| will get called when the scan finishes. If the object is deleted 34 // before it finishes, the scan will stop and |callback| will get called with 35 // success = false. 36 // MediaFolderFinder has a default set of per-platform paths to scan. 37 // Override in tests with SetRootsForTesting(). 38 explicit MediaFolderFinder(const MediaFolderFinderResultsCallback& callback); 39 virtual ~MediaFolderFinder(); 40 41 // Start the scan. 42 virtual void StartScan(); 43 44 const std::vector<base::FilePath>& graylisted_folders() const; 45 46 private: 47 friend class MediaFolderFinderTest; 48 friend class MediaGalleriesPlatformAppBrowserTest; 49 50 class Worker; 51 struct WorkerReply { 52 WorkerReply(); 53 ~WorkerReply(); 54 55 MediaGalleryScanResult scan_result; 56 std::vector<base::FilePath> new_folders; 57 }; 58 59 enum ScanState { 60 SCAN_STATE_NOT_STARTED, 61 SCAN_STATE_STARTED, 62 SCAN_STATE_FINISHED, 63 }; 64 65 void SetRootsForTesting(const std::vector<base::FilePath>& roots); 66 67 void OnInitialized(const std::vector<base::FilePath>& roots); 68 69 // Scan a folder from |folders_to_scan_|. 70 void ScanFolder(); 71 72 // Callback that handles the |reply| from |worker_| for a scanned |path|. 73 void GotScanResults(const base::FilePath& path, const WorkerReply& reply); 74 75 const MediaFolderFinderResultsCallback results_callback_; 76 MediaFolderFinderResults results_; 77 78 std::vector<base::FilePath> graylisted_folders_; 79 std::vector<base::FilePath> folders_to_scan_; 80 ScanState scan_state_; 81 82 scoped_refptr<base::SequencedTaskRunner> worker_task_runner_; 83 84 // Owned by MediaFolderFinder, but lives on |worker_task_runner_|. 85 Worker* worker_; 86 87 // Set of roots to scan for testing. 88 bool has_roots_for_testing_; 89 std::vector<base::FilePath> roots_for_testing_; 90 91 base::WeakPtrFactory<MediaFolderFinder> weak_factory_; 92 93 DISALLOW_COPY_AND_ASSIGN(MediaFolderFinder); 94 }; 95 96 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FOLDER_FINDER_H_ 97