Browse Source

Implement EnumerateDirectory

Cheng Zhao 10 years ago
parent
commit
9b1bcf51c6

+ 16 - 0
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) {

+ 7 - 0
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<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_,

+ 24 - 7
atom/browser/web_dialog_helper.cc

@@ -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

+ 7 - 7
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 <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_;