#ifndef CHROME_BROWSER_FILE_SELECT_HELPER_H_
#define CHROME_BROWSER_FILE_SELECT_HELPER_H_
#include <map>
#include <vector>
#include "base/compiler_specific.h"
#include "base/gtest_prod_util.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/common/file_chooser_params.h"
#include "net/base/directory_lister.h"
#include "ui/shell_dialogs/select_file_dialog.h"
class Profile;
namespace content {
class RenderViewHost;
class WebContents;
}
namespace ui {
struct SelectedFileInfo;
}
class FileSelectHelper
: public base::RefCountedThreadSafe<FileSelectHelper>,
public ui::SelectFileDialog::Listener,
public content::NotificationObserver {
public:
static void RunFileChooser(content::WebContents* tab,
const content::FileChooserParams& params);
static void EnumerateDirectory(content::WebContents* tab,
int request_id,
const base::FilePath& path);
private:
friend class base::RefCountedThreadSafe<FileSelectHelper>;
FRIEND_TEST_ALL_PREFIXES(FileSelectHelperTest, IsAcceptTypeValid);
explicit FileSelectHelper(Profile* profile);
virtual ~FileSelectHelper();
class DirectoryListerDispatchDelegate
: public net::DirectoryLister::DirectoryListerDelegate {
public:
DirectoryListerDispatchDelegate(FileSelectHelper* parent, int id)
: parent_(parent),
id_(id) {}
virtual ~DirectoryListerDispatchDelegate() {}
virtual void OnListFile(
const net::DirectoryLister::DirectoryListerData& data) OVERRIDE;
virtual void OnListDone(int error) OVERRIDE;
private:
FileSelectHelper* parent_;
int id_;
DISALLOW_COPY_AND_ASSIGN(DirectoryListerDispatchDelegate);
};
void RunFileChooser(content::RenderViewHost* render_view_host,
content::WebContents* web_contents,
const content::FileChooserParams& params);
void RunFileChooserOnFileThread(
const content::FileChooserParams& params);
void RunFileChooserOnUIThread(
const content::FileChooserParams& params);
void RunFileChooserEnd();
virtual void FileSelected(
const base::FilePath& path, int index, void* params) OVERRIDE;
virtual void FileSelectedWithExtraInfo(
const ui::SelectedFileInfo& file,
int index,
void* params) OVERRIDE;
virtual void MultiFilesSelected(const std::vector<base::FilePath>& files,
void* params) OVERRIDE;
virtual void MultiFilesSelectedWithExtraInfo(
const std::vector<ui::SelectedFileInfo>& files,
void* params) OVERRIDE;
virtual void FileSelectionCanceled(void* params) OVERRIDE;
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
void EnumerateDirectory(int request_id,
content::RenderViewHost* render_view_host,
const base::FilePath& path);
void StartNewEnumeration(const base::FilePath& path,
int request_id,
content::RenderViewHost* render_view_host);
virtual void OnListFile(
int id,
const net::DirectoryLister::DirectoryListerData& data);
virtual void OnListDone(int id, int error);
void EnumerateDirectoryEnd();
static scoped_ptr<ui::SelectFileDialog::FileTypeInfo>
GetFileTypesFromAcceptType(
const std::vector<base::string16>& accept_types);
static bool IsAcceptTypeValid(const std::string& accept_type);
Profile* profile_;
content::RenderViewHost* render_view_host_;
content::WebContents* web_contents_;
scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
scoped_ptr<ui::SelectFileDialog::FileTypeInfo> select_file_types_;
ui::SelectFileDialog::Type dialog_type_;
content::FileChooserParams::Mode dialog_mode_;
struct ActiveDirectoryEnumeration;
std::map<int, ActiveDirectoryEnumeration*> directory_enumerations_;
content::NotificationRegistrar notification_registrar_;
DISALLOW_COPY_AND_ASSIGN(FileSelectHelper);
};
#endif