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_
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_manager_private.h"
15 #include "chromeos/dbus/power_manager_client.h"
17 namespace file_manager
{
20 // Device is not being hard unplugged.
22 // Device is hard unplugged.
23 DEVICE_HARD_UNPLUGGED
,
24 // Device is hard unplugged and reported to the JavaScript side.
25 DEVICE_HARD_UNPLUGGED_AND_REPORTED
28 // Event router for device events.
29 class DeviceEventRouter
: public VolumeManagerObserver
,
30 public chromeos::PowerManagerClient::Observer
{
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 ~DeviceEventRouter() override
;
40 // Turns the startup flag on, and then turns it off after few seconds.
43 // VolumeManagerObserver overrides.
44 void OnDiskAdded(const chromeos::disks::DiskMountManager::Disk
& disk
,
45 bool mounting
) override
;
47 const chromeos::disks::DiskMountManager::Disk
& disk
) override
;
48 void OnDeviceAdded(const std::string
& device_path
) override
;
49 void OnDeviceRemoved(const std::string
& device_path
) override
;
50 void OnVolumeMounted(chromeos::MountError error_code
,
51 const Volume
& volume
) override
;
52 void OnVolumeUnmounted(chromeos::MountError error_code
,
53 const Volume
& volume
) override
;
54 void OnFormatStarted(const std::string
& device_path
, bool success
) override
;
55 void OnFormatCompleted(const std::string
& device_path
, bool success
) override
;
57 // PowerManagerClient::Observer overrides.
58 void SuspendImminent() override
;
59 void SuspendDone(const base::TimeDelta
& sleep_duration
) override
;
61 bool is_resuming() const { return is_resuming_
; }
62 bool is_starting_up() const { return is_starting_up_
; }
65 // Handles a device event containing |type| and |device_path|.
66 virtual void OnDeviceEvent(
67 extensions::api::file_manager_private::DeviceEventType type
,
68 const std::string
& device_path
) = 0;
69 // Returns external storage is disabled or not.
70 virtual bool IsExternalStorageDisabled() = 0;
73 void StartupDelayed();
74 void OnDeviceAddedDelayed(const std::string
& device_path
);
75 void SuspendDoneDelayed();
77 // Obtains device state of the device having |device_path|.
78 DeviceState
GetDeviceState(const std::string
& device_path
) const;
80 // Sets device state to the device having |device_path|.
81 void SetDeviceState(const std::string
& device_path
, DeviceState state
);
83 // Whther to use zero time delta for testing or not.
84 const base::TimeDelta resume_time_delta_
;
85 const base::TimeDelta startup_time_delta_
;
87 // Whether the profile is starting up or not.
90 // Whether the system is resuming or not.
93 // Map of device path and device state.
94 std::map
<std::string
, DeviceState
> device_states_
;
97 base::ThreadChecker thread_checker_
;
99 // Note: This should remain the last member so it'll be destroyed and
100 // invalidate the weak pointers before any other members are destroyed.
101 base::WeakPtrFactory
<DeviceEventRouter
> weak_factory_
;
102 DISALLOW_COPY_AND_ASSIGN(DeviceEventRouter
);
104 } // namespace file_manager
106 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_DEVICE_EVENT_ROUTER_H_