refactor: eliminate duplicate code (#29174)

This commit is contained in:
Milan Burda 2021-05-24 04:32:55 +02:00 committed by GitHub
parent 6330f8be9f
commit 241cceb2c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 66 additions and 151 deletions

View file

@ -109,13 +109,7 @@ base::win::ScopedHICON ReadICOFromPath(int size, const base::FilePath& path) {
NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image)
: image_(image), isolate_(isolate) {
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
auto* const image_skia = image_.ToImageSkia();
if (!image_skia->isNull()) {
isolate_->AdjustAmountOfExternalAllocatedMemory(
image_skia->bitmap()->computeByteSize());
}
}
AdjustAmountOfExternalAllocatedMemory(true);
}
#if defined(OS_WIN)
@ -125,21 +119,21 @@ NativeImage::NativeImage(v8::Isolate* isolate, const base::FilePath& hicon_path)
gfx::ImageSkia image_skia;
electron::util::ReadImageSkiaFromICO(&image_skia, GetHICON(256));
image_ = gfx::Image(image_skia);
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
if (!image_skia.isNull()) {
isolate_->AdjustAmountOfExternalAllocatedMemory(
image_.ToImageSkia()->bitmap()->computeByteSize());
}
}
AdjustAmountOfExternalAllocatedMemory(true);
}
#endif
NativeImage::~NativeImage() {
AdjustAmountOfExternalAllocatedMemory(false);
}
void NativeImage::AdjustAmountOfExternalAllocatedMemory(bool add) {
if (image_.HasRepresentation(gfx::Image::kImageRepSkia)) {
auto* const image_skia = image_.ToImageSkia();
if (!image_skia->isNull()) {
isolate_->AdjustAmountOfExternalAllocatedMemory(
-static_cast<int64_t>(image_skia->bitmap()->computeByteSize()));
int64_t size = image_skia->bitmap()->computeByteSize();
isolate_->AdjustAmountOfExternalAllocatedMemory(add ? size : -size);
}
}
}