1// Copyright (c) 2012 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// Manages all the gallery file watchers for the associated profile. This class
6// lives on the file thread. This class is instantiated per profile. This
7// is temporary and will be moved to a permanent, public place in the near
8// future. Please refer to crbug.com/166950 for more details.
9
10#ifndef CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_GALLERY_WATCH_MANAGER_H_
11#define CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_GALLERY_WATCH_MANAGER_H_
12
13#include <map>
14#include <string>
15
16#include "base/files/file_path.h"
17#include "base/memory/weak_ptr.h"
18#include "chrome/browser/media_galleries/media_galleries_preferences.h"
19
20namespace extensions {
21
22class MediaGalleriesPrivateEventRouter;
23
24class GalleryWatchManager {
25 public:
26  // Returns the GalleryWatchManager for |profile_id|, creating it if it is not
27  // yet created.
28  static GalleryWatchManager* GetForProfile(void* profile_id);
29
30  // Returns true if an GalleryWatchManager already exists for the specified
31  // |profile_id|.
32  static bool HasForProfile(void* profile_id);
33
34  // Notifies about the profile shutdown event.
35  static void OnProfileShutdown(void* profile_id);
36
37  // Sets up a gallery watch for the extension specified by the |extension_id|.
38  // |profile_id| specifies the extension profile identifier.
39  // |gallery_id| specifies the gallery identifier.
40  // |watch_path| specifies the absolute gallery path.
41  // Returns true, if the watch setup operation was successful.
42  static bool SetupGalleryWatch(
43      void* profile_id,
44      MediaGalleryPrefId gallery_id,
45      const base::FilePath& watch_path,
46      const std::string& extension_id,
47      base::WeakPtr<MediaGalleriesPrivateEventRouter> event_router);
48
49  // Cancels the gallery watch for the extension specified by the
50  // |extension_id|. |profile_id| specifies the extension profile identifier.
51  // |watch_path| specifies the absolute gallery path.
52  static void RemoveGalleryWatch(void* profile_id,
53                                 const base::FilePath& watch_path,
54                                 const std::string& extension_id);
55
56  // Notifies about the extension unloaded/uninstalled event.
57  static void OnExtensionUnloaded(void* profile_id,
58                                  const std::string& extension_id);
59
60 private:
61  class GalleryFilePathWatcher;
62  typedef std::map<base::FilePath, GalleryFilePathWatcher*> WatcherMap;
63
64  // Use GetForProfile().
65  GalleryWatchManager();
66  ~GalleryWatchManager();
67
68  // Initiates a gallery watch operation for the extension specified by
69  // the |extension_id|. |gallery_id| specifies the gallery identifier and
70  // |watch_path| specifies the absolute path of the gallery. Returns true,
71  // if the watch was set successfully.
72  bool StartGalleryWatch(
73      MediaGalleryPrefId gallery_id,
74      const base::FilePath& watch_path,
75      const std::string& extension_id,
76      base::WeakPtr<MediaGalleriesPrivateEventRouter> event_router);
77
78  // Cancels the gallery watch operation for the extension specified by the
79  // |extension_id|. |watch_path| specifies the absolute path of the gallery.
80  void StopGalleryWatch(const base::FilePath& watch_path,
81                        const std::string& extension_id);
82
83  // Handles the extension unloaded/uninstalled event.
84  void HandleExtensionUnloadedEvent(const std::string& extension_id);
85
86  // Deletes the gallery watchers.
87  void DeleteAllWatchers();
88
89  // Removes the GalleryFilePathWatcher entry associated with the given
90  // |watch_path|.
91  void RemoveGalleryFilePathWatcherEntry(const base::FilePath& watch_path);
92
93  // Map to manage the gallery file path watchers.
94  // Key: Gallery watch path.
95  // Value: GalleryFilePathWatcher*.
96  WatcherMap gallery_watchers_;
97
98  DISALLOW_COPY_AND_ASSIGN(GalleryWatchManager);
99};
100
101}  // namespace extensions
102
103#endif  // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_PRIVATE_GALLERY_WATCH_MANAGER_H_
104