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

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

Issue 2824923002: Suppress WebUSB notifications when appropriate (Closed)
Patch Set: Assert clicking removes notification Created 3 years, 8 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,
Reilly Grant (use Gerrit) 2017/04/27 20:25:43 Update tools/metrics/histograms/histograms.xml to
cco3 2017/04/27 21:31:16 Done. Ah, forgot to do this.
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 }
Reilly Grant (use Gerrit) 2017/04/27 20:25:43 nit: No braces around single-line ifs.
cco3 2017/04/27 21:31:16 Done.
91
92 TabStripModel* tab_strip_model = browser->tab_strip_model();
93 content::WebContents* web_contents =
94 tab_strip_model->GetWebContentsAt(tab_strip_model->active_index());
95 if (!web_contents) {
96 return GURL();
97 }
Reilly Grant (use Gerrit) 2017/04/27 20:25:43 nit: No braces around single-line ifs.
cco3 2017/04/27 21:31:16 Done.
98
99 return web_contents->GetURL();
100 }
101
77 void OpenURL(const GURL& url) { 102 void OpenURL(const GURL& url) {
78 GetBrowser()->OpenURL(content::OpenURLParams( 103 GetBrowser()->OpenURL(content::OpenURLParams(
79 url, content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB, 104 url, content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB,
80 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false /* is_renderer_initialized */)); 105 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false /* is_renderer_initialized */));
81 } 106 }
82 107
83 // Delegate for webusb notification 108 // Delegate for webusb notification
84 class WebUsbNotificationDelegate : public message_center::NotificationDelegate { 109 class WebUsbNotificationDelegate : public TabStripModelObserver,
110 public message_center::NotificationDelegate {
85 public: 111 public:
86 WebUsbNotificationDelegate(const GURL& landing_page, 112 WebUsbNotificationDelegate(const GURL& landing_page,
87 const std::string& notification_id) 113 const std::string& notification_id)
88 : landing_page_(landing_page), notification_id_(notification_id) {} 114 : landing_page_(landing_page),
115 notification_id_(notification_id),
116 disposition_(WEBUSB_NOTIFICATION_CLOSED),
117 browser_tab_strip_tracker_(this, nullptr, nullptr) {
118 browser_tab_strip_tracker_.Init();
119 }
120
121 void ActiveTabChanged(content::WebContents* old_contents,
122 content::WebContents* new_contents,
123 int index,
124 int reason) override {
125 if (new_contents->GetURL() == landing_page_) {
126 // If the disposition is not already set, go ahead and set it.
127 if (disposition_ == WEBUSB_NOTIFICATION_CLOSED) {
128 disposition_ = WEBUSB_NOTIFICATION_CLOSED_MANUAL_NAVIGATION;
129 }
Reilly Grant (use Gerrit) 2017/04/27 20:25:43 nit: No braces around single-line ifs.
130 message_center::MessageCenter::Get()->RemoveNotification(
131 notification_id_, false /* by_user */);
132 }
133 }
89 134
90 void Click() override { 135 void Click() override {
91 clicked_ = true; 136 disposition_ = WEBUSB_NOTIFICATION_CLOSED_CLICKED;
137
138 // If the URL is already open, activate that tab.
139 content::WebContents* tab_to_activate = nullptr;
140 Browser* browser = nullptr;
141 for (TabContentsIterator it; !it.done(); it.Next()) {
142 if (it->GetVisibleURL() == landing_page_ &&
143 (!tab_to_activate ||
144 it->GetLastActiveTime() > tab_to_activate->GetLastActiveTime())) {
145 tab_to_activate = *it;
146 browser = it.browser();
147 }
148 }
149 if (tab_to_activate) {
150 TabStripModel* tab_strip_model = browser->tab_strip_model();
151 tab_strip_model->ActivateTabAt(
152 tab_strip_model->GetIndexOfWebContents(tab_to_activate), false);
153 browser->window()->Activate();
154 return;
155 }
156
157 // If the URL is not already open, open it in a new tab.
92 OpenURL(landing_page_); 158 OpenURL(landing_page_);
93 message_center::MessageCenter::Get()->RemoveNotification(
94 notification_id_, false /* by_user */);
95 } 159 }
96 160
97 void Close(bool by_user) override { 161 void Close(bool by_user) override {
98 if (clicked_) 162 if (by_user) {
99 RecordNotificationClosure(WEBUSB_NOTIFICATION_CLOSED_CLICKED); 163 disposition_ = WEBUSB_NOTIFICATION_CLOSED_BY_USER;
100 else if (by_user) 164 }
Reilly Grant (use Gerrit) 2017/04/27 20:25:43 nit: No braces around single-line ifs.
cco3 2017/04/27 21:31:16 Done.
101 RecordNotificationClosure(WEBUSB_NOTIFICATION_CLOSED_BY_USER); 165 RecordNotificationClosure(disposition_);
102 else 166
103 RecordNotificationClosure(WEBUSB_NOTIFICATION_CLOSED); 167 browser_tab_strip_tracker_.StopObservingAndSendOnBrowserRemoved();
104 } 168 }
105 169
106 private: 170 private:
107 ~WebUsbNotificationDelegate() override = default; 171 ~WebUsbNotificationDelegate() override = default;
108 172
109 GURL landing_page_; 173 GURL landing_page_;
110 std::string notification_id_; 174 std::string notification_id_;
111 bool clicked_ = false; 175 WebUsbNotificationClosed disposition_;
176 BrowserTabStripTracker browser_tab_strip_tracker_;
112 177
113 DISALLOW_COPY_AND_ASSIGN(WebUsbNotificationDelegate); 178 DISALLOW_COPY_AND_ASSIGN(WebUsbNotificationDelegate);
114 }; 179 };
115 180
116 } // namespace 181 } // namespace
117 182
118 WebUsbDetector::WebUsbDetector() : observer_(this) {} 183 WebUsbDetector::WebUsbDetector() : observer_(this) {}
119 184
120 WebUsbDetector::~WebUsbDetector() {} 185 WebUsbDetector::~WebUsbDetector() {}
121 186
(...skipping 19 matching lines...) Expand all
141 const base::string16& product_name = device->product_string(); 206 const base::string16& product_name = device->product_string();
142 if (product_name.empty()) { 207 if (product_name.empty()) {
143 return; 208 return;
144 } 209 }
145 210
146 const GURL& landing_page = device->webusb_landing_page(); 211 const GURL& landing_page = device->webusb_landing_page();
147 if (!landing_page.is_valid() || !content::IsOriginSecure(landing_page)) { 212 if (!landing_page.is_valid() || !content::IsOriginSecure(landing_page)) {
148 return; 213 return;
149 } 214 }
150 215
216 if (landing_page == GetActiveTabURL()) {
217 return;
218 }
Reilly Grant (use Gerrit) 2017/04/27 20:25:43 nit: No braces around single-line ifs.
cco3 2017/04/27 21:31:16 Done.
219
151 std::string notification_id = device->guid(); 220 std::string notification_id = device->guid();
152 221
153 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 222 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
154 message_center::RichNotificationData rich_notification_data; 223 message_center::RichNotificationData rich_notification_data;
155 std::unique_ptr<message_center::Notification> notification( 224 std::unique_ptr<message_center::Notification> notification(
156 new message_center::Notification( 225 new message_center::Notification(
157 message_center::NOTIFICATION_TYPE_SIMPLE, notification_id, 226 message_center::NOTIFICATION_TYPE_SIMPLE, notification_id,
158 l10n_util::GetStringFUTF16( 227 l10n_util::GetStringFUTF16(
159 IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION_TITLE, product_name), 228 IDS_WEBUSB_DEVICE_DETECTED_NOTIFICATION_TITLE, product_name),
160 l10n_util::GetStringFUTF16( 229 l10n_util::GetStringFUTF16(
(...skipping 12 matching lines...) Expand all
173 } 242 }
174 243
175 void WebUsbDetector::OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) { 244 void WebUsbDetector::OnDeviceRemoved(scoped_refptr<device::UsbDevice> device) {
176 std::string notification_id = device->guid(); 245 std::string notification_id = device->guid();
177 message_center::MessageCenter* message_center = 246 message_center::MessageCenter* message_center =
178 message_center::MessageCenter::Get(); 247 message_center::MessageCenter::Get();
179 if (message_center->FindVisibleNotificationById(notification_id)) { 248 if (message_center->FindVisibleNotificationById(notification_id)) {
180 message_center->RemoveNotification(notification_id, false /* by_user */); 249 message_center->RemoveNotification(notification_id, false /* by_user */);
181 } 250 }
182 } 251 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/usb/web_usb_detector_unittest.cc » ('j') | chrome/browser/usb/web_usb_detector_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698