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#ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_MTP_DEVICE_MAP_SERVICE_H_
6#define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_MTP_DEVICE_MAP_SERVICE_H_
7
8#include <map>
9
10#include "base/files/file_path.h"
11#include "base/lazy_instance.h"
12#include "base/threading/thread_checker.h"
13
14class MTPDeviceAsyncDelegate;
15
16// This class provides media transfer protocol (MTP) device delegate to
17// complete media file system operations.
18// Lives on the IO thread in production.
19// TODO(gbillock): Make this class owned by the MediaFileSystemRegistry.
20class MTPDeviceMapService {
21 public:
22  static MTPDeviceMapService* GetInstance();
23
24  // Gets the media device delegate associated with |filesystem_id|.
25  // Return NULL if the |filesystem_id| is no longer valid (e.g. because the
26  // corresponding device is detached, etc).
27  // Called on the IO thread.
28  MTPDeviceAsyncDelegate* GetMTPDeviceAsyncDelegate(
29      const std::string& filesystem_id);
30
31  // Register that an MTP filesystem is in use for the given |device_location|.
32  void RegisterMTPFileSystem(
33    const base::FilePath::StringType& device_location,
34    const std::string& fsid);
35
36  // Removes the MTP entry associated with the given
37  // |device_location|. Signals the MTPDeviceMapService to destroy the
38  // delegate if there are no more uses of it.
39  void RevokeMTPFileSystem(const std::string& fsid);
40
41 private:
42  friend struct base::DefaultLazyInstanceTraits<MTPDeviceMapService>;
43
44  // Adds the MTP device delegate to the map service. |device_location|
45  // specifies the mount location of the MTP device.
46  // Called on the IO thread.
47  void AddAsyncDelegate(const base::FilePath::StringType& device_location,
48                        MTPDeviceAsyncDelegate* delegate);
49
50  // Removes the MTP device delegate from the map service. |device_location|
51  // specifies the mount location of the MTP device.
52  // Called on the IO thread.
53  void RemoveAsyncDelegate(const base::FilePath::StringType& device_location);
54
55  // Mapping of device_location and MTPDeviceAsyncDelegate* object. It is safe
56  // to store and access the raw pointer. This class operates on the IO thread.
57  typedef std::map<base::FilePath::StringType, MTPDeviceAsyncDelegate*>
58      AsyncDelegateMap;
59
60  // Map a filesystem id (fsid) to an MTP device location.
61  typedef std::map<std::string, base::FilePath::StringType>
62      MTPDeviceFileSystemMap;
63
64  // Map a MTP or PTP device location to a count of current uses of that
65  // location.
66  typedef std::map<const base::FilePath::StringType, int>
67      MTPDeviceUsageMap;
68
69
70  // Get access to this class using GetInstance() method.
71  MTPDeviceMapService();
72  ~MTPDeviceMapService();
73
74  // Map of attached mtp device async delegates.
75  AsyncDelegateMap async_delegate_map_;
76
77  MTPDeviceFileSystemMap mtp_device_map_;
78
79  MTPDeviceUsageMap mtp_device_usage_map_;
80
81  DISALLOW_COPY_AND_ASSIGN(MTPDeviceMapService);
82};
83
84#endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_MTP_DEVICE_MAP_SERVICE_H_
85