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

Side by Side Diff: chrome/browser/chrome_webusb_browser_client.cc

Issue 2146663002: Move //components/webusb/ to //chrome/browser/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merged web_usb_browser_client.* to web_usb_detector.* Created 4 years, 5 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 "chrome/browser/chrome_webusb_browser_client.h"
6
7 #include <utility>
8
9 #include "ash/common/system/system_notifier.h"
10 #include "base/macros.h"
11 #include "base/metrics/histogram_macros.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/net/referrer.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "chrome/grit/theme_resources.h"
19 #include "content/public/common/origin_util.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/page_transition_types.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/base/window_open_disposition.h"
24 #include "ui/gfx/image/image.h"
25 #include "ui/message_center/message_center.h"
26 #include "ui/message_center/notification.h"
27 #include "ui/message_center/notification_delegate.h"
28 #include "url/gurl.h"
29
30 namespace {
31
32 // The WebUSB notification should be displayed for all profiles. On ChromeOS
33 // that requires its notifier ID to be known by Ash so that it is not blocked in
34 // multi-profile mode.
35 #if defined(OS_CHROMEOS)
36 #define kNotifierWebUsb ash::system_notifier::kNotifierWebUsb
37 #else
38 const char kNotifierWebUsb[] = "webusb.connected";
39 #endif
40
41 // Reasons the notification may be closed. These are used in histograms so do
42 // not remove/reorder entries. Only add at the end just before
43 // WEBUSB_NOTIFICATION_CLOSED_MAX. Also remember to update the enum listing in
44 // tools/metrics/histograms/histograms.xml.
45 enum WebUsbNotificationClosed {
46 // The notification was dismissed but not by the user (either automatically
47 // or because the device was unplugged).
48 WEBUSB_NOTIFICATION_CLOSED,
49 // The user closed the notification.
50 WEBUSB_NOTIFICATION_CLOSED_BY_USER,
51 // The user clicked on the notification.
52 WEBUSB_NOTIFICATION_CLOSED_CLICKED,
53 // Maximum value for the enum.
54 WEBUSB_NOTIFICATION_CLOSED_MAX
55 };
56
57 void RecordNotificationClosure(WebUsbNotificationClosed disposition) {
58 UMA_HISTOGRAM_ENUMERATION("WebUsb.NotificationClosed", disposition,
59 WEBUSB_NOTIFICATION_CLOSED_MAX);
60 }
61
62 Browser* GetBrowser() {
63 chrome::ScopedTabbedBrowserDisplayer browser_displayer(
64 ProfileManager::GetActiveUserProfile());
65 DCHECK(browser_displayer.browser());
66 return browser_displayer.browser();
67 }
68
69 void OpenURL(const GURL& url) {
70 GetBrowser()->OpenURL(content::OpenURLParams(
71 url, content::Referrer(), NEW_FOREGROUND_TAB,
72 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false /* is_renderer_initialized */));
73 }
74
75 // Delegate for webusb notification
76 class WebUsbNotificationDelegate : public message_center::NotificationDelegate {
77 public:
78 WebUsbNotificationDelegate(const GURL& landing_page,
79 const std::string& notification_id)
80 : landing_page_(landing_page), notification_id_(notification_id) {}
81
82 void Click() override {
83 clicked_ = true;
84 OpenURL(landing_page_);
85 message_center::MessageCenter::Get()->RemoveNotification(
86 notification_id_, false /* by_user */);
87 }
88
89 void Close(bool by_user) override {
90 if (clicked_)
91 RecordNotificationClosure(WEBUSB_NOTIFICATION_CLOSED_CLICKED);
92 else if (by_user)
93 RecordNotificationClosure(WEBUSB_NOTIFICATION_CLOSED_BY_USER);
94 else
95 RecordNotificationClosure(WEBUSB_NOTIFICATION_CLOSED);
96 }
97
98 private:
99 ~WebUsbNotificationDelegate() override = default;
100
101 GURL landing_page_;
102 std::string notification_id_;
103 bool clicked_ = false;
104
105 DISALLOW_COPY_AND_ASSIGN(WebUsbNotificationDelegate);
106 };
107
108 } // namespace
109
110 ChromeWebUsbBrowserClient::ChromeWebUsbBrowserClient() {}
111
112 ChromeWebUsbBrowserClient::~ChromeWebUsbBrowserClient() {}
113
114 void ChromeWebUsbBrowserClient::OnDeviceAdded(
115 const base::string16& product_name,
116 const GURL& landing_page,
117 const std::string& notification_id) {
118 if (!content::IsOriginSecure(landing_page)) {
119 return;
120 }
121
122 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
123 message_center::RichNotificationData rich_notification_data;
124 std::unique_ptr<message_center::Notification> notification(
125 new message_center::Notification(
126 message_center::NOTIFICATION_TYPE_SIMPLE, notification_id,
127 l10n_util::GetStringFUTF16(
128 IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION_TITLE, product_name),
129 l10n_util::GetStringFUTF16(
130 IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION,
131 base::UTF8ToUTF16(landing_page.GetContent())),
132 rb.GetNativeImageNamed(IDR_USB_NOTIFICATION_ICON), base::string16(),
133 GURL(),
134 message_center::NotifierId(
135 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierWebUsb),
136 rich_notification_data,
137 new WebUsbNotificationDelegate(landing_page, notification_id)));
138
139 notification->SetSystemPriority();
140 message_center::MessageCenter::Get()->AddNotification(
141 std::move(notification));
142 }
143
144 void ChromeWebUsbBrowserClient::OnDeviceRemoved(
145 const std::string& notification_id) {
146 message_center::MessageCenter* message_center =
147 message_center::MessageCenter::Get();
148 if (message_center->FindVisibleNotificationById(notification_id)) {
149 message_center->RemoveNotification(notification_id, false /* by_user */);
150 }
151 }
OLDNEW
« no previous file with comments | « chrome/browser/chrome_webusb_browser_client.h ('k') | chrome/browser/chrome_webusb_browser_client_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698