Browse Source

feat: allow setting autoplayPolicy in webPreferences (#16396)

Jeremy Apthorp 6 years ago
parent
commit
6d4480ea6b
2 changed files with 32 additions and 1 deletions
  1. 27 1
      atom/browser/web_contents_preferences.cc
  2. 5 0
      docs/api/browser-window.md

+ 27 - 1
atom/browser/web_contents_preferences.cc

@@ -73,6 +73,29 @@ bool GetAsInteger(const base::Value* val,
   return false;
 }
 
+bool GetAsAutoplayPolicy(const base::Value* val,
+                         const base::StringPiece& path,
+                         content::AutoplayPolicy* out) {
+  std::string policy_str;
+  if (GetAsString(val, path, &policy_str)) {
+    if (policy_str == "no-user-gesture-required") {
+      *out = content::AutoplayPolicy::kNoUserGestureRequired;
+      return true;
+    } else if (policy_str == "user-gesture-required") {
+      *out = content::AutoplayPolicy::kUserGestureRequired;
+      return true;
+    } else if (policy_str == "user-gesture-required-for-cross-origin") {
+      *out = content::AutoplayPolicy::kUserGestureRequiredForCrossOrigin;
+      return true;
+    } else if (policy_str == "document-user-activation-required") {
+      *out = content::AutoplayPolicy::kDocumentUserActivationRequired;
+      return true;
+    }
+    return false;
+  }
+  return false;
+}
+
 }  // namespace
 
 namespace atom {
@@ -360,7 +383,10 @@ void WebContentsPreferences::OverrideWebkitPrefs(
       IsEnabled("textAreasAreResizable", true /* default_value */);
   prefs->navigate_on_drag_drop =
       IsEnabled("navigateOnDragDrop", false /* default_value */);
-  prefs->autoplay_policy = content::AutoplayPolicy::kNoUserGestureRequired;
+  if (!GetAsAutoplayPolicy(&preference_, "autoplayPolicy",
+                           &prefs->autoplay_policy)) {
+    prefs->autoplay_policy = content::AutoplayPolicy::kNoUserGestureRequired;
+  }
 
   // Check if webgl should be enabled.
   bool is_webgl_enabled = IsEnabled("webgl", true /* default_value */);

+ 5 - 0
docs/api/browser-window.md

@@ -371,6 +371,11 @@ It creates a new `BrowserWindow` with native properties as set by the `options`.
       English and not localized.
     * `navigateOnDragDrop` Boolean (optional) - Whether dragging and dropping a
       file or link onto the page causes a navigation. Default is `false`.
+    * `autoplayPolicy` String (optional) - Autoplay policy to apply to
+      content in the window, can be `no-user-gesture-required`,
+      `user-gesture-required`, `user-gesture-required-for-cross-origin`,
+      `document-user-activation-required`. Defaults to
+      `no-user-gesture-required`.
 
 When setting minimum or maximum window size with `minWidth`/`maxWidth`/
 `minHeight`/`maxHeight`, it only constrains the users. It won't prevent you from