diff --git a/browser/api/atom_api_dialog.cc b/browser/api/atom_api_dialog.cc index 89b83ce9e2c1..6f4bfdc668d5 100644 --- a/browser/api/atom_api_dialog.cc +++ b/browser/api/atom_api_dialog.cc @@ -33,10 +33,21 @@ v8::Handle ToV8Value(const base::FilePath& path) { return v8::String::New(path_string.data(), path_string.size()); } +v8::Handle ToV8Value(void*) { + return v8::Undefined(); +} + v8::Handle ToV8Value(int code) { return v8::Integer::New(code); } +v8::Handle ToV8Value(const std::vector& paths) { + v8::Handle result = v8::Array::New(paths.size()); + for (size_t i = 0; i < paths.size(); ++i) + result->Set(i, ToV8Value(paths[i])); + return result; +} + template void CallV8Function(v8::Persistent callback, T arg) { DCHECK(!callback.IsEmpty()); @@ -47,6 +58,16 @@ void CallV8Function(v8::Persistent callback, T arg) { callback.Dispose(node_isolate); } +template +void CallV8Function2(v8::Persistent callback, + bool result, + T arg) { + if (result) + return CallV8Function(callback, arg); + else + return CallV8Function(callback, NULL); +} + void Initialize(v8::Handle target) { v8::HandleScope scope; @@ -128,23 +149,41 @@ v8::Handle ShowOpenDialog(const v8::Arguments &args) { native_window = window->window(); } + v8::Persistent callback; + if (args[4]->IsFunction()) { + callback = v8::Persistent::New( + node_isolate, + v8::Local::Cast(args[4])); + } + std::string title(*v8::String::Utf8Value(args[0])); base::FilePath default_path(V8ValueToFilePath(args[1])); int properties = args[2]->IntegerValue(); - std::vector paths; - if (!file_dialog::ShowOpenDialog(native_window, - title, - default_path, - properties, - &paths)) + if (callback.IsEmpty()) { + std::vector paths; + if (!file_dialog::ShowOpenDialog(native_window, + title, + default_path, + properties, + &paths)) + return v8::Undefined(); + + v8::Handle result = v8::Array::New(paths.size()); + for (size_t i = 0; i < paths.size(); ++i) + result->Set(i, ToV8Value(paths[i])); + + return scope.Close(result); + } else { + file_dialog::ShowOpenDialog( + native_window, + title, + default_path, + properties, + base::Bind(&CallV8Function2>, + callback)); return v8::Undefined(); - - v8::Handle result = v8::Array::New(paths.size()); - for (size_t i = 0; i < paths.size(); ++i) - result->Set(i, ToV8Value(paths[i])); - - return scope.Close(result); + } } v8::Handle ShowSaveDialog(const v8::Arguments &args) { diff --git a/browser/api/lib/dialog.coffee b/browser/api/lib/dialog.coffee index 69713ab4c8c8..069858a36279 100644 --- a/browser/api/lib/dialog.coffee +++ b/browser/api/lib/dialog.coffee @@ -28,7 +28,8 @@ module.exports = binding.showOpenDialog String(options.title), String(options.defaultPath), properties, - window + window, + callback showSaveDialog: (window, options) -> throw new TypeError('Invalid window') unless window?.constructor is BrowserWindow