Chromium Code Reviews| 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 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FOLDER_FINDER_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FOLDER_FINDER_H_ |
| 6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FOLDER_FINDER_H_ | 6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FOLDER_FINDER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <utility> | |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/callback.h" | 12 #include "base/callback.h" |
| 12 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 14 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/sequenced_task_runner.h" |
| 15 #include "chrome/browser/media_galleries/media_scan_types.h" | 17 #include "chrome/browser/media_galleries/media_scan_types.h" |
| 16 | 18 |
| 17 // MediaFolderFinder scans local hard drives and look for folders that contain | 19 // MediaFolderFinder scans local hard drives and look for folders that contain |
| 18 // media files. | 20 // media files. |
| 19 class MediaFolderFinder { | 21 class MediaFolderFinder { |
| 20 public: | 22 public: |
| 21 typedef std::map<base::FilePath, MediaGalleryScanResult> | 23 typedef std::map<base::FilePath, MediaGalleryScanResult> |
| 22 MediaFolderFinderResults; | 24 MediaFolderFinderResults; |
| 23 typedef base::Callback<void(bool /*success*/, | 25 typedef base::Callback<void(bool /*success*/, |
| 24 const MediaFolderFinderResults& /*results*/)> | 26 const MediaFolderFinderResults& /*results*/)> |
| 25 MediaFolderFinderResultsCallback; | 27 MediaFolderFinderResultsCallback; |
| 26 typedef base::Callback<MediaGalleryScanFileType(const base::FilePath&)> | |
| 27 FilterCallback; | |
| 28 | 28 |
| 29 // |callback| will get called when the scan finishes. If the object is deleted | 29 // |callback| will get called when the scan finishes. If the object is deleted |
| 30 // before it finishes, the scan will stop and |callback| will get called with | 30 // before it finishes, the scan will stop and |callback| will get called with |
| 31 // success = false. | 31 // success = false. |
| 32 // MediaFolderFinder has a default set of per-platform paths to scan. | 32 // MediaFolderFinder has a default set of per-platform paths to scan. |
| 33 // Override in tests with SetRootsForTesting(). | 33 // Override in tests with SetRootsForTesting(). |
| 34 explicit MediaFolderFinder(const MediaFolderFinderResultsCallback& callback); | 34 explicit MediaFolderFinder(const MediaFolderFinderResultsCallback& callback); |
| 35 virtual ~MediaFolderFinder(); | 35 virtual ~MediaFolderFinder(); |
| 36 | 36 |
| 37 // Start the scan. | 37 // Start the scan. |
| 38 virtual void StartScan(); | 38 virtual void StartScan(); |
| 39 | 39 |
| 40 private: | 40 private: |
| 41 friend class MediaFolderFinderTest; | 41 friend class MediaFolderFinderTest; |
| 42 | 42 |
| 43 class Worker; | |
| 44 typedef std::pair<MediaGalleryScanResult, std::vector<base::FilePath> > | |
|
vandebo (ex-Chrome)
2014/02/14 17:11:51
Please use a struct instead of a pair.
Lei Zhang
2014/02/14 20:59:23
Done.
| |
| 45 WorkerReply; | |
| 46 | |
| 43 enum ScanState { | 47 enum ScanState { |
| 44 SCAN_STATE_NOT_STARTED, | 48 SCAN_STATE_NOT_STARTED, |
| 45 SCAN_STATE_STARTED, | 49 SCAN_STATE_STARTED, |
| 46 SCAN_STATE_FINISHED, | 50 SCAN_STATE_FINISHED, |
| 47 }; | 51 }; |
| 48 | 52 |
| 49 void SetRootsForTesting(const std::vector<base::FilePath>& roots); | 53 void SetRootsForTesting(const std::vector<base::FilePath>& roots); |
| 50 | 54 |
| 51 void OnInitialized(const std::vector<base::FilePath>& roots); | 55 void OnInitialized(const std::vector<base::FilePath>& roots); |
| 52 | 56 |
| 53 // Scan a folder from |folders_to_scan_|. | 57 // Scan a folder from |folders_to_scan_|. |
| 54 void ScanFolder(); | 58 void ScanFolder(); |
| 55 | 59 |
| 56 // Callback that returns the |scan_result| for |path| and the |new_folders| | 60 // Callback that handles the |reply| from |worker_| for a scanned |path|. |
| 57 // to scan in future calls to ScanFolder(). | 61 void GotScanResults(const base::FilePath& path, const WorkerReply& reply); |
| 58 void GotScanResults(const base::FilePath& path, | |
| 59 const MediaGalleryScanResult* scan_result, | |
| 60 const std::vector<base::FilePath>* new_folders); | |
| 61 | 62 |
| 62 const MediaFolderFinderResultsCallback results_callback_; | 63 const MediaFolderFinderResultsCallback results_callback_; |
| 63 MediaFolderFinderResults results_; | 64 MediaFolderFinderResults results_; |
| 64 | 65 |
| 65 std::vector<base::FilePath> folders_to_scan_; | 66 std::vector<base::FilePath> folders_to_scan_; |
| 66 ScanState scan_state_; | 67 ScanState scan_state_; |
| 67 | 68 |
| 68 // Token to make sure all calls with |filter_callback_| are on the same | 69 scoped_refptr<base::SequencedTaskRunner> worker_task_runner_; |
| 69 // sequence. | |
| 70 base::SequencedWorkerPool::SequenceToken token_; | |
| 71 | 70 |
| 72 // Callback used to filter through files and make sure they are media files. | 71 // Owned by MediaFolderFinder, but lives on |worker_task_runner_|. |
| 73 FilterCallback filter_callback_; | 72 Worker* worker_; |
| 74 | 73 |
| 75 // Set of roots to scan for testing. | 74 // Set of roots to scan for testing. |
| 76 bool has_roots_for_testing_; | 75 bool has_roots_for_testing_; |
| 77 std::vector<base::FilePath> roots_for_testing_; | 76 std::vector<base::FilePath> roots_for_testing_; |
| 78 | 77 |
| 79 base::WeakPtrFactory<MediaFolderFinder> weak_factory_; | 78 base::WeakPtrFactory<MediaFolderFinder> weak_factory_; |
| 80 | 79 |
| 81 DISALLOW_COPY_AND_ASSIGN(MediaFolderFinder); | 80 DISALLOW_COPY_AND_ASSIGN(MediaFolderFinder); |
| 82 }; | 81 }; |
| 83 | 82 |
| 84 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FOLDER_FINDER_H_ | 83 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_FOLDER_FINDER_H_ |
| OLD | NEW |