Image from icon node-style callback (#2)

* Try implementing node-style callbacks

* Add locker and handle scope
This commit is contained in:
Yury 2016-11-05 21:23:49 +03:00 committed by Kevin Sawicki
parent 2e85ff1f57
commit 1b4ee6e0d8
2 changed files with 16 additions and 3 deletions

View file

@ -474,7 +474,16 @@ int ImportIntoCertStore(
void OnIconDataAvailable(const App::FileIconCallback& callback, void OnIconDataAvailable(const App::FileIconCallback& callback,
gfx::Image* icon) { 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<v8::String> error_message =
v8::String::NewFromUtf8(isolate, "Failed to get file icon.");
callback.Run(v8::Exception::Error(error_message), gfx::Image());
}
} }
} // namespace } // namespace
@ -878,7 +887,10 @@ void App::GetFileIcon(const base::FilePath& path,
IconManager* icon_manager = IconManager::GetInstance(); IconManager* icon_manager = IconManager::GetInstance();
gfx::Image* icon = icon_manager->LookupIconFromFilepath(path, icon_size); gfx::Image* icon = icon_manager->LookupIconFromFilepath(path, icon_size);
if (icon) { 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 { } else {
icon_manager->LoadIcon(path, icon_size, icon_manager->LoadIcon(path, icon_size,
base::Bind(&OnIconDataAvailable, callback)); base::Bind(&OnIconDataAvailable, callback));

View file

@ -12,6 +12,7 @@
#include "atom/browser/atom_browser_client.h" #include "atom/browser/atom_browser_client.h"
#include "atom/browser/browser.h" #include "atom/browser/browser.h"
#include "atom/browser/browser_observer.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 "atom/common/native_mate_converters/callback.h"
#include "chrome/browser/icon_loader.h" #include "chrome/browser/icon_loader.h"
#include "chrome/browser/process_singleton.h" #include "chrome/browser/process_singleton.h"
@ -44,7 +45,7 @@ class App : public AtomBrowserClient::Delegate,
public BrowserObserver, public BrowserObserver,
public content::GpuDataManagerObserver { public content::GpuDataManagerObserver {
public: public:
using FileIconCallback = base::Callback<void(const gfx::Image&)>; using FileIconCallback = base::Callback<void(v8::Local<v8::Value>, const gfx::Image&)>;
static mate::Handle<App> Create(v8::Isolate* isolate); static mate::Handle<App> Create(v8::Isolate* isolate);