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