Skip to content

refactor: remove redundant NativeImage::GetBitmap() - #46696

Merged
codebytere merged 5 commits into
mainfrom
refactor/remove-NativeImage.GetBitmap
Apr 23, 2025
Merged

codebytere merged 5 commits into
mainfrom
refactor/remove-NativeImage.GetBitmap

Conversation

@ckerr

@ckerr ckerr commented Apr 20, 2025 •

Copy link
Copy Markdown
Member

Description of Change

Mark NativeImage.getBitmap() as deprecated and document it as an alias for NativeImage.toBitmap().

The documentation says:

The difference between getBitmap() and toBitmap() is that getBitmap() does not
copy the bitmap data, so you have to use the returned Buffer immediately in
current event loop tick; otherwise the data might be changed or destroyed.

...but this hasn't been true since June 2022 f61be57#diff-87426541166a1d650dedb7f3804023ffeb1718899ab7a83814ad538061546d60 because the old behavior of getBitmap() wasn't sandbox-compatible. For nearly three years, both functions have returned a newly-allocated copy of the bitmap and are functionally equivalent. 😸

So this PR:

  1. Removes the redundant C++ class method NativeImage::GetBitmap()
  2. Maps the JS method NativeImage.getBitmap() to C++ NativeImage::ToBitmap()
  3. Documents NativeImage.getBitmap() as an alias to NativeImage.toBitmap()
  4. Documents NativeImage.getBitmap() as deprecated

Items 1-3 are semver/patch bugfix. Item 4 is arguably semver/minor? Labeling the PR accordingly. If @electron/wg-api wants to keep both methods as non-deprecated for some reason, I'm open to the idea.

Checklist

Release Notes

Notes: Deprecated NativeImage.getBitmap() and fixed incorrect documentation.

@ckerr ckerr added semver/minor backwards-compatible functionality target/36-x-y PR should also be added to the "36-x-y" branch. labels Apr 20, 2025

@felixrieseberg felixrieseberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delightful!

@MarshallOfSound MarshallOfSound left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is deprecating a thing semver/minor? feels like a thing we can just land

@ckerr ckerr added semver/patch backwards-compatible bug fixes and removed semver/minor backwards-compatible functionality labels Apr 20, 2025
@ckerr

ckerr commented Apr 20, 2025

Copy link
Copy Markdown
Member Author

Bumping back down to semver/patch from review feedback 👍

@jkleinsc jkleinsc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs an entry added to docs/breaking-changes.md

@ckerr
ckerr requested a review from a team as a code owner April 21, 2025 15:53
@ckerr

ckerr commented Apr 21, 2025

Copy link
Copy Markdown
Member Author

@jkleinsc added in b14ce6b

@electron-cation

electron-cation Bot commented Apr 21, 2025 •

Copy link
Copy Markdown

🪦 Deprecation Checklist

🔥 New deprecations in this PR


@electron/wg-releases: Please confirm these deprecation changes conform to our deprecation policies listed in docs/breaking-changes.md, then check the applicable items in the checklist and remove any non-applicable items.

@github-project-automation github-project-automation Bot moved this to 👀 Needs Review in Deprecation Board Apr 21, 2025

@dsanders11 dsanders11 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a few usages we should also change to toBitmap() as part of this PR:

expect(image.getBitmap()).to.be.an.instanceOf(Buffer).with.lengthOf(expectedSize);

console.log(dirty, _image.getBitmap());

// updateBitmap(dirty, image.getBitmap())

Here's a suggested way to add the deprecation warnings and satisfy the checklist:

diff --git a/shell/common/api/electron_api_native_image.cc b/shell/common/api/electron_api_native_image.cc
index e991c618a3..d16023390d 100644
--- a/shell/common/api/electron_api_native_image.cc
+++ b/shell/common/api/electron_api_native_image.cc
@@ -286,6 +286,19 @@ std::string NativeImage::ToDataURL(gin::Arguments* args) {
       image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap());
 }
 
+v8::Local<v8::Value> NativeImage::GetBitmap(gin::Arguments* args) {
+  static bool deprecated_warning_issued = false;
+
+  if (!deprecated_warning_issued) {
+    deprecated_warning_issued = true;
+    util::EmitWarning(isolate_,
+                      "getBitmap() is deprecated, use toBitmap() instead.",
+                      "DeprecationWarning");
+  }
+
+  return ToBitmap(args);
+}
+
 v8::Local<v8::Value> NativeImage::GetNativeHandle(
     gin_helper::ErrorThrower thrower) {
 #if BUILDFLAG(IS_MAC)
@@ -582,7 +595,7 @@ gin::ObjectTemplateBuilder NativeImage::GetObjectTemplateBuilder(
       .SetMethod("toPNG", &NativeImage::ToPNG)
       .SetMethod("toJPEG", &NativeImage::ToJPEG)
       .SetMethod("toBitmap", &NativeImage::ToBitmap)
-      .SetMethod("getBitmap", &NativeImage::ToBitmap)
+      .SetMethod("getBitmap", &NativeImage::GetBitmap)
       .SetMethod("getScaleFactors", &NativeImage::GetScaleFactors)
       .SetMethod("getNativeHandle", &NativeImage::GetNativeHandle)
       .SetMethod("toDataURL", &NativeImage::ToDataURL)
diff --git a/shell/common/api/electron_api_native_image.h b/shell/common/api/electron_api_native_image.h
index b09269b5ec..60f4acb1a5 100644
--- a/shell/common/api/electron_api_native_image.h
+++ b/shell/common/api/electron_api_native_image.h
@@ -112,6 +112,7 @@ class NativeImage final : public gin::Wrappable<NativeImage> {
   v8::Local<v8::Value> ToJPEG(v8::Isolate* isolate, int quality);
   v8::Local<v8::Value> ToBitmap(gin::Arguments* args);
   std::vector<float> GetScaleFactors();
+  v8::Local<v8::Value> GetBitmap(gin::Arguments* args);
   v8::Local<v8::Value> GetNativeHandle(gin_helper::ErrorThrower thrower);
   gin::Handle<NativeImage> Resize(gin::Arguments* args,
                                   base::Value::Dict options);
diff --git a/spec/api-native-image-spec.ts b/spec/api-native-image-spec.ts
index bbd30cff7a..72a5ffeaee 100644
--- a/spec/api-native-image-spec.ts
+++ b/spec/api-native-image-spec.ts
@@ -4,6 +4,7 @@ import { expect } from 'chai';
 
 import * as path from 'node:path';
 
+import { expectDeprecationMessages } from './lib/deprecate-helpers';
 import { ifdescribe, ifit, itremote, useRemoteContext } from './lib/spec-helpers';
 
 describe('nativeImage module', () => {
@@ -78,15 +79,20 @@ describe('nativeImage module', () => {
   });
 
   describe('createEmpty()', () => {
-    it('returns an empty image', () => {
+    it('returns an empty image', async () => {
       const empty = nativeImage.createEmpty();
       expect(empty.isEmpty()).to.be.true();
       expect(empty.getAspectRatio()).to.equal(1);
       expect(empty.toDataURL()).to.equal('data:image/png;base64,');
       expect(empty.toDataURL({ scaleFactor: 2.0 })).to.equal('data:image/png;base64,');
       expect(empty.getSize()).to.deep.equal({ width: 0, height: 0 });
-      expect(empty.getBitmap()).to.be.empty();
-      expect(empty.getBitmap({ scaleFactor: 2.0 })).to.be.empty();
+      await expectDeprecationMessages(
+        () => {
+          expect(empty.getBitmap()).to.be.empty();
+          expect(empty.getBitmap({ scaleFactor: 2.0 })).to.be.empty();
+        },
+        'getBitmap() is deprecated, use toBitmap() instead.'
+      );
       expect(empty.toBitmap()).to.be.empty();
       expect(empty.toBitmap({ scaleFactor: 2.0 })).to.be.empty();
       expect(empty.toJPEG(100)).to.be.empty();

@ckerr
ckerr force-pushed the refactor/remove-NativeImage.GetBitmap branch from b14ce6b to e7379a4 Compare April 22, 2025 14:50
@electron-release-board electron-release-board Bot moved this from 👀 Needs Review to ✅ Reviewed in Deprecation Board Apr 22, 2025
@codebytere
codebytere dismissed jkleinsc’s stale review April 23, 2025 09:00

Needs an entry added to docs/breaking-changes.md

Change request was addressed!

@codebytere
codebytere merged commit 686ae47 into main Apr 23, 2025
@codebytere
codebytere deleted the refactor/remove-NativeImage.GetBitmap branch April 23, 2025 09:00
@release-clerk

release-clerk Bot commented Apr 23, 2025

Copy link
Copy Markdown

Release Notes Persisted

Deprecated NativeImage.getBitmap() and fixed incorrect documentation.

@github-project-automation github-project-automation Bot moved this from ✅ Reviewed to 🗑 Complete in Deprecation Board Apr 23, 2025
@trop

trop Bot commented Apr 23, 2025

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "36-x-y", please check out #46736

@trop trop Bot added in-flight/36-x-y merged/36-x-y PR was merged to the "36-x-y" branch. and removed target/36-x-y PR should also be added to the "36-x-y" branch. in-flight/36-x-y labels Apr 23, 2025
kigh-ota pushed a commit to kigh-ota/electron that referenced this pull request Sep 30, 2025
* refactor: remove redundant NativeImage::GetBitmap()

* docs: mark NativeImage.getBitmap() as deprecated

* have getBitmap() emit a deprecation warning

* docs: update obsolete refefence to getBitmap()

* test: update obsolete refefences to getBitmap()

---------

Co-authored-by: David Sanders <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deprecation-review/complete ✅ merged/36-x-y PR was merged to the "36-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

Status: 🗑 Complete

Development

Successfully merging this pull request may close these issues.

6 participants