diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index e55afe42d20a..c583689a3bce 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -12,6 +12,7 @@ #include "atom/browser/atom_javascript_dialog_manager.h" #include "atom/browser/browser.h" #include "atom/browser/ui/file_dialog.h" +#include "atom/browser/web_dialog_helper.h" #include "atom/browser/window_list.h" #include "atom/common/api/api_messages.h" #include "atom/common/atom_version.h" @@ -483,6 +484,21 @@ void NativeWindow::BeforeUnloadFired(content::WebContents* tab, } } +void NativeWindow::RunFileChooser(content::WebContents* web_contents, + const content::FileChooserParams& params) { + if (!web_dialog_helper_) + web_dialog_helper_.reset(new WebDialogHelper(this)); + web_dialog_helper_->RunFileChooser(web_contents, params); +} + +void NativeWindow::EnumerateDirectory(content::WebContents* web_contents, + int request_id, + const base::FilePath& path) { + if (!web_dialog_helper_) + web_dialog_helper_.reset(new WebDialogHelper(this)); + web_dialog_helper_->EnumerateDirectory(web_contents, request_id, path); +} + void NativeWindow::RequestToLockMouse(content::WebContents* web_contents, bool user_gesture, bool last_unlocked_by_target) { diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index ffccccc9b2c7..7af5ee9af508 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -51,6 +51,7 @@ namespace atom { class AtomJavaScriptDialogManager; struct DraggableRegion; +class WebDialogHelper; class NativeWindow : public brightray::DefaultWebContentsDelegate, public brightray::InspectableWebContentsDelegate, @@ -229,6 +230,11 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, void BeforeUnloadFired(content::WebContents* tab, bool proceed, bool* proceed_to_fire_unload) override; + void RunFileChooser(content::WebContents* web_contents, + const content::FileChooserParams& params) override; + void EnumerateDirectory(content::WebContents* web_contents, + int request_id, + const base::FilePath& path) override; void RequestToLockMouse(content::WebContents* web_contents, bool user_gesture, bool last_unlocked_by_target) override; @@ -313,6 +319,7 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, base::WeakPtrFactory weak_factory_; + scoped_ptr web_dialog_helper_; scoped_ptr dialog_manager_; // Notice that inspectable_web_contents_ must be placed after dialog_manager_, diff --git a/atom/browser/web_dialog_helper.cc b/atom/browser/web_dialog_helper.cc index d0b53bcd1776..938cd71836a2 100644 --- a/atom/browser/web_dialog_helper.cc +++ b/atom/browser/web_dialog_helper.cc @@ -4,26 +4,43 @@ #include "atom/browser/web_dialog_helper.h" +#include + #include "atom/browser/ui/file_dialog.h" #include "base/bind.h" +#include "base/files/file_enumerator.h" +#include "content/public/browser/render_view_host.h" +#include "content/public/browser/web_contents.h" namespace atom { -WebDialogHelper::WebDialogHelper(content::WebContents* web_contents, - NativeWindow* window) - : web_contents_(web_contents), - window_(window), +WebDialogHelper::WebDialogHelper(NativeWindow* window) + : window_(window), weak_factory_(this) { } WebDialogHelper::~WebDialogHelper() { } -void WebDialogHelper::RunFileChooser(const content::FileChooserParams& params) { +void WebDialogHelper::RunFileChooser(content::WebContents* web_contents, + const content::FileChooserParams& params) { } -void WebDialogHelper::EnumerateDirectory(int request_id, - const base::FilePath& path) { +void WebDialogHelper::EnumerateDirectory(content::WebContents* web_contents, + int request_id, + const base::FilePath& dir) { + int types = base::FileEnumerator::FILES | + base::FileEnumerator::DIRECTORIES | + base::FileEnumerator::INCLUDE_DOT_DOT; + base::FileEnumerator file_enum(dir, false, types); + + base::FilePath path; + std::vector paths; + while (!(path = file_enum.Next()).empty()) + paths.push_back(path); + + web_contents->GetRenderViewHost()->DirectoryEnumerationFinished( + request_id, paths); } } // namespace atom diff --git a/atom/browser/web_dialog_helper.h b/atom/browser/web_dialog_helper.h index 27d32d64b1c3..a3472da4acbb 100644 --- a/atom/browser/web_dialog_helper.h +++ b/atom/browser/web_dialog_helper.h @@ -5,8 +5,6 @@ #ifndef ATOM_BROWSER_WEB_DIALOG_HELPER_H_ #define ATOM_BROWSER_WEB_DIALOG_HELPER_H_ -#include - #include "base/memory/weak_ptr.h" namespace base { @@ -14,8 +12,8 @@ class FilePath; } namespace content { +struct FileChooserParams; class WebContents; -class FileChooserParams; } namespace atom { @@ -24,14 +22,16 @@ class NativeWindow; class WebDialogHelper { public: - WebDialogHelper(content::WebContents* web_contents, NativeWindow* window); + explicit WebDialogHelper(NativeWindow* window); ~WebDialogHelper(); - void RunFileChooser(const content::FileChooserParams& params); - void EnumerateDirectory(int request_id, const base::FilePath& path); + void RunFileChooser(content::WebContents* web_contents, + const content::FileChooserParams& params); + void EnumerateDirectory(content::WebContents* web_contents, + int request_id, + const base::FilePath& path); private: - content::WebContents* web_contents_; NativeWindow* window_; base::WeakPtrFactory weak_factory_;