]> khamidou — GitHub mirrors - electron.git/commitdiff
Implement EnumerateDirectory
authorCheng Zhao <zcbenz@gmail.com>
Fri, 31 Oct 2014 09:37:32 +0000 (17:37 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 31 Oct 2014 09:37:32 +0000 (17:37 +0800)
atom/browser/native_window.cc
atom/browser/native_window.h
atom/browser/web_dialog_helper.cc
atom/browser/web_dialog_helper.h

index e55afe42d20a6c5cdcdb96aa30b91b86d9b8fde6..c583689a3bce626aa8bb117c21718687b72dd492 100644 (file)
@@ -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) {
index ffccccc9b2c7a9edb294f962c4a01a2d8507ef57..7af5ee9af508f1e303a1d2503b71dbb140a2e69e 100644 (file)
@@ -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<NativeWindow> weak_factory_;
 
+  scoped_ptr<WebDialogHelper> web_dialog_helper_;
   scoped_ptr<AtomJavaScriptDialogManager> dialog_manager_;
 
   // Notice that inspectable_web_contents_ must be placed after dialog_manager_,
index d0b53bcd177681c9371b848d5cd288b8a05113df..938cd71836a20dc9e7db4b6a4e92dccf24651f24 100644 (file)
@@ -4,26 +4,43 @@
 
 #include "atom/browser/web_dialog_helper.h"
 
+#include <vector>
+
 #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<base::FilePath> paths;
+  while (!(path = file_enum.Next()).empty())
+    paths.push_back(path);
+
+  web_contents->GetRenderViewHost()->DirectoryEnumerationFinished(
+      request_id, paths);
 }
 
 }  // namespace atom
index 27d32d64b1c3448595183efe623c8a2b04a3afa9..a3472da4acbb936659f05670214fad62a98f5aa5 100644 (file)
@@ -5,8 +5,6 @@
 #ifndef ATOM_BROWSER_WEB_DIALOG_HELPER_H_
 #define ATOM_BROWSER_WEB_DIALOG_HELPER_H_
 
-#include <vector>
-
 #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<WebDialogHelper> weak_factory_;