diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 1e5fe42d81d6..d202ce94948d 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -294,6 +294,7 @@ It creates a new `BrowserWindow` with native properties as set by the `options`. * `allowRunningInsecureContent` Boolean (optional) - Allow an https page to run JavaScript, CSS or plugins from http URLs. Default is `false`. * `images` Boolean (optional) - Enables image support. Default is `true`. + * `imageAnimationPolicy` String (optional) - Specifies how to run image animations (E.g. GIFs). Can be `animate`, `animateOnce` or `noAnimation`. Default is `animate`. * `textAreasAreResizable` Boolean (optional) - Make TextArea elements resizable. Default is `true`. * `webgl` Boolean (optional) - Enables WebGL support. Default is `true`. diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 98188c837e09..12b0dd74608e 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -1923,6 +1923,20 @@ when the page becomes backgrounded. This also affects the Page Visibility API. Returns `String` - the type of the webContent. Can be `backgroundPage`, `window`, `browserView`, `remote`, `webview` or `offscreen`. +#### `contents.setImageAnimationPolicy(policy)` + +* `policy` String - Can be `animate`, `animateOnce` or `noAnimation`. + +Sets the image animation policy for this webContents. The policy only affects +_new_ images, existing images that are currently being animated are unaffected. +This is a known limitation in Chromium, you can force image animation to be +recalculated with `img.src = img.src` which will result in no network traffic +but will update the animation policy. + +This corresponds to the [animationPolicy][] accessibility feature in Chromium. + +[animationPolicy]: https://developer.chrome.com/docs/extensions/reference/accessibilityFeatures/#property-animationPolicy + ### Instance Properties #### `contents.audioMuted` diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 7471a3a54d7c..e2ff0660d0e7 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -3142,6 +3142,13 @@ void WebContents::NotifyUserActivation() { blink::mojom::UserActivationNotificationType::kInteraction); } +void WebContents::SetImageAnimationPolicy(const std::string& new_policy) { + auto* web_preferences = WebContentsPreferences::From(web_contents()); + web_preferences->preference()->SetKey(options::kImageAnimationPolicy, + base::Value(new_policy)); + web_contents()->OnWebPreferencesChanged(); +} + v8::Local WebContents::TakeHeapSnapshot( v8::Isolate* isolate, const base::FilePath& file_path) { @@ -3689,6 +3696,8 @@ v8::Local WebContents::FillObjectTemplate( &WebContents::GetWebRTCIPHandlingPolicy) .SetMethod("_grantOriginAccess", &WebContents::GrantOriginAccess) .SetMethod("takeHeapSnapshot", &WebContents::TakeHeapSnapshot) + .SetMethod("setImageAnimationPolicy", + &WebContents::SetImageAnimationPolicy) .SetProperty("id", &WebContents::ID) .SetProperty("session", &WebContents::Session) .SetProperty("hostWebContents", &WebContents::HostWebContents) diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index 11b7e9398ee9..743c598b7873 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -419,6 +419,7 @@ class WebContents : public gin::Wrappable, void SetTemporaryZoomLevel(double level); void DoGetZoomLevel( electron::mojom::ElectronBrowser::DoGetZoomLevelCallback callback); + void SetImageAnimationPolicy(const std::string& new_policy); private: // Does not manage lifetime of |web_contents|. diff --git a/shell/browser/web_contents_preferences.cc b/shell/browser/web_contents_preferences.cc index 8ae6a1e4df8f..0a2c3d411de2 100644 --- a/shell/browser/web_contents_preferences.cc +++ b/shell/browser/web_contents_preferences.cc @@ -28,6 +28,7 @@ #include "shell/common/process_util.h" #include "third_party/blink/public/common/web_preferences/web_preferences.h" #include "third_party/blink/public/mojom/v8_cache_options.mojom.h" +#include "third_party/blink/public/mojom/webpreferences/web_preferences.mojom-blink.h" // nogncheck #if defined(OS_WIN) #include "ui/gfx/switches.h" @@ -109,6 +110,26 @@ bool GetAsAutoplayPolicy(const base::Value* val, return false; } +bool GetImageAnimationPolicy(const base::Value* val, + blink::mojom::ImageAnimationPolicy* out) { + std::string policy; + if (GetAsString(val, electron::options::kImageAnimationPolicy, &policy)) { + if (policy == "animate") { + *out = blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyAllowed; + return true; + } else if (policy == "animateOnce") { + *out = + blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyAnimateOnce; + return true; + } else if (policy == "noAnimation") { + *out = + blink::mojom::ImageAnimationPolicy::kImageAnimationPolicyNoAnimation; + return true; + } + } + return false; +} + } // namespace namespace electron { @@ -367,6 +388,7 @@ void WebContentsPreferences::OverrideWebkitPrefs( prefs->javascript_enabled = IsEnabled(options::kJavaScript, true /* default_value */); prefs->images_enabled = IsEnabled(options::kImages, true /* default_value */); + GetImageAnimationPolicy(&preference_, &prefs->animation_policy); prefs->text_areas_are_resizable = IsEnabled(options::kTextAreasAreResizable, true /* default_value */); prefs->navigate_on_drag_drop = diff --git a/shell/common/options_switches.cc b/shell/common/options_switches.cc index cdcef7efe766..1f036963feef 100644 --- a/shell/common/options_switches.cc +++ b/shell/common/options_switches.cc @@ -171,6 +171,9 @@ const char kJavaScript[] = "javascript"; // Enables image support. const char kImages[] = "images"; +// Image animation policy. +const char kImageAnimationPolicy[] = "imageAnimationPolicy"; + // Make TextArea elements resizable. const char kTextAreasAreResizable[] = "textAreasAreResizable"; diff --git a/shell/common/options_switches.h b/shell/common/options_switches.h index d0d7cee1eb3e..e26ad9071297 100644 --- a/shell/common/options_switches.h +++ b/shell/common/options_switches.h @@ -84,6 +84,7 @@ extern const char kNodeIntegrationInSubFrames[]; extern const char kDisableHtmlFullscreenWindowResize[]; extern const char kJavaScript[]; extern const char kImages[]; +extern const char kImageAnimationPolicy[]; extern const char kTextAreasAreResizable[]; extern const char kWebGL[]; extern const char kNavigateOnDragDrop[];