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/command_line.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/strings/utf_string_conversions.h" | |
10 #include "chrome/browser/net/referrer.h" | |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "chrome/browser/ui/browser.h" | |
13 #include "chrome/browser/ui/browser_finder.h" | |
14 #include "chrome/browser/ui/host_desktop.h" | |
15 #include "chrome/common/chrome_switches.h" | |
16 #include "chrome/grit/generated_resources.h" | |
17 #include "grit/theme_resources.h" | |
18 #include "ui/base/l10n/l10n_util.h" | |
19 #include "ui/base/page_transition_types.h" | |
20 #include "ui/base/window_open_disposition.h" | |
21 #include "ui/message_center/message_center.h" | |
22 #include "ui/message_center/notification.h" | |
23 #include "ui/message_center/notification_delegate.h" | |
24 #include "url/gurl.h" | |
25 | |
26 namespace { | |
27 | |
28 const char kWebUsbDetectorNotificationID[] = "webusb.detector"; | |
29 | |
30 // Delegate for webusb notification | |
31 class WebUsbNotificationDelegate : public message_center::NotificationDelegate { | |
32 public: | |
33 WebUsbNotificationDelegate(Profile* profile, | |
34 const GURL& landing_page, | |
35 const std::string& notification_id) | |
36 : profile_(profile), | |
37 landing_page_(landing_page), | |
38 notification_id_(notification_id) {} | |
39 | |
40 void Click() override { | |
41 webusb::WebUsbBrowserClient::Get()->OpenURL(profile_, landing_page_); | |
42 message_center::MessageCenter::Get()->RemoveNotification( | |
43 notification_id_, false /* by_user */); | |
44 } | |
45 | |
46 private: | |
47 ~WebUsbNotificationDelegate() override = default; | |
48 | |
49 Profile* profile_; | |
50 GURL landing_page_; | |
51 std::string notification_id_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(WebUsbNotificationDelegate); | |
54 }; | |
55 | |
56 } // namespace | |
57 | |
58 ChromeWebUsbBrowserClient::ChromeWebUsbBrowserClient() {} | |
59 | |
60 ChromeWebUsbBrowserClient::~ChromeWebUsbBrowserClient() {} | |
61 | |
62 void ChromeWebUsbBrowserClient::OnDeviceAdded( | |
Reilly Grant (use Gerrit)
2015/08/18 00:23:43
Can you move OnDeviceAdded and OnDeviceRemoved int
juncai
2015/08/18 17:39:59
Done.
| |
63 Profile* profile, | |
64 const char* product_name, | |
65 const GURL& landing_page, | |
66 const std::string& notification_id) { | |
67 scoped_ptr<message_center::Notification> notification; | |
68 | |
69 message_center::RichNotificationData rich_notification_data; | |
70 rich_notification_data.context_message = | |
71 base::UTF8ToUTF16(landing_page.GetContent()); | |
72 | |
73 notification.reset(new message_center::Notification( | |
74 message_center::NOTIFICATION_TYPE_SIMPLE, notification_id, | |
75 l10n_util::GetStringFUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION_TITLE, | |
76 base::UTF8ToUTF16(product_name)), | |
77 l10n_util::GetStringUTF16(IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION), | |
78 // TODO(juncai): use generic USB device icon here. | |
79 gfx::Image(), base::string16(), | |
80 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | |
81 kWebUsbDetectorNotificationID), | |
82 rich_notification_data, | |
83 new WebUsbNotificationDelegate(profile, landing_page, notification_id))); | |
84 | |
85 notification->SetSystemPriority(); | |
86 message_center::MessageCenter::Get()->AddNotification(notification.Pass()); | |
87 } | |
88 | |
89 void ChromeWebUsbBrowserClient::OnDeviceRemoved( | |
90 const std::string& notification_id) { | |
91 message_center::MessageCenter* message_center = | |
92 message_center::MessageCenter::Get(); | |
93 if (message_center->FindVisibleNotificationById(notification_id)) { | |
94 message_center->RemoveNotification(notification_id, false /* by_user */); | |
95 } | |
96 } | |
97 | |
98 void ChromeWebUsbBrowserClient::OpenURL(Profile* profile, const GURL& url) { | |
99 Browser* browser = | |
100 chrome::FindBrowserWithProfile(profile, chrome::HOST_DESKTOP_TYPE_NATIVE); | |
101 content::OpenURLParams params(url, content::Referrer(), NEW_FOREGROUND_TAB, | |
102 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, true); | |
103 browser->OpenURL(params); | |
104 } | |
105 | |
106 bool ChromeWebUsbBrowserClient::WebUsbNotificationEnabled() { | |
107 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
108 switches::kEnableWebUsbNotification); | |
109 } | |
OLD | NEW |