1// Copyright 2013 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_WIN_PORTABLE_DEVICE_MAP_SERVICE_H_
6#define CHROME_BROWSER_MEDIA_GALLERIES_WIN_PORTABLE_DEVICE_MAP_SERVICE_H_
7
8#include <portabledeviceapi.h>
9#include <map>
10
11#include "base/lazy_instance.h"
12#include "base/strings/string16.h"
13#include "base/synchronization/lock.h"
14#include "base/win/scoped_comptr.h"
15
16// PortableDeviceMapService keeps track of initialized portable device
17// interfaces. PortableDeviceMapService owns the portable device interfaces.
18class PortableDeviceMapService {
19 public:
20  static PortableDeviceMapService* GetInstance();
21
22  // Adds the portable |device| interface to the map service for the device
23  // specified by the |device_location|. Called on a blocking pool thread.
24  void AddPortableDevice(const base::string16& device_location,
25                         IPortableDevice* device);
26
27  // Marks the IPortableDevice interface of the device specified by the
28  // |device_location| for deletion. This helps to cancel all the pending
29  // tasks before deleting the IPortableDevice interface.
30  //
31  // Callers of this function should post a task on a blocking pool thread to
32  // remove the IPortableDevice interface from the map service.
33  //
34  // Called on the IO thread.
35  void MarkPortableDeviceForDeletion(const base::string16& device_location);
36
37  // Removes the IPortableDevice interface from the map service for the device
38  // specified by the |device_location|. Callers of this function should have
39  // already called MarkPortableDeviceForDeletion() on the IO thread.
40  // Called on a blocking pool thread.
41  void RemovePortableDevice(const base::string16& device_location);
42
43  // Gets the IPortableDevice interface associated with the device specified
44  // by the |device_location|. Returns NULL if the |device_location| is no
45  // longer valid (e.g. the corresponding device is detached etc).
46  // Called on a blocking pool thread.
47  IPortableDevice* GetPortableDevice(const base::string16& device_location);
48
49 private:
50  friend struct base::DefaultLazyInstanceTraits<PortableDeviceMapService>;
51
52  struct PortableDeviceInfo {
53    PortableDeviceInfo();  // Necessary for STL.
54    explicit PortableDeviceInfo(IPortableDevice* device);
55
56    // The portable device interface.
57    base::win::ScopedComPtr<IPortableDevice> portable_device;
58
59    // Set to true if the |portable_device| is marked for deletion.
60    bool scheduled_to_delete;
61  };
62
63  typedef std::map<const base::string16, PortableDeviceInfo> PortableDeviceMap;
64
65  // Get access to this class using GetInstance() method.
66  PortableDeviceMapService();
67  ~PortableDeviceMapService();
68
69  // Mapping of |device_location| and IPortableDevice* object.
70  PortableDeviceMap device_map_;
71  base::Lock lock_;
72
73  DISALLOW_COPY_AND_ASSIGN(PortableDeviceMapService);
74};
75
76#endif  // CHROME_BROWSER_MEDIA_GALLERIES_WIN_PORTABLE_DEVICE_MAP_SERVICE_H_
77