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" |
| 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_printer_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 // TODO(justincarlson): Update failed printers. |
| 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(), ppd_provider, |
| 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 // |data| will be invalidated by the move below, so we have to latch it |
142 device::UsbService* usb_service = | 175 // before the call. |
143 device::DeviceClient::Get()->GetUsbService(); | 176 SetUpPrinterData* data_ptr = data.get(); |
144 if (!usb_service) | 177 data_ptr->configurer->SetUpPrinter( |
145 return; | 178 *(data_ptr->printer), |
146 observer_.Add(usb_service); | 179 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 // |
| 186 // Note |provider| is not used in this function, it's just passed along to |
| 187 // keep it alive during the USB resolution. |
| 188 void ResolveUsbIdsDone(scoped_refptr<PpdProvider> provider, |
| 189 std::unique_ptr<SetUpPrinterData> data, |
| 190 PpdProvider::CallbackResultCode result, |
| 191 const std::string& effective_make_and_model) { |
| 192 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 193 if (result == PpdProvider::SUCCESS) { |
| 194 // Got something based on usb ids. Go with it. |
| 195 data->printer->mutable_ppd_reference()->effective_make_and_model = |
| 196 effective_make_and_model; |
| 197 } else { |
| 198 // Couldn't figure this printer out based on usb ids, fall back to |
| 199 // guessing the make/model string from what the USB system reports. |
| 200 // |
| 201 // TODO(justincarlson): Consider adding a mechanism for aggregating data |
| 202 // about which usb devices are in the wild but unsupported? |
| 203 data->printer->mutable_ppd_reference()->effective_make_and_model = |
| 204 GuessEffectiveMakeAndModel(*data->device); |
| 205 } |
| 206 OnPrinterResolved(std::move(data)); |
| 207 } |
| 208 |
| 209 // Called with the result of asking to have a printer configured for CUPS. If |
| 210 // |printer_to_register| is non-null and we successfully configured, then the |
| 211 // printer is registered with the printers manager. |
| 212 void SetUpPrinterDone(std::unique_ptr<SetUpPrinterData> data, |
| 213 PrinterSetupResult result) { |
| 214 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 215 if (result == PrinterSetupResult::SUCCESS) { |
| 216 if (data->is_new) { |
| 217 PrintersManagerFactory::GetForBrowserContext(profile_)->RegisterPrinter( |
| 218 std::move(data->printer)); |
| 219 } |
| 220 // TODO(justincarlson): If the device was hotplugged, pop a timed |
| 221 // notification that says the printer is now available for printing. |
| 222 } |
| 223 // TODO(justincarlson): If this doesn't succeed, Pop a notification that |
| 224 // tells the user automatic setup failed and offers to open the CUPS printer |
| 225 // configuration settings. |
| 226 // |
| 227 // TODO(justincarlson): If this doesn't succeed, Update the list of printers |
| 228 // that failed to set up. |
147 } | 229 } |
148 | 230 |
149 void SetNotificationUIManagerForTesting( | 231 void SetNotificationUIManagerForTesting( |
150 NotificationUIManager* manager) override { | 232 NotificationUIManager* manager) override { |
151 LOG(FATAL) << "Not implemented for CUPS"; | 233 LOG(FATAL) << "Not implemented for CUPS"; |
152 } | 234 } |
153 | 235 |
154 void ShowUSBPrinterSetupNotification( | 236 // USB GUIDs of printers we've already dealt with. There's an inherent race |
155 scoped_refptr<device::UsbDevice> device) { | 237 // between initially querying all usb devices and receiving a notification |
156 // TODO(justincarlson) - Test this notification across a wide variety of | 238 // 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 | 239 // printer exactly once. |
158 // intelligible as possible. | 240 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 | |
180 g_browser_process->notification_ui_manager()->Add(*notification_, profile_); | |
181 } | |
182 | |
183 void OnSetUpUSBPrinterStarted() { | |
184 notification_->set_title(l10n_util::GetStringUTF16( | |
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 | 241 |
223 Profile* profile_; | 242 Profile* profile_; |
224 ScopedObserver<device::UsbService, device::UsbService::Observer> observer_; | 243 ScopedObserver<device::UsbService, device::UsbService::Observer> observer_; |
225 base::WeakPtrFactory<CupsPrinterDetectorImpl> weak_ptr_factory_; | 244 base::WeakPtrFactory<CupsPrinterDetectorImpl> weak_ptr_factory_; |
226 }; | 245 }; |
227 | 246 |
228 void USBPrinterSetupNotificationDelegate::ButtonClick(int button_index) { | |
229 printer_detector_->ClickOnNotificationButton(button_index); | |
230 } | |
231 | |
232 } // namespace | 247 } // namespace |
233 | 248 |
234 // static | 249 // static |
235 std::unique_ptr<PrinterDetector> PrinterDetector::CreateCups(Profile* profile) { | 250 std::unique_ptr<PrinterDetector> PrinterDetector::CreateCups(Profile* profile) { |
236 return base::MakeUnique<CupsPrinterDetectorImpl>(profile); | 251 return base::MakeUnique<CupsPrinterDetectorImpl>(profile); |
237 } | 252 } |
238 | 253 |
239 } // namespace chromeos | 254 } // namespace chromeos |
OLD | NEW |