refactor: use QuickLookThumbnailing where applicable (#32456)

This commit is contained in:
Shelley Vohr 2022-02-02 23:01:05 +01:00 committed by GitHub
parent c3d11e2ea2
commit 4c39eb32b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 26 deletions

View file

@ -501,6 +501,8 @@ source_set("electron_lib") {
"StoreKit.framework", "StoreKit.framework",
] ]
weak_frameworks = [ "QuickLookThumbnailing.framework" ]
sources += [ sources += [
"shell/browser/ui/views/autofill_popup_view.cc", "shell/browser/ui/views/autofill_popup_view.cc",
"shell/browser/ui/views/autofill_popup_view.h", "shell/browser/ui/views/autofill_popup_view.h",

View file

@ -10,8 +10,10 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import <QuickLook/QuickLook.h> #import <QuickLook/QuickLook.h>
#import <QuickLookThumbnailing/QuickLookThumbnailing.h>
#include "base/mac/foundation_util.h" #include "base/mac/foundation_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "gin/arguments.h" #include "gin/arguments.h"
#include "shell/common/gin_converters/image_converter.h" #include "shell/common/gin_converters/image_converter.h"
@ -53,32 +55,81 @@ v8::Local<v8::Promise> NativeImage::CreateThumbnailFromPath(
} }
CGSize cg_size = size.ToCGSize(); CGSize cg_size = size.ToCGSize();
base::ScopedCFTypeRef<CFURLRef> cfurl = base::mac::FilePathToCFURL(path);
base::ScopedCFTypeRef<QLThumbnailRef> ql_thumbnail( if (@available(macOS 10.15, *)) {
QLThumbnailCreate(kCFAllocatorDefault, cfurl, cg_size, NULL)); NSURL* nsurl = base::mac::FilePathToNSURL(path);
__block gin_helper::Promise<gfx::Image> p = std::move(promise);
// we do not want to blocking the main thread while waiting for quicklook to // We need to explicitly check if the user has passed an invalid path
// generate the thumbnail // because QLThumbnailGenerationRequest will generate a stock file icon
QLThumbnailDispatchAsync( // and pass silently if we do not.
ql_thumbnail, if (![[NSFileManager defaultManager] fileExistsAtPath:[nsurl path]]) {
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, /*flags*/ 0), ^{ promise.RejectWithErrorMessage(
base::ScopedCFTypeRef<CGImageRef> cg_thumbnail( "unable to retrieve thumbnail preview image for the given path");
QLThumbnailCopyImage(ql_thumbnail)); return handle;
if (cg_thumbnail) { }
NSImage* result =
[[[NSImage alloc] initWithCGImage:cg_thumbnail NSScreen* screen = [[NSScreen screens] firstObject];
size:cg_size] autorelease]; base::scoped_nsobject<QLThumbnailGenerationRequest> request(
gfx::Image thumbnail(result); [[QLThumbnailGenerationRequest alloc]
dispatch_async(dispatch_get_main_queue(), ^{ initWithFileAtURL:nsurl
p.Resolve(thumbnail); size:cg_size
}); scale:[screen backingScaleFactor]
} else { representationTypes:
dispatch_async(dispatch_get_main_queue(), ^{ QLThumbnailGenerationRequestRepresentationTypeAll]);
p.RejectWithErrorMessage("unable to retrieve thumbnail preview " __block gin_helper::Promise<gfx::Image> p = std::move(promise);
"image for the given path"); [[QLThumbnailGenerator sharedGenerator]
}); generateBestRepresentationForRequest:request
} completionHandler:^(
}); QLThumbnailRepresentation* thumbnail,
NSError* error) {
if (error || !thumbnail) {
std::string err_msg(
[error.localizedDescription UTF8String]);
dispatch_async(dispatch_get_main_queue(), ^{
p.RejectWithErrorMessage(
"unable to retrieve thumbnail preview "
"image for the given path: " +
err_msg);
});
} else {
NSImage* result = [[[NSImage alloc]
initWithCGImage:[thumbnail CGImage]
size:cg_size] autorelease];
gfx::Image image(result);
dispatch_async(dispatch_get_main_queue(), ^{
p.Resolve(image);
});
}
}];
} else {
base::ScopedCFTypeRef<CFURLRef> cfurl = base::mac::FilePathToCFURL(path);
base::ScopedCFTypeRef<QLThumbnailRef> ql_thumbnail(
QLThumbnailCreate(kCFAllocatorDefault, cfurl, cg_size, NULL));
__block gin_helper::Promise<gfx::Image> p = std::move(promise);
// Do not block the main thread waiting for quicklook to generate the
// thumbnail.
QLThumbnailDispatchAsync(
ql_thumbnail,
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, /*flags*/ 0), ^{
base::ScopedCFTypeRef<CGImageRef> cg_thumbnail(
QLThumbnailCopyImage(ql_thumbnail));
if (cg_thumbnail) {
NSImage* result =
[[[NSImage alloc] initWithCGImage:cg_thumbnail
size:cg_size] autorelease];
gfx::Image thumbnail(result);
dispatch_async(dispatch_get_main_queue(), ^{
p.Resolve(thumbnail);
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
p.RejectWithErrorMessage("unable to retrieve thumbnail preview "
"image for the given path");
});
}
});
}
return handle; return handle;
} }