OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/ui/exclusive_access/mouse_lock_controller.h" | 5 #include "chrome/browser/ui/exclusive_access/mouse_lock_controller.h" |
6 | 6 |
7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 const char kBubbleReshowsHistogramName[] = | 27 const char kBubbleReshowsHistogramName[] = |
28 "ExclusiveAccess.BubbleReshowsPerSession.MouseLock"; | 28 "ExclusiveAccess.BubbleReshowsPerSession.MouseLock"; |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 MouseLockController::MouseLockController(ExclusiveAccessManager* manager) | 32 MouseLockController::MouseLockController(ExclusiveAccessManager* manager) |
33 : ExclusiveAccessControllerBase(manager), | 33 : ExclusiveAccessControllerBase(manager), |
34 mouse_lock_state_(MOUSELOCK_NOT_REQUESTED), | 34 mouse_lock_state_(MOUSELOCK_UNLOCKED), |
35 fake_mouse_lock_for_test_(false) {} | 35 fake_mouse_lock_for_test_(false) {} |
36 | 36 |
37 MouseLockController::~MouseLockController() { | 37 MouseLockController::~MouseLockController() { |
38 } | 38 } |
39 | 39 |
40 bool MouseLockController::IsMouseLocked() const { | 40 bool MouseLockController::IsMouseLocked() const { |
41 return mouse_lock_state_ == MOUSELOCK_ACCEPTED || | 41 return mouse_lock_state_ == MOUSELOCK_LOCKED || |
42 mouse_lock_state_ == MOUSELOCK_ACCEPTED_SILENTLY; | 42 mouse_lock_state_ == MOUSELOCK_LOCKED_SILENTLY; |
43 } | 43 } |
44 | 44 |
45 bool MouseLockController::IsMouseLockSilentlyAccepted() const { | 45 bool MouseLockController::IsMouseLockedSilently() const { |
46 return mouse_lock_state_ == MOUSELOCK_ACCEPTED_SILENTLY; | 46 return mouse_lock_state_ == MOUSELOCK_LOCKED_SILENTLY; |
47 } | 47 } |
48 | 48 |
49 void MouseLockController::RequestToLockMouse(WebContents* web_contents, | 49 void MouseLockController::RequestToLockMouse(WebContents* web_contents, |
50 bool user_gesture, | 50 bool user_gesture, |
51 bool last_unlocked_by_target) { | 51 bool last_unlocked_by_target) { |
52 DCHECK(!IsMouseLocked()); | 52 DCHECK(!IsMouseLocked()); |
53 NotifyMouseLockChange(); | 53 NotifyMouseLockChange(); |
54 | 54 |
55 // Must have a user gesture to prevent misbehaving sites from constantly | 55 // Must have a user gesture to prevent misbehaving sites from constantly |
56 // re-locking the mouse. Exceptions are when the page has unlocked | 56 // re-locking the mouse. Exceptions are when the page has unlocked |
57 // (i.e. not the user), or if we're in tab fullscreen (user gesture required | 57 // (i.e. not the user), or if we're in tab fullscreen (user gesture required |
58 // for that) | 58 // for that) |
59 if (!last_unlocked_by_target && !user_gesture && | 59 if (!last_unlocked_by_target && !user_gesture && |
60 !exclusive_access_manager() | 60 !exclusive_access_manager() |
61 ->fullscreen_controller() | 61 ->fullscreen_controller() |
62 ->IsFullscreenForTabOrPending(web_contents)) { | 62 ->IsFullscreenForTabOrPending(web_contents)) { |
63 web_contents->GotResponseToLockMouseRequest(false); | 63 web_contents->GotResponseToLockMouseRequest(false); |
64 return; | 64 return; |
65 } | 65 } |
66 SetTabWithExclusiveAccess(web_contents); | 66 SetTabWithExclusiveAccess(web_contents); |
67 ExclusiveAccessBubbleType bubble_type = | |
68 exclusive_access_manager()->GetExclusiveAccessExitBubbleType(); | |
69 | 67 |
70 switch (GetMouseLockSetting(web_contents->GetURL())) { | 68 // Lock mouse. |
71 case CONTENT_SETTING_ALLOW: | 69 if (fake_mouse_lock_for_test_ || |
72 // If bubble already displaying buttons we must not lock the mouse yet, | 70 web_contents->GotResponseToLockMouseRequest(true)) { |
73 // or it would prevent pressing those buttons. Instead, merge the request. | 71 if (last_unlocked_by_target) { |
74 if (!exclusive_access_manager() | 72 mouse_lock_state_ = MOUSELOCK_LOCKED_SILENTLY; |
75 ->fullscreen_controller() | 73 } else { |
76 ->IsPrivilegedFullscreenForTab() && | 74 mouse_lock_state_ = MOUSELOCK_LOCKED; |
77 exclusive_access_bubble::ShowButtonsForType(bubble_type)) { | 75 } |
78 mouse_lock_state_ = MOUSELOCK_REQUESTED; | 76 } else { |
79 } else { | 77 SetTabWithExclusiveAccess(nullptr); |
80 // Lock mouse. | 78 mouse_lock_state_ = MOUSELOCK_UNLOCKED; |
81 if (fake_mouse_lock_for_test_ || | |
82 web_contents->GotResponseToLockMouseRequest(true)) { | |
83 if (last_unlocked_by_target) { | |
84 mouse_lock_state_ = MOUSELOCK_ACCEPTED_SILENTLY; | |
85 } else { | |
86 mouse_lock_state_ = MOUSELOCK_ACCEPTED; | |
87 } | |
88 } else { | |
89 SetTabWithExclusiveAccess(nullptr); | |
90 mouse_lock_state_ = MOUSELOCK_NOT_REQUESTED; | |
91 } | |
92 } | |
93 break; | |
94 case CONTENT_SETTING_BLOCK: | |
95 web_contents->GotResponseToLockMouseRequest(false); | |
96 SetTabWithExclusiveAccess(nullptr); | |
97 mouse_lock_state_ = MOUSELOCK_NOT_REQUESTED; | |
98 break; | |
99 case CONTENT_SETTING_ASK: | |
100 mouse_lock_state_ = MOUSELOCK_REQUESTED; | |
101 break; | |
102 default: | |
103 NOTREACHED(); | |
104 } | 79 } |
105 exclusive_access_manager()->UpdateExclusiveAccessExitBubbleContent(); | 80 exclusive_access_manager()->UpdateExclusiveAccessExitBubbleContent(); |
106 } | 81 } |
107 | 82 |
108 void MouseLockController::ExitExclusiveAccessIfNecessary() { | 83 void MouseLockController::ExitExclusiveAccessIfNecessary() { |
109 NotifyTabExclusiveAccessLost(); | 84 NotifyTabExclusiveAccessLost(); |
110 } | 85 } |
111 | 86 |
112 void MouseLockController::NotifyTabExclusiveAccessLost() { | 87 void MouseLockController::NotifyTabExclusiveAccessLost() { |
113 WebContents* tab = exclusive_access_tab(); | 88 WebContents* tab = exclusive_access_tab(); |
114 if (tab) { | 89 if (tab) { |
115 if (IsMouseLockRequested()) { | 90 UnlockMouse(); |
116 tab->GotResponseToLockMouseRequest(false); | |
117 NotifyMouseLockChange(); | |
118 } else { | |
119 UnlockMouse(); | |
120 } | |
121 SetTabWithExclusiveAccess(nullptr); | 91 SetTabWithExclusiveAccess(nullptr); |
122 mouse_lock_state_ = MOUSELOCK_NOT_REQUESTED; | 92 mouse_lock_state_ = MOUSELOCK_UNLOCKED; |
123 exclusive_access_manager()->UpdateExclusiveAccessExitBubbleContent(); | 93 exclusive_access_manager()->UpdateExclusiveAccessExitBubbleContent(); |
124 } | 94 } |
125 } | 95 } |
126 | 96 |
127 void MouseLockController::RecordBubbleReshowsHistogram( | 97 void MouseLockController::RecordBubbleReshowsHistogram( |
128 int bubble_reshow_count) { | 98 int bubble_reshow_count) { |
129 UMA_HISTOGRAM_COUNTS_100(kBubbleReshowsHistogramName, bubble_reshow_count); | 99 UMA_HISTOGRAM_COUNTS_100(kBubbleReshowsHistogramName, bubble_reshow_count); |
130 } | 100 } |
131 | 101 |
132 bool MouseLockController::HandleUserPressedEscape() { | 102 bool MouseLockController::HandleUserPressedEscape() { |
133 if (IsMouseLocked() || IsMouseLockRequested()) { | 103 if (IsMouseLocked()) { |
134 ExitExclusiveAccessIfNecessary(); | 104 ExitExclusiveAccessIfNecessary(); |
135 return true; | 105 return true; |
136 } | 106 } |
137 | 107 |
138 return false; | 108 return false; |
139 } | 109 } |
140 | 110 |
141 void MouseLockController::ExitExclusiveAccessToPreviousState() { | 111 void MouseLockController::ExitExclusiveAccessToPreviousState() { |
142 // Nothing to do for mouse lock. | 112 // Nothing to do for mouse lock. |
143 } | 113 } |
144 | 114 |
145 bool MouseLockController::OnAcceptExclusiveAccessPermission() { | |
146 ExclusiveAccessBubbleType bubble_type = | |
147 exclusive_access_manager()->GetExclusiveAccessExitBubbleType(); | |
148 bool mouse_lock = false; | |
149 exclusive_access_bubble::PermissionRequestedByType(bubble_type, nullptr, | |
150 &mouse_lock); | |
151 DCHECK(!(mouse_lock && IsMouseLocked())); | |
152 | |
153 if (mouse_lock && !IsMouseLocked()) { | |
154 DCHECK(IsMouseLockRequested()); | |
155 | |
156 HostContentSettingsMap* settings_map = | |
157 HostContentSettingsMapFactory::GetForProfile( | |
158 exclusive_access_manager()->context()->GetProfile()); | |
159 | |
160 GURL url = GetExclusiveAccessBubbleURL(); | |
161 | |
162 // TODO(markusheintz): We should allow patterns for all possible URLs here. | |
163 // | |
164 // Do not store preference on file:// URLs, they don't have a clean | |
165 // origin policy. | |
166 // TODO(estark): Revisit this when crbug.com/455882 is fixed. | |
167 if (!url.SchemeIsFile()) { | |
168 settings_map->SetContentSettingDefaultScope( | |
169 url, GURL(), CONTENT_SETTINGS_TYPE_MOUSELOCK, std::string(), | |
170 CONTENT_SETTING_ALLOW); | |
171 } | |
172 | |
173 WebContents* tab = exclusive_access_tab(); | |
174 if (tab && tab->GotResponseToLockMouseRequest(true)) { | |
175 mouse_lock_state_ = MOUSELOCK_ACCEPTED; | |
176 } else { | |
177 mouse_lock_state_ = MOUSELOCK_NOT_REQUESTED; | |
178 SetTabWithExclusiveAccess(nullptr); | |
179 } | |
180 NotifyMouseLockChange(); | |
181 return true; | |
182 } | |
183 | |
184 return false; | |
185 } | |
186 | |
187 bool MouseLockController::OnDenyExclusiveAccessPermission() { | |
188 WebContents* tab = exclusive_access_tab(); | |
189 | |
190 if (tab && IsMouseLockRequested()) { | |
191 mouse_lock_state_ = MOUSELOCK_NOT_REQUESTED; | |
192 tab->GotResponseToLockMouseRequest(false); | |
193 SetTabWithExclusiveAccess(nullptr); | |
194 NotifyMouseLockChange(); | |
195 return true; | |
196 } | |
197 | |
198 return false; | |
199 } | |
200 | |
201 void MouseLockController::LostMouseLock() { | 115 void MouseLockController::LostMouseLock() { |
202 RecordExitingUMA(); | 116 RecordExitingUMA(); |
203 mouse_lock_state_ = MOUSELOCK_NOT_REQUESTED; | 117 mouse_lock_state_ = MOUSELOCK_UNLOCKED; |
204 SetTabWithExclusiveAccess(nullptr); | 118 SetTabWithExclusiveAccess(nullptr); |
205 NotifyMouseLockChange(); | 119 NotifyMouseLockChange(); |
206 exclusive_access_manager()->UpdateExclusiveAccessExitBubbleContent(); | 120 exclusive_access_manager()->UpdateExclusiveAccessExitBubbleContent(); |
207 } | 121 } |
208 | 122 |
209 bool MouseLockController::IsMouseLockRequested() const { | |
210 return mouse_lock_state_ == MOUSELOCK_REQUESTED; | |
211 } | |
212 | |
213 void MouseLockController::NotifyMouseLockChange() { | 123 void MouseLockController::NotifyMouseLockChange() { |
214 content::NotificationService::current()->Notify( | 124 content::NotificationService::current()->Notify( |
215 chrome::NOTIFICATION_MOUSE_LOCK_CHANGED, | 125 chrome::NOTIFICATION_MOUSE_LOCK_CHANGED, |
216 content::Source<MouseLockController>(this), | 126 content::Source<MouseLockController>(this), |
217 content::NotificationService::NoDetails()); | 127 content::NotificationService::NoDetails()); |
218 } | 128 } |
219 | 129 |
220 void MouseLockController::UnlockMouse() { | 130 void MouseLockController::UnlockMouse() { |
221 WebContents* tab = exclusive_access_tab(); | 131 WebContents* tab = exclusive_access_tab(); |
222 | 132 |
(...skipping 11 matching lines...) Expand all Loading... |
234 | 144 |
235 if (!mouse_lock_view) { | 145 if (!mouse_lock_view) { |
236 RenderViewHost* const rvh = exclusive_access_tab()->GetRenderViewHost(); | 146 RenderViewHost* const rvh = exclusive_access_tab()->GetRenderViewHost(); |
237 if (rvh) | 147 if (rvh) |
238 mouse_lock_view = rvh->GetWidget()->GetView(); | 148 mouse_lock_view = rvh->GetWidget()->GetView(); |
239 } | 149 } |
240 | 150 |
241 if (mouse_lock_view) | 151 if (mouse_lock_view) |
242 mouse_lock_view->UnlockMouse(); | 152 mouse_lock_view->UnlockMouse(); |
243 } | 153 } |
244 | |
245 ContentSetting MouseLockController::GetMouseLockSetting(const GURL& url) const { | |
246 // The new policy is to always allow (even if the flag is disabled). We no | |
247 // longer give users control over this at the settings level (since it is very | |
248 // easy to escape mouse lock when it happens). Even if the user has blocked | |
249 // access to this site in the past, we now ignore that setting. | |
250 // TODO(mgiuca): Remove this function and clean up callers | |
251 // (https://crbug.com/610900). | |
252 return CONTENT_SETTING_ALLOW; | |
253 } | |
OLD | NEW |