OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/webusb/webusb_detector.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/string16.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/webusb/webusb_browser_client.h" |
| 11 #include "device/core/device_client.h" |
| 12 #include "device/usb/usb_device.h" |
| 13 #include "device/usb/usb_ids.h" |
| 14 #include "grit/components_strings.h" |
| 15 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "ui/gfx/image/image.h" |
| 17 #include "ui/message_center/message_center.h" |
| 18 #include "ui/message_center/notification.h" |
| 19 #include "ui/message_center/notification_delegate.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 const char kWebUsbDetectorNotificationID[] = "webusb.detector"; |
| 24 |
| 25 // Delegate for webusb notification |
| 26 class WebUsbNotificationDelegate : public message_center::NotificationDelegate { |
| 27 public: |
| 28 WebUsbNotificationDelegate(Profile* profile, |
| 29 const GURL& landing_page, |
| 30 const std::string& notification_id) |
| 31 : profile_(profile), |
| 32 landing_page_(landing_page), |
| 33 notification_id_(notification_id) {} |
| 34 |
| 35 void Click() override { |
| 36 webusb::WebUsbBrowserClient::Get()->OpenURL(profile_, landing_page_); |
| 37 message_center::MessageCenter::Get()->RemoveNotification( |
| 38 notification_id_, false /* by_user */); |
| 39 } |
| 40 |
| 41 private: |
| 42 ~WebUsbNotificationDelegate() override = default; |
| 43 |
| 44 Profile* profile_; |
| 45 GURL landing_page_; |
| 46 std::string notification_id_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(WebUsbNotificationDelegate); |
| 49 }; |
| 50 |
| 51 } // namespace |
| 52 |
| 53 namespace webusb { |
| 54 |
| 55 WebUsbDetector::WebUsbDetector(Profile* profile) |
| 56 : profile_(profile), observer_(this) { |
| 57 Initialize(); |
| 58 } |
| 59 |
| 60 WebUsbDetector::~WebUsbDetector() {} |
| 61 |
| 62 void WebUsbDetector::Shutdown() {} |
| 63 |
| 64 void WebUsbDetector::Initialize() { |
| 65 if (!webusb::WebUsbBrowserClient::Get()->WebUsbNotificationEnabled()) { |
| 66 return; |
| 67 } |
| 68 |
| 69 device::UsbService* usb_service = |
| 70 device::DeviceClient::Get()->GetUsbService(); |
| 71 if (!usb_service) |
| 72 return; |
| 73 observer_.Add(usb_service); |
| 74 } |
| 75 |
| 76 void WebUsbDetector::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) { |
| 77 const char* product_name = |
| 78 device::UsbIds::GetProductName(device->vendor_id(), device->product_id()); |
| 79 if (!product_name) { |
| 80 return; |
| 81 } |
| 82 |
| 83 const GURL& landing_page = device->webusb_landing_page(); |
| 84 if (!landing_page.is_valid()) { |
| 85 return; |
| 86 } |
| 87 |
| 88 std::string notification_id = device->guid(); |
| 89 |
| 90 scoped_ptr<message_center::Notification> notification; |
| 91 |
| 92 message_center::RichNotificationData rich_notification_data; |
| 93 rich_notification_data.context_message = |
| 94 base::UTF8ToUTF16(landing_page.GetContent()); |
| 95 |
| 96 notification.reset(new message_center::Notification( |
| 97 message_center::NOTIFICATION_TYPE_SIMPLE, notification_id, |
| 98 l10n_util::GetStringFUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION_TITLE, |
| 99 base::UTF8ToUTF16(product_name)), |
| 100 l10n_util::GetStringUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION), |
| 101 // TODO(juncai): use generic USB device icon here. |
| 102 gfx::Image(), base::string16(), |
| 103 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
| 104 kWebUsbDetectorNotificationID), |
| 105 rich_notification_data, |
| 106 new WebUsbNotificationDelegate(profile_, landing_page, notification_id))); |
| 107 |
| 108 notification->SetSystemPriority(); |
| 109 message_center::MessageCenter::Get()->AddNotification(notification.Pass()); |
| 110 } |
| 111 |
| 112 void WebUsbDetector::OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) { |
| 113 std::string notification_id = device->guid(); |
| 114 |
| 115 message_center::MessageCenter* message_center = |
| 116 message_center::MessageCenter::Get(); |
| 117 if (message_center->FindVisibleNotificationById(notification_id)) { |
| 118 message_center->RemoveNotification(notification_id, false /* by_user */); |
| 119 } |
| 120 } |
| 121 |
| 122 } // namespace webusb |
OLD | NEW |