OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/thread_task_runner_handle.h" |
| 7 #include "chrome/browser/chromeos/extensions/file_manager/device_event_router.h" |
| 8 #include "chrome/browser/chromeos/file_manager/volume_manager.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 |
| 11 namespace file_manager { |
| 12 namespace { |
| 13 namespace file_browser_private = extensions::api::file_browser_private; |
| 14 using content::BrowserThread; |
| 15 } // namespace |
| 16 |
| 17 DeviceEventRouter::DeviceEventRouter() |
| 18 : resume_time_delta_(base::TimeDelta::FromSeconds(5)), |
| 19 startup_time_delta_(base::TimeDelta::FromSeconds(10)), |
| 20 scan_time_delta_(base::TimeDelta::FromSeconds(5)), |
| 21 is_starting_up_(false), |
| 22 is_resuming_(false), |
| 23 weak_factory_(this) { |
| 24 } |
| 25 |
| 26 DeviceEventRouter::DeviceEventRouter(base::TimeDelta overriding_time_delta) |
| 27 : resume_time_delta_(overriding_time_delta), |
| 28 startup_time_delta_(overriding_time_delta), |
| 29 scan_time_delta_(overriding_time_delta), |
| 30 is_starting_up_(false), |
| 31 is_resuming_(false), |
| 32 weak_factory_(this) { |
| 33 } |
| 34 |
| 35 DeviceEventRouter::~DeviceEventRouter() { |
| 36 } |
| 37 |
| 38 void DeviceEventRouter::Startup() { |
| 39 is_starting_up_ = true; |
| 40 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 41 FROM_HERE, |
| 42 base::Bind(&DeviceEventRouter::StartupDelayed, |
| 43 weak_factory_.GetWeakPtr()), |
| 44 startup_time_delta_); |
| 45 } |
| 46 |
| 47 void DeviceEventRouter::StartupDelayed() { |
| 48 DCHECK(thread_checker_.CalledOnValidThread()); |
| 49 is_starting_up_ = false; |
| 50 } |
| 51 |
| 52 void DeviceEventRouter::OnDeviceAdded(const std::string& device_path) { |
| 53 DCHECK(thread_checker_.CalledOnValidThread()); |
| 54 |
| 55 if (is_starting_up_ || is_resuming_) { |
| 56 SetDeviceState(device_path, DEVICE_STATE_USUAL); |
| 57 return; |
| 58 } |
| 59 |
| 60 if (IsExternalStorageDisabled()) { |
| 61 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_DISABLED, |
| 62 device_path); |
| 63 SetDeviceState(device_path, DEVICE_STATE_USUAL); |
| 64 return; |
| 65 } |
| 66 |
| 67 SetDeviceState(device_path, DEVICE_SCANNED); |
| 68 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 69 FROM_HERE, |
| 70 base::Bind(&DeviceEventRouter::OnDeviceAddedDelayed, |
| 71 weak_factory_.GetWeakPtr(), |
| 72 device_path), |
| 73 scan_time_delta_); |
| 74 } |
| 75 |
| 76 void DeviceEventRouter::OnDeviceAddedDelayed(const std::string& device_path) { |
| 77 DCHECK(thread_checker_.CalledOnValidThread()); |
| 78 |
| 79 if (GetDeviceState(device_path) == DEVICE_SCANNED) { |
| 80 // TODO(hirono): Rename DEVICE_EVENT_TYPE_ADDED with |
| 81 // DEVICE_EVENT_TYPE_SCAN_STARTED. |
| 82 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_ADDED, device_path); |
| 83 SetDeviceState(device_path, DEVICE_SCANNED_AND_REPORTED); |
| 84 } |
| 85 } |
| 86 |
| 87 void DeviceEventRouter::OnDeviceRemoved(const std::string& device_path) { |
| 88 DCHECK(thread_checker_.CalledOnValidThread()); |
| 89 SetDeviceState(device_path, DEVICE_STATE_USUAL); |
| 90 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_REMOVED, device_path); |
| 91 } |
| 92 |
| 93 void DeviceEventRouter::OnDiskAdded( |
| 94 const chromeos::disks::DiskMountManager::Disk& disk, |
| 95 bool mounting) { |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); |
| 97 |
| 98 if (!mounting) { |
| 99 // If the disk is not being mounted, mark the device scan cancelled. |
| 100 const std::string& device_path = disk.system_path_prefix(); |
| 101 if (GetDeviceState(device_path) == DEVICE_SCANNED_AND_REPORTED) { |
| 102 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_SCAN_CANCELED, |
| 103 device_path); |
| 104 } |
| 105 SetDeviceState(device_path, DEVICE_STATE_USUAL); |
| 106 } |
| 107 } |
| 108 |
| 109 void DeviceEventRouter::OnDiskRemoved( |
| 110 const chromeos::disks::DiskMountManager::Disk& disk) { |
| 111 DCHECK(thread_checker_.CalledOnValidThread()); |
| 112 |
| 113 if (is_resuming_ || is_starting_up_) |
| 114 return; |
| 115 |
| 116 const std::string& device_path = disk.system_path_prefix(); |
| 117 if (!disk.mount_path().empty() && |
| 118 GetDeviceState(device_path) != DEVICE_HARD_UNPLUGGED_AND_REPORTED) { |
| 119 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_HARD_UNPLUGGED, |
| 120 device_path); |
| 121 SetDeviceState(device_path, DEVICE_HARD_UNPLUGGED_AND_REPORTED); |
| 122 } |
| 123 } |
| 124 |
| 125 void DeviceEventRouter::OnVolumeMounted(chromeos::MountError error_code, |
| 126 const VolumeInfo& volume_info, |
| 127 bool is_remounting) { |
| 128 DCHECK(thread_checker_.CalledOnValidThread()); |
| 129 |
| 130 const std::string& device_path = |
| 131 volume_info.system_path_prefix.AsUTF8Unsafe(); |
| 132 SetDeviceState(device_path, DEVICE_STATE_USUAL); |
| 133 } |
| 134 |
| 135 void DeviceEventRouter::OnVolumeUnmounted(chromeos::MountError error_code, |
| 136 const VolumeInfo& volume_info) { |
| 137 // Do nothing. |
| 138 } |
| 139 |
| 140 void DeviceEventRouter::OnFormatStarted(const std::string& device_path, |
| 141 bool success) { |
| 142 DCHECK(thread_checker_.CalledOnValidThread()); |
| 143 |
| 144 if (success) { |
| 145 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_FORMAT_START, |
| 146 device_path); |
| 147 } else { |
| 148 OnDeviceEvent(file_browser_private::DEVICE_EVENT_TYPE_FORMAT_FAIL, |
| 149 device_path); |
| 150 } |
| 151 } |
| 152 |
| 153 void DeviceEventRouter::OnFormatCompleted(const std::string& device_path, |
| 154 bool success) { |
| 155 DCHECK(thread_checker_.CalledOnValidThread()); |
| 156 |
| 157 OnDeviceEvent(success ? file_browser_private::DEVICE_EVENT_TYPE_FORMAT_SUCCESS |
| 158 : file_browser_private::DEVICE_EVENT_TYPE_FORMAT_FAIL, |
| 159 device_path); |
| 160 } |
| 161 |
| 162 void DeviceEventRouter::OnHardUnplugged(const std::string& device_path) { |
| 163 } |
| 164 |
| 165 void DeviceEventRouter::SuspendImminent() { |
| 166 DCHECK(thread_checker_.CalledOnValidThread()); |
| 167 is_resuming_ = true; |
| 168 } |
| 169 |
| 170 void DeviceEventRouter::SuspendDone(const base::TimeDelta& sleep_duration) { |
| 171 DCHECK(thread_checker_.CalledOnValidThread()); |
| 172 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 173 FROM_HERE, |
| 174 base::Bind(&DeviceEventRouter::SuspendDoneDelayed, |
| 175 weak_factory_.GetWeakPtr()), |
| 176 resume_time_delta_); |
| 177 } |
| 178 |
| 179 void DeviceEventRouter::SuspendDoneDelayed() { |
| 180 DCHECK(thread_checker_.CalledOnValidThread()); |
| 181 is_resuming_ = false; |
| 182 } |
| 183 |
| 184 DeviceState DeviceEventRouter::GetDeviceState( |
| 185 const std::string& device_path) const { |
| 186 const std::map<std::string, DeviceState>::const_iterator it = |
| 187 device_states_.find(device_path); |
| 188 return it != device_states_.end() ? it->second : DEVICE_STATE_USUAL; |
| 189 } |
| 190 |
| 191 void DeviceEventRouter::SetDeviceState(const std::string& device_path, |
| 192 DeviceState state) { |
| 193 if (state != DEVICE_STATE_USUAL) { |
| 194 device_states_[device_path] = state; |
| 195 } else { |
| 196 const std::map<std::string, DeviceState>::iterator it = |
| 197 device_states_.find(device_path); |
| 198 if (it != device_states_.end()) |
| 199 device_states_.erase(it); |
| 200 } |
| 201 } |
| 202 |
| 203 } // namespace file_manager |
OLD | NEW |