| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 void RemoveObserver(UsbPrinterDetector::Observer* observer) override { | 100 void RemoveObserver(UsbPrinterDetector::Observer* observer) override { |
| 101 observer_list_->RemoveObserver(observer); | 101 observer_list_->RemoveObserver(observer); |
| 102 } | 102 } |
| 103 | 103 |
| 104 // PrinterDetector interface function. | 104 // PrinterDetector interface function. |
| 105 std::vector<Printer> GetPrinters() override { | 105 std::vector<Printer> GetPrinters() override { |
| 106 base::AutoLock auto_lock(pp_lock_); | 106 base::AutoLock auto_lock(pp_lock_); |
| 107 return GetPrintersLocked(); | 107 return GetPrintersLocked(); |
| 108 } | 108 } |
| 109 | 109 |
| 110 void Start() override { |
| 111 started_ = true; |
| 112 observer_list_->Notify(FROM_HERE, |
| 113 &PrinterDetector::Observer::OnPrintersFound, |
| 114 GetPrintersLocked()); |
| 115 observer_list_->Notify(FROM_HERE, |
| 116 &PrinterDetector::Observer::OnPrinterScanComplete); |
| 117 } |
| 118 |
| 110 private: | 119 private: |
| 111 std::vector<Printer> GetPrintersLocked() { | 120 std::vector<Printer> GetPrintersLocked() { |
| 112 pp_lock_.AssertAcquired(); | 121 pp_lock_.AssertAcquired(); |
| 113 std::vector<Printer> printers; | 122 std::vector<Printer> printers; |
| 114 printers.reserve(present_printers_.size()); | 123 printers.reserve(present_printers_.size()); |
| 115 for (const auto& entry : present_printers_) { | 124 for (const auto& entry : present_printers_) { |
| 116 printers.push_back(*entry.second); | 125 printers.push_back(*entry.second); |
| 117 } | 126 } |
| 118 return printers; | 127 return printers; |
| 119 } | 128 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 136 // UsbService::observer override. | 145 // UsbService::observer override. |
| 137 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) override { | 146 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) override { |
| 138 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 139 if (!UsbDeviceIsPrinter(*device)) { | 148 if (!UsbDeviceIsPrinter(*device)) { |
| 140 return; | 149 return; |
| 141 } | 150 } |
| 142 | 151 |
| 143 base::AutoLock auto_lock(pp_lock_); | 152 base::AutoLock auto_lock(pp_lock_); |
| 144 if (base::ContainsKey(present_printers_, device->guid())) { | 153 if (base::ContainsKey(present_printers_, device->guid())) { |
| 145 present_printers_.erase(device->guid()); | 154 present_printers_.erase(device->guid()); |
| 146 auto printers = GetPrintersLocked(); | 155 if (started_) { |
| 147 // We already have pp_lock_, so need to call the pre-locked version of | 156 auto printers = GetPrintersLocked(); |
| 148 // GetPrinters to prevent deadlock. | 157 // We already have pp_lock_, so need to call the pre-locked version of |
| 149 observer_list_->Notify( | 158 // GetPrinters to prevent deadlock. |
| 150 FROM_HERE, | 159 observer_list_->Notify(FROM_HERE, |
| 151 &UsbPrinterDetector::Observer::OnAvailableUsbPrintersChanged, | 160 &PrinterDetector::Observer::OnPrintersFound, |
| 152 GetPrintersLocked()); | 161 GetPrintersLocked()); |
| 162 } |
| 153 } else { | 163 } else { |
| 154 // If the device has been removed but it's not in present_printers_, it | 164 // If the device has been removed but it's not in present_printers_, it |
| 155 // must still be in the setup flow. | 165 // must still be in the setup flow. |
| 156 deferred_printer_removals_.insert(device->guid()); | 166 deferred_printer_removals_.insert(device->guid()); |
| 157 } | 167 } |
| 158 } | 168 } |
| 159 | 169 |
| 160 // If this device is a printer and we haven't already tried to set it up, | 170 // If this device is a printer and we haven't already tried to set it up, |
| 161 // starts the process of setting the printer up. |hotplugged| | 171 // starts the process of setting the printer up. |hotplugged| |
| 162 // should be true if this was plugged in during the session. | 172 // should be true if this was plugged in during the session. |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 // tells the user automatic setup failed and offers to open the CUPS printer | 277 // tells the user automatic setup failed and offers to open the CUPS printer |
| 268 // configuration settings. | 278 // configuration settings. |
| 269 | 279 |
| 270 if (base::ContainsKey(deferred_printer_removals_, data->device->guid())) { | 280 if (base::ContainsKey(deferred_printer_removals_, data->device->guid())) { |
| 271 // The device was removed before we finished the flow, so just don't add | 281 // The device was removed before we finished the flow, so just don't add |
| 272 // it to present_printers_; | 282 // it to present_printers_; |
| 273 deferred_printer_removals_.erase(data->device->guid()); | 283 deferred_printer_removals_.erase(data->device->guid()); |
| 274 } else { | 284 } else { |
| 275 base::AutoLock auto_lock(pp_lock_); | 285 base::AutoLock auto_lock(pp_lock_); |
| 276 present_printers_.emplace(data->device->guid(), std::move(data->printer)); | 286 present_printers_.emplace(data->device->guid(), std::move(data->printer)); |
| 277 observer_list_->Notify( | 287 if (started_) { |
| 278 FROM_HERE, | 288 observer_list_->Notify(FROM_HERE, |
| 279 &UsbPrinterDetector::Observer::OnAvailableUsbPrintersChanged, | 289 &PrinterDetector::Observer::OnPrintersFound, |
| 280 GetPrintersLocked()); | 290 GetPrintersLocked()); |
| 291 } |
| 281 } | 292 } |
| 282 } | 293 } |
| 294 // Has Start() been called yet? |
| 295 bool started_ = false; |
| 283 | 296 |
| 284 // Map from USB GUID to Printer that we have detected as being currently | 297 // Map from USB GUID to Printer that we have detected as being currently |
| 285 // plugged in and have finished processing. Note present_printers_ may be | 298 // plugged in and have finished processing. Note present_printers_ may be |
| 286 // accessed from multiple threads, so is protected by pp_lock_. | 299 // accessed from multiple threads, so is protected by pp_lock_. |
| 287 std::map<std::string, std::unique_ptr<Printer>> present_printers_; | 300 std::map<std::string, std::unique_ptr<Printer>> present_printers_; |
| 288 base::Lock pp_lock_; | 301 base::Lock pp_lock_; |
| 289 | 302 |
| 290 // If the usb device is removed before we've finished processing it, we'll | 303 // If the usb device is removed before we've finished processing it, we'll |
| 291 // defer the cleanup until the setup flow finishes. This is the set of | 304 // defer the cleanup until the setup flow finishes. This is the set of |
| 292 // guids which have been removed before the flow finished. | 305 // guids which have been removed before the flow finished. |
| 293 std::set<std::string> deferred_printer_removals_; | 306 std::set<std::string> deferred_printer_removals_; |
| 294 | 307 |
| 295 Profile* profile_; | 308 Profile* profile_; |
| 296 ScopedObserver<device::UsbService, device::UsbService::Observer> | 309 ScopedObserver<device::UsbService, device::UsbService::Observer> |
| 297 usb_observer_; | 310 usb_observer_; |
| 298 scoped_refptr<base::ObserverListThreadSafe<UsbPrinterDetector::Observer>> | 311 scoped_refptr<base::ObserverListThreadSafe<UsbPrinterDetector::Observer>> |
| 299 observer_list_; | 312 observer_list_; |
| 300 base::WeakPtrFactory<UsbPrinterDetectorImpl> weak_ptr_factory_; | 313 base::WeakPtrFactory<UsbPrinterDetectorImpl> weak_ptr_factory_; |
| 301 }; | 314 }; |
| 302 | 315 |
| 303 } // namespace | 316 } // namespace |
| 304 | 317 |
| 305 // static | 318 // static |
| 306 std::unique_ptr<UsbPrinterDetector> UsbPrinterDetector::Create( | 319 std::unique_ptr<UsbPrinterDetector> UsbPrinterDetector::Create( |
| 307 Profile* profile) { | 320 Profile* profile) { |
| 308 return base::MakeUnique<UsbPrinterDetectorImpl>(profile); | 321 return base::MakeUnique<UsbPrinterDetectorImpl>(profile); |
| 309 } | 322 } |
| 310 | 323 |
| 311 } // namespace chromeos | 324 } // namespace chromeos |
| OLD | NEW |