OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "core/html/media/AutoplayPolicy.h" | 5 #include "core/html/media/AutoplayPolicy.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/ElementVisibilityObserver.h" | 8 #include "core/dom/ElementVisibilityObserver.h" |
9 #include "core/frame/ContentSettingsClient.h" | 9 #include "core/frame/ContentSettingsClient.h" |
10 #include "core/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
11 #include "core/frame/Settings.h" | 11 #include "core/frame/Settings.h" |
12 #include "core/html/HTMLMediaElement.h" | 12 #include "core/html/HTMLMediaElement.h" |
13 #include "core/html/media/AutoplayUmaHelper.h" | 13 #include "core/html/media/AutoplayUmaHelper.h" |
14 #include "platform/RuntimeEnabledFeatures.h" | 14 #include "platform/RuntimeEnabledFeatures.h" |
15 #include "platform/UserGestureIndicator.h" | 15 #include "platform/UserGestureIndicator.h" |
16 #include "public/platform/WebMediaPlayer.h" | 16 #include "public/platform/WebMediaPlayer.h" |
17 | 17 |
18 namespace blink { | 18 namespace blink { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 bool IsDocumentCrossOrigin(Document& document) { | 22 bool IsDocumentCrossOrigin(const Document& document) { |
23 const LocalFrame* frame = document.GetFrame(); | 23 const LocalFrame* frame = document.GetFrame(); |
24 return frame && frame->IsCrossOriginSubframe(); | 24 return frame && frame->IsCrossOriginSubframe(); |
25 } | 25 } |
26 | 26 |
27 // Returns whether |document| is whitelisted for autoplay. If true, the user | 27 // Returns whether |document| is whitelisted for autoplay. If true, the user |
28 // gesture lock will be initilized as false, indicating that the element is | 28 // gesture lock will be initilized as false, indicating that the element is |
29 // allowed to autoplay unmuted without user gesture. | 29 // allowed to autoplay unmuted without user gesture. |
30 bool IsDocumentWhitelisted(Document& document) { | 30 bool IsDocumentWhitelisted(const Document& document) { |
31 DCHECK(document.GetSettings()); | 31 DCHECK(document.GetSettings()); |
32 | 32 |
33 const String& whitelist_scope = | 33 const String& whitelist_scope = |
34 document.GetSettings()->GetMediaPlaybackGestureWhitelistScope(); | 34 document.GetSettings()->GetMediaPlaybackGestureWhitelistScope(); |
35 if (whitelist_scope.IsNull() || whitelist_scope.IsEmpty()) | 35 if (whitelist_scope.IsNull() || whitelist_scope.IsEmpty()) |
36 return false; | 36 return false; |
37 | 37 |
38 return document.Url().GetString().StartsWith(whitelist_scope); | 38 return document.Url().GetString().StartsWith(whitelist_scope); |
39 } | 39 } |
40 | 40 |
41 // Return true if and only if the document settings specifies media playback | 41 // Return true if and only if the document settings specifies media playback |
42 // requires user gesture. | 42 // requires user gesture. |
43 bool ComputeLockedPendingUserGesture(Document& document) { | 43 bool ComputeLockedPendingUserGesture(const Document& document) { |
44 if (!document.GetSettings()) | 44 switch (AutoplayPolicy::GetAutoplayPolicyForDocument(document)) { |
45 return false; | 45 case AutoplayPolicy::Type::kNoUserGestureRequired: |
46 | 46 return false; |
47 if (IsDocumentWhitelisted(document)) { | 47 case AutoplayPolicy::Type::kUserGestureRequiredForCrossOrigin: |
48 return false; | 48 return IsDocumentCrossOrigin(document); |
| 49 case AutoplayPolicy::Type::kUserGestureRequired: |
| 50 return true; |
49 } | 51 } |
50 | 52 |
51 if (document.GetSettings() | 53 NOTREACHED(); |
52 ->GetCrossOriginMediaPlaybackRequiresUserGesture() && | 54 return true; |
53 IsDocumentCrossOrigin(document)) { | |
54 return true; | |
55 } | |
56 | |
57 return document.GetSettings()->GetMediaPlaybackRequiresUserGesture(); | |
58 } | 55 } |
59 | 56 |
60 } // anonymous namespace | 57 } // anonymous namespace |
61 | 58 |
| 59 // static |
| 60 AutoplayPolicy::Type AutoplayPolicy::GetAutoplayPolicyForDocument( |
| 61 const Document& document) { |
| 62 if (!document.GetSettings()) |
| 63 return Type::kNoUserGestureRequired; |
| 64 |
| 65 if (IsDocumentWhitelisted(document)) |
| 66 return Type::kNoUserGestureRequired; |
| 67 |
| 68 return document.GetSettings()->GetAutoplayPolicy(); |
| 69 } |
| 70 |
62 AutoplayPolicy::AutoplayPolicy(HTMLMediaElement* element) | 71 AutoplayPolicy::AutoplayPolicy(HTMLMediaElement* element) |
63 : locked_pending_user_gesture_(false), | 72 : locked_pending_user_gesture_(false), |
64 locked_pending_user_gesture_if_cross_origin_experiment_enabled_(true), | 73 locked_pending_user_gesture_if_cross_origin_experiment_enabled_(true), |
65 element_(element), | 74 element_(element), |
66 autoplay_visibility_observer_(nullptr), | 75 autoplay_visibility_observer_(nullptr), |
67 autoplay_uma_helper_(AutoplayUmaHelper::Create(element)) { | 76 autoplay_uma_helper_(AutoplayUmaHelper::Create(element)) { |
68 locked_pending_user_gesture_ = | 77 locked_pending_user_gesture_ = |
69 ComputeLockedPendingUserGesture(element->GetDocument()); | 78 ComputeLockedPendingUserGesture(element->GetDocument()); |
70 locked_pending_user_gesture_if_cross_origin_experiment_enabled_ = | 79 locked_pending_user_gesture_if_cross_origin_experiment_enabled_ = |
71 IsDocumentCrossOrigin(element->GetDocument()); | 80 IsDocumentCrossOrigin(element->GetDocument()); |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 return element_->can_autoplay_ && element_->paused_ && element_->Autoplay(); | 301 return element_->can_autoplay_ && element_->paused_ && element_->Autoplay(); |
293 } | 302 } |
294 | 303 |
295 DEFINE_TRACE(AutoplayPolicy) { | 304 DEFINE_TRACE(AutoplayPolicy) { |
296 visitor->Trace(element_); | 305 visitor->Trace(element_); |
297 visitor->Trace(autoplay_visibility_observer_); | 306 visitor->Trace(autoplay_visibility_observer_); |
298 visitor->Trace(autoplay_uma_helper_); | 307 visitor->Trace(autoplay_uma_helper_); |
299 } | 308 } |
300 | 309 |
301 } // namespace blink | 310 } // namespace blink |
OLD | NEW |