| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/thread_task_runner_handle.h" | 6 #include "base/thread_task_runner_handle.h" |
| 7 #include "chrome/browser/chromeos/extensions/file_manager/device_event_router.h" | 7 #include "chrome/browser/chromeos/extensions/file_manager/device_event_router.h" |
| 8 #include "chrome/browser/chromeos/file_manager/volume_manager.h" | 8 #include "chrome/browser/chromeos/file_manager/volume_manager.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 | 10 |
| 11 namespace file_manager { | 11 namespace file_manager { |
| 12 namespace { | 12 namespace { |
| 13 namespace file_manager_private = extensions::api::file_manager_private; | 13 namespace file_manager_private = extensions::api::file_manager_private; |
| 14 using content::BrowserThread; | 14 using content::BrowserThread; |
| 15 } // namespace | 15 } // namespace |
| 16 | 16 |
| 17 DeviceEventRouter::DeviceEventRouter() | 17 DeviceEventRouter::DeviceEventRouter() |
| 18 : resume_time_delta_(base::TimeDelta::FromSeconds(5)), | 18 : resume_time_delta_(base::TimeDelta::FromSeconds(5)), |
| 19 startup_time_delta_(base::TimeDelta::FromSeconds(10)), | 19 startup_time_delta_(base::TimeDelta::FromSeconds(10)), |
| 20 scan_time_delta_(base::TimeDelta::FromSeconds(5)), | |
| 21 is_starting_up_(false), | 20 is_starting_up_(false), |
| 22 is_resuming_(false), | 21 is_resuming_(false), |
| 23 weak_factory_(this) { | 22 weak_factory_(this) { |
| 24 } | 23 } |
| 25 | 24 |
| 26 DeviceEventRouter::DeviceEventRouter(base::TimeDelta overriding_time_delta) | 25 DeviceEventRouter::DeviceEventRouter(base::TimeDelta overriding_time_delta) |
| 27 : resume_time_delta_(overriding_time_delta), | 26 : resume_time_delta_(overriding_time_delta), |
| 28 startup_time_delta_(overriding_time_delta), | 27 startup_time_delta_(overriding_time_delta), |
| 29 scan_time_delta_(overriding_time_delta), | |
| 30 is_starting_up_(false), | 28 is_starting_up_(false), |
| 31 is_resuming_(false), | 29 is_resuming_(false), |
| 32 weak_factory_(this) { | 30 weak_factory_(this) { |
| 33 } | 31 } |
| 34 | 32 |
| 35 DeviceEventRouter::~DeviceEventRouter() { | 33 DeviceEventRouter::~DeviceEventRouter() { |
| 36 } | 34 } |
| 37 | 35 |
| 38 void DeviceEventRouter::Startup() { | 36 void DeviceEventRouter::Startup() { |
| 39 is_starting_up_ = true; | 37 is_starting_up_ = true; |
| 40 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 38 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 41 FROM_HERE, | 39 FROM_HERE, |
| 42 base::Bind(&DeviceEventRouter::StartupDelayed, | 40 base::Bind(&DeviceEventRouter::StartupDelayed, |
| 43 weak_factory_.GetWeakPtr()), | 41 weak_factory_.GetWeakPtr()), |
| 44 startup_time_delta_); | 42 startup_time_delta_); |
| 45 } | 43 } |
| 46 | 44 |
| 47 void DeviceEventRouter::StartupDelayed() { | 45 void DeviceEventRouter::StartupDelayed() { |
| 48 DCHECK(thread_checker_.CalledOnValidThread()); | 46 DCHECK(thread_checker_.CalledOnValidThread()); |
| 49 is_starting_up_ = false; | 47 is_starting_up_ = false; |
| 50 } | 48 } |
| 51 | 49 |
| 52 void DeviceEventRouter::OnDeviceAdded(const std::string& device_path) { | 50 void DeviceEventRouter::OnDeviceAdded(const std::string& device_path) { |
| 53 DCHECK(thread_checker_.CalledOnValidThread()); | 51 DCHECK(thread_checker_.CalledOnValidThread()); |
| 54 | 52 |
| 55 if (is_starting_up_ || is_resuming_) { | 53 SetDeviceState(device_path, DEVICE_STATE_USUAL); |
| 56 SetDeviceState(device_path, DEVICE_STATE_USUAL); | |
| 57 return; | |
| 58 } | |
| 59 | |
| 60 if (IsExternalStorageDisabled()) { | 54 if (IsExternalStorageDisabled()) { |
| 61 OnDeviceEvent(file_manager_private::DEVICE_EVENT_TYPE_DISABLED, | 55 OnDeviceEvent(file_manager_private::DEVICE_EVENT_TYPE_DISABLED, |
| 62 device_path); | 56 device_path); |
| 63 SetDeviceState(device_path, DEVICE_STATE_USUAL); | |
| 64 return; | 57 return; |
| 65 } | 58 } |
| 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 OnDeviceEvent(file_manager_private::DEVICE_EVENT_TYPE_SCAN_STARTED, | |
| 81 device_path); | |
| 82 SetDeviceState(device_path, DEVICE_SCANNED_AND_REPORTED); | |
| 83 } | |
| 84 } | 59 } |
| 85 | 60 |
| 86 void DeviceEventRouter::OnDeviceRemoved(const std::string& device_path) { | 61 void DeviceEventRouter::OnDeviceRemoved(const std::string& device_path) { |
| 87 DCHECK(thread_checker_.CalledOnValidThread()); | 62 DCHECK(thread_checker_.CalledOnValidThread()); |
| 88 SetDeviceState(device_path, DEVICE_STATE_USUAL); | 63 SetDeviceState(device_path, DEVICE_STATE_USUAL); |
| 89 OnDeviceEvent(file_manager_private::DEVICE_EVENT_TYPE_REMOVED, device_path); | 64 OnDeviceEvent(file_manager_private::DEVICE_EVENT_TYPE_REMOVED, device_path); |
| 90 } | 65 } |
| 91 | 66 |
| 92 void DeviceEventRouter::OnDiskAdded( | 67 void DeviceEventRouter::OnDiskAdded( |
| 93 const chromeos::disks::DiskMountManager::Disk& disk, | 68 const chromeos::disks::DiskMountManager::Disk& disk, |
| 94 bool mounting) { | 69 bool mounting) { |
| 95 DCHECK(thread_checker_.CalledOnValidThread()); | 70 // Do nothing. |
| 96 | |
| 97 if (!mounting) { | |
| 98 // If the disk is not being mounted, mark the device scan cancelled. | |
| 99 const std::string& device_path = disk.system_path_prefix(); | |
| 100 if (GetDeviceState(device_path) == DEVICE_SCANNED_AND_REPORTED) { | |
| 101 OnDeviceEvent(file_manager_private::DEVICE_EVENT_TYPE_SCAN_CANCELLED, | |
| 102 device_path); | |
| 103 } | |
| 104 SetDeviceState(device_path, DEVICE_STATE_USUAL); | |
| 105 } | |
| 106 } | 71 } |
| 107 | 72 |
| 108 void DeviceEventRouter::OnDiskRemoved( | 73 void DeviceEventRouter::OnDiskRemoved( |
| 109 const chromeos::disks::DiskMountManager::Disk& disk) { | 74 const chromeos::disks::DiskMountManager::Disk& disk) { |
| 110 DCHECK(thread_checker_.CalledOnValidThread()); | 75 DCHECK(thread_checker_.CalledOnValidThread()); |
| 111 | 76 |
| 112 if (is_resuming_ || is_starting_up_) | 77 if (is_resuming_ || is_starting_up_) |
| 113 return; | 78 return; |
| 114 | 79 |
| 115 const std::string& device_path = disk.system_path_prefix(); | 80 const std::string& device_path = disk.system_path_prefix(); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 device_states_[device_path] = state; | 154 device_states_[device_path] = state; |
| 190 } else { | 155 } else { |
| 191 const std::map<std::string, DeviceState>::iterator it = | 156 const std::map<std::string, DeviceState>::iterator it = |
| 192 device_states_.find(device_path); | 157 device_states_.find(device_path); |
| 193 if (it != device_states_.end()) | 158 if (it != device_states_.end()) |
| 194 device_states_.erase(it); | 159 device_states_.erase(it); |
| 195 } | 160 } |
| 196 } | 161 } |
| 197 | 162 |
| 198 } // namespace file_manager | 163 } // namespace file_manager |
| OLD | NEW |