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

Side by Side Diff: third_party/WebKit/Source/modules/vibration/NavigatorVibration.cpp

Issue 2778693004: Remove navigator.vibrate without user gesture. (Closed)
Patch Set: Add entries in histograms.xml to fix the broken about_flags_unittest. 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 /* 1 /*
2 * Copyright (C) 2012 Samsung Electronics 2 * Copyright (C) 2012 Samsung Electronics
3 * 3 *
4 * This library is free software; you can redistribute it and/or 4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public 5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either 6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version. 7 * version 2 of the License, or (at your option) any later version.
8 * 8 *
9 * This library is distributed in the hope that it will be useful, 9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details. 12 * Library General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU Library General Public License 14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to 15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA. 17 * Boston, MA 02110-1301, USA.
18 */ 18 */
19 19
20 #include "modules/vibration/NavigatorVibration.h" 20 #include "modules/vibration/NavigatorVibration.h"
21 21
22 #include "core/dom/Document.h" 22 #include "core/dom/Document.h"
23 #include "core/frame/FrameConsole.h"
23 #include "core/frame/LocalDOMWindow.h" 24 #include "core/frame/LocalDOMWindow.h"
24 #include "core/frame/LocalFrame.h" 25 #include "core/frame/LocalFrame.h"
25 #include "core/frame/Navigator.h" 26 #include "core/frame/Navigator.h"
26 #include "core/frame/UseCounter.h" 27 #include "core/frame/UseCounter.h"
28 #include "core/inspector/ConsoleMessage.h"
27 #include "core/page/Page.h" 29 #include "core/page/Page.h"
28 #include "modules/vibration/VibrationController.h" 30 #include "modules/vibration/VibrationController.h"
29 #include "platform/Histogram.h" 31 #include "platform/Histogram.h"
30 #include "platform/UserGestureIndicator.h" 32 #include "platform/UserGestureIndicator.h"
31 #include "public/platform/site_engagement.mojom-blink.h" 33 #include "public/platform/site_engagement.mojom-blink.h"
32 34
33 namespace blink { 35 namespace blink {
34 36
35 NavigatorVibration::NavigatorVibration(Navigator& navigator) 37 NavigatorVibration::NavigatorVibration(Navigator& navigator)
36 : ContextLifecycleObserver(navigator.frame()->document()) {} 38 : ContextLifecycleObserver(navigator.frame()->document()) {}
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 if (!frame) 73 if (!frame)
72 return false; 74 return false;
73 collectHistogramMetrics(*frame); 75 collectHistogramMetrics(*frame);
74 76
75 DCHECK(frame->document()); 77 DCHECK(frame->document());
76 DCHECK(frame->page()); 78 DCHECK(frame->page());
77 79
78 if (!frame->page()->isPageVisible()) 80 if (!frame->page()->isPageVisible())
79 return false; 81 return false;
80 82
83 if (!frame->hasReceivedUserGesture()) {
84 if (frame->isCrossOriginSubframe()) {
85 frame->domWindow()->printErrorMessage(
Rick Byers 2017/03/29 18:01:40 Can you please have all these messages use Interve
binlu 2017/04/11 21:54:25 Done.
86 "Blocked call to navigator.vibrate inside a cross-origin iframe "
87 "because the frame has never been activated by the user: "
88 "https://www.chromestatus.com/feature/5682658461876224.");
89 return false;
90 }
91 // For other frames (except x-origin iframe) including top-level page.
92 if (!RuntimeEnabledFeatures::vibrateRequiresUserGestureEnabled()) {
93 // Just print a warning.
94 frame->domWindow()->frameConsole()->addMessage(ConsoleMessage::create(
95 JSMessageSource, WarningMessageLevel,
96 "A call to navigator.vibrate will soon require user tap on "
97 "the frame or any embedded frame: "
98 "https://www.chromestatus.com/feature/5644273861001216."));
99 } else {
100 frame->domWindow()->printErrorMessage(
101 "Blocked call to navigator.vibrate because user hasn't tapped on "
102 "the frame or any embedded frame yet: "
103 "https://www.chromestatus.com/feature/5644273861001216.");
104 return false;
105 }
106 }
107
81 // TODO(lunalu): When FeaturePolicy is ready, take out the check for the 108 // TODO(lunalu): When FeaturePolicy is ready, take out the check for the
82 // runtime flag. Please pay attention to the user gesture code below. 109 // runtime flag. Please pay attention to the user gesture code above.
83 if (RuntimeEnabledFeatures::featurePolicyEnabled() && 110 if (RuntimeEnabledFeatures::featurePolicyEnabled() &&
84 !frame->isFeatureEnabled(blink::WebFeaturePolicyFeature::Vibrate)) { 111 !frame->isFeatureEnabled(blink::WebFeaturePolicyFeature::Vibrate)) {
85 frame->domWindow()->printErrorMessage( 112 frame->domWindow()->printErrorMessage(
86 "Navigator.vibrate() is not enabled in feature policy for this " 113 "Navigator.vibrate() is not enabled in feature policy for this "
87 "frame."); 114 "frame.");
88 return false; 115 return false;
89 } 116 }
90 117
91 if (!RuntimeEnabledFeatures::featurePolicyEnabled() &&
92 frame->isCrossOriginSubframe() && !frame->hasReceivedUserGesture()) {
93 frame->domWindow()->printErrorMessage(
94 "Blocked call to navigator.vibrate inside a cross-origin iframe "
95 "because the frame has never been activated by the user: "
96 "https://www.chromestatus.com/feature/5682658461876224.");
97 return false;
98 }
99
100 return NavigatorVibration::from(navigator).controller(*frame)->vibrate( 118 return NavigatorVibration::from(navigator).controller(*frame)->vibrate(
101 pattern); 119 pattern);
102 } 120 }
103 121
104 // static 122 // static
105 void NavigatorVibration::collectHistogramMetrics(const LocalFrame& frame) { 123 void NavigatorVibration::collectHistogramMetrics(const LocalFrame& frame) {
106 NavigatorVibrationType type; 124 NavigatorVibrationType type;
107 bool userGesture = UserGestureIndicator::processingUserGesture(); 125 bool userGesture = frame.hasReceivedUserGesture();
108 UseCounter::count(&frame, UseCounter::NavigatorVibrate); 126 UseCounter::count(&frame, UseCounter::NavigatorVibrate);
109 if (!frame.isMainFrame()) { 127 if (!frame.isMainFrame()) {
110 UseCounter::count(&frame, UseCounter::NavigatorVibrateSubFrame); 128 UseCounter::count(&frame, UseCounter::NavigatorVibrateSubFrame);
111 if (frame.isCrossOriginSubframe()) { 129 if (frame.isCrossOriginSubframe()) {
112 if (userGesture) 130 if (userGesture)
113 type = NavigatorVibrationType::CrossOriginSubFrameWithUserGesture; 131 type = NavigatorVibrationType::CrossOriginSubFrameWithUserGesture;
114 else 132 else
115 type = NavigatorVibrationType::CrossOriginSubFrameNoUserGesture; 133 type = NavigatorVibrationType::CrossOriginSubFrameNoUserGesture;
116 } else { 134 } else {
117 if (userGesture) 135 if (userGesture)
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 } 183 }
166 } 184 }
167 185
168 DEFINE_TRACE(NavigatorVibration) { 186 DEFINE_TRACE(NavigatorVibration) {
169 visitor->trace(m_controller); 187 visitor->trace(m_controller);
170 Supplement<Navigator>::trace(visitor); 188 Supplement<Navigator>::trace(visitor);
171 ContextLifecycleObserver::trace(visitor); 189 ContextLifecycleObserver::trace(visitor);
172 } 190 }
173 191
174 } // namespace blink 192 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698