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 "chrome/browser/chrome_webusb_browser_client.h" |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/net/referrer.h" |
| 9 #include "chrome/browser/profiles/profile_manager.h" |
| 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_finder.h" |
| 12 #include "chrome/browser/ui/host_desktop.h" |
| 13 #include "chrome/grit/generated_resources.h" |
| 14 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/base/page_transition_types.h" |
| 16 #include "ui/base/window_open_disposition.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 ChromeWebUsbBrowserClient::ChromeWebUsbBrowserClient() {} |
| 52 |
| 53 ChromeWebUsbBrowserClient::~ChromeWebUsbBrowserClient() {} |
| 54 |
| 55 void ChromeWebUsbBrowserClient::OnDeviceAdded( |
| 56 const base::string16& product_name, |
| 57 const GURL& landing_page, |
| 58 const std::string& notification_id) { |
| 59 scoped_ptr<message_center::Notification> notification; |
| 60 |
| 61 message_center::RichNotificationData rich_notification_data; |
| 62 rich_notification_data.context_message = |
| 63 base::UTF8ToUTF16(landing_page.GetContent()); |
| 64 |
| 65 notification.reset(new message_center::Notification( |
| 66 message_center::NOTIFICATION_TYPE_SIMPLE, notification_id, |
| 67 l10n_util::GetStringFUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION_TITLE, |
| 68 product_name), |
| 69 l10n_util::GetStringUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION), |
| 70 // TODO(juncai): use generic USB device icon here. |
| 71 gfx::Image(), base::string16(), GURL(), |
| 72 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
| 73 kWebUsbDetectorNotificationID), |
| 74 rich_notification_data, |
| 75 new WebUsbNotificationDelegate(landing_page, notification_id))); |
| 76 |
| 77 notification->SetSystemPriority(); |
| 78 message_center::MessageCenter::Get()->AddNotification(notification.Pass()); |
| 79 } |
| 80 |
| 81 void ChromeWebUsbBrowserClient::OnDeviceRemoved( |
| 82 const std::string& notification_id) { |
| 83 message_center::MessageCenter* message_center = |
| 84 message_center::MessageCenter::Get(); |
| 85 if (message_center->FindVisibleNotificationById(notification_id)) { |
| 86 message_center->RemoveNotification(notification_id, false /* by_user */); |
| 87 } |
| 88 } |
| 89 |
| 90 void ChromeWebUsbBrowserClient::OpenURL(const GURL& url) { |
| 91 Browser* browser = chrome::FindBrowserWithProfile( |
| 92 ProfileManager::GetActiveUserProfile(), chrome::HOST_DESKTOP_TYPE_NATIVE); |
| 93 content::OpenURLParams params(url, content::Referrer(), NEW_FOREGROUND_TAB, |
| 94 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, true); |
| 95 browser->OpenURL(params); |
| 96 } |
OLD | NEW |