Make file dialogs work in <webview>, fixes #930

This commit is contained in:
Cheng Zhao 2014-12-18 12:58:17 -08:00
parent a1ae399d10
commit 47d7a355f2
4 changed files with 44 additions and 0 deletions

View file

@ -5,6 +5,9 @@
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/native_window.h"
#include "atom/browser/web_dialog_helper.h"
#include "atom/browser/web_view/web_view_renderer_state.h"
#include "atom/common/api/api_messages.h"
#include "atom/common/native_mate_converters/gfx_converter.h"
#include "atom/common/native_mate_converters/gurl_converter.h"
@ -34,6 +37,17 @@ namespace {
v8::Persistent<v8::ObjectTemplate> template_;
// Get the window that has the |guest| embedded.
NativeWindow* GetWindowFromGuest(const content::WebContents* guest) {
int guest_process_id = guest->GetRenderProcessHost()->GetID();
WebViewRendererState::WebViewInfo info;
if (!WebViewRendererState::GetInstance()->GetInfo(guest_process_id, &info))
return nullptr;
return NativeWindow::FromRenderView(
info.embedder->GetRenderProcessHost()->GetID(),
info.embedder->GetRoutingID());
}
} // namespace
WebContents::WebContents(content::WebContents* web_contents)
@ -135,6 +149,21 @@ content::WebContents* WebContents::OpenURLFromTab(
return web_contents();
}
void WebContents::RunFileChooser(content::WebContents* guest,
const content::FileChooserParams& params) {
if (!web_dialog_helper_)
web_dialog_helper_.reset(new WebDialogHelper(GetWindowFromGuest(guest)));
web_dialog_helper_->RunFileChooser(guest, params);
}
void WebContents::EnumerateDirectory(content::WebContents* guest,
int request_id,
const base::FilePath& path) {
if (!web_dialog_helper_)
web_dialog_helper_.reset(new WebDialogHelper(GetWindowFromGuest(guest)));
web_dialog_helper_->EnumerateDirectory(guest, request_id, path);
}
void WebContents::RequestMediaAccessPermission(
content::WebContents*,
const content::MediaStreamRequest& request,

View file

@ -24,6 +24,8 @@ class Dictionary;
namespace atom {
class WebDialogHelper;
namespace api {
class WebContents : public mate::EventEmitter,
@ -113,6 +115,11 @@ class WebContents : public mate::EventEmitter,
content::WebContents* OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params) 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 RequestMediaAccessPermission(
content::WebContents*,
const content::MediaStreamRequest&,
@ -165,6 +172,8 @@ class WebContents : public mate::EventEmitter,
void GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
const gfx::Size& new_size);
scoped_ptr<WebDialogHelper> web_dialog_helper_;
// Unique ID for a guest WebContents.
int guest_instance_id_;

View file

@ -65,6 +65,7 @@ void WebViewManager::AddGuest(int guest_instance_id,
WebViewRendererState::WebViewInfo web_view_info = {
guest_instance_id,
embedder,
options.node_integration,
options.plugins,
options.disable_web_security,

View file

@ -12,6 +12,10 @@
#include "base/files/file_path.h"
#include "base/memory/singleton.h"
namespace content {
class WebContents;
}
namespace atom {
class WebViewManager;
@ -22,6 +26,7 @@ class WebViewRendererState {
public:
struct WebViewInfo {
int guest_instance_id;
content::WebContents* embedder;
bool node_integration;
bool plugins;
bool disable_web_security;