Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: components/webusb/webusb_detector.cc

Issue 1289423002: Add webusb notification UI code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: modified WebUsbBrowserClient::Set function again Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "url/gurl.h"
22
23 namespace {
24
25 const char kWebUsbDetectorNotificationID[] = "webusb.detector";
26
27 // Delegate for webusb notification
28 class WebUsbNotificationDelegate : public message_center::NotificationDelegate {
29 public:
30 WebUsbNotificationDelegate(const GURL& landing_page,
31 const std::string& notification_id)
32 : landing_page_(landing_page), notification_id_(notification_id) {}
33
34 void Click() override {
35 webusb::WebUsbBrowserClient::Get()->OpenURL(landing_page_);
36 message_center::MessageCenter::Get()->RemoveNotification(
37 notification_id_, false /* by_user */);
38 }
39
40 private:
41 ~WebUsbNotificationDelegate() override = default;
42
43 GURL landing_page_;
44 std::string notification_id_;
45
46 DISALLOW_COPY_AND_ASSIGN(WebUsbNotificationDelegate);
47 };
48
49 } // namespace
50
51 namespace webusb {
52
53 WebUsbDetector* WebUsbDetector::GetInstance() {
54 return Singleton<WebUsbDetector>::get();
55 }
56
57 WebUsbDetector::WebUsbDetector() : observer_(this) {
58 Initialize();
59 }
60
61 WebUsbDetector::~WebUsbDetector() {}
62
63 void WebUsbDetector::Initialize() {
64 if (!webusb::WebUsbBrowserClient::Get()) {
65 return;
66 }
67
68 device::UsbService* usb_service =
69 device::DeviceClient::Get()->GetUsbService();
70 if (!usb_service)
71 return;
72
73 observer_.Add(usb_service);
74 }
75
76 void WebUsbDetector::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) {
77 const base::string16& product_name = device->product_string();
78 if (product_name.empty()) {
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 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(), GURL(),
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698