Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media_galleries / media_scan_manager.h
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_SCAN_MANAGER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_SCAN_MANAGER_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "chrome/browser/media_galleries/media_folder_finder.h"
17 #include "chrome/browser/media_galleries/media_scan_types.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20
21 class Profile;
22 class MediaScanManagerObserver;
23
24 namespace extensions {
25 class Extension;
26 }  // namespace extensions
27
28 // The MediaScanManager is owned by MediaFileSystemRegistry, which is global.
29 // This class manages multiple 'virtual' media scans, up to one per extension
30 // per profile, and also manages the one physical scan backing them.
31 // This class lives and is called on the UI thread.
32 class MediaScanManager : public content::NotificationObserver {
33  public:
34   MediaScanManager();
35   virtual ~MediaScanManager();
36
37   // There can only be ever one observer registered per profile. Does not take
38   // ownership of |observer|. An observer must be registered before scanning.
39   void AddObserver(Profile* profile, MediaScanManagerObserver* observer);
40   void RemoveObserver(Profile* profile);
41
42   // Must be called when |profile| is shut down.
43   void CancelScansForProfile(Profile* profile);
44
45   // The results of the scan are reported to the registered
46   // MediaScanManagerObserver via OnScanFinished. There must be an observer
47   // registered for |profile| before the scan starts.
48   void StartScan(Profile* profile, const extensions::Extension* extension,
49                  bool user_gesture);
50   void CancelScan(Profile* profile, const extensions::Extension* extension);
51
52  protected:
53   friend class MediaGalleriesPlatformAppBrowserTest;
54
55   typedef base::Callback<MediaFolderFinder*(
56       const MediaFolderFinder::MediaFolderFinderResultsCallback&)>
57           MediaFolderFinderFactory;
58
59   void SetMediaFolderFinderFactory(const MediaFolderFinderFactory& factory);
60
61  private:
62   struct ScanObservers {
63     ScanObservers();
64     ~ScanObservers();
65     MediaScanManagerObserver* observer;
66     std::set<std::string /*extension id*/> scanning_extensions;
67   };
68   typedef std::map<Profile*, ScanObservers> ScanObserverMap;
69
70   // content::NotificationObserver implementation.
71   virtual void Observe(int type,
72                        const content::NotificationSource& source,
73                        const content::NotificationDetails& details) OVERRIDE;
74
75   bool ScanInProgress() const;
76
77   void OnScanCompleted(
78       bool success,
79       const MediaFolderFinder::MediaFolderFinderResults& found_folders);
80
81   void OnFoundContainerDirectories(
82       const MediaFolderFinder::MediaFolderFinderResults& found_folders,
83       const MediaFolderFinder::MediaFolderFinderResults& container_folders);
84
85   scoped_ptr<MediaFolderFinder> folder_finder_;
86
87   // If not NULL, used to create |folder_finder_|. Used for testing.
88   MediaFolderFinderFactory testing_folder_finder_factory_;
89
90   // Set of extensions (on all profiles) that have an in-progress scan.
91   ScanObserverMap observers_;
92
93   // Used to listen for NOTIFICATION_EXTENSION_UNLOADED events.
94   content::NotificationRegistrar registrar_;
95
96   base::WeakPtrFactory<MediaScanManager> weak_factory_;
97
98   DISALLOW_COPY_AND_ASSIGN(MediaScanManager);
99 };
100
101 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_SCAN_MANAGER_H_