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