From 1b4ee6e0d87c5a881083966510ab62fd9beead58 Mon Sep 17 00:00:00 2001 From: Yury Date: Sat, 5 Nov 2016 21:23:49 +0300 Subject: [PATCH] Image from icon node-style callback (#2) * Try implementing node-style callbacks * Add locker and handle scope --- atom/browser/api/atom_api_app.cc | 16 ++++++++++++++-- atom/browser/api/atom_api_app.h | 3 ++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index cabe751a046f..7358960685d0 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -474,7 +474,16 @@ int ImportIntoCertStore( void OnIconDataAvailable(const App::FileIconCallback& callback, gfx::Image* icon) { - callback.Run(icon ? *icon : gfx::Image()); + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + if (icon && !icon->IsEmpty()) { + v8::Locker locker(isolate); + v8::HandleScope handle_scope(isolate); + callback.Run(v8::Null(isolate), *icon); + } else { + v8::Local error_message = + v8::String::NewFromUtf8(isolate, "Failed to get file icon."); + callback.Run(v8::Exception::Error(error_message), gfx::Image()); + } } } // namespace @@ -878,7 +887,10 @@ void App::GetFileIcon(const base::FilePath& path, IconManager* icon_manager = IconManager::GetInstance(); gfx::Image* icon = icon_manager->LookupIconFromFilepath(path, icon_size); if (icon) { - callback.Run(*icon); + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::Locker locker(isolate); + v8::HandleScope handle_scope(isolate); + callback.Run(v8::Null(isolate), *icon); } else { icon_manager->LoadIcon(path, icon_size, base::Bind(&OnIconDataAvailable, callback)); diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index 7d2f6f752b39..b89761ede5df 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -12,6 +12,7 @@ #include "atom/browser/atom_browser_client.h" #include "atom/browser/browser.h" #include "atom/browser/browser_observer.h" +#include "atom/common/api/atom_api_native_image.h" #include "atom/common/native_mate_converters/callback.h" #include "chrome/browser/icon_loader.h" #include "chrome/browser/process_singleton.h" @@ -44,7 +45,7 @@ class App : public AtomBrowserClient::Delegate, public BrowserObserver, public content::GpuDataManagerObserver { public: - using FileIconCallback = base::Callback; + using FileIconCallback = base::Callback, const gfx::Image&)>; static mate::Handle Create(v8::Isolate* isolate);