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

Side by Side Diff: chrome/browser/usb/web_usb_detector.cc

Issue 2824923002: Suppress WebUSB notifications when appropriate (Closed)
Patch Set: Use new enums.xml file Created 3 years, 7 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/usb/web_usb_detector.h" 5 #include "chrome/browser/usb/web_usb_detector.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/feature_list.h" 9 #include "base/feature_list.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/metrics/histogram_macros.h" 11 #include "base/metrics/histogram_macros.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/net/referrer.h" 13 #include "chrome/browser/net/referrer.h"
14 #include "chrome/browser/profiles/profile_manager.h" 14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_tab_strip_tracker.h"
18 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" 19 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
20 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/grit/generated_resources.h" 22 #include "chrome/grit/generated_resources.h"
18 #include "chrome/grit/theme_resources.h" 23 #include "chrome/grit/theme_resources.h"
24 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/origin_util.h" 25 #include "content/public/common/origin_util.h"
20 #include "device/base/device_client.h" 26 #include "device/base/device_client.h"
21 #include "device/base/features.h" 27 #include "device/base/features.h"
22 #include "device/usb/usb_device.h" 28 #include "device/usb/usb_device.h"
23 #include "device/usb/usb_ids.h" 29 #include "device/usb/usb_ids.h"
24 #include "ui/base/l10n/l10n_util.h" 30 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/page_transition_types.h" 31 #include "ui/base/page_transition_types.h"
26 #include "ui/base/resource/resource_bundle.h" 32 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/base/window_open_disposition.h" 33 #include "ui/base/window_open_disposition.h"
28 #include "ui/gfx/image/image.h" 34 #include "ui/gfx/image/image.h"
(...skipping 22 matching lines...) Expand all
51 // WEBUSB_NOTIFICATION_CLOSED_MAX. Also remember to update the enum listing in 57 // WEBUSB_NOTIFICATION_CLOSED_MAX. Also remember to update the enum listing in
52 // tools/metrics/histograms/histograms.xml. 58 // tools/metrics/histograms/histograms.xml.
53 enum WebUsbNotificationClosed { 59 enum WebUsbNotificationClosed {
54 // The notification was dismissed but not by the user (either automatically 60 // The notification was dismissed but not by the user (either automatically
55 // or because the device was unplugged). 61 // or because the device was unplugged).
56 WEBUSB_NOTIFICATION_CLOSED, 62 WEBUSB_NOTIFICATION_CLOSED,
57 // The user closed the notification. 63 // The user closed the notification.
58 WEBUSB_NOTIFICATION_CLOSED_BY_USER, 64 WEBUSB_NOTIFICATION_CLOSED_BY_USER,
59 // The user clicked on the notification. 65 // The user clicked on the notification.
60 WEBUSB_NOTIFICATION_CLOSED_CLICKED, 66 WEBUSB_NOTIFICATION_CLOSED_CLICKED,
67 // The user independently navigated to the landing page.
68 WEBUSB_NOTIFICATION_CLOSED_MANUAL_NAVIGATION,
61 // Maximum value for the enum. 69 // Maximum value for the enum.
62 WEBUSB_NOTIFICATION_CLOSED_MAX 70 WEBUSB_NOTIFICATION_CLOSED_MAX
63 }; 71 };
64 72
65 void RecordNotificationClosure(WebUsbNotificationClosed disposition) { 73 void RecordNotificationClosure(WebUsbNotificationClosed disposition) {
66 UMA_HISTOGRAM_ENUMERATION("WebUsb.NotificationClosed", disposition, 74 UMA_HISTOGRAM_ENUMERATION("WebUsb.NotificationClosed", disposition,
67 WEBUSB_NOTIFICATION_CLOSED_MAX); 75 WEBUSB_NOTIFICATION_CLOSED_MAX);
68 } 76 }
69 77
70 Browser* GetBrowser() { 78 Browser* GetBrowser() {
71 chrome::ScopedTabbedBrowserDisplayer browser_displayer( 79 chrome::ScopedTabbedBrowserDisplayer browser_displayer(
72 ProfileManager::GetLastUsedProfileAllowedByPolicy()); 80 ProfileManager::GetLastUsedProfileAllowedByPolicy());
73 DCHECK(browser_displayer.browser()); 81 DCHECK(browser_displayer.browser());
74 return browser_displayer.browser(); 82 return browser_displayer.browser();
75 } 83 }
76 84
85 GURL GetActiveTabURL() {
86 Browser* browser = chrome::FindLastActiveWithProfile(
87 ProfileManager::GetLastUsedProfileAllowedByPolicy());
88 if (!browser)
89 return GURL();
90
91 TabStripModel* tab_strip_model = browser->tab_strip_model();
92 content::WebContents* web_contents =
93 tab_strip_model->GetWebContentsAt(tab_strip_model->active_index());
94 if (!web_contents)
95 return GURL();
96
97 return web_contents->GetURL();
98 }
99
77 void OpenURL(const GURL& url) { 100 void OpenURL(const GURL& url) {
78 GetBrowser()->OpenURL(content::OpenURLParams( 101 GetBrowser()->OpenURL(content::OpenURLParams(
79 url, content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB, 102 url, content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB,
80 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false /* is_renderer_initialized */)); 103 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false /* is_renderer_initialized */));
81 } 104 }
82 105
83 // Delegate for webusb notification 106 // Delegate for webusb notification
84 class WebUsbNotificationDelegate : public message_center::NotificationDelegate { 107 class WebUsbNotificationDelegate : public TabStripModelObserver,
108 public message_center::NotificationDelegate {
85 public: 109 public:
86 WebUsbNotificationDelegate(const GURL& landing_page, 110 WebUsbNotificationDelegate(const GURL& landing_page,
87 const std::string& notification_id) 111 const std::string& notification_id)
88 : landing_page_(landing_page), notification_id_(notification_id) {} 112 : landing_page_(landing_page),
113 notification_id_(notification_id),
114 disposition_(WEBUSB_NOTIFICATION_CLOSED),
115 browser_tab_strip_tracker_(this, nullptr, nullptr) {
116 browser_tab_strip_tracker_.Init();
117 }
118
119 void ActiveTabChanged(content::WebContents* old_contents,
120 content::WebContents* new_contents,
121 int index,
122 int reason) override {
123 if (new_contents->GetURL() == landing_page_) {
124 // If the disposition is not already set, go ahead and set it.
125 if (disposition_ == WEBUSB_NOTIFICATION_CLOSED)
126 disposition_ = WEBUSB_NOTIFICATION_CLOSED_MANUAL_NAVIGATION;
127 message_center::MessageCenter::Get()->RemoveNotification(
128 notification_id_, false /* by_user */);
129 }
130 }
89 131
90 void Click() override { 132 void Click() override {
91 clicked_ = true; 133 disposition_ = WEBUSB_NOTIFICATION_CLOSED_CLICKED;
134
135 // If the URL is already open, activate that tab.
136 content::WebContents* tab_to_activate = nullptr;
137 Browser* browser = nullptr;
138 for (TabContentsIterator it; !it.done(); it.Next()) {
139 if (it->GetVisibleURL() == landing_page_ &&
140 (!tab_to_activate ||
141 it->GetLastActiveTime() > tab_to_activate->GetLastActiveTime())) {
142 tab_to_activate = *it;
143 browser = it.browser();
144 }
145 }
146 if (tab_to_activate) {
147 TabStripModel* tab_strip_model = browser->tab_strip_model();
148 tab_strip_model->ActivateTabAt(
149 tab_strip_model->GetIndexOfWebContents(tab_to_activate), false);
150 browser->window()->Activate();
151 return;
152 }
153
154 // If the URL is not already open, open it in a new tab.
92 OpenURL(landing_page_); 155 OpenURL(landing_page_);
93 message_center::MessageCenter::Get()->RemoveNotification(
94 notification_id_, false /* by_user */);
95 } 156 }
96 157
97 void Close(bool by_user) override { 158 void Close(bool by_user) override {
98 if (clicked_) 159 if (by_user)
99 RecordNotificationClosure(WEBUSB_NOTIFICATION_CLOSED_CLICKED); 160 disposition_ = WEBUSB_NOTIFICATION_CLOSED_BY_USER;
100 else if (by_user) 161 RecordNotificationClosure(disposition_);
101 RecordNotificationClosure(WEBUSB_NOTIFICATION_CLOSED_BY_USER); 162
102 else 163 browser_tab_strip_tracker_.StopObservingAndSendOnBrowserRemoved();
103 RecordNotificationClosure(WEBUSB_NOTIFICATION_CLOSED);
104 } 164 }
105 165
106 private: 166 private:
107 ~WebUsbNotificationDelegate() override = default; 167 ~WebUsbNotificationDelegate() override = default;
108 168
109 GURL landing_page_; 169 GURL landing_page_;
110 std::string notification_id_; 170 std::string notification_id_;
111 bool clicked_ = false; 171 WebUsbNotificationClosed disposition_;
172 BrowserTabStripTracker browser_tab_strip_tracker_;
112 173
113 DISALLOW_COPY_AND_ASSIGN(WebUsbNotificationDelegate); 174 DISALLOW_COPY_AND_ASSIGN(WebUsbNotificationDelegate);
114 }; 175 };
115 176
116 } // namespace 177 } // namespace
117 178
118 WebUsbDetector::WebUsbDetector() : observer_(this) {} 179 WebUsbDetector::WebUsbDetector() : observer_(this) {}
119 180
120 WebUsbDetector::~WebUsbDetector() {} 181 WebUsbDetector::~WebUsbDetector() {}
121 182
(...skipping 10 matching lines...) Expand all
132 device::UsbService* usb_service = 193 device::UsbService* usb_service =
133 device::DeviceClient::Get()->GetUsbService(); 194 device::DeviceClient::Get()->GetUsbService();
134 if (!usb_service) 195 if (!usb_service)
135 return; 196 return;
136 197
137 observer_.Add(usb_service); 198 observer_.Add(usb_service);
138 } 199 }
139 200
140 void WebUsbDetector::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) { 201 void WebUsbDetector::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) {
141 const base::string16& product_name = device->product_string(); 202 const base::string16& product_name = device->product_string();
142 if (product_name.empty()) { 203 if (product_name.empty())
143 return; 204 return;
144 }
145 205
146 const GURL& landing_page = device->webusb_landing_page(); 206 const GURL& landing_page = device->webusb_landing_page();
147 if (!landing_page.is_valid() || !content::IsOriginSecure(landing_page)) { 207 if (!landing_page.is_valid() || !content::IsOriginSecure(landing_page))
148 return; 208 return;
149 } 209
210 if (landing_page == GetActiveTabURL())
211 return;
150 212
151 std::string notification_id = device->guid(); 213 std::string notification_id = device->guid();
152 214
153 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 215 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
154 message_center::RichNotificationData rich_notification_data; 216 message_center::RichNotificationData rich_notification_data;
155 std::unique_ptr<message_center::Notification> notification( 217 std::unique_ptr<message_center::Notification> notification(
156 new message_center::Notification( 218 new message_center::Notification(
157 message_center::NOTIFICATION_TYPE_SIMPLE, notification_id, 219 message_center::NOTIFICATION_TYPE_SIMPLE, notification_id,
158 l10n_util::GetStringFUTF16( 220 l10n_util::GetStringFUTF16(
159 IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION_TITLE, product_name), 221 IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION_TITLE, product_name),
160 l10n_util::GetStringFUTF16( 222 l10n_util::GetStringFUTF16(
161 IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION, 223 IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION,
162 base::UTF8ToUTF16(landing_page.GetContent())), 224 base::UTF8ToUTF16(landing_page.GetContent())),
163 rb.GetNativeImageNamed(IDR_USB_NOTIFICATION_ICON), base::string16(), 225 rb.GetNativeImageNamed(IDR_USB_NOTIFICATION_ICON), base::string16(),
164 GURL(), 226 GURL(),
165 message_center::NotifierId( 227 message_center::NotifierId(
166 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierWebUsb), 228 message_center::NotifierId::SYSTEM_COMPONENT, kNotifierWebUsb),
167 rich_notification_data, 229 rich_notification_data,
168 new WebUsbNotificationDelegate(landing_page, notification_id))); 230 new WebUsbNotificationDelegate(landing_page, notification_id)));
169 231
170 notification->SetSystemPriority(); 232 notification->SetSystemPriority();
171 message_center::MessageCenter::Get()->AddNotification( 233 message_center::MessageCenter::Get()->AddNotification(
172 std::move(notification)); 234 std::move(notification));
173 } 235 }
174 236
175 void WebUsbDetector::OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) { 237 void WebUsbDetector::OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) {
176 std::string notification_id = device->guid(); 238 std::string notification_id = device->guid();
177 message_center::MessageCenter* message_center = 239 message_center::MessageCenter* message_center =
178 message_center::MessageCenter::Get(); 240 message_center::MessageCenter::Get();
179 if (message_center->FindVisibleNotificationById(notification_id)) { 241 if (message_center->FindVisibleNotificationById(notification_id))
180 message_center->RemoveNotification(notification_id, false /* by_user */); 242 message_center->RemoveNotification(notification_id, false /* by_user */);
181 }
182 } 243 }
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/profiles/profile_helper.cc ('k') | chrome/browser/usb/web_usb_detector_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698