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

Unified 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: moved webusb::WebUsbDetector::GetInstance() call to chrome_browser_main.cc 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 side-by-side diff with in-line comments
Download patch
Index: components/webusb/webusb_detector.cc
diff --git a/components/webusb/webusb_detector.cc b/components/webusb/webusb_detector.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0cb2c4625fb2d5105eaa464e2182bf299c858759
--- /dev/null
+++ b/components/webusb/webusb_detector.cc
@@ -0,0 +1,121 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/webusb/webusb_detector.h"
+
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/singleton.h"
+#include "base/strings/string16.h"
+#include "base/strings/utf_string_conversions.h"
+#include "components/webusb/webusb_browser_client.h"
+#include "device/core/device_client.h"
+#include "device/usb/usb_device.h"
+#include "device/usb/usb_ids.h"
+#include "grit/components_strings.h"
+#include "ui/base/l10n/l10n_util.h"
+#include "ui/gfx/image/image.h"
+#include "ui/message_center/message_center.h"
+#include "ui/message_center/notification.h"
+#include "ui/message_center/notification_delegate.h"
+
+namespace {
+
+const char kWebUsbDetectorNotificationID[] = "webusb.detector";
+
+// Delegate for webusb notification
+class WebUsbNotificationDelegate : public message_center::NotificationDelegate {
+ public:
+ WebUsbNotificationDelegate(const GURL& landing_page,
+ const std::string& notification_id)
+ : landing_page_(landing_page), notification_id_(notification_id) {}
+
+ void Click() override {
+ webusb::WebUsbBrowserClient::Get()->OpenURL(landing_page_);
+ message_center::MessageCenter::Get()->RemoveNotification(
+ notification_id_, false /* by_user */);
+ }
+
+ private:
+ ~WebUsbNotificationDelegate() override = default;
+
+ GURL landing_page_;
+ std::string notification_id_;
+
+ DISALLOW_COPY_AND_ASSIGN(WebUsbNotificationDelegate);
+};
+
+} // namespace
+
+namespace webusb {
+
+WebUsbDetector* WebUsbDetector::GetInstance() {
+ return Singleton<WebUsbDetector>::get();
+}
+
+WebUsbDetector::WebUsbDetector() : observer_(this) {
+ Initialize();
+}
+
+WebUsbDetector::~WebUsbDetector() {}
+
+void WebUsbDetector::Initialize() {
+ if (!webusb::WebUsbBrowserClient::Get()) {
+ return;
+ }
+
+ device::UsbService* usb_service =
+ device::DeviceClient::Get()->GetUsbService();
+ if (!usb_service)
+ return;
+
+ observer_.Add(usb_service);
+}
+
+void WebUsbDetector::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) {
+ const char* product_name =
+ 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.
+ if (!product_name) {
+ return;
+ }
+
+ const GURL& landing_page = device->webusb_landing_page();
+ if (!landing_page.is_valid()) {
+ return;
+ }
+
+ std::string notification_id = device->guid();
+
+ scoped_ptr<message_center::Notification> notification;
+
+ message_center::RichNotificationData rich_notification_data;
+ rich_notification_data.context_message =
+ base::UTF8ToUTF16(landing_page.GetContent());
+
+ notification.reset(new message_center::Notification(
+ message_center::NOTIFICATION_TYPE_SIMPLE, notification_id,
+ l10n_util::GetStringFUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION_TITLE,
+ base::UTF8ToUTF16(product_name)),
+ l10n_util::GetStringUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION),
+ // TODO(juncai): use generic USB device icon here.
+ gfx::Image(), base::string16(),
+ message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
+ kWebUsbDetectorNotificationID),
+ rich_notification_data,
+ new WebUsbNotificationDelegate(landing_page, notification_id)));
+
+ notification->SetSystemPriority();
+ message_center::MessageCenter::Get()->AddNotification(notification.Pass());
+}
+
+void WebUsbDetector::OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) {
+ std::string notification_id = device->guid();
+
+ message_center::MessageCenter* message_center =
+ message_center::MessageCenter::Get();
+ if (message_center->FindVisibleNotificationById(notification_id)) {
+ message_center->RemoveNotification(notification_id, false /* by_user */);
+ }
+}
+
+} // namespace webusb

Powered by Google App Engine
This is Rietveld 408576698