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 "content/public/common/origin_util.h" |
| 15 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "ui/base/page_transition_types.h" |
| 17 #include "ui/base/window_open_disposition.h" |
| 18 #include "ui/gfx/image/image.h" |
| 19 #include "ui/message_center/message_center.h" |
| 20 #include "ui/message_center/notification.h" |
| 21 #include "ui/message_center/notification_delegate.h" |
| 22 #include "url/gurl.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 const char kWebUsbDetectorNotificationID[] = "webusb.detector"; |
| 27 |
| 28 void OpenURL(const GURL& url) { |
| 29 Browser* browser = chrome::FindBrowserWithProfile( |
| 30 ProfileManager::GetActiveUserProfile(), chrome::HOST_DESKTOP_TYPE_NATIVE); |
| 31 content::OpenURLParams params(url, content::Referrer(), NEW_FOREGROUND_TAB, |
| 32 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, true); |
| 33 browser->OpenURL(params); |
| 34 } |
| 35 |
| 36 // Delegate for webusb notification |
| 37 class WebUsbNotificationDelegate : public message_center::NotificationDelegate { |
| 38 public: |
| 39 WebUsbNotificationDelegate(const GURL& landing_page, |
| 40 const std::string& notification_id) |
| 41 : landing_page_(landing_page), notification_id_(notification_id) {} |
| 42 |
| 43 void Click() override { |
| 44 OpenURL(landing_page_); |
| 45 message_center::MessageCenter::Get()->RemoveNotification( |
| 46 notification_id_, false /* by_user */); |
| 47 } |
| 48 |
| 49 private: |
| 50 ~WebUsbNotificationDelegate() override = default; |
| 51 |
| 52 GURL landing_page_; |
| 53 std::string notification_id_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(WebUsbNotificationDelegate); |
| 56 }; |
| 57 |
| 58 } // namespace |
| 59 |
| 60 ChromeWebUsbBrowserClient::ChromeWebUsbBrowserClient() {} |
| 61 |
| 62 ChromeWebUsbBrowserClient::~ChromeWebUsbBrowserClient() {} |
| 63 |
| 64 void ChromeWebUsbBrowserClient::OnDeviceAdded( |
| 65 const base::string16& product_name, |
| 66 const GURL& landing_page, |
| 67 const std::string& notification_id) { |
| 68 if (!content::IsOriginSecure(landing_page)) { |
| 69 return; |
| 70 } |
| 71 |
| 72 scoped_ptr<message_center::Notification> notification; |
| 73 |
| 74 message_center::RichNotificationData rich_notification_data; |
| 75 rich_notification_data.context_message = |
| 76 base::UTF8ToUTF16(landing_page.GetContent()); |
| 77 |
| 78 notification.reset(new message_center::Notification( |
| 79 message_center::NOTIFICATION_TYPE_SIMPLE, notification_id, |
| 80 l10n_util::GetStringFUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION_TITLE, |
| 81 product_name), |
| 82 l10n_util::GetStringUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION), |
| 83 // TODO(juncai): use generic USB device icon here. |
| 84 gfx::Image(), base::string16(), GURL(), |
| 85 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
| 86 kWebUsbDetectorNotificationID), |
| 87 rich_notification_data, |
| 88 new WebUsbNotificationDelegate(landing_page, notification_id))); |
| 89 |
| 90 notification->SetSystemPriority(); |
| 91 message_center::MessageCenter::Get()->AddNotification(notification.Pass()); |
| 92 } |
| 93 |
| 94 void ChromeWebUsbBrowserClient::OnDeviceRemoved( |
| 95 const std::string& notification_id) { |
| 96 message_center::MessageCenter* message_center = |
| 97 message_center::MessageCenter::Get(); |
| 98 if (message_center->FindVisibleNotificationById(notification_id)) { |
| 99 message_center->RemoveNotification(notification_id, false /* by_user */); |
| 100 } |
| 101 } |
OLD | NEW |