Add DeviceEventRouter class.
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / file_manager / device_event_router.h
blob175273324a479470ba54a64e319bd0ea9b799fa2
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.
5 #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_DEVICE_EVENT_ROUTER_H_
6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_DEVICE_EVENT_ROUTER_H_
8 #include <map>
9 #include <string>
11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/thread_checker.h"
13 #include "chrome/browser/chromeos/file_manager/volume_manager_observer.h"
14 #include "chrome/common/extensions/api/file_browser_private.h"
15 #include "chromeos/dbus/power_manager_client.h"
17 namespace file_manager {
19 enum DeviceState {
20 // Device is not being scanned and is not hard unplugged.
21 DEVICE_STATE_USUAL,
22 DEVICE_SCANNED,
23 DEVICE_SCANNED_AND_REPORTED,
24 DEVICE_HARD_UNPLUGGED,
25 DEVICE_HARD_UNPLUGGED_AND_REPORTED
28 // Event router for device events.
29 class DeviceEventRouter : public VolumeManagerObserver,
30 public chromeos::PowerManagerClient::Observer {
31 public:
32 DeviceEventRouter();
34 // |overriding_time_delta| overrides time delta of delayed tasks for testing
35 // |so that the tasks are executed by RunLoop::RunUntilIdle.
36 explicit DeviceEventRouter(base::TimeDelta overriding_time_delta);
38 virtual ~DeviceEventRouter();
40 // Turns the startup flag on, and then turns it off after few seconds.
41 void Startup();
43 // VolumeManagerObserver overrides.
44 virtual void OnDiskAdded(const chromeos::disks::DiskMountManager::Disk& disk,
45 bool mounting) OVERRIDE;
46 virtual void OnDiskRemoved(
47 const chromeos::disks::DiskMountManager::Disk& disk) OVERRIDE;
48 virtual void OnDeviceAdded(const std::string& device_path) OVERRIDE;
49 virtual void OnDeviceRemoved(const std::string& device_path) OVERRIDE;
50 virtual void OnVolumeMounted(chromeos::MountError error_code,
51 const VolumeInfo& volume_info,
52 bool is_remounting) OVERRIDE;
53 virtual void OnVolumeUnmounted(chromeos::MountError error_code,
54 const VolumeInfo& volume_info) OVERRIDE;
55 virtual void OnFormatStarted(const std::string& device_path,
56 bool success) OVERRIDE;
57 virtual void OnFormatCompleted(const std::string& device_path,
58 bool success) OVERRIDE;
60 // TODO(hirono): Remove the method from VolumeManagerObserver.
61 virtual void OnHardUnplugged(const std::string& device_path) OVERRIDE;
63 // PowerManagerClient::Observer overrides.
64 virtual void SuspendImminent() OVERRIDE;
65 virtual void SuspendDone(const base::TimeDelta& sleep_duration) OVERRIDE;
67 protected:
68 // Handles a device event containing |type| and |device_path|.
69 virtual void OnDeviceEvent(
70 extensions::api::file_browser_private::DeviceEventType type,
71 const std::string& device_path) = 0;
72 // Returns external storage is disabled or not.
73 virtual bool IsExternalStorageDisabled() = 0;
75 private:
76 void StartupDelayed();
77 void OnDeviceAddedDelayed(const std::string& device_path);
78 void SuspendDoneDelayed();
80 // Obtains device state of the device having |device_path|.
81 DeviceState GetDeviceState(const std::string& device_path) const;
83 // Sets device state to the device having |device_path|.
84 void SetDeviceState(const std::string& device_path, DeviceState state);
86 // Whther to use zero time delta for testing or not.
87 const base::TimeDelta resume_time_delta_;
88 const base::TimeDelta startup_time_delta_;
89 const base::TimeDelta scan_time_delta_;
91 // Whether the profile is starting up or not.
92 bool is_starting_up_;
94 // Whether the system is resuming or not.
95 bool is_resuming_;
97 // Map of device path and device state.
98 std::map<std::string, DeviceState> device_states_;
100 // Thread checker.
101 base::ThreadChecker thread_checker_;
103 // Note: This should remain the last member so it'll be destroyed and
104 // invalidate the weak pointers before any other members are destroyed.
105 base::WeakPtrFactory<DeviceEventRouter> weak_factory_;
106 DISALLOW_COPY_AND_ASSIGN(DeviceEventRouter);
108 } // namespace file_manager
110 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_DEVICE_EVENT_ROUTER_H_