Commit 938e3b65 authored by Kent Tamura's avatar Kent Tamura Committed by Commit Bot

FileSelectHelper: Simplify directory-enumeration code.

A FileSelectHelper instance handles at most one directory enumeration
during its lifetime.
- FileSelectHelper doesn't need to have |std::map<int,
  ActiveDirectoryEnumeration*>|. One |std::unique_ptr<
  ActiveDirectoryEnumeration>| and one |int request_id_| are enough.
- We don't need DirectoryListerDispatchDelegate. FileSelectHelper
  implements net::DirectoryLister::DirectoryListerDelegate.

This CL has no behavior changes.
This CL is a preparation to mojoify FileChooser IPC.

Bug: 869257
Change-Id: I19ecfbb49b54c2430f7f0358ca787e831c493e29
Reviewed-on: https://chromium-review.googlesource.com/1177082
Commit-Queue: Kent Tamura <tkent@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#583870}
parent 445904fd
...@@ -126,7 +126,6 @@ struct FileSelectHelper::ActiveDirectoryEnumeration { ...@@ -126,7 +126,6 @@ struct FileSelectHelper::ActiveDirectoryEnumeration {
explicit ActiveDirectoryEnumeration(const base::FilePath& path) explicit ActiveDirectoryEnumeration(const base::FilePath& path)
: rvh_(NULL), path_(path) {} : rvh_(NULL), path_(path) {}
std::unique_ptr<DirectoryListerDispatchDelegate> delegate_;
std::unique_ptr<net::DirectoryLister> lister_; std::unique_ptr<net::DirectoryLister> lister_;
RenderViewHost* rvh_; RenderViewHost* rvh_;
const base::FilePath path_; const base::FilePath path_;
...@@ -148,25 +147,6 @@ FileSelectHelper::~FileSelectHelper() { ...@@ -148,25 +147,6 @@ FileSelectHelper::~FileSelectHelper() {
// away so they don't try and call back to us. // away so they don't try and call back to us.
if (select_file_dialog_.get()) if (select_file_dialog_.get())
select_file_dialog_->ListenerDestroyed(); select_file_dialog_->ListenerDestroyed();
// Stop any pending directory enumeration, prevent a callback, and free
// allocated memory.
std::map<int, ActiveDirectoryEnumeration*>::iterator iter;
for (iter = directory_enumerations_.begin();
iter != directory_enumerations_.end();
++iter) {
iter->second->lister_.reset();
delete iter->second;
}
}
void FileSelectHelper::DirectoryListerDispatchDelegate::OnListFile(
const net::DirectoryLister::DirectoryListerData& data) {
parent_->OnListFile(id_, data);
}
void FileSelectHelper::DirectoryListerDispatchDelegate::OnListDone(int error) {
parent_->OnListDone(id_, error);
} }
void FileSelectHelper::FileSelected(const base::FilePath& path, void FileSelectHelper::FileSelected(const base::FilePath& path,
...@@ -245,25 +225,22 @@ void FileSelectHelper::FileSelectionCanceled(void* params) { ...@@ -245,25 +225,22 @@ void FileSelectHelper::FileSelectionCanceled(void* params) {
void FileSelectHelper::StartNewEnumeration(const base::FilePath& path, void FileSelectHelper::StartNewEnumeration(const base::FilePath& path,
int request_id, int request_id,
RenderViewHost* render_view_host) { RenderViewHost* render_view_host) {
request_id_ = request_id;
auto entry = std::make_unique<ActiveDirectoryEnumeration>(path); auto entry = std::make_unique<ActiveDirectoryEnumeration>(path);
entry->rvh_ = render_view_host; entry->rvh_ = render_view_host;
entry->delegate_.reset(new DirectoryListerDispatchDelegate(this, request_id));
entry->lister_.reset(new net::DirectoryLister( entry->lister_.reset(new net::DirectoryLister(
path, net::DirectoryLister::NO_SORT_RECURSIVE, entry->delegate_.get())); path, net::DirectoryLister::NO_SORT_RECURSIVE, this));
entry->lister_->Start(); entry->lister_->Start();
directory_enumerations_[request_id] = entry.release(); directory_enumeration_ = std::move(entry);
} }
void FileSelectHelper::OnListFile( void FileSelectHelper::OnListFile(
int id,
const net::DirectoryLister::DirectoryListerData& data) { const net::DirectoryLister::DirectoryListerData& data) {
ActiveDirectoryEnumeration* entry = directory_enumerations_[id];
// Directory upload only cares about files. // Directory upload only cares about files.
if (data.info.IsDirectory()) if (data.info.IsDirectory())
return; return;
entry->results_.push_back(data.path); directory_enumeration_->results_.push_back(data.path);
} }
void FileSelectHelper::LaunchConfirmationDialog( void FileSelectHelper::LaunchConfirmationDialog(
...@@ -275,11 +252,10 @@ void FileSelectHelper::LaunchConfirmationDialog( ...@@ -275,11 +252,10 @@ void FileSelectHelper::LaunchConfirmationDialog(
std::move(selected_files), web_contents_); std::move(selected_files), web_contents_);
} }
void FileSelectHelper::OnListDone(int id, int error) { void FileSelectHelper::OnListDone(int error) {
// This entry needs to be cleaned up when this function is done. // This entry needs to be cleaned up when this function is done.
std::unique_ptr<ActiveDirectoryEnumeration> entry( std::unique_ptr<ActiveDirectoryEnumeration> entry =
directory_enumerations_[id]); std::move(directory_enumeration_);
directory_enumerations_.erase(id);
if (!entry->rvh_) if (!entry->rvh_)
return; return;
if (error) { if (error) {
...@@ -290,10 +266,10 @@ void FileSelectHelper::OnListDone(int id, int error) { ...@@ -290,10 +266,10 @@ void FileSelectHelper::OnListDone(int id, int error) {
std::vector<ui::SelectedFileInfo> selected_files = std::vector<ui::SelectedFileInfo> selected_files =
FilePathListToSelectedFileInfoList(entry->results_); FilePathListToSelectedFileInfoList(entry->results_);
if (id == kFileSelectEnumerationId) { if (request_id_ == kFileSelectEnumerationId) {
LaunchConfirmationDialog(entry->path_, std::move(selected_files)); LaunchConfirmationDialog(entry->path_, std::move(selected_files));
} else { } else {
entry->rvh_->DirectoryEnumerationFinished(id, entry->results_); entry->rvh_->DirectoryEnumerationFinished(request_id_, entry->results_);
EnumerateDirectoryEnd(); EnumerateDirectoryEnd();
} }
} }
......
...@@ -45,7 +45,8 @@ class FileSelectHelper : public base::RefCountedThreadSafe< ...@@ -45,7 +45,8 @@ class FileSelectHelper : public base::RefCountedThreadSafe<
content::BrowserThread::DeleteOnUIThread>, content::BrowserThread::DeleteOnUIThread>,
public ui::SelectFileDialog::Listener, public ui::SelectFileDialog::Listener,
public content::WebContentsObserver, public content::WebContentsObserver,
public content::RenderWidgetHostObserver { public content::RenderWidgetHostObserver,
private net::DirectoryLister::DirectoryListerDelegate {
public: public:
// Show the file chooser dialog. // Show the file chooser dialog.
static void RunFileChooser(content::RenderFrameHost* render_frame_host, static void RunFileChooser(content::RenderFrameHost* render_frame_host,
...@@ -69,27 +70,6 @@ class FileSelectHelper : public base::RefCountedThreadSafe< ...@@ -69,27 +70,6 @@ class FileSelectHelper : public base::RefCountedThreadSafe<
explicit FileSelectHelper(Profile* profile); explicit FileSelectHelper(Profile* profile);
~FileSelectHelper() override; ~FileSelectHelper() override;
// Utility class which can listen for directory lister events and relay
// them to the main object with the correct tracking id.
class DirectoryListerDispatchDelegate
: public net::DirectoryLister::DirectoryListerDelegate {
public:
DirectoryListerDispatchDelegate(FileSelectHelper* parent, int id)
: parent_(parent),
id_(id) {}
~DirectoryListerDispatchDelegate() override {}
void OnListFile(
const net::DirectoryLister::DirectoryListerData& data) override;
void OnListDone(int error) override;
private:
// This FileSelectHelper owns this object.
FileSelectHelper* parent_;
int id_;
DISALLOW_COPY_AND_ASSIGN(DirectoryListerDispatchDelegate);
};
void RunFileChooser(content::RenderFrameHost* render_frame_host, void RunFileChooser(content::RenderFrameHost* render_frame_host,
std::unique_ptr<content::FileChooserParams> params); std::unique_ptr<content::FileChooserParams> params);
void GetFileTypesInThreadPool( void GetFileTypesInThreadPool(
...@@ -146,11 +126,10 @@ class FileSelectHelper : public base::RefCountedThreadSafe< ...@@ -146,11 +126,10 @@ class FileSelectHelper : public base::RefCountedThreadSafe<
int request_id, int request_id,
content::RenderViewHost* render_view_host); content::RenderViewHost* render_view_host);
// Callbacks from directory enumeration. // net::DirectoryLister::DirectoryListerDelegate overrides.
virtual void OnListFile( void OnListFile(
int id, const net::DirectoryLister::DirectoryListerData& data) override;
const net::DirectoryLister::DirectoryListerData& data); void OnListDone(int error) override;
virtual void OnListDone(int id, int error);
void LaunchConfirmationDialog( void LaunchConfirmationDialog(
const base::FilePath& path, const base::FilePath& path,
...@@ -244,11 +223,13 @@ class FileSelectHelper : public base::RefCountedThreadSafe< ...@@ -244,11 +223,13 @@ class FileSelectHelper : public base::RefCountedThreadSafe<
// The mode of file dialog last shown. // The mode of file dialog last shown.
content::FileChooserParams::Mode dialog_mode_; content::FileChooserParams::Mode dialog_mode_;
// Maintain a list of active directory enumerations. These could come from // Maintain an active directory enumeration. These could come from the file
// the file select dialog or from drag-and-drop of directories, so there could // select dialog or from drag-and-drop of directories. There could not be
// be more than one going on at a time. // more than one going on at a time.
struct ActiveDirectoryEnumeration; struct ActiveDirectoryEnumeration;
std::map<int, ActiveDirectoryEnumeration*> directory_enumerations_; std::unique_ptr<ActiveDirectoryEnumeration> directory_enumeration_;
// Keep |request_id| argument of EnumerateDirectory() to reply to RVH.
int request_id_;
ScopedObserver<content::RenderWidgetHost, content::RenderWidgetHostObserver> ScopedObserver<content::RenderWidgetHost, content::RenderWidgetHostObserver>
observer_; observer_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment