Merge pull request #777 from atom/web-dialog

Implement file dialog and color chooser for <input> tag
This commit is contained in:
Cheng Zhao 2014-11-03 17:47:08 +08:00
commit 92cfd96933
14 changed files with 768 additions and 15 deletions

View file

@ -18,25 +18,24 @@ class AtomBrowserClient : public brightray::BrowserClient {
protected:
// content::ContentBrowserClient:
virtual void RenderProcessWillLaunch(
content::RenderProcessHost* host) OVERRIDE;
virtual content::SpeechRecognitionManagerDelegate*
void RenderProcessWillLaunch(content::RenderProcessHost* host) override;
content::SpeechRecognitionManagerDelegate*
GetSpeechRecognitionManagerDelegate() override;
virtual content::AccessTokenStore* CreateAccessTokenStore() OVERRIDE;
virtual void OverrideWebkitPrefs(content::RenderViewHost* render_view_host,
const GURL& url,
content::WebPreferences* prefs) OVERRIDE;
virtual bool ShouldSwapBrowsingInstancesForNavigation(
content::AccessTokenStore* CreateAccessTokenStore() override;
void OverrideWebkitPrefs(content::RenderViewHost* render_view_host,
const GURL& url,
content::WebPreferences* prefs) override;
bool ShouldSwapBrowsingInstancesForNavigation(
content::SiteInstance* site_instance,
const GURL& current_url,
const GURL& new_url) OVERRIDE;
virtual std::string GetApplicationLocale() OVERRIDE;
virtual void AppendExtraCommandLineSwitches(base::CommandLine* command_line,
int child_process_id) OVERRIDE;
const GURL& new_url) override;
std::string GetApplicationLocale() override;
void AppendExtraCommandLineSwitches(base::CommandLine* command_line,
int child_process_id) override;
private:
virtual brightray::BrowserMainParts* OverrideCreateBrowserMainParts(
const content::MainFunctionParams&) OVERRIDE;
brightray::BrowserMainParts* OverrideCreateBrowserMainParts(
const content::MainFunctionParams&) override;
// The render process which would be swapped out soon.
content::RenderProcessHost* dying_render_process_;

View 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"
@ -30,6 +31,7 @@
#include "brightray/browser/inspectable_web_contents.h"
#include "brightray/browser/inspectable_web_contents_view.h"
#include "chrome/browser/printing/print_view_manager_basic.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/invalidate_type.h"
#include "content/public/browser/navigation_entry.h"
@ -483,6 +485,28 @@ void NativeWindow::BeforeUnloadFired(content::WebContents* tab,
}
}
content::ColorChooser* NativeWindow::OpenColorChooser(
content::WebContents* web_contents,
SkColor color,
const std::vector<content::ColorSuggestion>& suggestions) {
return chrome::ShowColorChooser(web_contents, color);
}
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) {

View file

@ -51,6 +51,7 @@ namespace atom {
class AtomJavaScriptDialogManager;
struct DraggableRegion;
class WebDialogHelper;
class NativeWindow : public brightray::DefaultWebContentsDelegate,
public brightray::InspectableWebContentsDelegate,
@ -229,6 +230,15 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
void BeforeUnloadFired(content::WebContents* tab,
bool proceed,
bool* proceed_to_fire_unload) override;
content::ColorChooser* OpenColorChooser(
content::WebContents* web_contents,
SkColor color,
const std::vector<content::ColorSuggestion>& suggestions) 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 +323,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_,

View file

@ -0,0 +1,85 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#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 "base/strings/utf_string_conversions.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "ui/shell_dialogs/selected_file_info.h"
namespace atom {
WebDialogHelper::WebDialogHelper(NativeWindow* window)
: window_(window),
weak_factory_(this) {
}
WebDialogHelper::~WebDialogHelper() {
}
void WebDialogHelper::RunFileChooser(content::WebContents* web_contents,
const content::FileChooserParams& params) {
std::vector<ui::SelectedFileInfo> result;
if (params.mode == content::FileChooserParams::Save) {
base::FilePath path;
if (file_dialog::ShowSaveDialog(window_,
base::UTF16ToUTF8(params.title),
params.default_file_name,
file_dialog::Filters(),
&path))
result.push_back(ui::SelectedFileInfo(path, path));
} else {
int flags = file_dialog::FILE_DIALOG_CREATE_DIRECTORY;
switch (params.mode) {
case content::FileChooserParams::OpenMultiple:
flags |= file_dialog::FILE_DIALOG_MULTI_SELECTIONS;
case content::FileChooserParams::Open:
flags |= file_dialog::FILE_DIALOG_OPEN_FILE;
break;
case content::FileChooserParams::UploadFolder:
flags |= file_dialog::FILE_DIALOG_OPEN_DIRECTORY;
break;
default:
NOTREACHED();
}
std::vector<base::FilePath> paths;
if (file_dialog::ShowOpenDialog(window_,
base::UTF16ToUTF8(params.title),
params.default_file_name,
file_dialog::Filters(),
flags,
&paths))
for (auto& path : paths)
result.push_back(ui::SelectedFileInfo(path, path));
}
web_contents->GetRenderViewHost()->FilesSelectedInChooser(
result, params.mode);
}
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

View file

@ -0,0 +1,44 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_WEB_DIALOG_HELPER_H_
#define ATOM_BROWSER_WEB_DIALOG_HELPER_H_
#include "base/memory/weak_ptr.h"
namespace base {
class FilePath;
}
namespace content {
struct FileChooserParams;
class WebContents;
}
namespace atom {
class NativeWindow;
class WebDialogHelper {
public:
explicit WebDialogHelper(NativeWindow* window);
~WebDialogHelper();
void RunFileChooser(content::WebContents* web_contents,
const content::FileChooserParams& params);
void EnumerateDirectory(content::WebContents* web_contents,
int request_id,
const base::FilePath& path);
private:
NativeWindow* window_;
base::WeakPtrFactory<WebDialogHelper> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(WebDialogHelper);
};
} // namespace atom
#endif // ATOM_BROWSER_WEB_DIALOG_HELPER_H_