Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / storage_monitor / volume_mount_watcher_win.h
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_STORAGE_MONITOR_VOLUME_MOUNT_WATCHER_WIN_H_
6 #define CHROME_BROWSER_STORAGE_MONITOR_VOLUME_MOUNT_WATCHER_WIN_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
12
13 #include "base/basictypes.h"
14 #include "base/callback.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/sequenced_task_runner.h"
18 #include "base/strings/string16.h"
19 #include "base/threading/sequenced_worker_pool.h"
20 #include "chrome/browser/storage_monitor/storage_info.h"
21 #include "chrome/browser/storage_monitor/storage_monitor.h"
22
23 class TestVolumeMountWatcherWin;
24
25 // This class watches the volume mount points and sends notifications to
26 // StorageMonitor about the device attach/detach events.
27 // This is a singleton class instantiated by StorageMonitorWin.
28 class VolumeMountWatcherWin {
29  public:
30   VolumeMountWatcherWin();
31   virtual ~VolumeMountWatcherWin();
32
33   // Returns the volume file path of the drive specified by the |drive_number|.
34   // |drive_number| inputs of 0 - 25 are valid. Returns an empty file path if
35   // the |drive_number| is invalid.
36   static base::FilePath DriveNumberToFilePath(int drive_number);
37
38   void Init();
39
40   // Gets the information about the device mounted at |device_path|. On success,
41   // returns true and fills in |info|.
42   // Can block during startup while device info is still loading.
43   bool GetDeviceInfo(const base::FilePath& device_path,
44                      StorageInfo* info) const;
45
46   // Processes DEV_BROADCAST_VOLUME messages and triggers a
47   // notification if appropriate.
48   void OnWindowMessage(UINT event_type, LPARAM data);
49
50   // Set the volume notifications object to be used when new
51   // removable volumes are found.
52   void SetNotifications(StorageMonitor::Receiver* notifications);
53
54   void EjectDevice(const std::string& device_id,
55                    base::Callback<void(StorageMonitor::EjectStatus)> callback);
56
57  protected:
58   typedef base::Callback<bool(const base::FilePath&,
59                               StorageInfo*)> GetDeviceDetailsCallbackType;
60
61   typedef base::Callback<std::vector<base::FilePath>(void)>
62       GetAttachedDevicesCallbackType;
63
64   // Handles mass storage device attach event on UI thread.
65   void HandleDeviceAttachEventOnUIThread(
66       const base::FilePath& device_path,
67       const StorageInfo& info);
68
69   // Handles mass storage device detach event on UI thread.
70   void HandleDeviceDetachEventOnUIThread(const base::string16& device_location);
71
72   // UI thread delegate to set up adding storage devices.
73   void AddDevicesOnUIThread(std::vector<base::FilePath> removable_devices);
74
75   // Runs |get_device_details_callback| for |device_path| on a worker thread.
76   // |volume_watcher| points back to the VolumeMountWatcherWin that called it.
77   static void RetrieveInfoForDeviceAndAdd(
78       const base::FilePath& device_path,
79       const GetDeviceDetailsCallbackType& get_device_details_callback,
80       base::WeakPtr<VolumeMountWatcherWin> volume_watcher);
81
82   // Mark that a device we started a metadata check for has completed.
83   virtual void DeviceCheckComplete(const base::FilePath& device_path);
84
85   virtual GetAttachedDevicesCallbackType GetAttachedDevicesCallback() const;
86   virtual GetDeviceDetailsCallbackType GetDeviceDetailsCallback() const;
87
88   // Worker pool used to collect device information. Used because some
89   // devices freeze workers trying to get device info, resulting in
90   // shutdown hangs.
91   scoped_refptr<base::SequencedWorkerPool> device_info_worker_pool_;
92   scoped_refptr<base::SequencedTaskRunner> task_runner_;
93
94  private:
95   friend class TestVolumeMountWatcherWin;
96
97   // Key: Mass storage device mount point.
98   // Value: Mass storage device metadata.
99   typedef std::map<base::FilePath, StorageInfo> MountPointDeviceMetadataMap;
100
101   // Maintain a set of device attribute check calls in-flight. Only accessed
102   // on the UI thread. This is to try and prevent the same device from
103   // occupying our worker pool in case of windows API call hangs.
104   std::set<base::FilePath> pending_device_checks_;
105
106   // A map from device mount point to device metadata. Only accessed on the UI
107   // thread.
108   MountPointDeviceMetadataMap device_metadata_;
109
110   base::WeakPtrFactory<VolumeMountWatcherWin> weak_factory_;
111
112   // The notifications object to use to signal newly attached volumes. Only
113   // removable devices will be notified.
114   StorageMonitor::Receiver* notifications_;
115
116   DISALLOW_COPY_AND_ASSIGN(VolumeMountWatcherWin);
117 };
118
119 #endif  // CHROME_BROWSER_STORAGE_MONITOR_VOLUME_MOUNT_WATCHER_WIN_H_