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 <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ptr_util.h" | |
14 #include "base/scoped_observer.h" | 15 #include "base/scoped_observer.h" |
15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
16 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/chromeos/printer_detector/printer_detector.h" | 18 #include "chrome/browser/chromeos/printer_detector/printer_detector.h" |
stevenjb
2017/03/17 17:34:10
nit: I don't know if our precommit allows it, but
Carlson
2017/03/17 18:47:34
Indeed, presubmit doesn't allow this, sadly.
I'm
| |
19 #include "chrome/browser/chromeos/printing/ppd_provider_factory.h" | |
20 #include "chrome/browser/chromeos/printing/printer_configurer.h" | |
21 #include "chrome/browser/chromeos/printing/printers_manager_factory.h" | |
22 #include "chrome/browser/chromeos/printing/usb_util.h" | |
18 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 23 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
19 #include "chrome/browser/notifications/notification.h" | 24 #include "chromeos/dbus/dbus_thread_manager.h" |
20 #include "chrome/browser/notifications/notification_delegate.h" | 25 #include "chromeos/dbus/debug_daemon_client.h" |
21 #include "chrome/browser/notifications/notification_ui_manager.h" | 26 #include "chromeos/printing/ppd_provider.h" |
22 #include "chrome/browser/ui/browser_navigator.h" | 27 #include "content/public/browser/browser_thread.h" |
23 #include "chrome/browser/ui/browser_navigator_params.h" | |
24 #include "chrome/common/url_constants.h" | |
25 #include "chrome/grit/generated_resources.h" | |
26 #include "chrome/grit/theme_resources.h" | |
27 #include "components/user_manager/user.h" | |
28 #include "components/user_manager/user_manager.h" | |
29 #include "device/base/device_client.h" | 28 #include "device/base/device_client.h" |
30 #include "device/usb/usb_device.h" | 29 #include "device/usb/usb_device.h" |
31 #include "device/usb/usb_device_filter.h" | 30 #include "device/usb/usb_device_filter.h" |
32 #include "device/usb/usb_service.h" | 31 #include "device/usb/usb_service.h" |
33 #include "extensions/browser/extension_registry.h" | |
34 #include "extensions/browser/extension_system.h" | |
35 #include "extensions/common/one_shot_event.h" | |
36 #include "ui/base/l10n/l10n_util.h" | |
37 #include "ui/base/resource/resource_bundle.h" | |
38 | 32 |
39 namespace chromeos { | 33 namespace chromeos { |
40 namespace { | 34 namespace { |
41 | 35 |
42 const char kUSBPrinterFoundNotificationID[] = | 36 using printing::PpdProvider; |
43 "chrome://settings/cupsPrinters/printer_found"; | |
44 | 37 |
45 // Base class used for printer USB interfaces | 38 // Aggregates the information needed for printer setup so it's easier to pass it |
46 // (https://www.usb.org/developers/defined_class). | 39 // around. |
47 const uint8_t kPrinterInterfaceClass = 7; | 40 struct SetUpPrinterData { |
41 // The configurer running the SetUpPrinter call. | |
42 std::unique_ptr<PrinterConfigurer> configurer; | |
48 | 43 |
49 // USBPrinterSetupNotificationDelegate takes a pointer to the Impl class, so | 44 // The printer being set up. |
50 // we have to forward declare it. | 45 std::unique_ptr<Printer> printer; |
51 class CupsPrinterDetectorImpl; | |
52 | 46 |
53 class USBPrinterSetupNotificationDelegate : public NotificationDelegate { | 47 // The usb device causing this setup flow. |
54 public: | 48 scoped_refptr<device::UsbDevice> device; |
55 explicit USBPrinterSetupNotificationDelegate( | |
56 CupsPrinterDetectorImpl* printer_detector) | |
57 : printer_detector_(printer_detector) {} | |
58 | 49 |
59 // NotificationDelegate override: | 50 // True if this printer is one that the user doesn't already have a |
60 std::string id() const override { return kUSBPrinterFoundNotificationID; } | 51 // configuration for. |
52 bool is_new; | |
61 | 53 |
62 // This is defined out of line because it needs the PrinterDetectorImpl | 54 // True if this was a printer that was plugged in during the session, false if |
63 // full class declaration, not just the forward declaration. | 55 // it was already plugged in when the session started. |
64 void ButtonClick(int button_index) override; | 56 bool hotplugged; |
57 }; | |
65 | 58 |
66 bool HasClickedListener() override { return true; } | 59 // Given a usb device, guesses the make and model for a driver lookup. |
67 | 60 // |
68 private: | 61 // TODO(justincarlson): Possibly go deeper and query the IEEE1284 fields |
69 ~USBPrinterSetupNotificationDelegate() override = default; | 62 // for make and model if we determine those are more likely to contain |
70 | 63 // what we want. |
71 CupsPrinterDetectorImpl* printer_detector_; | 64 std::string GuessEffectiveMakeAndModel(const device::UsbDevice& device) { |
72 | 65 return base::UTF16ToUTF8(device.manufacturer_string()) + " " + |
73 DISALLOW_COPY_AND_ASSIGN(USBPrinterSetupNotificationDelegate); | 66 base::UTF16ToUTF8(device.product_string()); |
74 }; | 67 } |
75 | 68 |
76 // The PrinterDetector that drives the flow for setting up a USB printer to use | 69 // The PrinterDetector that drives the flow for setting up a USB printer to use |
77 // CUPS backend. | 70 // CUPS backend. |
78 class CupsPrinterDetectorImpl : public PrinterDetector, | 71 class CupsPrinterDetectorImpl : public PrinterDetector, |
79 public device::UsbService::Observer { | 72 public device::UsbService::Observer { |
80 public: | 73 public: |
81 explicit CupsPrinterDetectorImpl(Profile* profile) | 74 explicit CupsPrinterDetectorImpl(Profile* profile) |
82 : profile_(profile), observer_(this), weak_ptr_factory_(this) { | 75 : profile_(profile), observer_(this), weak_ptr_factory_(this) { |
83 extensions::ExtensionSystem::Get(profile)->ready().Post( | 76 device::UsbService* usb_service = |
84 FROM_HERE, base::Bind(&CupsPrinterDetectorImpl::Initialize, | 77 device::DeviceClient::Get()->GetUsbService(); |
85 weak_ptr_factory_.GetWeakPtr())); | 78 if (usb_service) { |
79 observer_.Add(usb_service); | |
80 usb_service->GetDevices(base::Bind(&CupsPrinterDetectorImpl::OnGetDevices, | |
81 weak_ptr_factory_.GetWeakPtr())); | |
82 } | |
86 } | 83 } |
87 ~CupsPrinterDetectorImpl() override = default; | 84 ~CupsPrinterDetectorImpl() override = default; |
88 | 85 |
89 void ClickOnNotificationButton(int button_index) { | 86 private: |
90 // Remove the old notification first. | 87 // Callback for initial enumeration of usb devices. |
91 const ProfileID profile_id = NotificationUIManager::GetProfileID(profile_); | 88 void OnGetDevices( |
92 g_browser_process->notification_ui_manager()->CancelById( | 89 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { |
93 kUSBPrinterFoundNotificationID, profile_id); | 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
94 | 91 for (const auto& device : devices) { |
95 if (command_ == ButtonCommand::SETUP) { | 92 MaybeSetUpDevice(device, false); |
96 OnSetUpUSBPrinterStarted(); | |
97 // TODO(skau/xdai): call the CUPS backend to set up the USB printer and | |
98 // then call OnSetUpPrinterDone() or OnSetUpPrinterError() depending on | |
99 // the setup result. | |
100 } else if (command_ == ButtonCommand::CANCEL_SETUP) { | |
101 // TODO(skau/xdai): call the CUPS backend to cancel the printer setup. | |
102 } else if (command_ == ButtonCommand::GET_HELP) { | |
103 chrome::NavigateParams params(profile_, | |
104 GURL(chrome::kChromeUIMdCupsSettingsURL), | |
105 ui::PAGE_TRANSITION_LINK); | |
106 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; | |
107 params.window_action = chrome::NavigateParams::SHOW_WINDOW; | |
108 chrome::Navigate(¶ms); | |
109 } | 93 } |
110 } | 94 } |
111 | 95 |
112 private: | 96 // UsbService::observer override. |
113 // Action that should be performed when a notification button is clicked. | 97 void OnDeviceAdded(scoped_refptr<device::UsbDevice> device) override { |
114 enum class ButtonCommand { | 98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
115 SETUP, | 99 MaybeSetUpDevice(device, true); |
116 CANCEL_SETUP, | 100 } |
117 CLOSE, | |
118 GET_HELP, | |
119 }; | |
120 | 101 |
121 // UsbService::observer override: | 102 // UsbService::observer override. |
122 void OnDeviceAdded(scoped_refptr<device::UsbDevice> device) override { | 103 void OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) override { |
123 const user_manager::User* user = | 104 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
124 ProfileHelper::Get()->GetUserByProfile(profile_); | 105 if (!UsbDeviceIsPrinter(device)) { |
125 // TODO(justincarlson) - See if it's appropriate to relax any of these | 106 return; |
126 // constraints. | 107 } |
127 if (!user || !user->HasGaiaAccount() || !user_manager::UserManager::Get() || | 108 known_printers_.erase(device->guid()); |
128 user != user_manager::UserManager::Get()->GetActiveUser()) { | 109 failed_setup_printers_.erase(device->guid()); |
110 } | |
111 | |
112 // Returns the existing printer using this URI, if one exists, or | |
113 // null otherwise. | |
114 std::unique_ptr<Printer> FindExistingPrinter(const std::string& uri) { | |
115 // TODO(justincarlson): add a GetPrinterByURI to PrintersManager. | |
116 // https://crbug.com/700602 | |
117 auto existing_printers = | |
118 PrintersManagerFactory::GetForBrowserContext(profile_)->GetPrinters(); | |
119 for (std::unique_ptr<Printer>& printer : existing_printers) { | |
120 if (printer->uri() == uri) { | |
121 // Found a match, so use the existing configuration. | |
122 return std::move(printer); | |
123 } | |
124 } | |
125 return nullptr; | |
126 } | |
127 | |
128 // If this device is a printer and we haven't already tried to set it up, | |
129 // starts the process of setting the printer up. |hotplugged| | |
130 // should be true if this was plugged in during the session. | |
131 void MaybeSetUpDevice(scoped_refptr<device::UsbDevice> device, | |
132 bool hotplugged) { | |
133 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
134 | |
135 if (!UsbDeviceIsPrinter(device) || | |
136 base::ContainsKey(known_printers_, device->guid())) { | |
137 return; | |
138 } | |
139 known_printers_.insert(device->guid()); | |
140 | |
141 auto data = base::MakeUnique<SetUpPrinterData>(); | |
142 data->configurer = PrinterConfigurer::Create(profile_); | |
143 data->device = device; | |
144 data->hotplugged = hotplugged; | |
145 | |
146 data->printer = FindExistingPrinter(UsbPrinterUri(*device)); | |
147 if (data->printer != nullptr) { | |
148 data->is_new = false; | |
149 OnPrinterResolved(std::move(data)); | |
129 return; | 150 return; |
130 } | 151 } |
131 | 152 |
132 device::UsbDeviceFilter printer_filter; | 153 // It's not a device we have configured previously. |
133 printer_filter.interface_class = kPrinterInterfaceClass; | 154 // |
134 if (!printer_filter.Matches(device)) | 155 // TODO(justincarlson): Add a notification that we are attempting to set up |
156 // this printer at this point. | |
157 data->is_new = true; | |
158 data->printer = UsbDeviceToPrinter(*device); | |
159 if (data->printer == nullptr) { | |
135 return; | 160 return; |
161 } | |
136 | 162 |
137 ShowUSBPrinterSetupNotification(device); | 163 // Look for an exact match based on USB ids. |
164 scoped_refptr<PpdProvider> ppd_provider = | |
165 printing::CreateProvider(profile_); | |
166 ppd_provider->ResolveUsbIds( | |
167 device->vendor_id(), device->product_id(), | |
168 base::Bind(&CupsPrinterDetectorImpl::ResolveUsbIdsDone, | |
169 weak_ptr_factory_.GetWeakPtr(), | |
170 base::Passed(std::move(data)))); | |
138 } | 171 } |
139 | 172 |
140 // Initializes the printer detector. | 173 void OnPrinterResolved(std::unique_ptr<SetUpPrinterData> data) { |
141 void Initialize() { | 174 // Copy fields that will be invalidated by std::move. |
142 device::UsbService* usb_service = | 175 auto* configurer_tmp = data->configurer.get(); |
tbarzic
2017/03/16 04:11:29
it would be nicer if this pattern could be avoided
stevenjb
2017/03/17 17:34:10
I agree this is confusing. We definitely shouldn't
Carlson
2017/03/17 18:47:34
Using a local pointer to data is a much better ide
| |
143 device::DeviceClient::Get()->GetUsbService(); | 176 const Printer& printer_tmp = *(data->printer); |
144 if (!usb_service) | 177 // off to the configurer. |
145 return; | 178 configurer_tmp->SetUpPrinter( |
146 observer_.Add(usb_service); | 179 printer_tmp, base::Bind(&CupsPrinterDetectorImpl::SetUpPrinterDone, |
180 weak_ptr_factory_.GetWeakPtr(), | |
181 base::Passed(std::move(data)))); | |
182 } | |
183 | |
184 // Called when the query for a driver based on usb ids completes. | |
185 void ResolveUsbIdsDone(std::unique_ptr<SetUpPrinterData> data, | |
186 PpdProvider::CallbackResultCode result, | |
187 const std::string& effective_make_and_model) { | |
188 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
189 if (result == PpdProvider::SUCCESS) { | |
190 // Got something based on usb ids. Go with it. | |
191 data->printer->mutable_ppd_reference()->effective_make_and_model = | |
192 effective_make_and_model; | |
193 } else { | |
194 // Couldn't figure this printer out based on usb ids, fall back to | |
195 // guessing the make/model string from what the USB system reports. | |
196 // | |
197 // TODO(justincarlson): Consider adding a mechanism for aggregating data | |
198 // about which usb devices are in the wild but unsupported? | |
199 data->printer->mutable_ppd_reference()->effective_make_and_model = | |
200 GuessEffectiveMakeAndModel(*data->device); | |
201 } | |
202 OnPrinterResolved(std::move(data)); | |
203 } | |
204 | |
205 // Called with the result of asking to have a printer configured for CUPS. If | |
206 // |printer_to_register| is non-null and we successfully configured, then the | |
207 // printer is registered with the printers manager. | |
208 void SetUpPrinterDone(std::unique_ptr<SetUpPrinterData> data, | |
209 PrinterSetupResult result) { | |
210 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
211 if (result == PrinterSetupResult::SUCCESS) { | |
212 if (data->is_new) { | |
213 PrintersManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( | |
214 std::move(data->printer)); | |
215 } | |
216 if (data->hotplugged) { | |
stevenjb
2017/03/17 17:34:10
Remove the if () since it doesn't do anything yet,
Carlson
2017/03/17 18:47:34
Done.
| |
217 // TODO(justincarlson): Pop a timed notification that says the printer | |
218 // is now available for printing. | |
219 } | |
220 } else { | |
221 // TODO(justincarlson): Pop a notification that tells the user automatic | |
222 // setup failed and offers to open the CUPS printer configuration | |
223 // settings. | |
224 DCHECK(!base::ContainsKey(failed_setup_printers_, data->device->guid())); | |
225 failed_setup_printers_.insert({data->device->guid(), *(data->printer)}); | |
226 } | |
147 } | 227 } |
148 | 228 |
149 void SetNotificationUIManagerForTesting( | 229 void SetNotificationUIManagerForTesting( |
150 NotificationUIManager* manager) override { | 230 NotificationUIManager* manager) override { |
151 LOG(FATAL) << "Not implemented for CUPS"; | 231 LOG(FATAL) << "Not implemented for CUPS"; |
152 } | 232 } |
153 | 233 |
154 void ShowUSBPrinterSetupNotification( | 234 // USB GUIDs of printers we've already dealt with. There's an inherent race |
155 scoped_refptr<device::UsbDevice> device) { | 235 // between initially querying all usb devices and receiving a notification |
156 // TODO(justincarlson) - Test this notification across a wide variety of | 236 // about a new device, so this set lets us guarantee that we handle a given |
157 // less-than-sane printers to make sure the notification text stays as | 237 // printer exactly once. |
158 // intelligible as possible. | 238 std::set<std::string> known_printers_; |
159 base::string16 printer_name = device->manufacturer_string() + | |
160 base::UTF8ToUTF16(" ") + | |
161 device->product_string(); | |
162 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
163 message_center::RichNotificationData data; | |
164 data.buttons.push_back(message_center::ButtonInfo(l10n_util::GetStringUTF16( | |
165 IDS_PRINTER_DETECTED_NOTIFICATION_SET_UP_BUTTON))); | |
166 notification_.reset(new Notification( | |
167 message_center::NOTIFICATION_TYPE_SIMPLE, | |
168 l10n_util::GetStringUTF16( | |
169 IDS_PRINTER_DETECTED_NOTIFICATION_SET_UP_TITLE), // title | |
170 printer_name, // body | |
171 bundle.GetImageNamed(IDR_PRINTER_DETECTED_NOTIFICATION), // icon | |
172 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | |
173 kUSBPrinterFoundNotificationID), | |
174 base::string16(), // display_source | |
175 GURL(), kUSBPrinterFoundNotificationID, data, | |
176 new USBPrinterSetupNotificationDelegate(this))); | |
177 notification_->SetSystemPriority(); | |
178 command_ = ButtonCommand::SETUP; | |
179 | 239 |
180 g_browser_process->notification_ui_manager()->Add(*notification_, profile_); | 240 // Printers that this class failed to set up automatically. This is provided |
181 } | 241 // to the PrinterDiscoverer used in the settings flow to determine which |
182 | 242 // printers are available to be set up. This is a map from the USB GUID |
183 void OnSetUpUSBPrinterStarted() { | 243 // to Printer structure. |
184 notification_->set_title(l10n_util::GetStringUTF16( | 244 std::unordered_map<std::string, Printer> failed_setup_printers_; |
stevenjb
2017/03/17 17:34:10
having a chromeos::Printer class BTW is super conf
Carlson
2017/03/17 18:47:34
Yeah, this sort of cleanup is probably a bit overd
stevenjb
2017/03/17 20:49:54
I would strongly encourage moving this to the next
Carlson
2017/03/17 21:08:17
I think what you're worried about here is a micro-
stevenjb
2017/03/17 21:21:13
I most likely agree, but my point is since this is
| |
185 IDS_PRINTER_DETECTED_NOTIFICATION_SET_UP_IN_PROGRESS_TITLE)); | |
186 notification_->set_type(message_center::NOTIFICATION_TYPE_PROGRESS); | |
187 notification_->set_progress(-1); | |
188 std::vector<message_center::ButtonInfo> buttons; | |
189 buttons.push_back(message_center::ButtonInfo(l10n_util::GetStringUTF16( | |
190 IDS_PRINTER_DETECTED_NOTIFICATION_SET_UP_CANCEL_BUTTON))); | |
191 notification_->set_buttons(buttons); | |
192 command_ = ButtonCommand::CANCEL_SETUP; | |
193 g_browser_process->notification_ui_manager()->Add(*notification_, profile_); | |
194 } | |
195 | |
196 void OnSetUpUSBPrinterDone() { | |
197 notification_->set_title(l10n_util::GetStringUTF16( | |
198 IDS_PRINTER_DETECTED_NOTIFICATION_SET_UP_SUCCESS_TITLE)); | |
199 notification_->set_type(message_center::NOTIFICATION_TYPE_SIMPLE); | |
200 std::vector<message_center::ButtonInfo> buttons; | |
201 buttons.push_back(message_center::ButtonInfo(l10n_util::GetStringUTF16( | |
202 IDS_PRINTER_DETECTED_NOTIFICATION_SET_UP_CLOSE_BUTTON))); | |
203 notification_->set_buttons(buttons); | |
204 command_ = ButtonCommand::CLOSE; | |
205 g_browser_process->notification_ui_manager()->Add(*notification_, profile_); | |
206 } | |
207 | |
208 void OnSetUpUSBPrinterError() { | |
209 notification_->set_title(l10n_util::GetStringUTF16( | |
210 IDS_PRINTER_DETECTED_NOTIFICATION_SET_UP_FAILED_TITLE)); | |
211 notification_->set_type(message_center::NOTIFICATION_TYPE_SIMPLE); | |
212 std::vector<message_center::ButtonInfo> buttons; | |
213 buttons.push_back(message_center::ButtonInfo(l10n_util::GetStringUTF16( | |
214 IDS_PRINTER_DETECTED_NOTIFICATION_SET_UP_GET_HELP_BUTTON))); | |
215 notification_->set_buttons(buttons); | |
216 command_ = ButtonCommand::GET_HELP; | |
217 g_browser_process->notification_ui_manager()->Add(*notification_, profile_); | |
218 } | |
219 | |
220 std::unique_ptr<Notification> notification_; | |
221 ButtonCommand command_ = ButtonCommand::SETUP; | |
222 | 245 |
223 Profile* profile_; | 246 Profile* profile_; |
224 ScopedObserver<device::UsbService, device::UsbService::Observer> observer_; | 247 ScopedObserver<device::UsbService, device::UsbService::Observer> observer_; |
225 base::WeakPtrFactory<CupsPrinterDetectorImpl> weak_ptr_factory_; | 248 base::WeakPtrFactory<CupsPrinterDetectorImpl> weak_ptr_factory_; |
226 }; | 249 }; |
227 | 250 |
228 void USBPrinterSetupNotificationDelegate::ButtonClick(int button_index) { | |
229 printer_detector_->ClickOnNotificationButton(button_index); | |
230 } | |
231 | |
232 } // namespace | 251 } // namespace |
233 | 252 |
234 // static | 253 // static |
235 std::unique_ptr<PrinterDetector> PrinterDetector::CreateCups(Profile* profile) { | 254 std::unique_ptr<PrinterDetector> PrinterDetector::CreateCups(Profile* profile) { |
236 return base::MakeUnique<CupsPrinterDetectorImpl>(profile); | 255 return base::MakeUnique<CupsPrinterDetectorImpl>(profile); |
237 } | 256 } |
238 | 257 |
239 } // namespace chromeos | 258 } // namespace chromeos |
OLD | NEW |